Title: Docker Orchestration with Kubernetes
Objective:
The objective of this experiment is to demonstrate Docker orchestration using Kubernetes. Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. In this experiment, we will deploy a simple multi-container application using Kubernetes to showcase its orchestration capabilities.
Prerequisites:
Docker installed on your system.
Minikube installed for local Kubernetes cluster deployment (optional).
Experiment Steps:
Step 1: Set Up Kubernetes Cluster
If you don't have access to a Kubernetes cluster, you can use Minikube to set up a local single-node cluster for testing purposes. Install Minikube following the official documentation (https://minikube.sigs.k8s.io/docs/start/).
Start the Minikube cluster by running:
minikube start
Step 2: Prepare the Application
Create a new directory for your application (e.g., "my-k8s-app").
Inside the "my-k8s-app" directory, create two Dockerfiles, one for each container you want to deploy in the Kubernetes cluster:
Dockerfile-backend for the backend service (e.g., Flask or Node.js backend).
Dockerfile-frontend for the frontend service (e.g., React or Angular frontend).
Step 3: Build Docker Images
Open a terminal or command prompt.
Navigate to the "my-k8s-app" directory.
Build the Docker images for the backend and frontend services using the respective Dockerfiles:
docker build -t my-backend-service -f Dockerfile-backend .
docker build -t my-frontend-service -f Dockerfile-frontend .
Step 4: Deploy Kubernetes Pods
Create Kubernetes deployment YAML files for the backend and frontend services. For each service, create a .yaml file with the deployment configuration. Example configurations are as follows:
apiVersion: apps/v1
kind: Deployment
metadata:
name: backend-deployment
spec:
replicas: 2
selector:
matchLabels:
app: backend
template:
metadata:
labels:
app: backend
spec:
containers:
- name: backend-container
image: my-backend-service
ports:
- containerPort: 8000
frontend-deployment.yaml:
apiVersion: apps/v1
kind: Deployment metadata: name: frontend-deployment spec: replicas: 3 selector: matchLabels: app: frontend template: metadata: labels: app: frontend spec: containers: - name: frontend-container image: my-frontend-service ports: - containerPort: 80
Apply the deployment configurations to create the backend and frontend deployments in the Kubernetes cluster:
kubectl apply -f backend-deployment.yaml
kubectl apply -f frontend-deployment.yaml
Step 5: Expose Services
Create Kubernetes service YAML files for the backend and frontend services. For each service, create a .yaml file with the service configuration. Example configurations are as follows:
apiVersion: v1
kind: Service
metadata:
name: backend-service
spec:
selector:
app: backend
ports:
- protocol: TCP
port: 80
targetPort: 8000
type: LoadBalancer
frontend-service.yaml:
apiVersion: v1 kind: Service metadata: name: frontend-service spec: selector: app: frontend ports: - protocol: TCP port: 80 targetPort: 80 type: LoadBalancer
Apply the service configurations to create the backend and frontend services in the Kubernetes cluster:
kubectl apply -f backend-service.yaml
kubectl apply -f frontend-service.yaml
Step 6: Access the Application
Retrieve the external IP addresses of the services to access the application:
kubectl get services
Use the external IP address of the frontend service to access the application in a web browser.
Step 7: Scaling the Application
To demonstrate Kubernetes' ability to scale the application, increase the number of frontend replicas:
kubectl scale deployment frontend-deployment --replicas=5
Observe how Kubernetes automatically scales the number of frontend pods.
Step 8: Clean Up
Delete the Kubernetes deployments and services when you're done experimenting:
kubectl delete deployment frontend-deployment backend-deployment
kubectl delete service frontend-service backend-service
Conclusion:
In this experiment, we demonstrated Docker orchestration using Kubernetes. We deployed a multi-container application by defining Kubernetes deployments and services. Kubernetes automatically managed the application's lifecycle, including scaling the frontend replicas as needed. Docker orchestration with Kubernetes enables efficient management and scaling of containerized applications, making it a powerful tool for modern software development and deployment.
Comments
Post a Comment