k8s - IP and DNS for postgres with service

3/6/2019

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?

-- Pat
aws-eks
eks
kubernetes

1 Answer

3/7/2019

I have reproduced the issue you have reported. I have created a postgres cluster and also created a K8S service for the cluster.

The cluster looks like

root@k8-master:~# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
kibana                      1/1     Running   0          9s
postgres-59bcb7c9d4-lvg8v   1/1     Running   0          56m
postgres-59bcb7c9d4-rfppm   1/1     Running   0          56m
postgres-59bcb7c9d4-s9zc4   1/1     Running   0          56m

After creating the cluster and service

The service provides a service name which also translates to a IP address with the help of k8s DNS resolution.

So we need not use a static IP address for the postgres cluster, because the postgres k8s service will help you connect to the cluster containers. To prove that I have sent a curl request by using the service name rather than the IP address.

root@k8-master:~# kubectl exec -it kibana /bin/bash
bash-4.2$ curl http://postgres:5432
curl: (52) Empty reply from server
bash-4.2$ exit

When you create the cluster for the postgres, please make sure that the endpoints are connected to one of your cluster containers. It should look like this,

root@k8-master:~# kubectl describe services postgres
Name:                     postgres
Namespace:                default
Labels:                   app=postgres
Annotations:              kubectl.kubernetes.io/last-applied-configuration:
                            {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"postgres"},"name":"postgres","namespace":"default"},"spe...
Selector:                 app=postgres
Type:                     NodePort
IP:                       10.97.201.134
Port:                     <unset>  5432/TCP
TargetPort:               5432/TCP
NodePort:                 <unset>  30362/TCP
Endpoints:                10.244.1.12:5432,10.244.1.13:5432,10.244.2.242:5432
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

In the above example the endpoints are assigned to 10.244.1.12 and others. If that field is empty then the access to the postgres service will yield no result.

Another way of saying this is, the service merely redirects the request to the cluster. If the cluster doesn't recognize the service then the access from Java won't work.

Hope this helps.

-- keerthi sunkara
Source: StackOverflow