k8s的service和endpoint

kubernets1年前 (2022)发布 megou
98 0

service: 在k8s中,pod之间是通信是一般通过service名称完成的
endpoint: pod和service之间的关联关系,是通过endpoint实现的。 Endpoints表示了一个Service对应的所有Pod副本的访问地址,而Endpoints Controller负责生成和维护所有Endpoints对象的控制器。它负责监听Service和对应的Pod副本的变化。

对于pod,endpoint是集群自动创建的,用于将service和pod关联起来;而对于外部服务(部署在集群外边的数据库啥的),我们可以人工的创建endpoint和service。使其能够更规范的和pod进行通信。

这里主要记录一下endpoint和service的书写规则。两种场景:

一个端口,多个地址
创建脚本
[root@10-9-71-11 ~]# cat redis.yaml

查看servcie和endpoint详情,可看到ip和端口的对应关系
[root@10-9-71-11 ~]# kubectl describe endpoints redis-cluster
Name: redis-cluster
Namespace: default
Labels:
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{“apiVersion”:”v1″,”kind”:”Endpoints”,”metadata”:{“annotations”:{},”name”:”redis-cluster”,”namespace”:”default”},”subsets”:[{“addresses”:[…
Subsets:
Addresses: 10.130.25.161,10.130.25.162,10.130.25.163
NotReadyAddresses:
Ports:
Name Port Protocol
—- —- ——–
6379 TCP

Events:
[root@10-9-71-11 ~]# kubectl describe svc redis-cluster
Name: redis-cluster
Namespace: default
Labels:
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{“apiVersion”:”v1″,”kind”:”Service”,”metadata”:{“annotations”:{},”name”:”redis-cluster”,”namespace”:”default”},”spec”:{“ports”:[{“port”:63…
Selector:
Type: ClusterIP
IP: 10.0.147.90
Port: 6379/TCP
TargetPort: 6379/TCP
Endpoints: 10.130.25.161:6379,10.130.25.162:6379,10.130.25.163:6379
Session Affinity: None
Events:

简单查看service和endpoint
[root@10-9-71-11 ~]# kubectl get endpoints redis-cluster
NAME ENDPOINTS AGE
redis-cluster 10.130.25.161:6379,10.130.25.162:6379,10.130.25.163:6379 2m3s
[root@10-9-71-11 ~]#
[root@10-9-71-11 ~]# kubectl get svc redis-cluster
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
redis-cluster ClusterIP 10.0.147.90 6379/TCP 2m16s

一个地址,多个端口
创建脚本
[root@10-9-71-11 ~]# cat redis.yaml

使用多个端口时,端口必须有名字
多端口之间通过列表的形式展现
查看servcie和endpoint详情,可看到ip和端口的对应关系
[root@10-9-71-11 ~]# kubectl describe endpoints redis
Name: redis
Namespace: default
Labels:
Annotations: kubectl.kubernetes.io/last-applied-configuration:
# {“apiVersion”:”v1″,”kind”:”Endpoints”,”metadata”:{“annotations”:{},”name”:”redis”,”namespace”:”default”},”subsets”:[{“addresses”:[{“ip”:”1…}]
Subsets:
Addresses: 10.130.25.161
NotReadyAddresses:
Ports:
Name Port Protocol
—- —- ——–
port3 26381 TCP
port2 26380 TCP
port1 26379 TCP

Events:
————————————————–
[root@10-9-71-11 ~]# kubectl describe service redis
Name: redis
Namespace: default
Labels:
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{“apiVersion”:”v1″,”kind”:”Service”,”metadata”:{“annotations”:{},”name”:”redis”,”namespace”:”default”},”spec”:{“ports”:[{“name”:”port1″,”p…
Selector:
Type: ClusterIP
IP: 10.0.92.98
Port: port1 26379/TCP
TargetPort: 26379/TCP
Endpoints: 10.130.25.161:26379
Port: port2 26380/TCP
TargetPort: 26380/TCP
Endpoints: 10.130.25.161:26380
Port: port3 26381/TCP
TargetPort: 26381/TCP
Endpoints: 10.130.25.161:26381
Session Affinity: None
Events:

简单查看service和endpoint
[root@10-9-71-11 ~]# kubectl get endpoints redis
NAME ENDPOINTS AGE
redis 10.130.25.161:26381,10.130.25.161:26380,10.130.25.161:26379 48m
[root@10-9-71-11 ~]# kubectl get svc redis
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
redis ClusterIP 10.0.92.98 26379/TCP,26380/TCP,26381/TCP 49m

————————————————
版权声明:本文为CSDN博主「jjt_!!」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/jjt_zaj/article/details/122432291

© 版权声明

相关文章