What is Kubernetes?
Kubernetes or k8s is a free, open-source platform for managing containerized application environments and services. Kubernetes allows you to create portable and highly extensible containerized applications which are easy to deploy and manage. It is commonly used alongside Docker to develop better control of containerized applications and services.
Features of Kubernetes
The following are the essential features offered by Kubernetes:
- Automated rollouts and rollbacks in case errors occur.
- Auto scalable infrastructure.
- Horizontal scaling
- Load balancers
- Automated health checks and self-healing capabilities.
- Highly predictable infrastructure
- Mounts and storage system to run applications
- Efficient resource usage
- Each Kubernetes unit is loosely tied with each other, and each can act as a standalone component.
- Automatic management of security, network, and network components.
Understanding the Kubernetes architecture will help you gain a deeper knowledge of how to work with Kubernetes.
The following are hardware components of the Kubernetes architecture:
A node is a representation of a single machine in a Kubernetes cluster. The node represents a single worker machine, either in a virtual machine or physical hardware.
Each node in Kubernetes comprises various Kubernetes software components such as Pods, Kubulet, kube-proxy, and the container runtime such as Docker.
A pod refers to one or more containerized applications bundled together. Kubernetes manages the respective pods instead of the containers and creates replicas if one of them fails. Pods provide shared resources such as network interfaces and storage devices.
A container is an isolated, self-contained software package. A container contains everything required to run an application, including the code, system libraries, and other dependencies. Once a container is deployed, you cannot change the code as they are immutable. By default, Kubernetes container runtime is provided by Docker.
The kubelet is a simple application that allows communication between the node and the master node. It is responsible for managing the pods and the containers. The master node uses the kubectl to perform the necessary actions to the specific node.
The kube-proxy is a network proxy that is available in all Kubernetes nodes. It manages network communications inside and outside of the cluster.
A cluster is a collection of Kubernetes nodes which aggregate their resources to create a powerful machine. Resources shared by the Kubernetes nodes include memory, CPU, and disks.
A Kubernetes cluster is made up of one master node and other slave nodes. The master node controls the Kubernetes cluster, including scheduling and scaling applications, pushing and applying updates, and managing the cluster states.
Persistent volumes are used to store data in a Kubernetes cluster. Persistent volumes are made of o various volumes from the cluster nodes. Once a node in the cluster is removed or added, the master node distributes the work efficiently.
The Kubernetes Ingress is an API object that allows access to the Kubernetes service outside the cluster. Ingress typically uses HTTP/HTTPS protocol to expose the services. An ingress is implemented to a cluster using an ingress controller or a load balancer.
The master, also known as the control plane, refers to the central control component for the Kubernetes architecture. It is responsible for managing the workload and interfacing communication between the cluster and its members.
The master is comprised of various components. These include:
- Control manager
- API server
The control manager or kube-control-manager is responsible for running and managing the cluster. The control daemon collects information about the cluster and reports them back to the API server.
The kube-scheduler or simply the scheduler is responsible for the distribution of the workload. It performs functions such as determining if the cluster is healthy, creation of container, and more.
It keeps track of resources such as memory and CPU and schedules pods to the appropriate compute nodes.
The kube-apiserver is a front-end interface to the Kubernetes master. It allows you to talk to the Kubernetes cluster. Once the API server receives a request, it determines if the request is valid and processes it if true.
To interact with the API server, you need to use REST calls via the command-line control tools such as kubectl or kubeadm.
The ETCD is a key-value database responsible for storing configuration data and information about the state of the Kubernetes cluster. It receives commands from other cluster components and performs the necessary actions.
This section covers how to get started with Kubernetes. This illustration is tested on a Debian system.
Launch the terminal and update your system.
sudo apt-get upgrade
Next, install various requirements as shown in the command below:
sudo apt-get install curl gnupg ca-certificates apt-transport-https
Next, we need to install Docker as Kubernetes will use it as the container runtime. The instructions below are for Debian operating system. Learn how to install docker on your system.
Add the Docker Official GPG key:
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo
gpg –dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Next, setup the Docker repositories to the sources as shown in the command:
“deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian
$(lsb_release -cs) stable” | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Next, update and install Docker:
sudo apt-get update && sudo apt-get install docker-ce containerd.io docker-ce-cli
Finally, start and enable the Docker service
sudo systemctl enable docker.service
sudo systemctl start docker.service
Next, we need to install Kubernetes on the system. Similarly, the instructions in this guide are tested on a Debian system.
Learn how to install Kubernetes on your system
Start by downloading the Google Cloud signing key:
sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
Next, add Kubernetes repository:
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
Finally, update and install Kubernetes components
sudo apt-get update
sudo apt-get install kubectl kubelet kubeadm kubernetes kubernetes-cni
Initialiaze Kubernetes Master Node
The next step is to start the Kubernetes master node. Before doing this, it is recommended to turn off the swap.
To do this, use the command:
Once you turn off swap, initialize the master node with the commands:
Once the command executes successfully, you should get three commands.
Copy and run the commands as:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Deploy Pod Network
The next step is to deploy a Pod network. In this guide, we will use the AWS VPC CNI for Kubernetes.
Use the command as:
sudo kubectl apply -f https://raw.githubusercontent.com/aws/amazon-vpc-cni-k8s/v1.8.0/config/v1.8/aws-k8s-cni.yaml
Upon completion, ensure the cluster is up and running by running the command:
sudo kubectl cluster-info
It would be best if you got output as shown:
Kubernetes control plane is running at https://192.168.43.29:6443
CoreDNS is running at https://192.168.43.29:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To show all the running nodes, use the command:
Deploy an Application
Let us deploy a MySQL application and expose the service on port 3306. Use the command as shown:
sudo kubectl apply -f https://k8s.io/examples/application/mysql/mysql-deployment.yaml
sudo kubectl expose deployment mysql –port=3306 –name=mysql-server
To show the list of deployments, use the command:
To get information about the pods, use the command:
sudo kubectl describe pods
To Sum Up
Kubernetes is a robust container deployment and management tool. This tutorial only scratches the surface of Kubernetes and its capabilities.