Exercise - Deploy an application on your Azure Kubernetes Service cluster
In this exercise, deploy your company's website as a test app onto Azure Kubernetes Service (AKS). The website is a static website with an underlying technology stack of HTML, CSS, and JavaScript. It doesn't receive as many requests as the other services and provides us with a safe way to test deployment options.
Note
The code for the web app is available in this GitHub repository if you want to explore the source code further. Also, this sample app only deploys on a Linux node pool.
Important
You need your own Azure subscription to complete this exercise, and you might incur charges. If you don't already have an Azure subscription, create a free account before you begin.
Create a deployment manifest
You create a deployment manifest file to deploy your application. The manifest file allows you to define what type of resource you want to deploy and all the details associated with the workload.
Kubernetes groups containers into logical structures called pods, which have no intelligence. Deployments add the missing intelligence to create your application. Let's create a deployment file.
On your computer, use a text editor like Visual Studio Code, and copy and paste the following YAML code into a file named deployment.yaml and save the file.
# deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: contoso-website spec: selector: matchLabels: app: contoso-website template: metadata: labels: app: contoso-website spec: nodeSelector: kubernetes.io/os: linux containers: - image: mcr.microsoft.com/mslearn/samples/contoso-website name: contoso-website resources: requests: cpu: 100m memory: 128Mi limits: cpu: 250m memory: 256Mi ports: - containerPort: 80 name: http- Two keys are used to define the
apiVersionandkindof manifest you're creating. For more information aboutapiVersionand what values to put in this key, see the official Kubernetes deployments documentation. - The
nameis the name of the deployment. Use it to identify and query the deployment information when you usekubectl. - The template definition defines the pod information within the manifest file. The template is placed in the manifest file under the deployment specification section.
- The
labelskey to allow deployments to find and group pods. - In an AKS cluster that has Linux and Windows node pools, the deployment manifest defines a
nodeSelectorto tell your AKS cluster to run the sample application's pod on a node that can run Linux containers. Linux nodes can't run Windows containers and vice versa. - The
containerskey is an array of container specifications because a pod can have one or more containers. The specification defines animage, aname,resources,ports, and other important information about the container. All running pods follow the namecontoso-website-<UUID>, where UUID is a generated ID to identify all resources uniquely. - It's a good practice to define a minimum and a maximum amount of resources that the app is allowed to use from the cluster. You use the
resourceskey to specify this information. The resource section allows you to specify the minimum resource amount as a request and the maximum resource amount as a limit. - The ports this container exposes externally through the
portskey. Theportskey is an array of objects, which means that a container in a pod can expose multiple ports with multiple names. You name the port by using thenamekey. Naming ports allows you to change the exposed port without changing files that reference that port.
- Two keys are used to define the
Upload deployment.yaml to your Cloud Shell session by selecting Manage files > Upload.
Apply the manifest
In Cloud Shell, run the
kubectl applycommand to submit the deployment manifest to your cluster.kubectl apply -f ./deployment.yamlThe command should output a result similar to the following example.
deployment.apps/contoso-website createdRun the
kubectl get deploycommand to check if the deployment was successful.kubectl get deploy contoso-websiteThe command should output a table similar to the following example.
NAME READY UP-TO-DATE AVAILABLE AGE contoso-website 1/1 1 1 19sRun the
kubectl get podscommand to check if the pod is running.kubectl get podsThe command should output a table similar to the following example.
NAME READY STATUS RESTARTS AGE contoso-website-1c2b3b4b5b-dzsvm 1/1 Running 0 68s