Kubernetes服务架构 [英] Kubernetes service architecture
问题描述
在同一kubernetes集群中,
Within the same kubernetes cluster,
-
我可以将多个StatefulSet附加到一个无头服务中,还是每个StatefulSet都应具有自己的无头服务?这样做的利弊是什么?
Can I have multiple StatefulSets attached to one headless service or should each StatefulSet have it's own headless service? What are the pros and cons of doing this?
我可以在同一群集中混合使用标准服务和无头服务吗?具体来说,我想使用LoadBalancer服务来平衡无头服务.是否可以定义LoadBalancer类型的服务并为其附加无头服务(ClusterIP = None)?如果是,我该如何实现?
Can I mix standard and headless services in the same cluster? Specifically, I would like to use LoadBalancer service to load balance headless services. Can I define a service of type LoadBalancer and have headless services (ClusterIP = None) attached to it? If yes, how can I achieve this?
这是我想要的体系结构:
Here is my intended architecture:
Load Balancer Service
- Headless Service (Database-service)
- MySql
- BlazeGraph
- Headless Service (Web / Tomcat)
- Web Service (RESTful / GraphQL)
任何建议和见识都受到赞赏.
Any advice and insight is appreciated.
我的设置
我的服务及其附加的状态集具有不同的标签.
My service and the statefulsets attached to it have different labels.
database-service: app=database
mysqlset: app=mysql
我的豆荚
khteh@khteh-T580:~ 2007 $ k get pods -l app=mysql -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE
mysql-0 1/1 Running 1 18h 10.1.1.4 khteh-t580 <none>
khteh@khteh-T580:~ 2008 $ k get pods -l app=blazegraph -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE
blazegraph-0 1/1 Running 1 18h 10.1.1.254 khteh-t580 <none>
khteh@khteh-T580:~ 2009 $ k describe service database-service
Name: database-service
Namespace: default
Labels: app=database
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"database"},"name":"database-service","namespace":"defaul...
Selector: app=database,tier=database
Type: ClusterIP
IP: None
Port: mysql 3306/TCP
TargetPort: 3306/TCP
Endpoints: <none>
Port: blazegraph 9999/TCP
TargetPort: 9999/TCP
Endpoints: <none>
Session Affinity: None
Events: <none>
请注意,服务端点为<none>
.我不确定这是正确的设置.
Notice the service Endpoints is <none>
. I am not sure this is the right setup.
推荐答案
无头服务在任何情况下都应该使用,如果您想自动发现该服务下的所有Pod,而不是常规服务,而要获取ClusterIP则是常规服务.作为上述示例的说明,此处是服务(带有ClusterIP)和无头服务(没有ClusterIP)的DNS条目之间的区别:
Headless Service you should use in any case where you want to automatically discover all pods under the service as opposed to regular Service where you get ClusterIP instead. As an illustration from above mentioned example here is difference between DNS entries for Service (with ClusterIP) and Headless Service (without ClusterIP):
-
标准服务,您将获得clusterIP值:
Standard service you will get the clusterIP value:
kubectl exec zookeeper-0 -- nslookup zookeeper
Server: 10.0.0.10
Address: 10.0.0.10#53
Name: zookeeper.default.svc.cluster.local
Address: 10.0.0.213
无头服务,您将获得每个吊舱的IP
Headless service you will get IP of each pod
kubectl exec zookeeper-0 -- nslookup zookeeper
Server: 10.0.0.10
Address: 10.0.0.10#53
Name: zookeeper.default.svc.cluster.local
Address: 172.17.0.6
Name: zookeeper.default.svc.cluster.local
Address: 172.17.0.7
Name: zookeeper.default.svc.cluster.local
Address: 172.17.0.8
现在,如果您使用单个无头服务连接两个有状态集,它将返回两个有状态集中每个Pod的地址.如果您为此创建两个statefulset和一个headless服务,则无法将Pod与两个应用程序区分开.请参阅以下文章,以了解为什么使用无头服务
Now, If you connect two statefulset with single headless service, it will return the address of each pod in both the statefulset. There will be no way to differentiate the pods from two applications if you create two statefulset and one headless service for that. See the following article to understand why headless services are used
无头服务使开发人员可以通过发现自己的方式来减少与kubernetes系统的耦合.对于此类服务,不会分配clusterIP,kube-proxy无法处理这些服务,并且平台也不会为它们完成负载平衡和代理.因此,如果您在服务中定义clusterIP: None
,那么从kubernetes端将不会进行任何负载平衡.
Headless service allow developer to reduce coupling from kubernetes system by allowing them to do discovery their own way. For such services, clusterIP is not allocated, kube-proxy doesn't handle these services and there is no load balancing and proxying done by platform for them. So, If you define clusterIP: None
in your service there will be no load-balancing will be done from kubernetes end.
希望这会有所帮助.
我做了一个小实验来回答您的查询,创建了两个名为mysql和mysql2的mysql数据库的有状态集,每个状态有1个副本.它们具有自己的PV,PVC,但仅受一次无头服务约束.
I did a little experiment to answer your queries, created two statefulsets of mysql database named mysql and mysql2, with 1 replica for each statefulset. They have their own PV, PVC but bound by only single headless service.
[root@ip-10-0-1-235 centos]# kubectl get pods -l app=mysql -o wide
NAME READY STATUS RESTARTS AGE IP NODE
mysql-0 1/1 Running 0 4m 192.168.13.21 ip-10-0-1-235.ec2.internal
mysql2-0 1/1 Running 0 3m 192.168.13.22 ip-10-0-1-235.ec2.internal
现在您可以看到两个吊舱都附有一个无头服务
Now you can see the single headless service attached to both the pods
[root@ip-10-0-1-235 centos]# kubectl describe svc mysql
Name: mysql
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=mysql
Type: ClusterIP
IP: None
Port: <unset> 3306/TCP
TargetPort: 3306/TCP
Endpoints: 192.168.13.21:3306,192.168.13.22:3306
Session Affinity: None
Events: <none>
现在,当您从其他Pod中查找服务时,它将返回两个Pod的IP地址:
Now when you lookup the service from some other pod, it returns IP address of both the pods:
[root@rtp-worker-0 /]# nslookup mysql
Server: 10.96.0.10
Address: 10.96.0.10#53
Name: mysql.default.svc.cluster.local
Address: 192.168.13.21
Name: mysql.default.svc.cluster.local
Address: 192.168.13.22
现在,无法确定哪个地址(pod)是哪个状态集.现在,我尝试使用其元数据名称来标识有状态集,但不能
Now, it is impossible to identify which address(pod) is of which statefulset. Now I tried to identify the statefulset using its metadata name, but couldn't
[root@rtp-worker-0 /]# nslookup mysql2.mysql.default.svc.cluster.local
Server: 10.96.0.10
Address: 10.96.0.10#53
** server can't find mysql2.mysql.default.svc.cluster.local: NXDOMAIN
希望它能澄清.
这篇关于Kubernetes服务架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!