k8s的service和endpoint
service: 在k8s中,pod之间是通信是一般通过service名称完成的endpoint:pod和service之间的关联关系,是通过endpoint实现的。Endpoint Endpoints表示了一个Service对应的所有Pod副本的访问地址,而Endpoints Controller负责生成和维护所有Endpoints对象的控制器。它负责监听Service和对应的Pod副本的变化。
·
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
kind: Endpoints
apiVersion: v1
metadata:
name: redis-cluster
subsets:
- addresses:
- ip: 10.130.25.161
- ip: 10.130.25.162
- ip: 10.130.25.163
ports:
- port: 6379
---
kind: Service
apiVersion: v1
metadata:
name: redis-cluster
spec:
ports:
- protocol: TCP
port: 6379
targetPort: 6379
查看servcie和endpoint详情,可看到ip和端口的对应关系
[root@10-9-71-11 ~]# kubectl describe endpoints redis-cluster
Name: redis-cluster
Namespace: default
Labels: <none>
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: <none>
Ports:
Name Port Protocol
---- ---- --------
<unset> 6379 TCP
Events: <none>
[root@10-9-71-11 ~]# kubectl describe svc redis-cluster
Name: redis-cluster
Namespace: default
Labels: <none>
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"redis-cluster","namespace":"default"},"spec":{"ports":[{"port":63...
Selector: <none>
Type: ClusterIP
IP: 10.0.147.90
Port: <unset> 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: <none>
简单查看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 <none> 6379/TCP 2m16s
一个地址,多个端口
创建脚本
[root@10-9-71-11 ~]# cat redis.yaml
kind: Endpoints
apiVersion: v1
metadata:
name: redis
subsets:
- addresses:
- ip: 10.130.25.161
ports:
- name: port1
port: 26379
- name: port2
port: 26380
- name: port3
port: 26381
---
kind: Service
apiVersion: v1
metadata:
name: redis
spec:
ports:
- name: port1
protocol: TCP
port: 26379
targetPort: 26379
- name: port2
protocol: TCP
port: 26380
targetPort: 26380
- name: port3
protocol: TCP
port: 26381
targetPort: 26381
- 使用多个端口时,端口必须有名字
- 多端口之间通过列表的形式展现
查看servcie和endpoint详情,可看到ip和端口的对应关系
[root@10-9-71-11 ~]# kubectl describe endpoints redis
Name: redis
Namespace: default
Labels: <none>
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: <none>
Ports:
Name Port Protocol
---- ---- --------
port3 26381 TCP
port2 26380 TCP
port1 26379 TCP
Events: <none>
--------------------------------------------------
[root@10-9-71-11 ~]# kubectl describe service redis
Name: redis
Namespace: default
Labels: <none>
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"redis","namespace":"default"},"spec":{"ports":[{"name":"port1","p...
Selector: <none>
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: <none>
简单查看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 <none> 26379/TCP,26380/TCP,26381/TCP 49m
更多推荐
已为社区贡献1条内容
所有评论(0)