services之Endpoints
2022/1/28 23:38:44
本文主要是介绍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 已经被更新掉了
这篇关于services之Endpoints的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15PingCAP 黄东旭参与 CCF 秀湖会议,共探开源教育未来
- 2024-05-13PingCAP 戴涛:构建面向未来的金融核心系统
- 2024-05-09flutter3.x_macos桌面os实战
- 2024-05-09Rust中的并发性:Sync 和 Send Traits
- 2024-05-08使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B
- 2024-05-08完工标准(DoD)与验收条件(AC)究竟有什么不同?
- 2024-05-084万 star 的 NocoDB 在 sealos 上一键起,轻松把数据库编程智能表格
- 2024-05-08Mac 版Stable Diffusion WebUI的安装
- 2024-05-08解锁CodeGeeX智能问答中3项独有的隐藏技能
- 2024-05-08RAG算法优化+新增代码仓库支持,CodeGeeX的@repo功能效果提升