Kubernetes Load Balancer Type not responding to External IP Address

12/19/2018

I've been trying to use the below to expose my application to a public IP. This is being done on Azure. The public IP is generated but when I browse to it I get nothing.

This is a Django app which runs the container on Port 8000. The service runs at Port 80 at the moment but even if I configure the service to run at port 8000 it still doesn't work.

Is there something wrong with the way my service is defined?

apiVersion: v1
kind: Service
metadata:
  name: web
  labels:
    app: hmweb
spec:
  ports:
    - port: 80
  selector:
    app: hmweb
    tier: frontend
  type: LoadBalancer

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hmweb-deployment
  labels:
    app: hmweb
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hmweb
  template:
    metadata:
      labels:
        app: hmweb
    spec:
      containers:
      - name: hmweb
        image: nw_webimage
        envFrom:
          - configMapRef:
              name: new-config
        command: ["/bin/sh","-c"]
        args: ["gunicorn saleor.wsgi -w 2 -b 0.0.0.0:8000"]
        ports:
        - containerPort: 8000
      imagePullSecrets:
      - name: key

Output of kubectl describe service web (name of service:)

Name:                     web
Namespace:                default
Labels:                   app=hmweb
Annotations:              kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"hmweb"},"name":"web","namespace":"default"},"spec":{"ports":[{"port":...
Selector:                 app=hmweb
Type:                     LoadBalancer
IP:                       10.0.86.131
LoadBalancer Ingress:     13.69.127.16
Port:                     <unset>  80/TCP
TargetPort:               8000/TCP
NodePort:                 <unset>  31827/TCP
Endpoints:                10.244.0.112:8000
Session Affinity:         None
External Traffic Policy:  Cluster
Events:
  Type    Reason                Age   From                Message
  ----    ------                ----  ----                -------
  Normal  EnsuringLoadBalancer  8m    service-controller  Ensuring load balancer
  Normal  EnsuredLoadBalancer   7m    service-controller  Ensured load balancer
-- Rutnet
kubernetes

1 Answer

12/19/2018

The reason behind that is your service has two selector app: hmweb and tier: frontend and your deployment pods has only single label named app: hmweb. Hence when your service is created it could not find the pods which has both the labels and doesn't connect to any pods. Also, if you have container running on 8000 port then you must define targetPort which has the value of container port on which container is running, else it will take both targetPort and port value as same you defined in your service i.e. port: 80

The correct yaml for your deployment is:

apiVersion: v1
kind: Service
metadata:
  name: web
  labels:
    app: hmweb
spec:
  ports:
    - port: 80
      targetPort: 8000
      protocol: TCP
  selector:
    app: hmweb
  type: LoadBalancer

Hope this helps.

-- Prafull Ladha
Source: StackOverflow