Kubernetes is an open source container orchestration system that helps users automate the management of multiple containerized applications. This article will introduce how to deploy a Kubernetes cluster on a CentOS server.
Environmental preparation
1. Install Docker
Install Docker on all nodes.
sudo yum install -y docker
sudo systemctl start docker
sudo systemctl enable docker
2. Install kubeadm
Install kubeadm, kubelet and kubectl on all nodes.
sudo tee /etc/yum.repos.d/kubernetes.repo <<EOF
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
sudo yum install -y kubelet kubeadm kubectl
sudo systemctl start kubelet
sudo systemctl enable kubelet
3. Close the swap partition
Kubernetes does not support the use of swap partitions, so swap needs to be turned off.
sudo swapoff -a
sudo sed -i '/swap/d' /etc/fstab
Initialize the Kubernetes master node
Initialize the Kubernetes cluster on the master node.
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
After the command is executed, it will output information similar to the following:
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.0.10:6443 --token abcdef.1234567890abcdef \
--discovery-token-ca-cert-hash sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
Copy sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
the output of the command and execute it on the local machine.
ssh user@master_ip sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
Then install the pod network plugin on your local machine.
kubectl apply -f https://docs.projectcalico.org/v3.14/manifests/calico.yaml
After the master node is initialized, you can use the following command to view the status:
kubectl get nodes
Join a Kubernetes worker node
Execute the following command on the server that needs to be added as a working node:
sudo kubeadm join 192.168.0.10:6443 --token abcdef.1234567890abcdef \
--discovery-token-ca-cert-hash sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
where 192.168.0.10
is the IP address of the master node, and the details of the join command have been output when the master node is initialized.
After the worker node joins, you can use the following command to view the status:
kubectl get nodes
deploy application
Now the Kubernetes cluster is ready to deploy applications. Here is a simple Nginx example:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort