Day 32 Task: Launching your Kubernetes Cluster with Deployment

I have created an deployment file for nginx directly and exposed port with help of service in Kubernetes

  1. To create deployment file with help of image directly
kubectl create deployment my-nginx --image=nginx:latest

Checked deployment file and pod:

kubectl get deployments
kubectl get pod
  1. Now with help of service called “load Balancer” we will expose over Port 80.
kubectl expose deployment my-nginx --port=80 --type=LoadBalancer
  1. If the service is configured as a NodePort, you can access it using the cluster node's IP address and the NodePort

     kubectl get svc my-nginx
    

    Look for the PORT(S) column, which will show something like 80:30001/TCP. The 30001 is the NodePort.

  2. Access the service using the host's IP:

     http://<node-ip>:<NodePort>
    

For Kind, the Node IP is typically localhost or 127.0.0.1.

  1. To access a service running in your Kind cluster:

    Forward a local port to the service's port:

     kubectl port-forward svc/<service-name> <local-port>:<service-port> 
     kubectl port-forward svc/my-nginx 8080:80
    

For deployment you can also use the deployment.yml file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

Best Approach for Kind:

  • For local development, port-forwarding is the quickest and easiest method.

  • For external access or multiple developers, using NodePort or MetalLB is more suitable.