Experiment No: 8
Integrate Kubernetes and Docker
Aim: Integrate Kubernetes and Docker
Description:
Kubernetes and Docker are both popular technologies for managing containers, but they are
used for different purposes. Kubernetes is an orchestration platform that provides a higher-
level abstraction for managing containers, while Docker is a containerization technology
that provides a lower-level runtime for containers.
To integrate Kubernetes and Docker, you need to use Docker to build and package your
application as a container image, and then use Kubernetes to manage and orchestrate the
containers.
Lab Manual: Integrate Kubernetes and Docker
Prerequisites
1. Windows OS
Ensure your system has Windows 10/11 with WSL2 (Windows Subsystem for Linux)
enabled.
2. Installed Tools:
- Docker Desktop for Windows
- Kubernetes (enabled within Docker Desktop)
- A text editor (e.g., Visual Studio Code, Notepad++)
- Command Prompt/PowerShell/WSL Terminal
3. Cloud Account (Optional):
Access to a container registry like Docker Hub or Google Container Registry.
Steps
1. Install and Configure Docker
1. Download and install [Docker Desktop](https://www.docker.com/products/docker-
desktop/) for Windows.
2. Enable Kubernetes in Docker Desktop:
- Open Docker Desktop.
- Go to Settings > Kubernetes.
- Enable Kubernetes and apply changes.
- Wait for Kubernetes to be initialized.
2. Build a Docker Image
1. Create a `Dockerfile`:
Open your text editor and create a file named `Dockerfile` in your application directory.
Add the following contents:
dockerfile
# Use a base image
FROM python:3.9-slim
# Set the working directory
WORKDIR /app
# Copy application code
COPY . /app
# Install dependencies
RUN pip install -r requirements.txt
# Specify the command to run the application
CMD ["python", "app.py"]
```
2. Build the Docker image:
Open your terminal and navigate to the directory containing the `Dockerfile`. Run the
following command:
docker build -t your-dockerhub-username/app-name:tag .
3. Push Docker Image to a Registry
1. Log in to Docker Hub:
docker login
Enter your Docker Hub credentials when prompted.
2. Push the image:
docker push your-dockerhub-username/app-name:tag
4. Deploy the Docker Image to Kubernetes
1. Create a Kubernetes Deployment YAML file (`deployment.yaml`):
apiVersion: apps/v1
kind: Deployment
metadata:
name: k8s-demo
spec:
replicas: 2
selector:
matchLabels:
app: k8s-demo
template:
metadata:
labels:
app: k8s-demo
spec:
containers:
- name: k8s-demo
image: myusername/k8s-demo:1.0
ports:
- containerPort: 5000
2. Create a Kubernetes Service YAML file (`service.yaml`):
apiVersion: v1
kind: Service
metadata:
name: k8s-demo-service
spec:
selector:
app: k8s-demo
ports:
- protocol: TCP
port: 80
targetPort: 5000
type: LoadBalancer
3. Apply the configuration:
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
4. Verify the deployment:
kubectl get pods
kubectl get services
5. Monitor and Manage Containers
- View Pod Logs:
kubectl logs <pod-name>
- Scale the Deployment:
kubectl scale deployment k8s-demo --replicas=4
- Update the Image:
Update the `image` field in `deployment.yaml` and reapply:
kubectl apply -f deployment.yaml
6. Set Up Continuous Integration and Deployment (Optional)
Use a CI/CD tool like GitHub Actions, Jenkins, or GitLab CI/CD to automate building,
pushing, and deploying the Docker image.
Result
By following this manual, you successfully integrated Kubernetes and Docker, built a
container image, deployed it to Kubernetes, and managed it effectively.