Deploy Docker on Kubernetes: Step-by-Step Guide

To deploy a Docker image on Kubernetes, you can follow these steps:

  1. Firstly, make sure you have installed and configured the Kubernetes cluster.
  2. 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
  1. Deploy the Deployment object to the Kubernetes cluster using the kubectl command.
kubectl apply -f deployment.yaml
  1. To check the status of a Pod after waiting for it to start and run, you can use the following command:
kubectl get pods
  1. 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
  1. Deploy the Service object to the Kubernetes cluster using the kubectl command.
kubectl apply -f service.yaml
  1. 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.

bannerAds