简体   繁体   English

k8s-Postgres服务的IP和DNS

[英]k8s - IP and DNS for postgres with service

I have created stateful service which is backed by a postgres deployment with k8s. 我创建了有状态服务,该服务由使用k8s的postgres部署支持。

Setup is 3 public subnet|AZ and 3 private subnet|AZ. 设置为3个公共子网| AZ和3个私有子网| AZ。 postgres deployment is in place to create 1 replica and Service with clusterIP: none 已进行postgres部署以使用clusterIP: none创建1个副本和服务clusterIP: none

But now every time I delete the service and create again IP is changing and I was reading something about DNS resolution. 但是现在每次我删除服务并重新创建IP时,IP都在变化,我正在阅读有关DNS解析的信息。 I want to access the DB from java client to be deployed another pod on n/w; 我想从Java客户端访问数据库,以在n / w上部署另一个pod; here i am unable to get static IP. 在这里,我无法获得静态IP。

Can I create a service with clusterIP: #some_IP_from_one_of_the_subnet_range# ? 我可以使用clusterIP: #some_IP_from_one_of_the_subnet_range#创建服务clusterIP: #some_IP_from_one_of_the_subnet_range# What will happen if the service goes down and k8s respawns it? 如果服务中断并重新生成k8s会发生什么? Will it be started in same AZ and subnet? 它将在相同的可用区和子网中启动吗? what if AZ is down? 如果AZ下降怎么办?

I have reproduced the issue you have reported. 我已转载您报告的问题。 I have created a postgres cluster and also created a K8S service for the cluster. 我创建了一个postgres集群,还为该集群创建了K8S服务。

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. 该服务提供了一个服务名称,借助k8s DNS解析,该服务名称也转换为IP地址。

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. 因此,我们无需为postgres集群使用静态IP地址,因为postgres k8s服务将帮助您连接到集群容器。 To prove that I have sent a curl request by using the service name rather than the IP address. 为了证明我已经通过使用服务名称而不是IP地址发送了curl请求。

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. 在为postgres创建集群时,请确保将端点连接到您的集群容器之一。 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. 在上面的示例中,将端点分配给了10.244.1.12等。 If that field is empty then the access to the postgres service will yield no result. 如果该字段为空,则对postgres服务的访问将不会产生任何结果。

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. 如果群集无法识别服务,则无法通过Java访问。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM