Kubernetes Architecture, Components, Install, Config, Creating a Cluster and Deploy Demo App
#Day1 - #KubeWeek challenge
Table of contents
No headings in the article.
▶What is Kubernetes?
It is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It has a large, rapidly growing ecosystem. (kubernetes.io)
▶Kubernetes Components
A Kubernetes cluster consists of two main components:
\>>The Control plane (Master node) that coordinates the cluster
\>>(Worker) Nodes are the workers that run applications
Following, I will summarize each component's main facts with their sub-components to give you a basic understanding of what each part is doing.
⇨ Control plane
>> API server*:* Exposes the Kubernetes API and the API for the Kubernetes control plane
>> kube-scheduler*:* watches for newly created Pods and selects a node for them to run on
>> kube-controller-manager*:* Control Plane component that runs controller processes like (1) Node controller (Is the node running?), (2) Replication controller (Is there a correct number of pods for every replication), (3)Endpoints controller (Endpoints object, joins Services & Pods), (4) Service Account & Token controllers (Create default accounts and API access tokens for new namespaces)
>> cloud-controller-manager (optional)*:* link your cluster to your cloud provider’s API
>> etcd*:* key-value store used as Kubernetes’ backing store for all cluster data
⇨Nodes
>> Kubelet*:* An agent that runs on each node in the cluster. It makes sure that containers are running in a Pod
>> Kube-proxy*:* maintains network rules on nodes. These network rules allow network communication to your Pods from network sessions inside or outside of your cluster.
>> Container Runtime*:* Container runtime like Docker
In this article, we will demonstrate how to get started by creating a Kubernetes cluster. 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 tutorial is to enable you to run a demo microservices application on a Kubernetes cluster you have created.
Step 1 - Set up each server in the cluster to run Kubernetes on each of the 3 servers(Control Plane-Master, 2 Workers) run the following commands as root, you can type the commands or highlight and copy/paste them in the terminal window:
\>> Download the Google Cloud public signing key and add the Kubernetes apt repository:
$ curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
$ echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
\>> Update the apt package index with the new repository and install the utilities:
$ apt update
$ apt install -y kubelet=1.20.0-00 kubeadm=1.20.0-00 kubectl=1.20.0-00
$ apt-mark hold kubelet kubeadm kubectl
\>> Install Docker:
$ export VERSION=19.03 && curl -sSL get.docker.com | sh
Step 2 - Setup the Kubernetes Control Plane
Next, we are going to configure the Docker daemon to use systemd
for the management of the container’s cgroups
, copy the following block and paste it in the terminal window of each node (Control Plane, Worker-02, Worker-03):
$ mkdir /etc/docker
cat <<EOF | sudo tee /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}
EOF
$ systemctl enable docker
$ systemctl daemon-reload
$ systemctl restart docker
\>> On the Control Plane node only, run this command in order to initialize the Kubernetes control plane:
$ kubeadm init --ignore-preflight-errors=all
This can take a minute or two to run.
Before we begin using the cluster, we need to configure the kubectl
command line tool that let us control the cluster. For configuration, kubectl
looks for a file named config in the $HOME/.kube
directory.
\>> Create the directory, and copy the config file generated by kubeadm and
changing ownership of the file to the currently logged-in user.
$ mkdir -p $HOME/.kube
$ $HOME/.kube/: cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ chown $(id -u):$(id -g) $HOME/.kube/config
Your Kubernetes Control Plane has initialized successfully!
Step 3 - Join your nodes in your Kubernetes cluster
You can now join any number of machines by running the kubeadm
join command on each node as root.
\>> To join Worker-02 and Worker-03 as worker nodes in the cluster, we need the token generated by the kubeadm init
command.
Run the following command on the Control Plane node:
$ kubeadm token create --print-join-command
Copy the output and run it on both worker nodes (Worker-02, Worker-03).
When you join your worker nodes you will 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 Control Plane node:
$ kubectl get nodes
The successful result will look like this:
The status will remain NotReady until we configure networking on the Control Plane 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 add-ons for a range of cluster features including Networking and Visualization.
We are going to install the Weave Net Add-On on the 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.
Weave-net is a CNI plugin which provides a virtual network that connects docker containers across multiple hosts.
By default Kubernetes comes with a simple network plugin that does not have the overlay networking capabilities.
Next, we will deploy a pod network to the cluster.
The options are listed at: https://kubernetes.io/docs/concepts/cluster-administration/addons/
\>> Installing the Weave Net Add-On:
On the Control Plane node, run the kubectl apply
command which will create all the networking resources in the cluster from the supplied manifest file:
$ kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s-1.11.yaml
The result will look like this:
It may take a minute or two for DNS to be ready, continue to check for DNS to be ready before moving on by running the kubectl get
command which allows us to display the resources and prints a table of the most important information about the specified resources:
$ watch kubectl get pods --all-namespaces
Ctrl-c
to exit.
The successful result will look like this, every container should be running:
Check the status of the nodes again: kubectl get nodes
Congratulations, now your Kubernetes cluster running up and ready for you to deploy a microservices application!!
Step 5 - Deploying The Microservices Sock Shop
Next, we will deploy a demo microservices application to your Kubernetes cluster.
\>> First, on Control Plane, clone the microservices sock shop git repo:
$ git clone https://github.com/microservices-demo/microservices-demo.git
\>> Go to the microservices-demo/deploy/kubernetes folder:
$ cd microservices-demo/deploy/kubernetes
\>> Next apply the demo to your Kubernetes cluster:
$ kubectl apply -f complete-demo.yaml
You will see the following result
i.e. from the YAML file
\>> Check to see if all of your pods are running:
$ watch kubectl get pods --namespace sock-shop
Press Ctrl-C
to exit.
You will see that when all pods are ready, they will have the status of “Running”.
Now, go to your browser and try to open the Weave Sock Shop application.
Congratulations! You have successfully completed this hands-on.
Conclusion
You have created a Kubernetes cluster and learned how to use the Kubernetes command-line tool kubectl
. You then deployed Weave Socks Shop Microservices Application as a demonstration of how to run microservices on Kubernetes.