In this section, we will walk through the process of deploying a web application on a Kubernetes cluster. This will include creating necessary Kubernetes resources, configuring them, and ensuring the application is accessible.

Objectives

By the end of this section, you will:

  • Understand how to create and configure Kubernetes resources for a web application.
  • Deploy a web application using Kubernetes.
  • Expose the web application to external traffic.

Prerequisites

  • Basic understanding of Kubernetes concepts such as Pods, Services, and Deployments.
  • A running Kubernetes cluster.
  • kubectl command-line tool configured to interact with your cluster.

Step-by-Step Guide

  1. Create a Deployment

A Deployment ensures that a specified number of pod replicas are running at any given time. Let's start by creating a Deployment for our web application.

Example Deployment YAML:

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

Explanation:

  • apiVersion: apps/v1: Specifies the API version.
  • kind: Deployment: Specifies the resource type.
  • metadata: Contains the name of the Deployment.
  • spec: Defines the desired state of the Deployment.
    • replicas: Number of pod replicas.
    • selector: Label selector to identify the pods managed by this Deployment.
    • template: Pod template that describes the pods to be created.
      • metadata: Labels for the pods.
      • spec: Container specifications.
        • containers: List of containers to run in the pod.
          • name: Name of the container.
          • image: Docker image to use.
          • ports: Ports to expose.

Create the Deployment:

kubectl apply -f deployment.yaml

  1. Create a Service

A Service exposes the Deployment to external traffic. We will create a Service of type LoadBalancer to expose our web application.

Example Service YAML:

apiVersion: v1
kind: Service
metadata:
  name: web-app-service
spec:
  selector:
    app: web-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer

Explanation:

  • apiVersion: v1: Specifies the API version.
  • kind: Service: Specifies the resource type.
  • metadata: Contains the name of the Service.
  • spec: Defines the desired state of the Service.
    • selector: Label selector to identify the pods targeted by this Service.
    • ports: List of ports to expose.
      • protocol: Protocol to use.
      • port: Port on which the Service is exposed.
      • targetPort: Port on the container to which traffic is directed.
    • type: Type of Service (LoadBalancer to expose the Service externally).

Create the Service:

kubectl apply -f service.yaml

  1. Verify the Deployment and Service

Check the Deployment:

kubectl get deployments

Check the Pods:

kubectl get pods

Check the Service:

kubectl get services

  1. Access the Web Application

Once the Service is created, it will be assigned an external IP address. You can access the web application using this IP address.

Get the External IP:

kubectl get services web-app-service

Look for the EXTERNAL-IP column in the output. Open a web browser and navigate to the external IP address to see your web application.

Practical Exercise

Exercise: Deploy a Custom Web Application

  1. Create a Docker image for a simple web application (e.g., a basic HTML page served by Nginx).
  2. Push the Docker image to a container registry (e.g., Docker Hub).
  3. Create a Kubernetes Deployment YAML file for your custom web application.
  4. Create a Kubernetes Service YAML file to expose your web application.
  5. Deploy the web application to your Kubernetes cluster.
  6. Verify the Deployment and Service.
  7. Access the web application using the external IP address.

Solution:

  1. Dockerfile:

    FROM nginx:latest
    COPY index.html /usr/share/nginx/html/index.html
    
  2. Build and Push Docker Image:

    docker build -t <your-dockerhub-username>/custom-web-app:latest .
    docker push <your-dockerhub-username>/custom-web-app:latest
    
  3. Deployment YAML:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: custom-web-app-deployment
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: custom-web-app
      template:
        metadata:
          labels:
            app: custom-web-app
        spec:
          containers:
          - name: custom-web-app
            image: <your-dockerhub-username>/custom-web-app:latest
            ports:
            - containerPort: 80
    
  4. Service YAML:

    apiVersion: v1
    kind: Service
    metadata:
      name: custom-web-app-service
    spec:
      selector:
        app: custom-web-app
      ports:
        - protocol: TCP
          port: 80
          targetPort: 80
      type: LoadBalancer
    
  5. Deploy the Application:

    kubectl apply -f custom-deployment.yaml
    kubectl apply -f custom-service.yaml
    
  6. Verify and Access:

    kubectl get deployments
    kubectl get pods
    kubectl get services custom-web-app-service
    

    Access the web application using the external IP address.

Conclusion

In this section, we learned how to deploy a web application on a Kubernetes cluster. We created a Deployment to manage our application pods and a Service to expose the application to external traffic. This foundational knowledge will help you deploy more complex applications and services in a Kubernetes environment. In the next section, we will explore CI/CD with Kubernetes to automate the deployment process.

Kubernetes Course

Module 1: Introduction to Kubernetes

Module 2: Core Kubernetes Components

Module 3: Configuration and Secrets Management

Module 4: Networking in Kubernetes

Module 5: Storage in Kubernetes

Module 6: Advanced Kubernetes Concepts

Module 7: Monitoring and Logging

Module 8: Security in Kubernetes

Module 9: Scaling and Performance

Module 10: Kubernetes Ecosystem and Tools

Module 11: Case Studies and Real-World Applications

Module 12: Preparing for Kubernetes Certification

© Copyright 2024. All rights reserved