K8s新手系列之Endponit
概述官方文档: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实现的。
https://img2024.cnblogs.com/blog/3468887/202505/3468887-20250510154721378-1501340591.png
负载分发策略
对Service的访问被分发到了后端的Pod上去,现在kubernetes提供了两种负载分发策略:
[*]如果不定义,默认使用kube-proxy的策略,比如随机、轮询
[*]基于客户端地址的会话保持模式,即来自同一个客户端发起的所有哀求都会转发到固定的一个Pod上,此模式可以使在spec中添加sessionAffinity:ClientIP选项
验证Endpoint
创建service和deploy
# 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
# 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保持一致的
# 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-nginx80/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资源
# 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之后看看Endpoints列表会发生什么
重启Pod
# 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
# 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
# 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-nginx80/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
# 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
Endpoints资源是通过name和namespace两个字段与Service进行关联的,所以Endpoints的名称和Service的名称相同.
示例:
# 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
# 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
# kubectl get ep endpoint-huangsir
NAME ENDPOINTS AGE
endpoint-huangsir 10.0.0.250:80 51s
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]