Deploying a Custom Docker Image
Building a Custom Docker Image
Before you can deploy a custom Docker image to your Kubernetes cluster, you need to build the image. Here's an example of how to build a simple "Hello, World!" application using Docker:
## Create a new directory for the project
mkdir my-app
cd my-app
## Create a new Dockerfile
cat << EOF > Dockerfile
FROM nginx:latest
COPY index.html /usr/share/nginx/html/
EOF
## Create the index.html file
cat << EOF > index.html
<h1>Hello, LabEx!</h1>
EOF
## Build the Docker image
docker build -t labex/my-app:v1 .
This will create a new Docker image named labex/my-app:v1
that includes a simple "Hello, World!" web page.
Deploying the Custom Image
To deploy the custom Docker image to your Kubernetes cluster, you can update the image
field in the Deployment YAML configuration:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: labex/my-app:v1
ports:
- containerPort: 80
Then, apply the updated configuration using kubectl apply
:
kubectl apply -f deployment.yaml
This will create a new Deployment that uses the custom labex/my-app:v1
Docker image.
Verifying the Deployment
You can use the kubectl get
and kubectl describe
commands to verify that the Deployment was created successfully and that the Pods are running the custom Docker image:
kubectl get deployments
kubectl describe deployment my-deployment
You can also access the application by exposing the Deployment as a Kubernetes Service:
kubectl expose deployment my-deployment --type=LoadBalancer --port=80
This will create a new Service that exposes the Deployment on port 80. You can then access the application using the Service's external IP address.