DNS or static IP for service with multi subnet or AZs



I have created stateful service which is backed by a postgres deployment with k8s.

Setup is 3 public subnet|AZ and 3 private subnet|AZ. postgres deployment is in place to create 1 replica and Service with clusterIP: none

But now every time I delete the service and create again IP is changing and I was reading something about DNS resolution. I want to access the DB from java client to be deployed another pod on n/w; here i am unable to get static IP.

Can I create a service with clusterIP: #some_IP_from_one_of_the_subnet_range# ? What will happen if the service goes down and k8s respawns it? Will it be started in same AZ and subnet? what if AZ is down?


Do you want load-balancing or direct-to-pod connections?

If you want LB, just create a Service and don’t delete it.

If you want direct-to-pod connections, create a headless Service and let your clients resolve that DNS name.


will the ip of service change if restarted by k8s? i m trying to avoid scenario where java client dont follow dns ttl if ip os service keeps on changing.


You can also create a service specifically targeting that instance. Each pod in a StatefulSet has a label that can be used in a selector to specifically target that instance: statefulset.kubernetes.io/pod-name. It would not have to be a headless service as its explicitly targeting that one giving it a static service IP.


No, It won’t


Thank you so much. i need the same i think. One QQ: Once I create the service what would be hostname for the service? I need the hostname for db connection properties. <service_name>.<app_name> ?


The DNS entry created is as documented. Please read the documentation in the kubernetes site, that goes way more in detail that I can go in an email, and don’t hesitate to ask if something is not clear :slight_smile:


It’d follow the standard service naming schema:
<service name>.<namespace>.svc.cluster.local


I want to be very clear here. I think you are using “service” to mean something other than Kubernetes Service API.

A Kubernetes Service is a backend selector and optional VIP. The backends it selects are Pods. When Pods get moved to different machines *(e.g. because of failure) they WILL get new IPs. A Service VIP will not change.

If your app uses Service VIPs, you are safe. They were literally designed for this use case.

If your app uses pod IPs directly, your app must tolerate backends changing.

If your app can not tolerate this, you must user some sort of VIP, e.g. a Service.


If I do StatefulSet for postgres and headless svc on top; Can I use <service name>.<namespace>.svc.cluster.local as host name for jdbc client? I confused with DNS resolution; whether it is done for Service VIP or pod VIP.


Will the Service VIP change if service stops working and k8s restarts it? Initially I thought of using clusterIP with static IP within range and use it as JDBC host value. But I have 3 AZs; so trying to figure out if svc would ever fail, if yes will it be restarted within same AZ or another.


I would really like to thank you guys for being patient with all the newbie queries. Thank you for time and valuable inputs.


A service never gets “restarted” because it is not running. The pods behind it are running, but a Service exists to provide a stable IP frontend to those pods. Pods can some and go, start, stop, crash, resume, whatever and the Service IP is stable.