Environment Preparation#
Install three CentOS 7.x operating systems in virtual machines.
Configure system names and IP addresses (same subnet) as follows:
Role | IP |
---|---|
master | 192.168.66.100 |
node1 | 192.168.66.101 |
node2 | 192.168.66.102 |
Commands to be executed on all three machines: (Using Xshell to execute commands is more convenient)
# Stop the firewall
systemctl stop firewalld
systemctl disable firewalld
# Permanently disable selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config
# Permanently disable swap
sed -ri 's/.*swap.*/#&/' /etc/fstab
# Add hosts
cat >> /etc/hosts << EOF
192.168.66.100 master
192.168.66.101 node1
192.168.66.102 node2
EOF
# Pass bridged IPv4 traffic to iptables chains
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
# Apply changes
sysctl --system
# Time synchronization
yum install ntpdate -y
ntpdate time.windows.com
Install Docker (needs to be installed on all three machines)#
Configure Docker's Aliyun yum source.
cat >/etc/yum.repos.d/docker.repo<<EOF
[docker-ce-edge]
name=Docker CE Edge - \$basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/\$basearch/edge
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
EOF
Install Docker using yum.
# Install with yum
yum -y install docker-ce
# Check Docker version
docker --version
# Start Docker
systemctl enable docker
systemctl start docker
Configure Docker's image source.
cat >> /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF
Restart Docker.
systemctl restart docker
Add Kubernetes Software Source#
Configure yum's k8s software source.
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[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
EOF
Install kubeadm, kubelet, and kubectl#
Due to frequent version updates, specify version numbers for deployment:
# Install kubelet, kubeadm, kubectl, specifying versions
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
# Enable on boot
systemctl enable kubelet
Deploy Master Node#
Execute on the master node (replace 192.168.66.100 with the IP of your master machine).
kubeadm init --apiserver-advertise-address=192.168.66.100 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.18.0 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16
Wait for the success message to appear.
Then, follow the prompt to enter the command on the master node.
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Add new nodes to the cluster according to the code generated by the master (operations on node1 and node2).
Execute the kubeadm join command generated on the master on node1 and node2: (depending on the actual situation, the following is for reference only)
kubeadm join 192.168.66.100:6443 --token 7fqt6v.729wvdcjmgivns7y \
--discovery-token-ca-cert-hash sha256:ef79029853fa3c5454cbfc5273a636c843db0ab96e4592467b8a1490b6b6d3c6
We can go to the Master node and execute the following command to check the status.
kubectl get node
Deploy CNI Network Plugin#
The above status is still NotReady, we need a network plugin to enable network access.
# Add
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
# Check status
kubectl get pods -n kube-system
At this point, there are still pods not in the running state. Wait a little longer and check the status again. When all are in the running state, execute the following command to check:
kubectl get nodes
After the operation is complete, you can see that it has changed to Ready state. If there are still nodes in NotReady state, wait a little longer, and then execute kubectl get pods -n kube-system
command. If the status is running, execute kubectl get node
to check.
Test Kubernetes Cluster#
Create a pod in the Kubernetes cluster to verify if it runs normally:
# Download nginx
kubectl create deployment nginx --image=nginx
# Check status
kubectl get pod
When it shows Running status, it indicates that it has been successfully running.
Next, we need to expose the port so that others can access it.
# Expose port
kubectl expose deployment nginx --port=80 --type=NodePort
# Check the external port
kubectl get pod,svc
You can see that we have successfully exposed port 80 to 30374.
In the browser, visit the following address (any node's IP plus the displayed port).
http://192.168.66.102:30374/
You will find that our nginx has successfully started.