Infrastructure Requirements
Before initiating the deployment, ensure your environment meets the following specifications:
- Operating System: CentOS 7.x (64-bit).
- Hardware: Minimum 2 CPUs, 2GB RAM, and 30GB disk space.
- Connectivity: Full network interoperability between all nodes and internet access for image retrieval.
- System State: Swap must be permanently disabled.
Node Configuration
This setup utilizes three virtual instances:
- Control Plane (Master): 192.168.31.61 (hostname:
k8s-master) - Worker Node 01: 192.168.31.62 (hostname:
k8s-node1) - Worker Node 02: 192.168.31.63 (hostnmae:
k8s-node2)
OS Initialization
Execute these configuration steps on all nodes to prepare the environment.
Security and Swap Management
Disable the firewall and SELinux to prevent interference with inter-node communication:
systemctl stop firewalld && systemctl disable firewalld
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config
Turn off swap space to ensure the Kubelet functions correctly:
swapoff -a
sed -i '/swap/s/^/#/' /etc/fstab
Networking and Host Resolution
Map the hostnames in /etc/hosts for consistent resolution:
cat <<EOF >> /etc/hosts
192.168.31.61 k8s-master
192.168.31.62 k8s-node1
192.168.31.63 k8s-node2
EOF
Configure the kernel to allow bridged IPv4 traffic to reach iptables chains:
cat <<EOF > /etc/sysctl.d/kubernetes.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
Sync the system clock:
yum install ntpdate -y
ntpdate time.windows.com
Installing Container Runtime and Kubernetes Toooling
Kubernetes requires a Container Runtime Interface (CRI). Here, we install Docker CE and the Kubernetes binaries (kubeadm, kubectl, kubelet).
Docker Setup
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum -y install docker-ce-18.06.1.ce-3.el7
systemctl enable docker && systemctl start docker
Kubernetes Repositories and Packages
Configure the package manager to use the Kubernetes mirror and install the toolset:
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
EOF
yum install -y kubelet kubeadm kubectl
systemctl enable kubelet
Initializing the Control Plane
On the Master node (192.168.31.61), initialize the cluster. We specify a mirror repository to avoid connection issues with the default Google registry.
kubeadm init \
--apiserver-advertise-address=192.168.31.61 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.17.0 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16
After the processs completes, configure kubectl for the local user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Deploying the Pod Network
Apply the Flannel CNI plugin to enable networking between Pods:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
Integrating Worker Nodes
On each worker node, use the join command generated during the kubeadm init phase. It will look similar to this:
kubeadm join 192.168.31.61:6443 --token <token> --discovery-token-ca-cert-hash sha256:<hash>
Verify that all nodes have successfully joined the cluster by running kubectl get nodes on the Master.
Verifying the Cluster
Deploy a sample Nginx workload to confirm the cluster is operational:
kubectl create deployment web-server --image=nginx
kubectl expose deployment web-server --port=80 --type=NodePort
kubectl get pods,svc
You can access the Nginx service via the assigned NodePort on any cluster node's IP address.