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 | total used free shared buff/cache available |
Change kernel parameters
Execute the following commands sequentially:
1 | sudo modprobe overlay |
1 | sudo modprobe br_netfilter |
1 | sudo tee /etc/sysctl.d/kubernetes.conf<<EOF |
1 | sudo sysctl --system |
Verify, Execute:
1 | lsmod | grep br_netfilter |
You can see that the system displays the following content:
1 | br_netfilter 28672 0 |
Install docker
- Update Ubuntu and Install Docker
1 | sudo apt update |
1 | sudo apt install -y docker.io |
- Modify the configuration file of docker
1 | sudo tee /etc/docker/daemon.json <<EOF |
- Restart Docker
1 | sudo systemctl restart docker |
- Set docker to boot startup
1 | sudo systemctl enable docker |
- 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
- 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 | Your Kubernetes control-plane has initialized successfully! |
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 | Warning: policy/v1beta1 PodSecurityPolicy is deprecated in v1.21+, unavailable in v1.25+ |
Verify
Executes on the master node
1 | kubectl get nodes |
You can see it
1 | NAME STATUS ROLES AGE VERSION |
Join the worker node
Executes on the worker node
1 | sudo kubeadm join 192.168.11.16:6443 --token pekml2.0s4kxso66inl2tww \ |
You can see information similar to the following
1 | [preflight] Running pre-flight checks |
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 | NAME STATUS ROLES AGE VERSION |