“This is the sixth day of my participation in the First Challenge 2022. For details: First Challenge 2022”
Kubeadm is a K8s deployment tool that provides Kubeadm Init and Kubeadm Join for rapid deployment of Kubernetes clusters. The official address: kubernetes. IO/docs/refere…
The tool can deploy a Kubernetes cluster with two instructions:
1) create a Master node kubeadm init
$kubeadm join <Master Node IP address and port >
One, installation requirements
- One or more machines, operating system Centos 7.X-86_X64
- Hardware: 2GB or more RAM, 2 oR more cpus, 30GB or more hard disk
- All the machines in the cluster are communicating properly
- You can access the Internet. You need to pull the mirror
- Disabling swap partitions
Two, prepare the environment
2.1 Three VMS
role | ip | hostname |
---|---|---|
master | 192.168.184.137 | k8s-master |
worker node | 192.168.184.138 | k8s-node1 |
worker node | 192.168.184.139 | k8s-node2 |
2.2 Linux Initialization
2.2.1 Disabling the Firewall
systemctl stop firewalld
systemctl disable firewalld
Copy the code
2.2.2 closed selinux
# permanent sed -i 's/enforcing/disabled/' /etc/selinux/config # temporary setenforce 0Copy the code
Then close the swap
A swap partition is a swap partition, or virtual memory on Windows. When the physical memory is insufficient, the operating system moves some of the temporarily unused data from the physical memory to the swap partition, thus leaving enough physical memory space for the currently running programs.
Kubelet made it mandatory for swap to be closed after version 1.8.
Temporary closure:
swapoff -a
Copy the code
Permanently close the following file and comment out the swap line
vi /etc/fstab
#/dev/mapper/centos-swap swap swap defaults 0 0
#Permanently shut down and restart Linux
reboot
Copy the code
2.2.4 Setting the Host Name
The host names of the three VMS are as follows:
hostnamectl set-hostname <hostname>
Copy the code
2.2.5 Modifying hosts on the Master node
vi /etc/hosts
Copy the code
Add the following:
192.168.184.137k8S-master 192.168.184.138k8S-node1 192.168.184.139k8S-node2Copy the code
2.2.6 Transferring bridge ipv4 Traffic to Iptables
echo "1" >/proc/sys/net/bridge/bridge-nf-call-iptables
Copy the code
2.2.7 Synchronizing time
yum install ntpdate -y
ntpdate time.windows.com
Copy the code
2.3 installation Docker/kubeadm/kubelet
Install Docker/kubeadm/kubelet among the three nodes
2.3.1 installation docker
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo yum -y install El7 systemctl enable docker && Systemctl start docker docker --versionCopy the code
Set ali source for Docker
vi /etc/docker/daemon.json
Copy the code
Add the following:
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
Copy the code
2.3.2 Adding the YUM Source
vi /etc/yum.repos.d/kubernetes.repo
Copy the code
Add the following:
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
Copy the code
2.3.3 Installing kubeadm, kubelet and kubectl
Yum install -y kubelet-1.18.1-0 kubeadm-1.18.1-0 kubectl-1.18.1-0 systemctl enable kubelet-1.18.1-0Copy the code
3. Deploy nodes
3.1 Deploying a Master Node
3.1.1 Initializing kubeadm
Run the following command on 192.168.184.137:
Kubeadm init \ --apiserver-advertise-address=192.168.184.137 \ --image-repository Registry.aliyuncs.com/google_containers \ - kubernetes - version v1.18.1 \ - service - cidr = 10.96.0.0/12 \ - pod - network - cidr = 10.244.0.0/16Copy the code
The result is as follows:
. . 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 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.184.137:6443 --token cple2x.gieng4q082nrc2ml \ --discovery-token-ca-cert-hash sha256:5bd8349e530333725f7b70f6bce716156f81c337fc9cbb9eed9a68f11436375dCopy the code
3.1.2 kubectl tools
Next, using the Kubectl tool, execute the following commands respectively:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl get nodes
Copy the code
View nodes:
NAME STATUS ROLES AGE VERSION K8S-Master NotReady Control-plane, Master 50S V1.21.1Copy the code
3.1.3 Installing a POD Network Plug-in (CNI)
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
Copy the code
3.2 Adding a K8S Node
After executing kubeadm init, the command to add a node is printed:
Kubeadm join 192.168.184.137:6443 --token gr4zvu.05mcKhlvhve1rrvq -- discovery-tok-ca-cert-hash sha256:e9bc42fe3b0b6c991b7cb4489a319d8367f862b1034780cf1e0fca81909ef2ecCopy the code
If it is not recorded, run the following command to view it:
kubeadm token create --print-join-command
Copy the code
If any of the following problems occur, you can use the kubeadm reset command to reset:
[preflight] Running pre-flight checks [WARNING Hostname]: hostname "k8s-node1" could not be reached [WARNING Hostname]: Hostname "k8S-node1 ": lookup k8s-node1 on 192.168.184.2:53: no such host error Execution phase preflight: [preflight] Some fatal errors occurred: [ERROR FileAvailable--etc-kubernetes-kubelet.conf]: /etc/kubernetes/kubelet.conf already exists [ERROR FileAvailable--etc-kubernetes-pki-ca.crt]: /etc/kubernetes/pki/ca.crt already exists [preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=... `Copy the code
4. Cluster verification
Create a pod in the Kubernetes cluster and verify that it is working properly by executing the following commands:
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
Copy the code
To view:
[root@k8s-master ~]# kubectl get pod,svc NAME READY STATUS RESTARTS AGE pod/nginx-6799fc88d8-zx89s 1/1 Running 0 27m NAME TYPE cluster-ip external-ip PORT(S) AGE service/kubernetes ClusterIP 10.96.0.1 < None > 443/TCP 143m service/nginx NodePort 10.111.56.177 < None > 80:30880/TCP 27mCopy the code
Respectively through three nodes access nginx, get the following interface: http://192.168.184.137:30880 http://192.168.184.138:30880 http://192.168.184.139:30880