Kubernetes service not exposed when ClusterIp is set

5/22/2018

I have the following YAML file -

apiVersion: v1
kind: Service
metadata:
  labels:
    name: mariadb
  name: mariadb
spec:
  ports:
    - port: 3306
  selector:
    name: mariadb

When this service is created, a ClusterIP is automatically set. My stateful set 'mariadb' is exposed using this service. But if I login to another pod on Kubernetes, I cannot ping this pod using

ping mariadb-0.mariadb.[namespace].svc.cluster.local

It also does not work if the ServiceType is set to 'NodePort'.

If I update the service to

apiVersion: v1
kind: Service
metadata:
  labels:
    name: mariadb
  name: mariadb
spec:
  ports:
    - port: 3306
  clusterIP: None
  selector:
    name: mariadb

When I login to another pod on Kubernetes, I can ping this pod using

ping mariadb-0.mariadb.[namespace].svc.cluster.local

Is there any reason why this internal url is not accessible when the ClusterIP is set?

-- jdoe
ibm-cloud
kubernetes
kubernetes-service

1 Answer

5/22/2018

The key is 'clusterIP: None'.

If clusterIP is not set, k8s will allocate one for the service automatically, also the kube-dns will set a domain name for the service, named mariadb.[namespace].svc.cluster.local, that's your first case.

While if clusterIP is set to 'None', that means k8s doesn't allocate a ip for the service, in this case, kube-dns will set a domain name for every endpoints that the service points to, in your second case, it's mariadb-0.mariadb.[namespace].svc.cluster.local.

Also you can set clusterIP to a ip address, in that case, it's the same as your first case.

That's why you can ping mariadb-0.mariadb.[namespace].svc.cluster.local in your second case, while can't in your first case.

-- Kun Li
Source: StackOverflow