What is it?
The goal of Creating Containerized Services on Kubernetes is to build, test, and deploy containers. Kubernetes objects support the functions needed to scale and run your applications properly. So given a container image, let’s discuss the steps required to deploy a container image with Kubernetes resources.
Creating a Deployment
Below is an example of a Deployment object's configuration written in YAML format. When creating any Kubernetes resource, you need to specify the spec field in the configuration data. The Kubernetes API server uses the spec object to fulfill the API request for additional Kubernetes resources. The spec section describes the desired state, along with some necessary information, such as the object's name. Although the API server accepts object definition files in a JSON format, most often, we provide such files in a YAML format, which is converted by the kubectl CLI in a JSON payload and sent to the API server.
Let’s go over the required components of a basic deployment.
- The apiVersion field is the first required field. The value is an API path that specifies the API version supported by Kubernetes. Read the official documentation on the API versions Kubernetes supports here.
- The second required field is kind, specifying the Kubernetes object type. Here we want to define a Deployment.
- The third required field metadata holds the object’s information, such as name, labels, and annotations. We define a Deployment named nginx-deployment. Read how to use Kubernetes metadata to organize your containers across your cloud environment(s)
- The fourth required field spec marks the beginning of the block defining the desired state of the Deployment object.
- we specified 3 replicas for the deployment object. The Deployment configures what is called a ReplicaSet to manage each Pod’s lifecycle. A ReplicaSet’s purpose is to maintain a stable set of replica Pods running at any given time. As such, it is often used to guarantee the availability of a specified number of identical Pods.
- The selector field defines how the Deployment finds which Pods to manage. In this case, you simply select a label that is defined in the Pod template (app: nginx). The template field contains a metadata field which means the pods are labeled app: nginx via the labels field.
- In spec.template.spec, we define the desired state of the Pod. Our Pod creates a single container running the nginx:1.15.11 image from Docker Hub.
Follow the Kubernetes documentation here to create the above Deployment.
Here is a summary of the commands needed to create a Deployment.
To apply a request for a Kubernetes resource given an input file use:
kubectl apply -f <file_name.yaml or .json>
To list all running pods in a namespace:
kubectl get pods
To get details on a specific pod:
kubectl describe pod <Pod Name>
See the kubectl cheatsheet for more commands.
Why use it?
A Deployment provides declarative updates for Pods and ReplicaSets. You describe the desired state in a Deployment, and the Deployment controller changes the actual state to the desired state at a controlled rate. You can define Deployments to create new ReplicaSets or to remove existing Deployments and adopt all their resources with new Deployments. If you’d like to learn the use cases for Deployments click here.