-
Notifications
You must be signed in to change notification settings - Fork 0
/
Setting Up a Kubernetes Cluster.txt
198 lines (116 loc) · 7.44 KB
/
Setting Up a Kubernetes Cluster.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# Cloud Playground Settings
# Ubuntu 22.04 - Jammy Jellyfish, Medium (3 units), Tag = 'Kubernetes Master'
# Ubuntu 22.04 - Jammy Jellyfish, Small (2 units), Tag = 'Kubernetes Worker'
# Chapter 2.2 - 'Setting Up a Kubernetes Cluster'
# URL: https://learn.acloud.guru/course/97037e05-88ed-41a1-92ee-f5a8080318c2/learn/6507da5b-10a7-4c04-81d9-c3d58ad9c2d2/90cfb59d-c97b-44c3-b2ab-f9d1024ec4c1/watch
# In this lesson, you will setup your Kubernetes cluster. We will start by installing containerd.
# 1 - Installing containerd
# Here are the commands used install containerd. Run these on all servers.
# Disable swap:
sudo swapoff -a
# Create configuration file for containerd:
cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF
# Load modules:
sudo modprobe overlay
sudo modprobe br_netfilter
# Set system configurations for Kubernetes networking:
cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF
# Apply new settings:
sudo sysctl --system
# Add Docker’s official GPG key:
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
# Use the following command to set up the repository:
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Install containerd. Note: use the following command "sudo kill -9 $( sudo lsof /var/lib/dpkg/lock-frontend | awk '{ print $2 }' | tail -1 )" if you receive a "E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?" message.
sudo apt-get update && sudo apt-get install -y containerd
# Create default configuration file for containerd:
sudo mkdir -p /etc/containerd
# Generate default containerd configuration and save to the newly created default file:
sudo containerd config default | sudo tee /etc/containerd/config.toml
# Update the systemd cgroups parameter in the /etc/containerd/config.toml file:
sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/g' /etc/containerd/config.toml
# Restart containerd to ensure new configuration file usage:
sudo systemctl restart containerd
# Verify that containerd is running:
sudo systemctl status containerd
# 2 - Installing Kubeadm, Kubelet, and Kubectl
# Here are the commands used to install the Kubernetes components. Run these on all servers.
# Install dependency packages:
sudo apt-get update && sudo apt-get install -y apt-transport-https ca-certificates curl gpg software-properties-common
# Download and add GPG key:
sudo mkdir -p /etc/apt/keyrings && curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.31/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
# Allow unprivileged APT programs to read this keyring
sudo chmod 644 /etc/apt/keyrings/kubernetes-apt-keyring.gpg
# Add Kubernetes to repository list:
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.31/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
# Helps tools such as command-not-found to work correctly
sudo chmod 644 /etc/apt/sources.list.d/kubernetes.list
# Update package listings:
sudo apt-get update
# Install Kubernetes packages. Note: use the following command "sudo kill -9 $( sudo lsof /var/lib/dpkg/lock-frontend | awk '{ print $2 }' | tail -1 )" if you receive a "E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?" message.
sudo apt-get install -y kubelet kubeadm kubectl
# Turn off automatic updates:
sudo apt-mark hold kubelet kubeadm kubectl
# Enable the kubelet service:
sudo systemctl enable --now kubelet
# 3 - Bootstrapping the Cluster
# On Kube Master and Kube Node, stop and disable ufw:
sudo systemctl stop ufw
sudo ufw disable
# On the Kube Master node, initialize the cluster:
sudo kubeadm init --pod-network-cidr 192.168.0.0/16
# Set kubectl access:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# The kubeadm init command should output a kubeadm join command containing a token and hash. Copy that command and run it with sudo on both worker nodes. It should look something like this:
sudo kubeadm join $some_ip:6443 --token $some_token --discovery-token-ca-cert-hash $some_hash
# Use can print the full 'kubeadm join' flag needed to join the cluster with the following command:
kubeadm token create --print-join-command
# Run the kubeadm join command on your Kube Node server:
sudo kubeadm join <IP_ADDRESS> --token <TOKEN> --discovery-token-ca-cert-hash sha256:<HASH>
# From your Kube Master node, verify that all nodes have successfully joined the cluster:
kubectl get nodes
# You should see all three of your nodes listed. It should look something like this:
NAME STATUS ROLES AGE VERSION
f8bbdd78c31c.mylabserver.com NotReady control-plane 2m13s v1.31.0
f8bbdd78c32c.mylabserver.com NotReady <none> 22s v1.31.0
# Note: The nodes are expected to have a STATUS of NotReady at this point
# 4 - Configuring Networking with Calico
# Install Calico in the cluster by running this only on the Master node:
kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.25.0/manifests/calico.yaml
# Verify that all the nodes now have a STATUS of "Ready":
kubectl get nodes
# You should see all three of your servers listed, and all should have a STATUS of "Ready". It should look something like this:
NAME STATUS ROLES AGE VERSION
f8bbdd78c31c.mylabserver.com Ready control-plane 3m36s v1.31.0
f8bbdd78c32c.mylabserver.com Ready <none> 105s v1.31.0
# Note: It may take a few moments for all nodes to enter the "Ready" status, so if they are not all "Ready", wait a few moments and try again.
# It is also a good idea to verify that the Calico pods are up and running. Run this command to get a list of system pods:
kubectl get pods -n kube-system
# Expected output:
NAME READY STATUS RESTARTS AGE
calico-kube-controllers-6879d4fcdc-67mhx 1/1 Running 0 71s
calico-node-rq8xs 1/1 Running 0 71s
calico-node-t7h62 1/1 Running 0 71s
coredns-6f6b679f8f-cx4dd 1/1 Running 0 3m57s
coredns-6f6b679f8f-grnvs 1/1 Running 0 3m57s
etcd-f8bbdd78c31c.mylabserver.com 1/1 Running 0 4m2s
kube-apiserver-f8bbdd78c31c.mylabserver.com 1/1 Running 0 4m5s
kube-controller-manager-f8bbdd78c31c.mylabserver.com 1/1 Running 0 4m2s
kube-proxy-bljnf 1/1 Running 0 2m16s
kube-proxy-jgsss 1/1 Running 0 3m57s
kube-scheduler-f8bbdd78c31c.mylabserver.com 1/1 Running 0 4m2s
# You should have three pods with "calico" in the name, and all three should have a status of "Running".