时间:2025-05-10 21:15
人气:
作者:admin
官方文档:https://kubernetes.io/zh-cn/docs/reference/kubernetes-api/service-resources/endpoints-v1/
Endpoint简称ep
Endpoint是kubernetes中的一个资源对象,存储在etcd中,用来记录一个service对应的所有pod的访问地址,它是根据service配置文件中selector描述产生的。
一个Service由一组Pod组成,这些Pod通过Endpoints暴露出来,Endpoints是实现实际服务的端点集合。换句话说,service和pod之间的联系是通过endpoints实现的。

对Service的访问被分发到了后端的Pod上去,目前kubernetes提供了两种负载分发策略:
[root@master01 ~/ingress]# cat nginx-deploy.yaml
# service
apiVersion: v1
kind: Service
metadata:
namespace: default
name: nginx-svc-clusterip
spec:
type: ClusterIP
selector:
app: nginx
ports:
- name: clusterip-nginx
port: 80
targetPort: 80
protocol: TCP
---
# deploy
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-nginx
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
name: pod-nginx
labels:
app: nginx
spec:
containers:
- name: container-nginx
image: nginx:1.14.1
restartPolicy: Always
# 查看pod
[root@master01 ~/ingress]# kubectl get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
deployment-nginx-6d84458cd8-g8lkv 1/1 Running 0 35m 100.117.144.139 node01 <none> <none>
deployment-nginx-6d84458cd8-j8m6c 1/1 Running 0 35m 100.95.185.234 node02 <none> <none>
deployment-nginx-6d84458cd8-znr7t 1/1 Running 0 35m 100.117.144.140 node01 <none> <none>
# 查看svc详情,注意Endpoints列表是和pod的IP保持一致的
[root@master01 ~/ingress]# kubectl describe svc nginx-svc-clusterip
Name: nginx-svc-clusterip
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=nginx
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.96.1.52
IPs: 10.96.1.52
Port: clusterip-nginx 80/TCP
TargetPort: 80/TCP
Endpoints: 100.117.144.139:80,100.117.144.140:80,100.95.185.234:80
Session Affinity: None
Events: <none>
# 查看endpoints资源
[root@master01 ~/ingress]# kubectl get endpoints
NAME ENDPOINTS AGE
nginx-svc-clusterip 100.117.144.139:80,100.117.144.140:80,100.95.185.234:80 36m
重启Pod
[root@master01 ~/ingress]# kubectl delete po deployment-nginx-6d84458cd8-g8lkv deployment-nginx-6d84458cd8-j8m6c deployment-nginx-6d84458cd8-znr7t
pod "deployment-nginx-6d84458cd8-g8lkv" deleted
pod "deployment-nginx-6d84458cd8-j8m6c" deleted
pod "deployment-nginx-6d84458cd8-znr7t" deleted
查看资源,发现对应的IP是会进行变化的
# 查看Pod
[root@master01 ~/ingress]# kubectl get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
deployment-nginx-6d84458cd8-4z4cb 1/1 Running 0 7s 100.95.185.236 node02 <none> <none>
deployment-nginx-6d84458cd8-ht2z9 1/1 Running 0 7s 100.117.144.141 node01 <none> <none>
deployment-nginx-6d84458cd8-ns47j 1/1 Running 0 7s 100.95.185.238 node02 <none> <none>
# 查看service
[root@master01 ~/ingress]# kubectl describe svc nginx-svc-clusterip
Name: nginx-svc-clusterip
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=nginx
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.96.1.52
IPs: 10.96.1.52
Port: clusterip-nginx 80/TCP
TargetPort: 80/TCP
Endpoints: 100.117.144.141:80,100.95.185.236:80,100.95.185.238:80
Session Affinity: None
Events: <none>
#查看endpoints
[root@master01 ~/ingress]# kubectl get endpoints
NAME ENDPOINTS AGE
kubernetes 10.0.0.30:6443 78m
nginx-svc-clusterip 100.117.144.141:80,100.95.185.236:80,100.95.185.238:80 39m
Endpoints资源是通过name和namespace两个字段与Service进行关联的,所以Endpoints的名称和Service的名称相同.
示例:
[root@master01 ~]# cat ep.yaml
# Endpoints资源
apiVersion: v1
kind: Endpoints
metadata:
name: harbor-huangxin
subsets:
- addresses:
- ip: 10.0.0.250
ports:
- port: 80
protocol: TCP
---
# Service资源
apiVersion: v1
kind: Service
metadata:
name: harbor-huangxin
spec:
type: ClusterIP
ports:
- port: 80
targetPort: 80
# 查看service
[root@master01 ~]# kubectl describe svc endpoint-huangsir
Name: endpoint-huangsir
Namespace: default
Labels: <none>
Annotations: <none>
Selector: <none>
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.96.1.127
IPs: 10.96.1.127
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.0.0.250:80
Session Affinity: None
Events: <none>
# 查看endpoint
[root@master01 ~]# kubectl get ep endpoint-huangsir
NAME ENDPOINTS AGE
endpoint-huangsir 10.0.0.250:80 51s
本文来自博客园,作者:huangSir-devops,转载请注明原文链接:https://www.cnblogs.com/huangSir-devops/p/18869685,微信Vac6666666,欢迎交流
下一篇:K8s新手系列之Pod的基本存储
Ubuntu离线环境部署Kubernetes v1.31.3(ARM64)