KUBERNETES PODs & DEPLOYMENT

Differences Between Container, Pod, and Deployment in Kubernetes with E-commerce Example:

Container:

  1. Definition:

    • A container is a lightweight, standalone, and executable software package that includes everything needed to run a piece of software, including the code, runtime, libraries, and system tools.
  2. E-commerce Example:

    • In an e-commerce application, a container may encapsulate a microservice responsible for handling user authentication. It includes the necessary code, dependencies, and runtime for authentication logic.

Pod:

  1. Definition:

    • A pod is the smallest deployable unit in Kubernetes and represents a group of one or more containers that share the same network namespace, IP address, and port space.
  2. E-commerce Example:

    • In the context of an e-commerce application, a pod may consist of multiple containers, such as one container for the main application handling product catalog and another container for a sidecar logging service. These containers share the same network and resources within the pod.

Deployment:

  1. Definition:

    • A deployment in Kubernetes is an abstraction that defines a desired state for the application and manages the deployment and scaling of replica sets.
  2. E-commerce Example:

    • Consider an e-commerce application that has a deployment managing the product catalog service. The deployment specifies the desired number of replicas, rolling update strategy, and scaling policies. It ensures that a specific number of instances of the product catalog service are running, handling updates and scaling based on demand.

Industry-Level Example:

Let's say we have an e-commerce platform with microservices for user authentication, product catalog, and order processing.

  • Container:

    • Containerizing the user authentication microservice ensures that it runs consistently across different environments, handling user login and authorization.
  • Pod:

    • The product catalog microservice and a logging sidecar may run together in a pod. They share the same network space and resources, collaborating to provide a seamless product browsing experience.
  • Deployment:

    • The deployment manages the order processing microservice, ensuring that multiple replicas are available to handle incoming orders. It facilitates updates without downtime, and scaling based on traffic spikes.

In summary, containers encapsulate microservices, pods group containers, and deployments manage the deployment and scaling of pods. In the e-commerce context, these abstractions allow for the efficient and scalable deployment of various services that collectively form a robust online shopping experience.

KUBERNETES POD

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
    - name: nginx
      image: nginx:latest
      ports:
        - containerPort: 80

Now, let's break down each part:

  • apiVersion: Specifies the Kubernetes API version for this object. In this case, it's using the core/v1 API version.

  • kind: Defines the type of Kubernetes object. Here, it's a Pod, which represents a single instance of a running process in a cluster.

  • metadata: Contains metadata about the pod, such as its name. In this example, the pod is named "nginx"

  • spec: Describes the desired state for the pod.

    • containers: An array of containers that should be run in the pod. In this case, there's one container named "nginx" or what you want to added here ,like " nginx-container"

      • name: The name of the container.

      • image: Specifies the Docker image to use for the container. Here, it's using the latest Nginx image from Docker Hub.

      • ports: Specifies the ports to expose from the container.

        • containerPort: The port on which the container will listen for incoming traffic. In this example, it's port 80, the default port for HTTP traffic.

With this YAML file, when you apply it to your Kubernetes cluster using kubectl apply -f pod.yaml, it creates a pod running the Nginx web server. The pod is a basic unit of deployment in Kubernetes, and it encapsulates one or more containers. The Nginx container listens on port 80, and you can access it within the cluster.

KUBERNETES DEPLOYMENT

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

Now, let's break down each part:

  • apiVersion: Specifies the Kubernetes API version for this object. In this case, it's using the apps/v1 API version, which is commonly used for Deployments.

  • kind: Defines the type of Kubernetes object. Here, it's a Deployment, which manages the deployment and scaling of a set of pods.

  • metadata: Contains metadata about the deployment, such as its name. In this example, the deployment is named "nginx-deployment."

  • spec: Describes the desired state for the deployment.

    • replicas: Specifies the desired number of replicas (pods) for the deployment. In this example, it's set to 3, meaning it wants to maintain three replicas.

    • selector: Defines how the deployment identifies which pods to manage. It uses labels to match pods.

      • matchLabels: Specifies the labels that the deployment should match to select the pods. Here, it's looking for pods with the label "app: nginx."
    • template: Specifies the pod template used to create new pods.

      • metadata: Contains labels for the pods created from this template.

        • labels: Labels for the pods.

          • app: nginx: Labels the pods with the label "app" and the value "nginx."
      • spec: Describes the pod's desired state.

        • containers: An array of containers that should be run in the pod.

          • name: The name of the container.

          • image: Specifies the Docker image to use for the container. Here, it's using the latest Nginx image from Docker Hub.

          • ports: Specifies the ports to expose from the container.

            • containerPort: The port on which the container will listen for incoming traffic. In this example, it's port 80, the default port for HTTP traffic.

With this YAML file, when you apply it to your Kubernetes cluster using kubectl apply -f nginx-deployment.yaml, it creates a deployment managing three replicas of the Nginx pod. The deployment ensures that the specified number of replicas is maintained, and it can scale pods up or down based on the desired state.