- Published on
Setting Up a Kubernetes Cluster With Containerd
- Authors
- Name
- Surya Harahap
- @suryaharahap18
containerd works well for running single containers or small groups of containers on a single host. But what if you have more than a few containers, or want those containers to be spread across multiple servers, or even made highly available?
For these scenarios, you'll need a container orchestrator. Kubernetes is a popular container orchestration framework that easily works with containerd. In this lab, you'll learn to set up a Kubernetes cluster with containerd.
Configure the Kubernetes Cluster
- On all three nodes, add the Kubernetes repo to /etc/yum.repos.d:
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
- Install the repo:
[kubernetes] name=Kubernetes baseurl=https://pkgs.k8s.io/core:/stable:/v1.30/rpm/ enabled=1 gpgcheck=1 gpgkey=https://pkgs.k8s.io/core:/stable:/v1.30/rpm/repodata/repomd.xml.key exclude=kubelet kubeadm kubectl cri-tools kubernetes-cni EOF
- Disable SELinux:
sudo setenforce 0 sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
- Install Kubernetes:
sudo yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
- Enable and start kubelet:
sudo systemctl enable --now kubelet 6. Check your containerd status on all nodes:
sudo ctr -n k8s.io containers list
- From Node 1, initialize the controller node, and set the code network CIDR to 10.244.0.0/16:
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
- Run the following commands to start using the cluster:
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
- Copy the sudo kubeadm join command, then paste and run it in your Node 2, and Node 3 terminal windows.
sudo kubeadm join <YOUR_IP>:6443 --token <YOUR_TOKEN> \ --discovery-token-ca-cert-hash sha256:<YOUR_HASH>
- From the worker nodes, verify that they can see the cluster:
sudo ctr -n k8s.io containers list
- From Node 1, check the status of the nodes:
kubectl get nodes
- Install Flannel:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
Create a Pod
- Create the pod.yml file:
vim pod.yml
- In the file, paste the following:
apiVersion: v1 kind: Pod metadata: name: nginx-pod-demo labels: app: nginx-demo spec: containers: - image: nginx:latest name: nginx-demo ports: - containerPort: 80 imagePullPolicy: Always
- Save the file:
ESC
:wq
- Create the pod:
kubectl apply -f pod.yml
- Check the status of the pod:
kubectl get pods
Create the Service
- Create the service.yml file:
vim service.yml
- In the file, paste the following:
apiVersion: v1 kind: Service metadata: name: service-demo spec: selector: app: nginx-demo ports: - protocol: TCP port: 80 targetPort: 80 type: NodePort
- Save the file:
ESC :wq
- Create the service:
kubectl apply -f service.yml
- Run the following command to view the service:
kubectl get services
- Take note of the service-demo port number.
- In a web browser, navigate to the public IP address for a server in the cluster, and verify connectivity:
<PUBLIC_IP_ADDRESS>:<SERVICE_DEMO_PORT_NUMBER>
END