Kubernetes Guides

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.

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
Image – Kubectl – Create deployment command
  • Now that deployment is created, let’s check the deployment information using kubectl get deployment command :
Image – 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
Image- Kubectl – describe deployment command
  • Check the list of pods created by the deployment by using kubectl get pods command:
Image – 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

    Image – Kubectl – get Svc command command

  • Use describe command to discover more details about the configuration of all the Service objects deployed

    Image – Kubectl – describe svc command command

Use curl command to Issuing requests to the port 30080

Image – curl command to issue request

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

Like this post? Don’t forget to share it!

Additional Resources :

Summary
Article Name
Kubernetes tutorial - Create deployments using YAML file
Description
In this post, we are going to learn how to create application deployment using YAML file, how to create services & expose them up.
Author
Publisher Name
Upnxtblog
Publisher Logo
Karthik

Allo! My name is Karthik,experienced IT professional.Upnxtblog covers key technology trends that impacts technology industry.This includes Cloud computing,Blockchain,Machine learning & AI,Best mobile apps, Best tools/open source libs etc.,I hope you would love it and you can be sure that each post is fantastic and will be worth your time.

Share
Published by
Karthik

Recent Posts

Finding the Right Time to Build Your Software Instead of Buy

There are few things as valuable to a business as well-designed software. Organizations today rely…

5 days ago

Innovators in Crypto: Prominent AI-Powered Coins

The cryptocurrency industry is being reshaped by the fusion of blockchain technology and artificial intelligence…

3 weeks ago

Top AI Design Tools Every Graphic Designer Should Use in 2024

Introduction Artificial Intelligence (AI) has also found its relevance in graphic design and is quickly…

2 months ago

Transforming Industries: The Integration of AI and Blockchain

Imagine a world where the brilliance of Artificial Intelligence (AI) meets the unbreakable security of…

2 months ago

How Can I Use Automation to Streamline My Digital Marketing Efforts?

In today’s fast-paced digital landscape, automation is not just a luxury but a necessity for…

2 months ago

Top 5 AI Technologies Transforming the Casino Gaming Landscape in 2025

The world of casino gaming has leveraged the emerging technology advancements to create immersive and…

3 months ago

This website uses cookies.