Kubernetes service not creating endpoint despite selector

9/1/2020

I am sure I am missing something clear as day, but here goes. I have a frontend and backend being deployed behind an Nginx ingress. The requests to the backend were timing out, so I tested with a curl pod. I found that I was able to hit the pods directly, but not the service. This led me to run:

> kubectl get endpoints

NAME                                               ENDPOINTS                        AGE
backend-api                                        <none>                           10m
kubernetes                                         167.99.101.163:443               121d
nginx-ingress-ingress-nginx-controller             10.244.0.17:80,10.244.0.17:443   96m
nginx-ingress-ingress-nginx-controller-admission   10.244.0.17:8443                 96m
vue-frontend                                       10.244.0.24:80                   84m

No endpoints... I recall (from when I initially set this deployment up) that this usually has to do with selectors. I have checked and checked and checked, but I swear I have it set up correctly. Clearly not though.

## api-deployment.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend-api
  labels:
    app: backend-api
spec:
  replicas: 2
  selector:
    matchLabels:
      app: backend-api
  template:
    metadata:
      labels:
        app: backend-api
    spec:
      containers:
      - name: backend-api
        image: us.gcr.io/container-registry-276104/backend-api:0.88
        ports:
        - containerPort: 8080
        env:
        ...
> kubectl get pods --show-labels
NAME                                                      READY   STATUS    RESTARTS   AGE   LABELS
backend-api-76d6d4f4c9-drbmn                              1/1     Running   0          85m   app=backend-api,pod-template-hash=76d6d4f4c9
backend-api-76d6d4f4c9-qpbhk                              1/1     Running   0          85m   app=backend-api,pod-template-hash=76d6d4f4c9
curl-curlpod-7b46d7776f-jkt6f                             1/1     Running   0          47m   pod-template-hash=7b46d7776f,run=curl-curlpod
nginx-ingress-ingress-nginx-controller-5475c95bbf-psmzr   1/1     Running   0          97m   app.kubernetes.io/component=controller,app.kubernetes.io/instance=nginx-ingress,app.kubernetes.io/name=ingress-nginx,pod-template-hash=5475c95bbf
vue-frontend-6dbf68446f-pzv5h                             1/1     Running   0          85m   app=vue-frontend,pod-template-hash=6dbf68446f

and the service:

## api-service.yaml
---
apiVersion: v1
kind: Service
metadata:
  name: backend-api
spec:
  selector:
    app: backend-api
  ports:
  - port: 8080
    protocol: TCP
    targetPort: http
  publishNotReadyAddresses: true

Help is, of course, appreciated :) I normally beat my head against the wall for hours, but I'm learning to work smarter, not harder!

Happy to answer any questions - thanks in advance!

-- createchange
kubernetes

2 Answers

9/1/2020
  ports:
  - port: ***
    targetPort: ***

In Service the port defines in which port, you finally want to expose your app. targetPort is the one where you exposed your pod.

So, the targetPort in your Service should be same as containerPort in your Pod.

-- Masudur Rahman
Source: StackOverflow

9/1/2020

It looks like you got the service ports mixed up. targetPort is set to http, which is port 80. This is the port that the service forwards to on the pod. The port value of the service is the port service exposes. So if you want your service to forward traffic to port 8080 of your pod, set targetPort to 8080.

-- Burak Serdar
Source: StackOverflow