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
- 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:
- 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:
- Verify the Deployment and Service
Check the Deployment:
Check the Pods:
Check the Service:
- 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:
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
- Create a Docker image for a simple web application (e.g., a basic HTML page served by Nginx).
- Push the Docker image to a container registry (e.g., Docker Hub).
- Create a Kubernetes Deployment YAML file for your custom web application.
- Create a Kubernetes Service YAML file to expose your web application.
- Deploy the web application to your Kubernetes cluster.
- Verify the Deployment and Service.
- Access the web application using the external IP address.
Solution:
-
Dockerfile:
FROM nginx:latest COPY index.html /usr/share/nginx/html/index.html
-
Build and Push Docker Image:
docker build -t <your-dockerhub-username>/custom-web-app:latest . docker push <your-dockerhub-username>/custom-web-app:latest
-
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
-
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
-
Deploy the Application:
kubectl apply -f custom-deployment.yaml kubectl apply -f custom-service.yaml
-
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
- What is Kubernetes?
- Kubernetes Architecture
- Key Concepts and Terminology
- Setting Up a Kubernetes Cluster
- Kubernetes CLI (kubectl)
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
- Monitoring with Prometheus
- Logging with Elasticsearch, Fluentd, and Kibana (EFK)
- Health Checks and Probes
- Metrics Server
Module 8: Security in Kubernetes
Module 9: Scaling and Performance
Module 10: Kubernetes Ecosystem and Tools
Module 11: Case Studies and Real-World Applications
- Deploying a Web Application
- CI/CD with Kubernetes
- Running Stateful Applications
- Multi-Cluster Management