• 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

    创建带 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>
    #
    

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

  • 相关阅读:
    IOS开发关于测试的好的网址资源
    创建型模式--工厂模式
    在XcodeGhost事件之后,获取更纯净的Xcode的方法。
    算法积累:解决如何获取指定文件夹路径或者文件路径下所有子文件后缀为.h .m .c的文本的行数
    结构型模式--装饰模式
    设计模式 总揽 通过这篇随笔可以访问所需要了解的设计模式
    IOS之未解问题--关于IOS图像渲染CPU和GPU
    链接
    Matlab2014下载和破解方法,以及Matlab很好的学习网站
    苹果Mac隐藏壁纸在哪里?Mac隐藏壁纸查找教程
  • 原文地址:https://www.cnblogs.com/NoneID/p/15853989.html
Copyright © 2020-2023  润新知