Kubernetes tutorial – Create deployments using YAML file
This is in continuation of the Kubernetes article series. In the last post, we have learned how to create & deploy the app to the Kubernetes cluster. Now in this post, we are going to learn how to create application deployment using the YAML file, and also we can check how to create services to control the application communication.
Quick Snapshot
Step #1.Create an nginx deployment
Using Deployment controller we can provide declarative updates for Pods and ReplicaSets. Create deployment.yaml file in your current folder like the below to describe the Nginx deployment.
Kubernetes manifest file defines a desired state for the cluster, including what container images should be running.For example, this YAML file describes a Deployment that runs the nginx:latest Docker image
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
In this example:
- A Deployment named nginx-deployment is created, indicated by the metadata: name field.
- The Deployment creates 1 replicated Pods, indicated by the replicas field.
- The Pod template’s specification, or template: spec field, indicates that the Pods run one container, nginx, which runs the nginx Docker Hub latest image (you can also specify the version ex.1.7.9.)
- The Deployment opens port 80 for use by the Pods.
Step #2.Create Deployment based on the YAML file
- Based on the deployment described in deployment.yaml created in the previous step, create the deployment using kubectl create command
kubectl create -f deployment.yaml
- Now that deployment is created, let’s check the deployment information using kubectl get deployment command :
When you inspect the Deployments in your cluster, the following fields are displayed:
NAME
lists the names of the Deployments in the cluster.DESIRED
displays the desired number of replicas of the application, which you define when you create the Deployment. This is the desired state.CURRENT
displays how many replicas are currently running.UP-TO-DATE
displays the number of replicas that have been updated to achieve the desired state.AVAILABLE
displays how many replicas of the application are available to your users.AGE
displays the amount of time that the application has been running.
- Describe the deployment using kubectl describe deployment command to check the details on the deployment
- Check the list of pods created by the deployment by using kubectl get pods command:
Step #3.Create service
Kubernetes has powerful networking capabilities that control how applications communicate. These networking configurations can also be controlled via YAML. The Service selects all applications with the label nginx. As multiple replicas, or instances, are deployed, they will be automatically load balanced based on this common label. The Service makes the application available via a NodePort.
Kubernetes Service
is an abstraction which defines a logical set of Pods
and a policy by which to access them  (micro-service). The set of Pods
targeted by a Service
is determined by a Label Selector
apiVersion: v1 kind: Service metadata: name: nginx-svc labels: app: nginx spec: type: NodePort ports: - port: 80 nodePort: 30080 selector: app: nginx
Step #4.Deploy service
- Use kubectl create command to create a new service based on the service.yaml file created in the previous step
- Check the details of all the Service objects deployed
- Use describe command to discover more details about the configuration of all the Service objects deployed
Use curl command to Issuing requests to the port 30080
Step #5.Update nginx deployment to have 4 replicas
Modify deployment.yaml to update the nginx deployment to have 4 replicas.
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 4
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
Step #6.Apply the updated nginx deployment to have 4 replicas
- Apply the changes using kubectl apply command
kubectl apply -f deployment.yaml
- Now that deployment is created, let’s check the deployment information using kubectl get deployment command :
When you inspect the Deployments in your cluster, the following fields are displayed:
NAME
lists the names of the Deployments in the cluster.DESIRED
displays the desired number of replicas of the application, which you define when you create the Deployment. This is the desired state.CURRENT
displays how many replicas are currently running.UP-TO-DATE
displays the number of replicas that have been updated to achieve the desired state.AVAILABLE
displays how many replicas of the application are available to your users.AGE
displays the amount of time that the application has been running.
- Describe the deployment using kubectl describe deployment command to check the details on the deployment
- Check the list of pods created by the deployment by using kubectl get pods command:
As all the Pods have the same label selector, they’ll be load balanced behind the Service NodePort deployed. Issuing requests to the port will result in different containers processing the request.
Congrats! we have learned how to create application deployment using the YAML file and also how to create services to control how application communicates.
Like this post? Don’t forget to share it!
Additional Resources :
- Kubectl cheat sheet
- Take a free course on Building Scalable Java Microservices with Spring Boot and Spring Cloud
- Kubernetes tutorial – Create simple cluster & Deploy app
- Kubernetes tutorial – Scale & perform updates to your app
- Kubernetes tutorial – Create deployments using YAML file
- Official documentation as a reference to understand any command.
- If you’re looking for Kubernetes examples, here it is GitHubÂ
[…] Kubernetes tutorial – Create deployments using YAML file […]
[…] Kubernetes tutorial – Create deployments using YAML file […]
[…] Kubernetes tutorial – Create deployments using YAML file […]
[…] Kubernetes tutorial – Create deployments using a YAML file […]