Kubernetes Deployments with Docker Images
Kubernetes deployments allow you to declaratively manage applications, enabling you to scale, update, and maintain applications running in Pods. When working with Docker containers in Kubernetes, the Docker images serve as the foundation for creating Pods, which are the smallest deployable units in Kubernetes.
In this article, we’ll explore how to use Docker images in Kubernetes deployments and provide step-by-step instructions for deploying applications with Docker images in a Kubernetes cluster.
Key Concepts for Kubernetes Deployments with Docker Images
Pod: A Pod is the smallest unit in Kubernetes. It is a group of one or more containers (in our case, Docker containers) that share the same network and storage. A Pod can host a single Docker container or multiple containers working together.
Deployment: A Deployment is a Kubernetes object used to manage a set of identical Pods. Deployments ensure that a specified number of Pods are always running, even when they fail or are updated. A Deployment can be used to deploy Docker containers on a large scale.
Docker Image: A Docker image is a lightweight, stand-alone, and executable package that contains everything needed to run a piece of software — the code, runtime, libraries, and dependencies. In Kubernetes, Docker images are used to define the container that will run inside a Pod.
ReplicaSet: A ReplicaSet ensures that a specified number of identical Pods are running at any given time. It is automatically created by the Deployment controller and manages the replication of Pods.
Container: A container is an instance of a Docker image running inside a Pod. In Kubernetes, containers are encapsulated inside Pods.
Steps to Deploy a Docker Image in Kubernetes
Let's go through the process of deploying a Docker image in a Kubernetes cluster.
Step 1: Prepare Your Docker Image
-
Build the Docker Image: Ensure that you have your Docker image ready. You can create it using a
Dockerfile
and then build it using thedocker build
command.
Example Dockerfile
for a Node.js application:
# Use official Node.js image
FROM node:14
# Set the working directory
WORKDIR /app
# Copy package.json and install dependencies
COPY package.json .
RUN npm install
# Copy the rest of the app
COPY . .
# Expose the application port
EXPOSE 8080
# Run the app
CMD ["npm", "start"]
Build the image:
docker build -t my-node-app .
- Push the Docker Image to a Registry: Once you’ve built your Docker image, you need to push it to a container registry. You can use Docker Hub, Google Container Registry (GCR), Amazon Elastic Container Registry (ECR), or any other container registry.
For Docker Hub:
docker tag my-node-app yourdockerhubusername/my-node-app:latest
docker push yourdockerhubusername/my-node-app:latest
Step 2: Create a Kubernetes Deployment Manifest
A Deployment manifest is a YAML file that defines the desired state of your Kubernetes deployment. It includes the Docker image to be used, the number of replicas (Pods), and other configurations.
Here’s an example deployment.yaml
file to deploy the my-node-app
Docker image:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-node-app-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-node-app
template:
metadata:
labels:
app: my-node-app
spec:
containers:
- name: my-node-app
image: yourdockerhubusername/my-node-app:latest
ports:
- containerPort: 8080
- apiVersion: Specifies the version of the API to use.
-
kind: Defines the type of resource, in this case, a
Deployment
. - metadata: Contains the name of the Deployment.
-
spec: Contains the specifications of the deployment.
- replicas: The number of Pods you want to run.
- selector: Defines the label selector to match Pods.
- template: The template for the Pods that will be created by the Deployment. This includes the Docker image and ports for the container.
Step 3: Apply the Deployment to Kubernetes
Once you have created the deployment.yaml
file, you can apply it to your Kubernetes cluster using the kubectl
command.
kubectl apply -f deployment.yaml
This command tells Kubernetes to create a Deployment and Pods based on the configuration in the YAML file. Kubernetes will pull the Docker image from the registry (Docker Hub in this case) and create Pods to run the application.
Step 4: Expose the Application with a Service
To make your application accessible from outside the Kubernetes cluster, you need to expose it using a Service.
Here's an example service.yaml
file to expose the my-node-app
Deployment:
apiVersion: v1
kind: Service
metadata:
name: my-node-app-service
spec:
selector:
app: my-node-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer
- apiVersion: Specifies the API version for the Service resource.
-
kind: The kind of Kubernetes resource, in this case, a
Service
. -
selector: This selector is used to match Pods with the label
app: my-node-app
. - ports: The port on which the service will be exposed (port 80) and the port the container listens to (8080).
-
type: The type of the Service. In this case,
LoadBalancer
will provision an external load balancer (in cloud environments) to expose the service.
Apply the Service configuration to Kubernetes:
kubectl apply -f service.yaml
Now, your application is exposed on port 80 (or using an external load balancer if in a cloud environment).
Step 5: Verify the Deployment
Once the Deployment and Service are applied, you can verify their status:
- To check the status of the Deployment:
kubectl get deployments
- To check the status of Pods:
kubectl get pods
- To check the status of the Service and get the external IP (if using LoadBalancer):
kubectl get services
Scaling the Deployment
One of the key features of Kubernetes is scaling. You can easily scale the number of replicas (Pods) in your deployment by updating the replicas
field in your Deployment YAML file or by using the kubectl scale
command.
Example:
kubectl scale deployment my-node-app-deployment --replicas=5
This will scale the Deployment to 5 Pods.
Rolling Updates and Rollbacks
Kubernetes supports rolling updates for deployments, ensuring that your application is updated without downtime. When you update the Docker image version or change configurations, Kubernetes will automatically manage the update by gradually replacing Pods while keeping the service available.
To update the Docker image:
- Modify the
deployment.yaml
file with the new image tag:
image: yourdockerhubusername/my-node-app:new-tag
- Apply the changes:
kubectl apply -f deployment.yaml
Kubernetes will perform a rolling update, ensuring no downtime for your application.
To roll back to a previous version of your deployment:
kubectl rollout undo deployment/my-node-app-deployment
Conclusion
Using Docker images in Kubernetes deployments is a powerful way to manage containerized applications at scale. With Kubernetes, you get the benefits of automated scaling, rolling updates, and high availability for your Docker-based applications. By defining your deployments in YAML files, you can manage, scale, and update your applications in a consistent and reliable way.
Top comments (0)