0

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?

Vishal Biyani
  • 4,297
  • 28
  • 55
jdoe
  • 3
  • 4

1 Answers1

2

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
  • 2,570
  • 10
  • 15
  • I have a stateful set called mariadb, and a service (let's call it 'mariadb-service' to avoid confusion). How does my application use this mongodb stateful set? So far, I've been setting clusterIP to None and then used mariadb-0.mariadb-service.[namespace].svc.cluster.local in another pod running my application. My question is, How do I use it without setting the clusterIP to None. Will something like this be accessible in another pod? mariadb-service.[namespace].svc.cluster.local – jdoe May 22 '18 at 06:37
  • Statefulset is supposed to use 'ClusterIP: None' to config service. In this way, you can either use mariadb-0.mariadb-service to access the pod, or you can use mariadb-service.[namespace]... to access one of the live pod. – Kun Li May 22 '18 at 07:35