Deploying an Application to Azure Kubernetes Service (AKS)
This article guides you through the process of deploying a sample application to your Azure Kubernetes Service (AKS) cluster. We'll cover creating the necessary Kubernetes manifests and applying them to your cluster.
Prerequisites
- An active Azure subscription.
- An AKS cluster deployed and configured.
- The Azure CLI installed and logged in (`az login`).
kubectl
installed and configured to connect to your AKS cluster.- A container image for your application (e.g., in Azure Container Registry or Docker Hub). For this example, we'll use a simple Nginx image.
Step 1: Create a Kubernetes Deployment
A Kubernetes Deployment manages the desired state of your application. It ensures that a specified number of replicas of your application pods are running at all times. We'll create a YAML file named deployment.yaml
.
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx-deployment
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
This manifest defines a Deployment named my-nginx-deployment
that will run 2 replicas of a pod using the nginx:latest
container image. The container will expose port 80.
Step 2: Apply the Deployment to AKS
Use kubectl
to apply the deployment manifest to your AKS cluster.
kubectl apply -f deployment.yaml
You should see output similar to:
deployment.apps/my-nginx-deployment created
You can verify the deployment status with:
kubectl get deployments
And check the pods created by the deployment:
kubectl get pods -l app=nginx
Step 3: Create a Kubernetes Service
A Kubernetes Service provides a stable network endpoint to access your application pods. This is crucial because pods are ephemeral and their IP addresses can change. We'll create a Service to expose our Nginx deployment. Create a YAML file named service.yaml
.
apiVersion: v1
kind: Service
metadata:
name: my-nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
This manifest defines a Service named my-nginx-service
.
selector: app: nginx
tells the Service to route traffic to pods with the labelapp: nginx
.ports
defines how the Service exposes the application.type: LoadBalancer
requests an external IP address from Azure to make the service accessible from the internet.
Step 4: Apply the Service to AKS
Apply the Service manifest to your AKS cluster.
kubectl apply -f service.yaml
You should see output similar to:
service/my-nginx-service created
It may take a few minutes for Azure to provision an external IP address for the LoadBalancer. You can check the status of the Service and its external IP with:
kubectl get service my-nginx-service
Look for the EXTERNAL-IP
field. Once it populates with an IP address, you can access your Nginx application by navigating to that IP address in your web browser.
Step 5: Access Your Application
Once the external IP address is assigned to the Service, open a web browser and navigate to http://
. You should see the default Nginx welcome page.
Clean Up
To remove the deployed application and its associated resources, you can delete the Service and then the Deployment:
kubectl delete service my-nginx-service
kubectl delete deployment my-nginx-deployment
This will release the external IP address and remove the pods and ReplicaSet created by the deployment.
Next Steps
Congratulations! You have successfully deployed a simple application to AKS. From here, you can explore more advanced deployment strategies:
- Deploying custom applications from your own container images.
- Using Helm for package management and more complex deployments.
- Configuring Ingress controllers for advanced routing and SSL termination.
- Implementing rolling updates and rollbacks for your applications.