Services can't communicate because there is not DNS resolving in Kubernetes

4/21/2020

I configure my services with type ClusterIP. And I want to make them communicated.

Service

apiVersion: v1
kind: Service
metadata:
 labels:
  app: app-backend-deployment
 name: app-backend
spec:
 type: ClusterIP
 ports:
 - port: 8020
   protocol: TCP
   targetPort: 8100
 selector:
  app: app-backend

Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
 labels:
  app: app-backend
 name: app-backend-deployment
spec:
 replicas: 1
 selector:
    matchLabels:
      app: app-backend
 template:
    metadata:
      labels:
        app: app-backend
    spec:
     containers:
     - name: app-backend
       image: app-backend
       ports:
       - containerPort: 8100
       imagePullPolicy: Never

ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: backend-conf # name of configMap
data:
  BACKEND_SERVICE_HOST: app-backend:8020

And that is what I pass to the frontend service, and I want to make a REST call through the DNS name for example http://app-backend:8020/get/1. But like I see in the console app cannot resolve DNS name net::ERR_NAME_NOT_RESOLVED.

I also check pod nslookup:

busybox nslookup app-backend.default.svc.cluster.local
Server:         10.96.0.10
Address:        10.96.0.10:53

Name:   app-backend.default.svc.cluster.local
Address: 10.106.41.36

And compare it to

kubectl describe svc app-backend
Name:              app-backend
Namespace:         default
Labels:            app=app-backend-deployment
Annotations:       kubectl.kubernetes.io/last-applied-configuration:
                     {"...
Selector:          app=app-backend
Type:              ClusterIP
IP:                10.106.41.36
Port:              <unset>  8020/TCP
TargetPort:        8100/TCP

And like you can see there is the same IP on Address but I don't know and where to look what is wrong why dns resolver doesn't work. kubectl version Client "v1.15.5", Server Version:"v1.17.3",

-- Adriano
kubernetes

1 Answer

4/23/2020

Because of that frontend service that was served to the local machine (that how Angular works) REST request cannot go through Kubernetes DNS with another backend service. I need to communicate them through the Ingress. Due to different annotations, I have to use 2 Ingress. Meaby there is a better way to use just one, but when I want to use only one Ingress I can't find a way to make them both working, with the same annotation.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
 annotations:
  kubernetes.io/ingress.class: nginx
  nginx.ingress.kubernetes.io/rewrite-target: /$2
 name: app-backend-ingress
spec:
 rules:
 - host: app.io
   http:
     paths:
       - path: /api(/|$)(.*)
         backend:
          serviceName: app-backend
          servicePort: 8020
---  
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
 annotations:
  kubernetes.io/ingress.class: nginx
  nginx.ingress.kubernetes.io/rewrite-target: /
 name: app-frontend-ingress
spec:
 rules:
 - host: app.io
   http:
     paths:
       - path: /
         backend:
          serviceName: app-frontend
          servicePort: 80
-- Adriano
Source: StackOverflow