Introduction
Welcome to the world of Kubernetes, a powerful platform for automating the deployment, scaling, and management of containerized applications. In this step-by-step guide, we’ll walk you through the process of creating your first deployment in Kubernetes, from setting up your environment to scaling your application.
Before we begin, you will need:
- A Kubernetes cluster set up and running. You can create one using services like Google Kubernetes Engine (GKE), Amazon Elastic Kubernetes Service (EKS), or set up a local cluster using Minikube or kind.
- kubectl installed, which is the command line tool for interacting with your Kubernetes cluster.
Step-by-Step Instructions
Step 1: Writing a Deployment Manifest
A deployment in Kubernetes describes a desired state for your application. It tells Kubernetes how many copies of the application to run and how to update the instances with new versions of the application. Let’s start by creating a simple deployment manifest file for running an Nginx server.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
Save this file as nginx-deployment.yaml.
Step 2: Deploying the Application
With the manifest file ready, deploy your application using the kubectl apply command.
$ kubectl apply -f nginx-deployment.yaml
deployment.apps/nginx-deployment created
Kubernetes will create the deployment and launch the specified number of replicas.
Step 3: Verifying the Deployment
You can verify that your deployment was successful by listing the deployments in your cluster.
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 2/2 2 2 10s
Additionally, you can list the pods created by the deployment.
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-XXXXX-XXXXX 1/1 Running 0 15s
nginx-deployment-YYYYY-YYYYY 1/1 Running 0 15s
Step 4: Exposing Your Application
To make your application accessible from outside the Kubernetes cluster, you’ll need to expose it through a service. Here’s how you can create a service for the Nginx deployment.
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
Apply this service using kubectl.
$ kubectl apply -f nginx-service.yaml
service/nginx-service created
You can then find the external IP for your service with the command:
$ kubectl get service nginx-service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-service LoadBalancer 10.100.200.1 198.51.100.1 80:31777/TCP 30s
Step 5: Scaling Your Deployment
If you need more instances of your application, you can easily scale your deployment.
$ kubectl scale deployment/nginx-deployment --replicas=4
deployment.apps/nginx-deployment scaled
Verify the scaling with kubectl get pods, and you’ll see the additional replicas.
Step 6: Updating Your Application
Kubernetes allows you to perform rolling updates with zero downtime. Let’s update the Nginx image version in your deployment.
$ kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1
deployment.apps/nginx-deployment image updated
Kubernetes will start rolling out the change and you can watch the rollout status with:
$ kubectl rollout status deployment/nginx-deployment
Step 7: Rollback Your Application
If something goes wrong with the update, you can roll back to the previous stable version of your application.
$ kubectl rollout undo deployment/nginx-deployment
deployment.apps/nginx-deployment rolled back
Conclusion
Creating a deployment in Kubernetes can be straightforward once you understand the basic concepts and steps involved. With the knowledge from this tutorial, you can start deploying and managing your applications on a Kubernetes platform, making your workflows more efficient and your applications more reliable.