
A container platform deployment example with Kubernetes
In the following Kubernetes YAML configuration, we will define various Kubernetes keywords related to deployment. One important variable is container: image. Here, we are referring to an existing Docker container image that Kubernetes will use to create a container under pod deployment. This container image should already be customized to suit your requirements. The kubectl command will read this configuration and start your container as appropriate. It will start three replica pods with the same container image and it will use the matchLables value to replace the specific container inside the three newly created pods. The keyword replica indicates that a specific value has been used to create pods with the same container image.
There are three different kinds of replicas:
- kind: Kind defines the type of work kubectl will be doing on the given configuration. Currently, we are using Deployment.
- metadata: This will assign a name to the deployment and label your POD with the given name under app.
- spec: This stands for specification, where you configure the number of replicas, the selector name for pod replacement, and so on.
Create a deployment controller file with the following content. This can then be provided to the kubectl command as a parameter during the create or apply steps.
The file name is as follows: /my-hello-packt-application-deployment.yaml. Refer to the following code:
#vim my-packt-application-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-hello-packt-application-deployment
labels:
app: my-hello—packt-application
spec:
replicas: 3
selector:
matchLabels:
app: my-hello-packt-application
template:
metadata:
labels:
app: my-hello-packt-application
spec:
containers:
- name: my-hello-packt-application
image: repository-hub.packthub.example.com/my-hello-packt-application:1.1
ports:
- containerPort: 8080
In this example code, we have carried out the following steps:
- We have created a deployment named my-hello-packt-application-deployment, indicated by the .metadata.name field
- This deployment creates three replicated pods, which is shown in the replicas field
- The selector field is used by the deployment module to identify which pod to manage
- The template field contains the following sub fields:
- The pods are labeled app: my-hello-packt-application-deployment using the labels field
- The pod template's specification, or the .template.spec field, indicates that the pods run on one container, my-hello-packt-application, which runs the NGINX Docker hub image Version 1.7.9
- We have created one container and named it my-hello-packt-application-deployment using the name field
- We have run the my-hello-packt-deployment image Version 1.1
- We have opened port 8080 so that the container can send and accept traffic
Let's take a look at the following steps that show how to create or deploy services using the previously mentioned configuration file:
- To create this deployment, run the following command:
kubectl create -f my-hello-packt-application-deployment.yaml
OR
kubectl apply -f my-hello-packt-application-deployment.yaml
- We can append the -- record to this command to record the current command in the annotations of the created or updated resource. This is useful for future reviews, such as investigating which commands were executed in each deployment revision.
- Next, run kubectl to get the deployments. The results will look as follows:
- When you check your deployments in your Kubernetes cluster, the following fields are shown:
- NAME: The deployment name
- DESIRED: The desired number of replicas of our application
- CURRENT: The number of replicas currently running
- UP-TO-DATE: The number of replicas that have been updated to get the desired state
- AVAILABLE: The number of replicas of the application are available to our users
- AGE: The amount of time that the application has been running
- To see the rollout status deployment, run the following kubectl command:
kubectl rollout status deployment/my-hello-packt-application- deployment
This command will show the rollout status of the kubectl apply command. It will also show you the number of desired pods. In the following screenshot, you will notice that it has created three replica sets, which are all up to date, with the corresponding provided YAML file:
Then, run the following command a few seconds later:
kubectl get deployments
In the following output, we can see that the deployment created three replicas:
- To see the ReplicaSet (rs) created by the deployment, run the following command:
kubectl get rs
The ReplicaSet is always shown or formatted as follows:
[DEPLOYMENT-NAME]-[POD-TEMPLATE-HASH-VALUE].
- Hash values are auto-generated during deployment. The following screenshot shows the dynamically generated value. Run the following command to get the auto-generated label for the pods:
kubectl get pods --show-labels
The following output is returned: