Skip to main content

Installing and Testing the Components of a Kubernetes Cluster

Hands-On Lab

 

Photo of Chad Crowell

Chad Crowell

DevOps Training Architect II in Content

Length

00:30:00

Difficulty

Intermediate

In this lab, you will build a cluster from scratch, as well as test the components of the cluster (including the deployments, pods, port forwarding, and services) and execute a command from within a pod. In order to build the Kubernetes cluster, we need to install the container runtime, as well as kubeadm, kubectl, and kubelet. We will then initialize the cluster, add our CNI, and add the nodes to the cluster.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Installing and Testing the Components of a Kubernetes Cluster

We have been given three nodes, in which we must install the components necessary to build a running Kubernetes cluster. Once the cluster has been built and we have verified all nodes are in the ready status, we need to start testing deployments, pods, services, and port forwarding, as well as executing commands from a pod.

Log in to all three nodes (the controller/master and workers) using the credentials on the lab page (either in your local terminal, using the Instant Terminal feature, or using the public IPs), and work through the objectives listed.

Get the Docker gpg, and add it to your repository.

  1. In all three terminals, run the following command to get the Docker gpg key:

    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  2. In all three terminals, add it to your repository:

    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Get the Kubernetes gpg key, and add it to your repository.

  1. In all three terminals, run the following command to get the Kubernetes gpg key:

    curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
  2. In all three terminals, add it to your repository:

    cat << EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
    deb https://apt.kubernetes.io/ kubernetes-xenial main
    EOF
  3. In all three terminals, update the packages:

    sudo apt-get update

Install Docker, kubelet, kubeadm, and kubectl.

  1. In all three terminals, run the following command to install kubelet, kubeadm, and kubectl:

    sudo apt-get install docker-ce kubelet kubeadm kubectl

Initialize the Kubernetes cluster.

  1. In the master node terminal, run the following command to initialize the cluster using kubeadm:

    sudo kubeadm init --pod-network-cidr=10.244.0.0/16

Set up local kubeconfig.

  1. In the master node terminal, run the following commands to set up local kubeconfig:

    mkdir -p $HOME/.kube
    
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    
    sudo chown $(id -u):$(id -g) $HOME/.kube/config

Apply the flannel CNI plugin as a network overlay.

  1. In the master node terminal, run the following command to apply flannel:

    kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/bc79dd1505b0c8681ece4de4c0d86c5cd2643275/Documentation/kube-flannel.yml

Join the worker nodes to the cluster, and verify they have joined successfully.

  1. In both of the worker node terminals, run the following command to join the worker nodes to the cluster:

    sudo kubeadm join <your unique string from the output of kubeadm init>

Run a deployment that includes at least one pod, and verify it was successful.

  1. In the master node terminal, run the following command to run a deployment of ngnix:

    kubectl run nginx --image=nginx
  2. In the master node terminal, verify its success:

    kubectl get deployments

Verify the pod is running and available.

  1. In the master node terminal, run the following command to verify the pod is up and running:

    kubectl get pods

Use port forwarding to extend port 80 to 8081, and verify access to the pod directly.

  1. In the master node terminal, run the following command to forward the container port 80 to 8081:

    kubectl port-forward <pod_name> 8081:80
  2. Open a new terminal session and log in to the master node. Then, run this command to verify we can access this container directly:

    curl --head http://127.0.0.1:8081

Execute a command directly on a pod.

  1. In the original master node terminal, run this command to execute the nginx version command from a pod (first use the command kubectl get pods to get the pod's name):

    kubectl exec -it <pod_name> -- nginx -v

Create a service, and verify connectivity on the node port.

  1. In the original master node terminal, run the following command to create a NodePort service:

    kubectl expose deployment nginx --port 80 --type NodePort
  2. In the original master node terminal, view the service:

    kubectl get services
  3. In one of the worker node terminals, verify its connectivity (get the $node_port number from the PORT(S) column of the above service output):

    curl -I localhost:$node_port

Conclusion

Congratulations on completing this lab!