services之Endpoints

本文涉及的产品
云解析 DNS,旗舰版 1个月
全局流量管理 GTM,标准版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介: 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/xxxxxxxx/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

创建带 endpointsservices

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>
#



# 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 已经被更新掉了

相关文章
|
3月前
|
Kubernetes 负载均衡 安全
如何为Azure Kubernetes Services启用Internal Loadbalancer
执行上述步骤,您就可以搭建一个仅在Azure虚拟网络内部可用的内部负载均衡器了。这为内部服务通信提供了安全性,避免了外部世界的直接接入。与外部负载均衡器相比,内部负载均衡器通常用于企业内部或多层应用架构中的后端服务。通过仔细配置和管理AKS中的Service对象,您可以确保应用架构符合网络和安全性要求。
37 2
|
负载均衡 Perl
k8s--service 之 HeadLiness、NodePort 使用
k8s--service 之 HeadLiness、NodePort 使用
Understanding services:理解服务(Service)
Understanding services:理解服务(Service)
124 0
Understanding services:理解服务(Service)
|
容器
SAP Gateway service language determination
Created by Jerry Wang, last modified on Sep 14, 2015
102 0
SAP Gateway service language determination
backend system available odata service detection
Created by Wang, Jerry, last modified on Jan 17, 2015
111 0
backend system available odata service detection
|
监控 网络协议 关系型数据库