Mastering Kubernetes: Setting up a cluster and deploying a demo app with Weaveworks Socks Shop on Ubuntu 20.04.
Table of contents
- Introduction
- Prerequisites
- Step 1 — Get each of the servers in the cluster to run Kubernetes.
- Step 2 - Set up each server in the cluster to run Kubernetes.
- Step 3 - Initialize the Cluster and Master Node
- Step 4 - Join the Worker Nodes to your Kubernetes Cluster
- Step 4: Setup a Kubernetes Add-On For Networking
- Step 5: Installing the Weave Net Add-On
- Step 6 - Deploying The Weaveworks Microservices Sock Shop
Introduction
Kubernetes is a system designed to manage applications built within containers across clustered environments. It handles the entire life cycle of a containerized application including deployment and scaling.
In this guide, we'll demonstrate how to get started by creating a Kubernetes cluster on Ubuntu 20.04. We will be using kubeadm to set up Kubernetes. We will then deploy the Weaveworks Socks Shop Microservices Application as a demonstration of how to run microservices on Kubernetes.
The purpose of this guide is to enable you to run a demo microservices application on a Kubernetes cluster you have created.
This article marks the first in a series i intend writing dedicated to mastering Kubernetes. In subsequent series, we will explore how to set up a Kubernetes cluster using both the eksctl command-line utility and Terraform.
Prerequisites
you would need the following along the course of this guide:
3 Ubuntu 20.04 servers with 4GM RAM and private networking enabled i.e open up some ports on your machine.
Step 1 — Get each of the servers in the cluster to run Kubernetes.
To set up a three-member cluster for the Socks Shop Demo application, begin by creating three Ubuntu 20.04 servers with a minimum of 4GB RAM and Private Networking enabled. Once created, name these servers kube-01, kube-02, and kube-03.
Kubernetes would need to assign role to each of the servers. we would have to set up one of the servers to be the master node. let's make kube-01 the master,
Step 2 - Set up each server in the cluster to run Kubernetes.
SSH to each of the servers you created. Proceed with executing the following commands as root. You may become the root user by executing sudo su
after SSH-ing to each host.
On each of the three Ubuntu 20.04 servers run the following commands as root:
1. apt-get update && apt-get install -y apt-transport-https
2. curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
3. cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
4. deb http://apt.kubernetes.io/ kubernetes-xenial main
5. EOF
6. apt-get update
7. apt-get install -y kubelet=1.15.4-00 kubeadm=1.15.4-00 kubectl=1.15.4-00 docker.io
Kubelet: The primary agent that runs on a kubernetes node and communicates with the control plane to issue commands to this node and report status
Kubeadm: A tool that performs the actions necessary to build up a Kubernetes cluster
Kubectl: The command line tool that lets you control Kubernetes clusters and issue commands to the api-server
Docker Engine: A container engine used by Kubernetes to run containers
Step 3 - Initialize the Cluster and Master Node
On the kube-01, which is the master node run the following command:
kubeadm init --ignore-preflight-errors=all
This can take a minute or two to run.
To start using your cluster, you need to configure the kubectl
command line tool that helps us control the cluster, run the following as a regular user on the kube-01:
1. mkdir -p $HOME/.kube
2. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
3. sudo chown $(id -u):$(id -g) $HOME/.kube/config
Overview of kubernetes architecture
Step 4 - Join the Worker Nodes to your Kubernetes Cluster
At this point, you should have a Kubernetes cluster with a single master node joined. We need to join the other worker nodes to the cluster. To do that, instead of using kubeadm init
we use `kubeadm join .
You can join any number of machines to the master node by running the kubeadm join
command on each node as root.
Run the following command on the master node{kube-01}:
kubeadm token create --print-join-command
It should generate an output for us, you then copy the output and run it on both worker nodes {kube-02, kube-03}.
An example of what this looks like is below:
kubeadm join --token 702ff6.bc7aacff7aacab17 174.138.15.158:6443 --discovery-token-ca-cert-hash sha256:68bc22d2c631800fd358a6d7e3998e598deb2980ee613b3c2f1da8978960c8ab
After running the command to join your worker nodes you should see the following output:
This node has joined the cluster:*
Certificate signing request was sent to Control Plane and a response was received.* The Kubelet was informed of the new secure connection details.`
To check that all nodes are now joined to the Control Plane run the following command on the Kubernetes master node{kube-01}:
kubectl get nodes
you should get this result below if it works succesfully
NAME STATUS ROLES AGE VERSION
kube-01 NotReady control-plane,master 3m56s v1.x.x
kube-02 NotReady <none> 84s v1.x.x
kube-03 NotReady <none> 84s v1.x.x
As you can see from above the status is indicating NotReady
Status will remain so until we configure the networking on the master node.
Step 4: Setup a Kubernetes Add-On For Networking
Kubernetes Add-Ons are pods and services that implement cluster features. Pods extend the functionality of Kubernetes. You can install addons for a range of cluster features including Networking and Visualization.
We are going to install the Weave Net Add-On on the kube-01 Control Plane, which provides networking and network policy, will carry on work on both sides of a network partition, and does not require an external database. Read more about the Weave Net Add-on in the Weave Works Docs.
You then proceed to deploy a pod network to the cluster.
The options are listed at: https://kubernetes.io/docs/concepts/cluster-administration/addons/
Step 5: Installing the Weave Net Add-On
To create all the networking resources in the cluster from the supplied manifest file, execute the kubectl apply
command on the master node.
Get the Weave Net yaml:
kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml
On the kube-01 Kubernetes master node run the following commands:
kubectl apply -f weave.yaml
You should get the following output:
serviceaccount "weave-net" created
clusterrole "weave-net" created
clusterrolebinding "weave-net" created
role "weave-net" created
rolebinding "weave-net" created
daemonset "weave-net" created
You would have to be a bit patient here as it might take a couple minute for the DNS to be active. After that, we then proceed to run the following command:
kubectl get pods --all-namespaces
The output should give you something like this below
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system etcd-kube-01 1/1 Running 0 5m
kube-system kube-apiserver-kube-01 1/1 Running 0 6m
kube-system kube-controller-manager-kube-01 1/1 Running 0 5m
kube-system kube-dns-6f4fd4bdf-whbhd 3/3 Running 0 6m
kube-system kube-proxy-2hdhk 1/1 Running 0 6m
kube-system kube-proxy-tvhjk 1/1 Running 0 5m
kube-system kube-proxy-wspmv 1/1 Running 0 5m
kube-system kube-scheduler-kube-01 1/1 Running 0 6m
kube-system weave-net-9ghn5 2/2 Running 1 5m
kube-system weave-net-lh8tq 2/2 Running 0 5m
kube-system weave-net-qhr25 2/2 Running 0 5m
You then CTRL+C
to exit.
We then check the status of the nodes again
kubectl get nodes
NAME STATUS ROLES AGE VERSION
kube-01 Ready Control Plane 8m v1.x.x
kube-02 Ready <none> 6m v1.x.x
kube-03 Ready <none> 6m v1.x.x
whoop, now we have our kubernetes cluster running on ubuntu 20.04, we can now proceed to deploy our microservices application on it.
Step 6 - Deploying The Weaveworks Microservices Sock Shop
We have now gotten to the point of deploying the microservices application sock shop on our kubernetes cluster
To do this, we would have to clone into the microservices socks shop repository on the control plane{kube-01}:
git clone https://github.com/microservices-demo/microservices-demo.git
we then cd
to the microservices-demo/deploy/kubernetes folder:
cd microservices-demo/deploy/kubernetes
Next apply the demo to our kubernetes cluster:
kubectl apply -f complete-demo.yaml
we should see the following result:
deployment "carts-db" created
service "carts-db" created
deployment "carts" created
service "carts" created
deployment "catalogue-db" created
service "catalogue-db" created
deployment "catalogue" created
service "catalogue" created
deployment "front-end" created
service "front-end" created
deployment "orders-db" created
service "orders-db" created
deployment "orders" created
service "orders" created
deployment "payment" created
service "payment" created
deployment "queue-master" created
service "queue-master" created
deployment "rabbitmq" created
service "rabbitmq" created
deployment "shipping" created
service "shipping" created
deployment "user-db" created
service "user-db" created
deployment "user" created
service "user" created
We then check to see if all our pods are working:
watch kubectl get pods --namespace sock-shop
You will see the following result when all pods are ready, they will have the status of “Running”:
NAMESPACE NAME READY STATUS RESTARTS AGE
sock-shop carts-74f4558cb8-h9924 1/1 Running 0 11m
sock-shop carts-db-7fcddfbc79-v64fw 1/1 Running 0 11m
sock-shop catalogue-676d4b9f7c-55n4g 1/1 Running 0 11m
sock-shop catalogue-db-5c67cdc8cd-hvk96 1/1 Running 0 11m
sock-shop front-end-977bfd86-hq9x9 1/1 Running 0 11m
sock-shop orders-787bf5b89f-xfdl6 1/1 Running 0 11m
sock-shop orders-db-775655b675-gv456 1/1 Running 0 11m
sock-shop payment-75f75b467f-4zzqs 1/1 Running 0 11m
sock-shop queue-Control Plane-5c86964795-t8sjg 1/1 Running 0 11m
sock-shop rabbitmq-96d887875-lf46w 1/1 Running 0 11m
sock-shop shipping-5bd69fb4cc-vprmp 1/1 Running 0 11m
sock-shop user-5bd9b9c468-4rms8 1/1 Running 0 11m
sock-shop user-db-5f9d89bbbb-r69pd 1/1 Running 0 11m
You can now Visit the Sock Shop application.
We have successfully established a Kubernetes cluster and learned how to use the Kubernetes command-line utility, kubectl. As a demonstration of how to run microservices on Kubernetes, we deployed the Weave Socks Shop Microservices Application.