How to Create a Basic Kubernetes Deployment⚓
What is Kubernetes?
Kubernetes or k8s is an open-source platform that automates Linux container operations. It eliminates many manual processes in deploying and scaling containerized applications. Clustering together groups of hosts running Linux containers, and Kubernetes helps you easily and efficiently manage those clusters. In Kubernetes, a Pod is the most basic deployable unit within a Kubernetes cluster. A Pod runs one or more containers.
What is a YAML file?
YAML is a human-readable data serialization language, just like XML and JSON. Serialization is a process where one application or service that has different data structures and is written in a different set of technologies can transfer data to another application using a standard format.
Prerequisites
⚓AWS account with elevated privileges
⚓Integrated Deployment Environment (AWS Cloud9)
⚓Basic Knowledge of Containerization
⚓Three Linux virtual environments with Ubuntu
⚓Kubernetes Cluster Created (Instructions Below)
Objective
- Create a deployment that runs a single Nginx image and displays the details of this deployment
- Check the event logs from the deployment and delete the deployment
- Create the same deployment using a YAML file and display the details of this deployment via the command line
- Update the YAML file to scale the deployment to four Nginx containers and verify the change via the command line
Let’s get to it!
Setting up Kubernetes Cluster with Cloud9
We will create a cluster with our control and two worker nodes for this example.
If a new cluster needs to be set up, the following code can be used to install containderd, Kubernetes, and initiate the cluster.
#Setting up Cloud9 with Kubernets
#Choose Ubuntu
#Choose t3.small instances for each environment
#Choose SSH
#Go to EC2 instance of each Cloud9 environment
#Allow all inbound traffic on all instances in each security group
#Following Commands added on control and workers
#Repeat for each desired node
#Newer version of Kubernetes, or K3s can be used
#Kubernetes Prerequisite Commands
cat << EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
cat << EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
sudo sysctl --system
#Update and Install Containerd
sudo apt-get update && sudo apt-get install -y containerd.io
sudo mkdir -p /etc/containerd
sudo containerd config default | sudo tee /etc/containerd/config.toml
sudo systemctl restart containerd
sudo swapoff -a && sudo sed -i '/ swap / s/^/#/' /etc/fstab
sudo apt-get update && sudo apt-get install -y apt-transport-https curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
cat << EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
#Update dependencies, install kubelet, kubeadm, & kubectl (Updated Versions of these can be used)
sudo apt-get update
sudo apt-get install -y kubelet=1.24.0-00 kubeadm=1.24.0-00 kubectl=1.24.0-00
#Set each service to only update manually
sudo apt-mark hold kubelet kubeadm kubectl
###Control node only###
#Initiate the Cluster
sudo kubeadm init --pod-network-cidr=192.168.0.0/16 --kubernetes-version 1.24.0
#After init runs, the next three lines of code will be output
#Run in control node
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
#Ensure all created nodes are visible
kubectl get nodes
#Install Calico (Used for Container Networking in the Cluster)
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
###Worker Nodes COPY AND PASTE THIS FROM THE RESULT OF INIT###
sudo kubeadm join <IP> --token <Token> --discovery-token-ca-cert-hash sha256: This provided after the init. copy and paste. Yours will obviously be different tokens and join IP.
#All container commands should be implemented on Control Node
kubectl get nodes
#Nodes should be created and in Ready Status
#Cluster Created
Step 1: Create a Deployment (Single Node)
Creating a new pod can be accomplished in multiple ways. First, we will launch a single pod from the command line, using the default Nginx image.
kubectl run <Pod Name> --image=<Image Name>
Next, we will display the pod information, verify it is running, and use the internal IP given to demonstrate the web service is running.
kubectl get pods -o wide
curl <IP>
Step 2. Retrieve Event logs and Delete the Deployment
The next task is to retrieve the deployment logs, and then delete our deployment.
kubectl logs <Node Name>
And finally, delete the pod we just created.
kubectl delete pod <Pod Name>
Step 3. Create the same Deployment with a YAML file
This same process above can be completed with a YAML file. This is how more intricate deployments are created. A deployment tells Kubernetes how to create or modify instances of the pods that hold a containerized application.
The YAML file needs to be created using Vi or VIM editor. For this example, I am using the same pod name as above.
sudo vi <Deployment Name>.yml
Before interacting with the text editor, use type :set paste then i to interact if using copy/paste for the code. This will allow for the correct spacing.
:set paste <---- To be entered before interacting with text editor to ensure spacing is correct.
The following code will create a pod, name it, name the container, and use the Nginx container image.
apiVersion: v1
kind: Pod
metadata:
name: <Pod Name>
spec:
containers:
- name: nginx
image: nginx
Run the apply command to apply the file. Curl command with local IP again to test if it is working.
kubectl apply -f <YAML Filename>
curl <Node IP>
Step 4. Scale the deployment to a pod including four Nginx containers
Our single Nginx pod has been deleted again. Time to create a new YAML file that will include four replicas of our Nginx service. These will be spread across both worker nodes. Again open Vi and enter the following code. Do not forget to use the :set paste option again before interacting.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-web-deployment
spec:
replicas: 4
selector:
matchLabels:
app: nginx-web-deployment
template:
metadata:
labels:
app: nginx-web-deployment
spec:
containers:
- name: nginx
image: nginx
Once the YAML file is completed the following code will create the deployment:
sudo kubectl apply -f <Deployment Name>
kubectl get deployments
Our new deployment is listed.
Examine our work in the console. By running get pods again.
kubectl get pods -o wide
All four pods are evenly split across both worker nodes.
For a more detailed look at the deployment:
kubectl describe pods
Let's see what happens when we try to delete a specific node that is part of a deployment.
kubectl delete pod <pod-name>
Below are the results showing the specific pod was deleted.
Surprise! Kubernetes uses a replication controller, which is one of the largest benefits of using Kubernetes. This controller ensures high availability by automatically replacing unhealthy/dead pods with healthy ones to keep them at the desired replica count. As we see below, the pod was immediately replaced by a new node with the same attributes.
Deleting the nodes can be completed in a few different ways:
Reduce the number of replicas to zero. This will keep the deployment, but there will be no pods running.
kubectl scale deployment nginx-web-deployment --replicas=0
Delete the entire deployment with the following:
kubectl delete deploy <Deployment Name>
🎉Congrats! We created a successful Kubernetes Deployment!🎉
Please give me a follow on medium and check out my other articles.👀 Thanks for reading!