Service not finding endpoint

2/13/2019

I'm trying to map an external Postgres box into my Kubernetes cluster using the Service and Endpoint resources but the service is not detecting the endpoint.

terraform $ kubectl describe services postgres -n infrastructure
Name:              postgres
Namespace:         infrastructure
Labels:            <none>
Annotations:       <none>
Selector:          <none>
Type:              ClusterIP
IP:                10.222.4.128
Port:              postgres  5432/TCP
TargetPort:        5432/TCP
Endpoints:         
Session Affinity:  None
Events:            <none>

The service and endpoint were created with:

kind: Service
apiVersion: v1
metadata:
 name: postgres
 namespace: infrastructure
spec:
 type: ClusterIP
 ports:
 - port: 5432
   targetPort: 5432
----
kind: Endpoints
apiVersion: v1
metadata:
 name: postgres
 namespace: infrastructure
subsets:
 - addresses:
     - ip: "10.0.0.1"
   ports:
     - port: 5432

I can connect to the Postgres box from a running container if I use the IP for the instance, but not with the Kubernetes service name (postgres.infrastructure.svc.cluster.local.)

-- Jacob Lambert
google-kubernetes-engine
kubernetes-service

1 Answer

2/15/2019

I just copied your manifest file directly and applied to my cluster without any running pods and I get an endpoint (unlike yours):

kubectl describe svc/postgres -n infrastructure
Name:              postgres
Namespace:         infrastructure
Labels:            <none>
Annotations:       kubectl.kubernetes.io/last-applied-configuration:
                     {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"postgres","namespace":"infrastructure"},"spec":{"ports":[{"port":...
Selector:          <none>
Type:              ClusterIP
IP:                10.109.74.181
Port:              <unset>  5432/TCP
TargetPort:        5432/TCP
Endpoints:         10.0.0.1:5432
Session Affinity:  None
Events:            <none>

If your "Endpoints" field above is empty, something’s wrong and it's no surprise you can't connect via hostname.

Similarly, hostname lookup works from within a pod on the cluster

$ kubectl run --rm '--restart=Never' '--image-pull-policy=IfNotPresent' -i -t '--image=alpine' tmp-29399
# apk-add install --no-cache bind-tools
[output omitted]
# dig +short postgres.infrastructure.svc.cluster.local.
10.109.74.181

I'm suspecting something's wrong with your service. Can you run kubectl describe svc/postgres -n infrastructure (look at Events section at the end, if exists) to see if there's any problem on service syncing from endpoint resource.

Also please specify where you're running at. Is this GKE?

-- AhmetB - Google
Source: StackOverflow