Skip to main content

Setting Up a Kubernetes Cluster with Docker

Hands-On Lab

 

Photo of Travis Thomsen

Travis Thomsen

Course Development Director in Content

Length

01:30:00

Difficulty

Intermediate

For the last six months, the Acme Anvil Corporation has been migrating some of their bare metal infrastructure to Docker containers. A schism has developed between the members of your team on whether to use Docker Swarm or Kubernetes. To settle the dispute, your manager has decided to create a series of challenges. You have been tasked with helping to create the Kubernetes cluster. Create a Kubernetes cluster with 3 nodes and then create a pod and service using Nginx.

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.

Setting Up a Kubernetes Cluster with Docker

Introduction

In this hands-on lab, we will create a Kubernetes cluster with 3 nodes and then create a pod and service using Nginx.

Log In to the Environment

Log In to the Node 1 Server

  1. Navigate to the lab instructions page, and copy the Node 1 Public IP address to your clipboard.
  2. Open your terminal application and run the following command:
    ssh cloud_user@<NODE1_PUBLIC_IP>
  3. Type yes at the prompt.
  4. Enter your password from the lab instructions page when prompted.
  5. Elevate permissions to root.
    sudo su -
  6. Enter your password again.

Log In to the Node 2 Server

  1. Go back to the lab instructions page, and copy the Node 2 Public IP address to your clipboard.
  2. Open a new window of your terminal application and run the following command:
    ssh cloud_user@<NODE2_PUBLIC_IP>
  3. Type yes at the prompt.
  4. Enter your password from the lab instructions page when prompted.
  5. Elevate permissions to root.
    sudo su -
  6. Enter your password again.

Log In to the Node 3 Server

  1. Go back to the lab instructions page, and copy the Node 3 Public IP address to your clipboard.
  2. Open a new window of your terminal application and run the following command:
    ssh cloud_user@<NODE3_PUBLIC_IP>
  3. Type yes at the prompt.
  4. Enter your password from the lab instructions page when prompted.
  5. Elevate permissions to root.
    sudo su -
  6. Enter your password again.

We are now successfully logged in to the cloud server environment.

Configure the Kubernetes Cluster

  1. In your Node 1 terminal window, add the Kubernetes repo to /etc/yum.repos.d.
    cat << EOF > /etc/yum.repos.d/kubernetes.repo
  2. Install the repo.
    [kubernetes]
    name=Kubernetes
    baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
    enabled=1
    gpgcheck=1
    repo_gpgcheck=1
    gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
    exclude=kube*
    EOF
  3. Disable SELinux.
    setenforce 0
  4. Install Kubernetes.
    yum install -y kubelet-1.11.3 kubeadm-1.11.3 kubectl-1.11.3 --disableexcludes=kubernetes
  5. Enable and start kubelet.
    systemctl enable kubelet && systemctl start kubelet
  6. Run the following command to set up the bridge network:
    cat <<EOF > /etc/sysctl.d/k8s.conf
    net.bridge.bridge-nf-call-ip6tables = 1
    net.bridge.bridge-nf-call-iptables = 1
    EOF
  7. Reload the system so that our changes take effect.
    sysctl --system
  8. Initialize the master node, and set the code network CIDR to 10.244.0.0/16.
    kubeadm init --pod-network-cidr=10.244.0.0/16 --kubernetes-version=v1.11.3
  9. Run the following command 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
  10. Install Flannel on the master node.
    kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.9.1/Documentation/kube-flannel.yml
  11. Repeat Steps 1-7 in your Node 2 and Node 3 terminal windows.
  12. In your Node 1 terminal window, run the following command:
    kubeadmin token create --print-join-command
  13. Copy the output of this command to your clipboard. Then, paste and run it in your Node 2 and Node 3 terminal windows.
  14. Run the following command to verify that everything is working correctly:
    kubectl get nodes

    The output should show three nodes: two with the role of none and one with the role of master.

Create a Pod

  1. Create the pod.yml file.
    vi pod.yml
  2. In the vi text editor, specify the following for the pod.yml file:
    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. Create the pod.
    kubectl create -f pod.yml
  4. Run the following command to view the pod we just created:
    kubectl get pods

Create the Service

  1. Create the service.yml file.
    vi service.yml
  2. In the vi text editor, specify the following for service.yml:
    kind: Service
    apiVersion: v1
    metadata:
    name: service-demo
    spec:
    selector:
      app: nginx-demo
    ports:
    - protocol: TCP
      port: 80
      targetPort: 80
    type: NodePort
  3. Create the service.
    kubectl create -f service.yml
  4. Run the following command to view the service we just created:
    kubectl get services
  5. Copy one of the public IP addresses from the lab instructions page, and paste it in a new tab of your browser, followed by a colon (:).
  6. Copy the service port number from your terminal window, and paste it in the address bar after the public IP address. It should look like this: PUBLIC_IP_ADDRESS:PORT_NUMBER
  7. Load the page. You should see the Nginx welcome screen.

Conclusion

Congratulations, you've successfully completed this hands-on lab!