DevOps & Automation

Mastering Kubernetes: A Guide to Efficient Application Deployment

Mastering Kubernetes: A Guide to Efficient Application Deployment

Introduction to Application Deployment with Kubernetes

In the previous article on Setting up a Kubernetes, we understood the architecture of Kubernetes (k8s) and learned about the basic setup of Kubernetes. In this article, we will start the Application Deployment with a Kubernetes cluster and understand the management of pods.

Understanding Kubernetes Objects

Kubernetes has a variety of objects that define a desired state for your deployed applications. We can define them as below:-

Pods

The smallest and most basic deployable objects in Kubernetes. They can hold one or more containers (like Docker). Think of them as a single instance of a running process in a cluster.

Deployments

A Deployment provides declarative updates for Pods. It allows you to describe an application’s life cycle, such as which images to use, the number of pod replicas, and how to roll out updates, among other aspects.

Services

A way to expose your application. If you have pods that need to communicate or be accessible via the network, you use a Service. They can also load balance traffic across a set of pods.

ConfigMaps and Secrets

Both used to store configuration data and sensitive information respectively. While ConfigMaps are for non-confidential data, Secrets are better suited for storing sensitive data.

Volumes

They allow you to persist data and provide storage for your pods, abstracting the underlying storage systems.

Namespaces

Logical clusters within your Kubernetes cluster. Useful for dividing cluster resources between multiple users.

Application Deployment with Kubernetes

Creating and Managing Pods

1. Creating a Pod

We  use a YAML or JSON file to define the Pod. Here’s a simple example of a Pod manifest:

apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: nginx:1.14.2
view raw gistfile1.txt hosted with ❤ by GitHub

To create the Pod:

kubectl apply -f pod.yaml
view raw gistfile1.txt hosted with ❤ by GitHub

2. Managing the Pod

Check the status of the Pod:

kubectl get pods
view raw gistfile1.txt hosted with ❤ by GitHub

To describe the details of a specific Pod:

kubectl describe pod my-pod
view raw gistfile1.txt hosted with ❤ by GitHub

To delete the Pod:

kubectl delete pod my-pod
view raw gistfile1.txt hosted with ❤ by GitHub

Scaling Applications

1. Scaling with Deployments

Deployments are the recommended way to manage the scaling and lifecycle of your pods. Here’s a simple Deployment manifest:

apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
view raw gistfile1.txt hosted with ❤ by GitHub

To scale up:

kubectl scale deployment my-deployment --replicas=5
view raw gistfile1.txt hosted with ❤ by GitHub

To scale down:

kubectl scale deployment my-deployment --replicas=2
view raw gistfile1.txt hosted with ❤ by GitHub

2. Using Horizontal Pod Autoscaling

Automatically scales the number of pods based on CPU usage or other select metrics.

First, deploy the metrics server:

Kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.3.6/components.yml
view raw gistfile1.txt hosted with ❤ by GitHub

Then, set up the autoscaler:

kubectl autoscale deployment my-deployment --min=2 --max=10 --cpu-percent=80
view raw gistfile1.txt hosted with ❤ by GitHub

Updating and Rolling Back Deployments

1. Updating a Deployment

Simply modify your Deployment manifest, and then apply it:

kubectl apply -f deployment.yaml
view raw gistfile1.txt hosted with ❤ by GitHub

Kubernetes will handle the process of updating the Pods.

2. Checking the Rollout Status

Monitor the status of a Deployment update with:

kubectl rollout status deployment my-deployment
view raw gistfile1.txt hosted with ❤ by GitHub

3. Rolling Back a Deployment

If something goes wrong, Kubernetes allows you to rollback your Deployment:

kubectl rollout undo deployment my-deployment
view raw gistfile1.txt hosted with ❤ by GitHub

To rollback to a specific revision:

kubectl rollout undo deployment my-deployment --to-revision=2
view raw gistfile1.txt hosted with ❤ by GitHub

Conclusion

Kubernetes provides a robust platform for deploying and managing containerized applications at scale. With the knowledge of key Kubernetes objects and the associated commands, we are now equipped to deploy, scale, update, and manage your applications efficiently.