Deploying a container image

We will create a deployment file and deploy it to Kubernetes. To do this, perform the following steps:

  1. Make a new file in your favorite text editor and call it deploy.yaml. Add the following information to the deploy.yaml file:
apiVersion: apps/v1
kind: Deployment
metadata:
name: kubernetes-deployment
labels:
app: customapplication
spec:
replicas: 3
selector:
matchLabels:
app: customapplication
template:
metadata:
labels:
app: customapplication
spec:
containers:
- name: azuredevops
image: msftazuredevops.azurecr.io/azuredevops:586
ports:
- containerPort: 80

In this example, the following is specified:

    • A deployment is created with the name kubernetes-deployment (metadata.name).
    • The deployment will create three replicas of the specified container (spec.replicas).
    • The selector, in combination with the labels tag, is used to specify which components this deployment file will manage within Kubernetes.
    • The deployment file will create a container for the msftazuredevops.azurecr.io/azuredevops:586 image file.
  1. To deploy this file to Kubernetes, we will again use the kubectl command line and make use of the apply command:
kubectl apply -f deploy.yaml

The -f argument is used to specify that a local path is used as a reference to a deployment file. After executing the command, you can open the Kubernetes dashboard to see the status and maybe even observe errors.

It is possible that you encounter an error stating that pulling the image from your location failed. This could be a security issue. Under the hood, AKS is using a service principal. You should also see this when creating a new Kubernetes cluster. Make sure to give this service principal access rights on the Azure registry.
  1. Following a successful execution, try the get pods command to see whether there are three pods within the system. If everything proceeded correctly, there should be three pods running within Kubernetes, but the application is still not available to the outside world.

To make it available, we need to add a service to the deployment file.

If you want to add it to the same file, add a line with these characters, ---, between the deployments. This is not required when you also define separate files for deployment.

In the deploy.yaml file, add the following section:

---
apiVersion: v1
kind: Service
metadata:
name: customapplication-service
spec:
type: LoadBalancer
ports:
- port: 80
selector:
app: customapplication

This YAML section creates a load balancer and attaches it to the specified selector (spec.selector.app), meaning it will be used for the pods as we previously specified.

In the background, Kubernetes will create an Azure load balancer and a public IP for connection to the pods.

  1. To retrieve the external IP address of the service, use the following command until it displays the external IP address:
kubectl get service

This will return all services and their external IP addresses if it is present. Also take a quick peak at the additional resource group of Kubernetes to see which Azure resources are created.

Well done! In this section, you learned how to create a Kubernetes cluster and deploy a container image on it via kubectl and deployment files. In the next section, we will take this forward and learn how to upgrade these containers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.172.252