Connect with kubectl and deploy an app
In this task you'll see the workloads running on an AKS cluster.
To run an app on Kubernetes you describe it, and the cluster makes it so. A deployment says which image to run and how many copies (pods). A service gives the pods one stable address, and a service of type LoadBalancer gives them a public IP.
kubectl is the command-line tool for talking to any Kubernetes cluster, and it's already installed in Cloud Shell. The portal shows the same objects under Kubernetes resources.
Example. SS Kaiser deploys its stock API with three pods behind one service. When a pod crashes, Kubernetes starts a new one within seconds, and customers don't notice.
Costs money. This uses the cluster from the previous task, which costs money while it runs. The public IP for the app's load balancer adds a little more. Delete everything as soon as you've finished.
Before you start
You need: Azure Kubernetes Service Cluster User role (or Contributor) on the cluster. Time: 15 minutes.
Where to look
- Kubernetes services
- aks-practice
- Workloads
Steps
- Open Cloud Shell (Bash) and connect kubectl to your cluster:Type
az aks get-credentials --resource-group rg-portal-practice --name aks-practice. - Check the node is ready with
kubectl get nodes. - Create a deployment from a small sample image:Type
kubectl create deployment hello --image=mcr.microsoft.com/azuredocs/aks-helloworld:v1. - Make it public with
kubectl expose deployment hello --type=LoadBalancer --port=80. - Run
kubectl get service hello --watchuntil an address appears under EXTERNAL-IP, then press Ctrl+C and open that address in your browser. - In the portal, open the cluster's Workloads and Services, gateways and ingresses pages to see the same deployment and service.Kubernetes keeps the portal and kubectl in step, because both read the same cluster.
Tip. Workloads and Services, gateways and ingresses are under Kubernetes resources in the cluster's left menu.
Commands
# Connect kubectl to the cluster
az aks get-credentials --resource-group rg-portal-practice --name aks-practice
# Deploy, publish and check the app
kubectl create deployment hello --image=mcr.microsoft.com/azuredocs/aks-helloworld:v1
kubectl expose deployment hello --type=LoadBalancer --port=80
kubectl get service hello
# Remove it again
kubectl delete service hello
kubectl delete deployment hello
Watch out for
- Cost
- The LoadBalancer service adds a public IP and load balancer rules on top of the cluster's own costs. Deleting the service removes them.
- Security
- A LoadBalancer service is open to the whole internet. For real apps, put an ingress controller with HTTPS in front, and limit who can run kubectl against the cluster.
- Common mistakes
- Running kubectl against the wrong cluster:
kubectl config current-contextshows which one you're using. Waiting for an external IP on a stopped cluster.
Clean up
Remove the app with kubectl delete service hello and kubectl delete deployment hello, then delete the cluster or the resource group if you're not doing the next task.
Words to know
- Pod
- The smallest unit Kubernetes runs: one or more containers that share a network address.
- Deployment
- A description of an app: its image and how many pods to keep running.
- Service
- A stable address in front of a set of pods. Type LoadBalancer makes it public.
- kubectl
- The command-line tool for Kubernetes.
Tick off each step and track your progress in the guide.
Open this task in Azure Portal, Hands On