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

How Can I Write Faster with AI Tools?

Writing software backed by artificial intelligence can create everything from emails to blog articles. AI…

8 hours ago

Strengthening Cyber Defenses: The Benefits of Outsourcing Cybersecurity

In today's interconnected digital realm, cybersecurity stands as a paramount concern for organizations, irrespective of…

1 day ago

Navigating Volatility: Investing in Crypto Derivatives and Risk Management Strategies

The cryptocurrency market is famed for its volatility, presenting each opportunity and demanding situations for…

3 weeks ago

How Game Developers Use AI in Mobile Games in 2024?

Games since time immemorial have been winning at captivating the users and teleporting them onto…

3 weeks ago

The Impact of AI on Software Development

We are living within an innovation curve wherein cutting-edge technologies are making a hustle and…

3 weeks ago

AI Tools for Research Paper Writing: Learn What They Can Do

Whether it’s the healthcare industry or the automobile sector, artificial intelligence has left its impact…

1 month ago

This website uses cookies.