Deploy Docker on Kubernetes: Step-by-Step Guide
To deploy a Docker image on Kubernetes, you can follow these steps:
- Firstly, make sure you have installed and configured the Kubernetes cluster.
- Create a Deployment object that describes the application you want to deploy. Define a Pod template under the spec of the Deployment object that includes container image, container ports, and other relevant information.
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: your-docker-image
ports:
- containerPort: 80
- Deploy the Deployment object to the Kubernetes cluster using the kubectl command.
kubectl apply -f deployment.yaml
- To check the status of a Pod after waiting for it to start and run, you can use the following command:
kubectl get pods
- If you need to expose the services of the Deployment to external access, you can create a Service object and associate it with the Deployment. Ensure that the selector defined in the spec of the Service object matches the labels of the Deployment.
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 80
type: NodePort
- Deploy the Service object to the Kubernetes cluster using the kubectl command.
kubectl apply -f service.yaml
- After the Service is created, you can use the following command to view the NodePort port of the Service:
kubectl get svc my-service
You have now successfully deployed a Docker image on a Kubernetes cluster and can access the service through the NodePort port.