Deploy an Application to Azure Kubernetes Service (AKS)
This tutorial guides you through deploying a sample application to Azure Kubernetes Service (AKS). We'll cover containerizing your application, creating an AKS cluster, and deploying your containerized app.
Prerequisites
- An Azure account with an active subscription. Create a free account.
- Azure CLI installed and configured. Install Azure CLI.
kubectlinstalled. Install kubectl.- Docker installed locally. Install Docker.
Step 1: Create a Sample Application
For this tutorial, we'll use a simple Node.js web application. Create a new directory for your application and add the following files:
app.js
const express = require('express');
const app = express();
const port = 8080;
app.get('/', (req, res) => {
res.send('Hello from AKS!');
});
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});
package.json
{
"name": "aks-demo-app",
"version": "1.0.0",
"description": "A simple Node.js app for AKS deployment",
"main": "app.js",
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "^4.17.1"
}
}
Install the Node.js dependencies:
npm install
Step 2: Containerize the Application
Create a Dockerfile in the same directory as your application files. This file defines how to build your Docker image.
Dockerfile
FROM node:16-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD ["npm", "start"]
Build the Docker image:
docker build -t aks-demo-app:v1 .
You can test the container locally:
docker run -p 4000:8080 aks-demo-app:v1
Open http://localhost:4000 in your browser to see the "Hello from AKS!" message.
Step 3: Create an Azure Kubernetes Service (AKS) Cluster
First, log in to your Azure account using the Azure CLI:
az login
Create a resource group for your AKS cluster:
az group create --name aksResourceGroup --location eastus
Create the AKS cluster. This may take several minutes.
az aks create \
--resource-group aksResourceGroup \
--name myAKSCluster \
--node-count 1 \
--enable-addons monitoring \
--generate-ssh-keys
Step 4: Connect kubectl to your AKS Cluster
Configure kubectl to connect to your new AKS cluster:
az aks get-credentials --resource-group aksResourceGroup --name myAKSCluster
Verify the connection:
kubectl get nodes
Step 5: Deploy the Application to AKS
First, we need to push our Docker image to a container registry accessible by AKS. Azure Container Registry (ACR) is recommended.
Create an ACR instance:
az acr create --resource-group aksResourceGroup --name myaksacr12345 --sku Basic
Replace myaksacr12345 with a unique name for your registry.
Log in to your ACR:
az acr login --name myaksacr12345
Tag your local Docker image for ACR and push it:
docker tag aks-demo-app:v1 myaksacr12345.azurecr.io/aks-demo-app:v1
docker push myaksacr12345.azurecr.io/aks-demo-app:v1
Now, create Kubernetes deployment and service manifest files.
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: aks-demo-app-deployment
spec:
replicas: 3
selector:
matchLabels:
app: aks-demo-app
template:
metadata:
labels:
app: aks-demo-app
spec:
containers:
- name: aks-demo-app
image: myaksacr12345.azurecr.io/aks-demo-app:v1
ports:
- containerPort: 8080
name: http
service.yaml
apiVersion: v1
kind: Service
metadata:
name: aks-demo-app-service
spec:
selector:
app: aks-demo-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer
Apply these manifests to your AKS cluster:
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
Step 6: Access the Deployed Application
Get the external IP address of your service:
kubectl get service aks-demo-app-service
It may take a few minutes for the LoadBalancer to be provisioned and assign an external IP. Once you see an IP address, open it in your web browser. You should see "Hello from AKS!".
Conclusion
Congratulations! You have successfully deployed a sample application to Azure Kubernetes Service. You've learned how to:
- Containerize a Node.js application using Docker.
- Create and configure an AKS cluster.
- Push a Docker image to Azure Container Registry.
- Deploy your application to AKS using Kubernetes manifests.
- Expose your application to the internet using a LoadBalancer service.
For more advanced scenarios, explore Kubernetes Ingress controllers, persistent storage, and CI/CD integration with AKS.