How to create a Nginx deployment with one replica in Kubernetes?

KubernetesKubernetesBeginner
Practice Now

Introduction

Kubernetes is a powerful open-source container orchestration platform that has become the de facto standard for managing and scaling containerized applications. In this tutorial, you will learn how to create a Nginx deployment with one replica in Kubernetes, covering the fundamental concepts and step-by-step instructions to get your first Kubernetes application up and running.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/TroubleshootingandDebuggingCommandsGroup(["`Troubleshooting and Debugging Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedCommandsGroup(["`Advanced Commands`"]) kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/describe("`Describe`") kubernetes/BasicCommandsGroup -.-> kubernetes/create("`Create`") kubernetes/BasicCommandsGroup -.-> kubernetes/get("`Get`") kubernetes/BasicCommandsGroup -.-> kubernetes/run("`Run`") kubernetes/AdvancedCommandsGroup -.-> kubernetes/apply("`Apply`") subgraph Lab Skills kubernetes/describe -.-> lab-417504{{"`How to create a Nginx deployment with one replica in Kubernetes?`"}} kubernetes/create -.-> lab-417504{{"`How to create a Nginx deployment with one replica in Kubernetes?`"}} kubernetes/get -.-> lab-417504{{"`How to create a Nginx deployment with one replica in Kubernetes?`"}} kubernetes/run -.-> lab-417504{{"`How to create a Nginx deployment with one replica in Kubernetes?`"}} kubernetes/apply -.-> lab-417504{{"`How to create a Nginx deployment with one replica in Kubernetes?`"}} end

Kubernetes Fundamentals

What is Kubernetes?

Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It was originally designed by Google and is now maintained by the Cloud Native Computing Foundation (CNCF).

Key Kubernetes Components

Kubernetes consists of several key components that work together to manage and orchestrate containerized applications:

  1. Nodes: Nodes are the physical or virtual machines that run the Kubernetes workloads.
  2. Pods: Pods are the smallest deployable units in Kubernetes, and they encapsulate one or more containers.
  3. Deployments: Deployments are used to manage the lifecycle of stateless applications, ensuring that a specified number of replicas are running at all times.
  4. Services: Services provide a stable network endpoint for accessing applications running in Kubernetes.
  5. Volumes: Volumes are used to provide persistent storage for Kubernetes applications.

Kubernetes Architecture

Kubernetes uses a master-worker architecture, where the master node(s) manage the overall cluster, and the worker nodes run the containerized applications. The master node(s) consist of the following components:

graph LR Master --> API-Server Master --> Scheduler Master --> Controller-Manager Master --> etcd

The worker nodes run the following components:

graph LR Worker --> Kubelet Worker --> Kube-Proxy Worker --> Container-Runtime

Deploying Applications in Kubernetes

To deploy applications in Kubernetes, you can use YAML configuration files that define the desired state of your application. These configuration files can be used to create Deployments, Services, and other Kubernetes resources.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.19.0
        ports:
        - containerPort: 80

Creating a Nginx Deployment

Preparing the Environment

Before creating the Nginx deployment, you need to have a Kubernetes cluster up and running. You can use a local Kubernetes environment like minikube or a managed Kubernetes service provided by cloud providers.

Creating the Nginx Deployment

To create a Nginx deployment with one replica in Kubernetes, you can use the following YAML configuration file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.19.0
        ports:
        - containerPort: 80

Save this configuration to a file, for example, nginx-deployment.yaml.

Applying the Deployment

To create the Nginx deployment, run the following command in your terminal:

kubectl apply -f nginx-deployment.yaml

This command will create the Nginx deployment based on the configuration file you provided.

Verifying the Deployment

After creating the Nginx deployment, you can verify its status using the following commands:

## List all deployments
kubectl get deployments

## Describe the Nginx deployment
kubectl describe deployment nginx-deployment

These commands will show you the status of the Nginx deployment, including the number of replicas, the container image, and other relevant information.

Verifying the Deployment

Checking the Deployment Status

After creating the Nginx deployment, you can check its status using the kubectl get deployments command:

kubectl get deployments

This will show you the current status of the deployment, including the number of replicas, the container image, and the age of the deployment.

Inspecting the Deployment Details

To get more detailed information about the Nginx deployment, you can use the kubectl describe deployment command:

kubectl describe deployment nginx-deployment

This will provide you with detailed information about the deployment, including the pod template, the container specifications, and any events related to the deployment.

Accessing the Nginx Service

To access the Nginx service running in the Kubernetes cluster, you can use the kubectl get services command to list all the services in the cluster:

kubectl get services

This will show you the service that has been created for the Nginx deployment. You can then use the service's IP address or hostname to access the Nginx application.

Scaling the Deployment

If you need to scale the Nginx deployment, you can use the kubectl scale command:

kubectl scale deployment nginx-deployment --replicas=3

This will scale the Nginx deployment to 3 replicas, ensuring that there are 3 instances of the Nginx container running in the Kubernetes cluster.

By following these steps, you can verify that the Nginx deployment has been created successfully and learn how to interact with the deployment to scale it or access the Nginx service.

Summary

In this Kubernetes tutorial, you have learned how to create a Nginx deployment with one replica. By understanding the Kubernetes fundamentals, you can now deploy your own containerized applications and manage them effectively using this powerful platform. Kubernetes provides a robust and scalable solution for running and managing your applications in a cloud-native environment.

Other Kubernetes Tutorials you may like