k8s-带服务的postgres的IP和DNS [英] k8s - IP and DNS for postgres with service

查看:308
本文介绍了k8s-带服务的postgres的IP和DNS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了有状态服务,该服务由使用k8s的postgres部署支持.

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

设置为3个公共子网| AZ和3个私有子网| AZ.已进行postgres部署,以使用clusterIP: none

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

但是现在每次我删除服务并再次创建时,IP都在变化,并且我在阅读有关DNS解析的信息.我想从Java客户端访问数据库,以在n/w上部署另一个pod;在这里,我无法获得静态IP.

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.

我可以使用clusterIP: #some_IP_from_one_of_the_subnet_range#创建服务吗?如果服务中断并重新生成k8s会发生什么?它将在相同的可用区和子网中启动吗?如果AZ下降了怎么办?

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?

推荐答案

我转载了您报告的问题.我创建了一个postgres集群,还为该集群创建了K8S服务.

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

集群看起来像

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

创建集群和服务后

该服务提供了一个服务名称,该名称也可以通过k8s DNS解析转换为IP地址.

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

因此我们不需要为postgres集群使用静态IP地址,因为postgres k8s服务将帮助您连接到集群容器. 为了证明我已经通过使用服务名称而不是IP地址发送了curl请求.

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

为postgres创建集群时,请确保将端点连接到您的集群容器之一.看起来应该是这样,

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>

在上面的示例中,端点被分配给10.244.1.12等.如果该字段为空,则对postgres服务的访问将不会产生任何结果.

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.

另一种说法是,服务仅将请求重定向到集群.如果群集无法识别该服务,则无法通过Java访问.

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.

希望这会有所帮助.

这篇关于k8s-带服务的postgres的IP和DNS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆