services之Endpoints
作者:互联网
services之Endpoints
Services: 将运行在一组pods上的应用程序公开为网络服务的抽象方法。
1. 为什么需要Services
若我们使用 Deployment
来管理 Pod
, 那么该Deployment
可以动态的 创建 和 销毁 Pod
, 而每个Pod
都有自己的IP地址
, 此时就需要一种服务, 能够帮助我们找到集群对应的 Pod
机器的IP地址
,这就需要services
。
2. 什么是endpoints
endpoints
资源就是暴露一个服务的IP地址和端口的列表, 只要服务中的 Pod
集合发生改变,endpoints
就会被更新。
默认在创建Servers
时,若编写了标签选择器selector
,则会自动创建 endpoints
,否则不会创建。
3. 案例
3.1 创建 deployment
若有deployment
资源(该镜像是简单的dns服务器
),定义为如下
apiVersion: apps/v1
kind: Deployment
metadata:
name: sampledns
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: dns
template:
metadata:
labels:
app: dns
spec:
containers:
- name: sampledns2
image: docker.io/2859413527/sample-dns
env:
- name: redisHost
value: '192.168.1.5:6379'
- name: proxyDNSHost
value: '8.8.8.8'
- name: dnsOnlineStatus
value: 'false'
ports:
- name: tcpdns
containerPort: 53
protocol: TCP
- name: udpdns
containerPort: 53
protocol: UDP
- name: web
containerPort: 5001
protocol: TCP
我们查看pod
的状态信息
# kubectl get pods -o wide -l app=dns
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
sampledns-77c56c9fb9-6twzl 1/1 Running 0 2m22s 10.244.2.205 node2 <none> <none>
sampledns-77c56c9fb9-g97jw 1/1 Running 0 106s 10.244.1.195 node1 <none> <none>
sampledns-77c56c9fb9-l7j5x 1/1 Running 0 2m5s 10.244.1.194 node1 <none> <none>
#
3.2 创建 services
创建带 endpoints
的 services
apiVersion: v1
kind: Service
metadata:
name: dnsservices
namespace: default
spec:
selector:
app: dns
type: NodePort
ports:
- name: tcpdns
port: 53
targetPort: 53
protocol: TCP
- name: udpdns
port: 53
targetPort: 53
nodePort: 53
protocol: UDP
- name: web
port: 5001
targetPort: 5001
nodePort: 5001
protocol: TCP
创建好后,查看 services
# kubectl get services dnsservices -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
dnsservices NodePort 10.1.146.200 <none> 53:53/TCP,53:53/UDP,5001:5001/TCP 8s app=dns
#
3.3 观察endpoints
再次查看 endpoints
# kubectl describe endpoints dnsservices
Name: dnsservices
Namespace: default
Labels: <none>
Annotations: endpoints.kubernetes.io/last-change-trigger-time: 2022-01-28T10:20:13Z
Subsets:
Addresses: 10.244.1.194,10.244.1.195,10.244.2.205
NotReadyAddresses: <none>
Ports:
Name Port Protocol
---- ---- --------
tcpdns 53 TCP
udpdns 53 UDP
web 5001 TCP
Events: <none>
#
3.4 重建pods并且查看endpoints
# kubectl delete pod sampledns-77c56c9fb9-6twzl
pod "sampledns-77c56c9fb9-6twzl" deleted
#
由于使用的 deployment
来定义的 Pod
, 所以,该 Pod
会被重建,如下
# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
sampledns-77c56c9fb9-g97jw 1/1 Running 0 8m39s 10.244.1.195 node1 <none> <none>
sampledns-77c56c9fb9-gcqcd 1/1 Running 0 28s 10.244.2.206 node2 <none> <none>
sampledns-77c56c9fb9-l7j5x 1/1 Running 0 8m58s 10.244.1.194 node1 <none> <none>
#
再次查看 endpoints
# kubectl describe endpoints dnsservices
Name: dnsservices
Namespace: default
Labels: <none>
Annotations: endpoints.kubernetes.io/last-change-trigger-time: 2022-01-28T10:24:31Z
Subsets:
Addresses: 10.244.1.194,10.244.1.195,10.244.2.206
NotReadyAddresses: <none>
Ports:
Name Port Protocol
---- ---- --------
tcpdns 53 TCP
udpdns 53 UDP
web 5001 TCP
Events: <none>
#
可以看到 endpoints
Address 已经被更新掉了
标签:Endpoints,name,53,10.244,TCP,sampledns,services,endpoints 来源: https://www.cnblogs.com/NoneID/p/15853989.html