Nodeport type service hostname cannot be resolved by kube-dns (But headless works)

10/2/2018

I'm trying to create Cassandra service with StatefulSet. When I'm using headless service like below, everything works fine, the Cassandra pod hostname is cassandra-0.cassandra.default.svc.cluster.local And I can ping this hostname from another pod.

apiVersion: v1
kind: Service
metadata:
  labels:
    app: cassandra
  name: cassandra
spec:
  clusterIP: None
  ports:
    - port: 9042
  selector:
    app: cassandra

However, as I need to expose Cassandra service to the outside of the cluster, then I change the service to nodeport type, like below, then the DNS stops working, the same hostname cassandra-0.cassandra.default.svc.cluster.local cannot be reached anymore.

apiVersion: v1
kind: Service
metadata:
  labels:
    app: cassandra
  name: cassandra
spec:
  type: NodePort
  ports:
    - port: 9042
      nodePort: 30042
  selector:
    app: cassandra

Any idea on what I'm doing wrong, let me know if additional information is needed.

Thanks.

(Updated:) In my setting, the service is in subset 10.96.0.0/12, and the pod is in subnet 10.244.0.0/16.

When using the type NodePort, cassandra service is assigned with cluster-ip in subset 10.96.0.0/12

root@kube-master:/hostdir# kubectl get all
NAME              READY     STATUS    RESTARTS   AGE
pod/cassandra-0   1/1       Running   0          11m

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
service/cassandra    NodePort    10.98.104.176   <none>        9042:30042/TCP   11m
service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP          16m

NAME                         DESIRED   CURRENT   AGE
statefulset.apps/cassandra   1         1         11m

And the nslookup results is

root@cassandra-0:/# nslookup cassandra.default
Server:     10.96.0.10
Address:    10.96.0.10#53

Name:   cassandra.default.svc.cluster.local
Address: 10.98.104.176

While all other pods are in subset 10.244.0.0/16, so I believe that is the reason why ping on cassandra-0.cassandra.default.svc.cluster.local does not work, since the subset 10.96.0.0/12 is not accessible from those pods.

root@kube-master:/hostdir# kubectl get pods -o wide
NAME          READY     STATUS    RESTARTS   AGE       IP           NODE
cassandra-0   1/1       Running   0          12m       10.244.4.2   kube-node-2

Then how can I solve this issue? I tried to put service and pod in the same subset, but it does not seem to work in that way, as in this case I cannot even ping anything.

Thanks.

-- Messi
kube-dns
kubernetes

0 Answers