Surya.dev
Published on

Setting Up a Kubernetes Cluster With Containerd

Authors

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

  1. On all three nodes, add the Kubernetes repo to /etc/yum.repos.d:
    cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
    
  2. 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
    
  3. Disable SELinux:
    sudo setenforce 0
    sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
    
  4. Install Kubernetes:
    sudo yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
    
  5. Enable and start kubelet:

sudo systemctl enable --now kubelet 6. Check your containerd status on all nodes:

sudo ctr -n k8s.io containers list
  1. 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
    
  2. 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
    
  3. 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>
    
  4. From the worker nodes, verify that they can see the cluster:
    sudo ctr -n k8s.io containers list
    
  5. From Node 1, check the status of the nodes:
    kubectl get nodes
    
  6. Install Flannel:
    kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
    

Create a Pod

  1. Create the pod.yml file:
    vim pod.yml
    
  2. 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
    
  3. Save the file:
ESC
:wq
  1. Create the pod:
kubectl apply -f pod.yml
  1. Check the status of the pod:
kubectl get pods

Create the Service

  1. Create the service.yml file:
    vim service.yml
    
  2. 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
    
  3. Save the file:
    ESC
    :wq
    
  4. Create the service:
    kubectl apply -f service.yml
    
  5. Run the following command to view the service:
    kubectl get services
    
  6. Take note of the service-demo port number.
  7. 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