Deployment
Learn how to create a Kubernetes deployment and deploying the Hello World container image.
Last updated
Learn how to create a Kubernetes deployment and deploying the Hello World container image.
Last updated
This section continues from the previous section - make sure you do the tutorial in sequence.
Container ImageKubernetes ClusterA Kubernetes Pod is a group of tightly coupled containers, tied together that must start, stop, and scale together. In many case a Pod is associated to only one container. Multiple containers in a single pod can be useful when you have, for example, a container that runs the application, and another container that periodically polls logs/metrics from the application container.
Every Pod has a unique, ephemeral, and routable IP address. I.e., a container inside of a Pod can directly reach another the IP address of another Pod (and the containers in that Pod).
All containers within a single pod are scheduled to a single physical resource (the same Node), and all containers within a Pod will share the same networking interface, IP address, volumes, etc.
You can start a single Pod in Kubernetes by creating a Pod resource. However, a Pod created this way would be known as an Unmanaged Pod. If an Unmanaged Pod dies/exits, it will not be restarted by Kubernetes. A better way to start a Pod, is by using a higher-level construct such as a Deployment.
Deployment provides declarative way to manage a set of Pods. You only need to describe the desired state in a Deployment resource, and behind the scenes, a Kubernetes Deployment controller will change the actual state to the desired state for you. It does this using a resource called a ReplicaSet under the covers.
You can create a Deployment and deploy into Kubernetes using kubectl
command line like in the Hello World tutorial. That's great to get a feel of Kubernetes. However, it's best that you create a YAML file first, and then deploy the YAML file.
You can open the k8s/deployment.yaml
file to see the content. Following is a version of the YAML file where it's slimmed down to the bare minimum.
You can read more about Deployment in the Kubernetes Deployment Guide.
Use kubectl
command line to deploy the YAML file:
To verify the application is deployed, see all the pods that are running:
You should see that there is one pod running!
For every Kubernetes resource, you can describe the details of a resource, and see its current state, and any events, errors that may have occurred.
Describe a Deployment:
Describe a Pod:
kubectl get pods
shows you every pod running in the current namespace. You can limit the output to just the application you are interested in by select only pods matching certain label key/value pairs.
Scale the number of instances from 1 to 2.
Verify that there are now 2 pods running:
Out of the 2 pods, pick one to delete, and then observe that Kubernetes automatically starts another pod instance so that there are always 2 pods running.
Verify that there still 2 pods running, but one of them has a lower age
indicating it's recently started.
You can use labels to delete all pods matching certain label key/value pairs.
You can see the logs from the pod, and follow the log as new logs are produced:
You can execute commands directly in the container instance. However, the container image will need to contains the command that you'd like to run. The Hello World application built with Jib uses a Distroless base image by default - and the Distroless base image does not come with any shell commands for security purposes.
Let's deploy an Nginx container that contains the executables and see how you can shell into the container instance.
See that Nginx container is running:
Use a specific Nginx pod, and shell into the container instance:
The -ti
flag means to receive output from TTY, and also that the session is interactive (i.e., you'll be typing commands).
Once you are in the container instance's shell, you can explore the container instance:
In this Nginx container image, you can see that there are actually many command line utilities that's not needed for production deployment of an Nginx server. Exposing more commands like this may increase attack surface area if the container instance is compromised. For this reason, Distroless base images do not include any commands. On the other hand, lack of commands may increase the difficulty to debug the application instance.
Delete the Nginx deployment before you continue!
kubectl delete deployment nginx-deployment