在 Ubuntu 20.04 中安装 k8s 集群

In this article, we use three hosts(cloud or vhost) to build a kubernetes cluster on Ubuntu 20.04.

Prepare Host

Install Ubuntu 20.04 for the three hosts and set their intranet addresses:

Master - 192.168.11.16
Node-01 - 192.168.11.2
Node-02 - 192.168.11.6

Close the swap partition

Close the swap partition for the three machines.
Perform the following steps in each of the three machines

Temporarily Closed

1
sudo swapoff -a

To closed permanently, you need to modify the fstab file.

1
sudo vi /etc/fstab  #Comment out the swap line

Verify, Execute

1
free -h

As you can see, the number of swaps is 0

1
2
3
              total        used        free      shared  buff/cache   available
Mem: 3.8Gi 215Mi 1.9Gi 2.0Mi 1.7Gi 3.3Gi
Swap: 0B 0B 0B

Change kernel parameters

Execute the following commands sequentially:

1
sudo modprobe overlay
1
sudo modprobe br_netfilter
1
2
3
4
5
sudo tee /etc/sysctl.d/kubernetes.conf<<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
1
sudo sysctl --system

Verify, Execute:

1
lsmod | grep br_netfilter

You can see that the system displays the following content:

1
2
br_netfilter           28672  0
bridge 176128 1 br_netfilter

Install docker

  1. Update Ubuntu and Install Docker
1
sudo apt update
1
sudo apt install -y docker.io
  1. Modify the configuration file of docker
1
2
3
4
5
6
7
8
9
10
sudo tee /etc/docker/daemon.json <<EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}
EOF
  1. Restart Docker
1
sudo systemctl restart docker
  1. Set docker to boot startup
1
sudo systemctl enable docker
  1. Add the current user to the docker group so that the docker command does not need to be entered into sudo later. Assumes that the current user name is stu, execute:
1
sudo usermod -aG docker stu

Note that for the above command to take effect, you need to log in again

  1. To verify the installation, you can execute some commonly used docker commands to see if they are working properly, such as:
1
docker images
1
docker --version

Change the host name

Use the following command to change the host。

Install the list at the beginning of the article to make ip addresses to make modifications.

Master - 192.168.11.16
Node-01 - 192.168.11.2
Node-02 - 192.168.11.6

As executed at 192.168.11.16:

1
sudo hostnamectl set-hostname "Master"

Install kubeadm kubeadm kubectl

1
sudo apt-get install -y kubelet kubeadm kubectl

Block automatic updates

1
sudo apt-mark hold kubelet kubeadm kubectl

View the version

1
kubectl version --client && kubeadm version

Set kubelet to boot start

1
sudo systemctl enable kubelet

Initialize the master node

Execute the comamnd in Master

1
sudo kubeadm init --apiserver-advertise-address=192.168.11.16 --pod-network-cidr=10.244.0.0/16

Note: Replace the apiserver-advertise-address value with your master node IP

  • –apiserver-advertise-address: The deployment address of the main service apiserver in k8s, fill in your own management node IP

  • –pod-network-cidr: This is the node network used by k8s, because we will use flannel as the k8s network, as a learning environment, here is 10.244.0.0/16

After the running is complete, you can see a prompt similar to the following

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Alternatively, if you are the root user, you can run:

export KUBECONFIG=/etc/kubernetes/admin.conf

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.11.16:6443 --token pekml2.0s4kxso66inl2tww \
--discovery-token-ca-cert-hash sha256:6f1b5b42a7a09351a8805a7f2b0bebabb07dcb4e782cc3e74461de2a16962502

Follow the prompts

1
mkdir -p $HOME/.kube
1
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
1
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Next, install a flannel network for the cluster

1
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

You can see information similar to the following

1
2
3
4
5
6
7
Warning: policy/v1beta1 PodSecurityPolicy is deprecated in v1.21+, unavailable in v1.25+
podsecuritypolicy.policy/psp.flannel.unprivileged created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.apps/kube-flannel-ds created

Verify

Executes on the master node

1
kubectl get nodes

You can see it

1
2
NAME     STATUS   ROLES                  AGE   VERSION
master Ready control-plane,master 21m v1.23.0

Join the worker node

Executes on the worker node

1
2
sudo kubeadm join 192.168.11.16:6443 --token pekml2.0s4kxso66inl2tww \
--discovery-token-ca-cert-hash sha256:6f1b5b42a7a09351a8805a7f2b0bebabb07dcb4e782cc3e74461de2a16962502

You can see information similar to the following

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[preflight] Running pre-flight checks
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
W1213 11:09:04.079403 1772383 utils.go:69] The recommended value for "resolvConf" in "KubeletConfiguration" is: /run/systemd/resolve/resolv.conf; the provided value is: /run/systemd/resolve/resolv.conf
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

After the above command line was executed on all worker nodes,Can be returned to the master node for execution:

1
kubectl get nodes

If you see a similar message, the k8s cluster has been successfully installed

1
2
3
4
NAME      STATUS   ROLES                  AGE     VERSION
master Ready control-plane,master 26m v1.23.0
node-02 Ready <none> 62s v1.23.0
node-03 Ready <none> 2m58s v1.23.0

本文标题:在 Ubuntu 20.04 中安装 k8s 集群

文章作者:Morning Star

发布时间:2021年10月07日 - 14:10

最后更新:2022年01月11日 - 18:01

原始链接:https://www.mls-tech.info/microservice/k8s/k8s-installation-on-ubuntu20.04-en/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。