Docker containers have become the standard for packaging and deploying applications in modern software development. These containers provide a consistent runtime environment that can be easily moved between development, testing, and production environments. Kubernetes, on the other hand, is the most popular container orchestration platform that provides robust management, scaling, and deployment of containerized applications.
In this tutorial, we will walk you through the process of building and deploying a Docker container to Azure Kubernetes Service (AKS). AKS is a fully managed Kubernetes service provided by Microsoft Azure that simplifies the deployment and management of containerized applications.
Prerequisites
Before we get started, make sure you have the following prerequisites in place:
- An Azure account with active subscription
- Docker installed
- Kubernetes CLI (kubectl) installed
- Azure CLI installed
- Basic understanding of Docker and Kubernetes
Creating a Docker Image for the Application
The first step is to create a Docker image that contains the application code, dependencies, and other necessary files. To create a Docker image, we need a Dockerfile that defines the steps needed to build the image.
In this example, we will be using an example Node.js application to demonstrate building a Docker image. Create a new directory on your computer, navigate to it, and create a new file called Dockerfile
with the following content:
# Define the base image to use
FROM node:14-alpine
# Set working directory for subsequent commands
WORKDIR /app
# Copy package.json and package-lock.json
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy application files to the container image
COPY . .
# Expose the port on which the application listens
EXPOSE 3000
# Start the application
CMD ["npm", "start"]
Here is what the Dockerfile does:
- Line 1: It specifies the base image to use, which is the official Node.js 14 Alpine image.
- Line 3: The
WORKDIR
instruction sets the working directory for subsequent instructions. - Line 6: It copies
package.json
andpackage-lock.json
to the container. - Line 8: It installs the Node.js application dependencies using the
npm install
command. - Line 11: It copies the application files to the container image.
- Line 13: It exposes port 3000 on the container.
- Line 15: It specifies the command to run when the container starts.
This Dockerfile defines a simple Node.js application and installs its dependencies in the container. You can modify it to suit your application needs. Save the Dockerfile and navigate to the application’s root directory in your terminal.
To build the Docker image, run the following command:
docker build -t myapp:v1 .
This command builds a Docker image with the tag myapp:v1
using the Dockerfile located in the current directory. The .
at the end of the command specifies the build context. The build context is the location where Docker looks for files when it builds the image. In this case, it is the current directory. This command may take a few minutes to complete depending on the size of your application and the number of dependencies it has.
Once the image is built successfully, you can test it by running it locally:
docker run -p 3000:3000 myapp:v1
This command runs the Docker image and maps port 3000 in the container to port 3000 on your local machine. The myapp:v1
tag refers to the image you built in the previous step. You should now be able to access the application by navigating to `http://localhost:3000` in your browser.
Pushing the Docker Image to Azure Container Registry
The next step is to push the Docker image to a container registry. The container registry is where Kubernetes pulls the Docker images from when it deploys the application.
Azure offers a fully managed container registry called Azure Container Registry (ACR) that provides secure storage and easy integration with other Azure services such as AKS. To push the Docker image to ACR, you first need to create a new registry in your Azure account.
Run the following Azure CLI command to create a new container registry:
az acr create --resource-group <my-resource-group> --name <my-acr-name> --sku Basic
Replace <my-resource-group>
with the name of your existing Azure resource group, and <my-acr-name>
with a unique name for your container registry. You can also choose a different Azure SKU other than Basic
depending on your requirements and budget.
Once the container registry is created successfully, you need to log in to the registry using Docker:
docker login <my-acr-name>.azurecr.io
Replace <my-acr-name>
with the name of your container registry. Docker prompts you for your Azure login credentials. Once authenticated, you can push the Docker image to ACR:
docker tag myapp:v1 <my-acr-name>.azurecr.io/myapp:v1
docker push <my-acr-name>.azurecr.io/myapp:v1
The first command tags your Docker image with the registry address and application image name. The second command pushes the image to the container registry. This command may take a few minutes to complete depending on the size of your image.
You can verify that the image is pushed successfully by visiting the container registry in the Azure portal and checking the repository’s name and tag.
Deploying the Application to Azure Kubernetes Service
The final step is to deploy the Docker container to AKS. To create a new Kubernetes deployment, you need to create a YAML configuration file that describes the application deployment.
Create a new file called deployment.yml
in your application’s root directory with the following content:
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: <my-acr-name>.azurecr.io/myapp:v1
ports:
- containerPort: 3000
imagePullPolicy: Always
This YAML file describes a Kubernetes deployment with the following properties:
-
apiVersion
: The Kubernetes API version to use for this deployment. -
kind
: The Kubernetes resource type, which is “Deployment” in this case. -
metadata
: The metadata for the deployment, such as the name of the deployment. -
spec
: The specification of the deployment, which includes the desired number of replicas, the selector used to match the pods, and the template for the pods.
The template specifies the configuration for the individual pods that make up the deployment. In this case, the template includes a single container that runs the myapp:v1
Docker image from the ACR container registry.
Save the deployment.yml
file and run the following command to deploy the application to AKS:
kubectl apply -f deployment.yml
This command creates a new Kubernetes deployment based on the configuration specified in the YAML file. You can check the status of your deployment by running the following command:
kubectl get pods
This command should show your newly created pod with the name myapp
and status as Running
.
Finally, to expose the application to the outside world, you need to create a Kubernetes service. Create a new file called service.yml
in your application’s root directory with the following content:
apiVersion: v1
kind: Service
metadata:
name: myapp
spec:
selector:
app: myapp
ports:
- protocol: TCP
port: 80
targetPort: 3000
type: LoadBalancer
This YAML file describes a Kubernetes service with the following properties:
-
apiVersion
: The Kubernetes API version to use for this service. -
kind
: The Kubernetes resource type, which is “Service” in this case. -
metadata
: The metadata for the service, such as the name of the service. -
spec
: The specification of the service, which includes the selector used to match the pods and the port configuration.
The targetPort
is set to 3000
to match the port exposed by the application in the Docker container. The type
is set to LoadBalancer
to expose the service through an external IP address.
Save the service.yml
file and run the following command to create the service:
kubectl apply -f service.yml
This command creates a new Kubernetes service based on the configuration specified in the YAML file. You can check the status of your service by running the following command:
kubectl get service
This command should show your newly created service with the name myapp
and an external IP address that you can use to access the application externally.
Conclusion
In this tutorial, we walked you through the process of building and deploying a Docker container to Azure Kubernetes Service. We started by creating a Dockerfile for the application and building a Docker image. Then, we pushed the Docker image to Azure Container Registry and created a Kubernetes deployment and service using YAML configuration files. Finally, we exposed the service through an external IP address to access the application externally.
This tutorial covers the basic steps required to deploy a containerized application on AKS. However, there are many other Kubernetes concepts and features that you should explore to become proficient in deploying and managing containerized applications in production environments.