• 【服务发现--service】


    在这里插入图片描述

    1、service的定义

    • "Service"简写"svc”。Pod不能直接提供给外网访问,而是应该使用service。Service就是把Pod暴露出来提供服务,Service才是真正的“服务”,它的中文名就叫“服务”。
    • 可以说Service是一个应用服务的抽象,定义了Pod逻辑集合和访问这个Pod集合的策路。Service代理Pod集合,对外表现为一个访问入口,访问该入口的请求将经过负载均衡,转发到后端Pod中的容器。

    2、service和pod之间的网络是如何打通的

    • service和pod之间的网络布局如下图所示
      在这里插入图片描述

    2.1 service与endpoints的关系

    在这里插入图片描述

    2.2 endpoints和pod的关系

    在这里插入图片描述

    2.3 查看创建service的配置文件和pod的关联信息

    在这里插入图片描述

    2.4 service和pod通信过程

    • 1、创建service的时候会同时创建一个endpoints,创建service的时候会带有一个选择器,通过这个标签可以找到对应的pod,同时service也会生成ip地址,提供集群内访问。
    • 2、endpoints中包含了pod的ip和端口信息,通过iptables转发数据给node上的kube-proxy,node上的kube-proxy把数据转发给pod中的根容器。

    3、service的配置文件解析

    apiVersion: v1
    kind: Service  # 资源类型为 Service
    metadata:
      name: nginx-svc  # service 名字
      labels:
        app: nginx   # service 的标签
    spec:
      selector:  # 选择器,匹配那些pod会被该service 代理
        app: nginx-deploy   # 所有匹配到这个标签的pod 都可以通过该 service 进行访问
      ports:  # 端口樱色
      - port: 80    # service自己的端口哦,在使用内网ip访问时使用
        targetPort: 80  # 目标 pod的端口
        name: web   # 为service端口起一个名字
    # 随机启动一个端口(30000-32767),映射到ports中的端口,该端口是直接绑定在node上的,且集群中的每一个node都会绑定这个端口
    # 也可以用于将服务器暴露给外部访问,但是这种方式实际生产环境不推荐,效率低,而且service是四层负载    
      type: NodePort  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    4、service的代理集群内的资源

    4.1 创建service

    kubectl  create -f  nginx-svc.yaml
    
    • 1

    4.2 查看service 信息

    [root@k8s-master ~]# kubectl get svc
    NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
    kubernetes   ClusterIP   10.1.0.1       <none>        443/TCP        5d4h
    nginx-svc    NodePort    10.1.224.211   <none>        80:31231/TCP   4h16m```
    ## 4.3 查看service的描述信息
    
    ```c
    [root@k8s-master ~]# kubectl describe svc nginx-svc
    Name:                     nginx-svc
    Namespace:                default
    Labels:                   app=nginx
    Annotations:              <none>
    Selector:                 app=nginx-deploy
    Type:                     NodePort
    IP Family Policy:         SingleStack
    IP Families:              IPv4
    IP:                       10.1.224.211
    IPs:                      10.1.224.211
    Port:                     web  80/TCP
    TargetPort:               80/TCP
    NodePort:                 web  31231/TCP
    Endpoints:                10.2.1.55:80,10.2.2.31:80
    Session Affinity:         None
    External Traffic Policy:  Cluster
    Events:                   <none>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    4.4 进入其他Pod后通过 service name 进行访问

    [root@k8s-master ~]# kubectl exec -it dns-test -- sh
    / # wget http://nginx-svc
    Connecting to nginx-svc (10.1.224.211:80)
    index.html           100% |*************************************************************************************************************************************************************|   612   0:00:00 ETA
    / # cat  index.html
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
            width: 35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    4.5 service默认是命名空间级,跨namespace如何访问?

    • 默认在当前namespace中访问,如果需要跨namespace访问pod,则在service name的后面加上 . 即可
    • eg: curl http://nginx-svc.default

    5、Pod通过service访问外部资源方式

    • 实现方式如下:
    • 编写service配置文件时,不指定selector属性的时候,就不会创建endpoints
    • 自己创建endpoints

    5.1 service代理k8s的外部服务(通过IP地址访问)

    5.1.1 创建一个service的配置文件

    apiVersion: v1
    kind: Service  # 资源类型为 Service
    metadata:
      name: nginx-svc-external   # service 名字
      labels:
        app: nginx   # service 的标签
    spec:
      ports:  # 端口映射
      - port: 80    # service自己的端口哦,在使用内网ip访问时使用
        targetPort: 80  # 目标 pod的端口
        name: web   # 为service端口起一个名字  
      type: ClusterIP  # 只能在集群内部使用
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    5.1.2 创建一个endpoints的配置文件

    apiVersion: v1
    kind: Endpoints
    metadata:
      labels:
        name: nginx # 与service保持一致
      name:  nginx-svc-external   # 与service一致
      namespace: default  # 与 service 一致
    subsets:
    - addresses:
      - ip: 47.110.152.250  # pod需要访问的目标ip地址
      ports:        # 与service保持一致
      - name: web
        port: 80
        # protocol: TCP
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    5.1.3 创建service资源

    [root@k8s-master ~]# kubectl create -f nginx-svc-external.yaml
    service/nginx-svc-external created
    
    [root@k8s-master ~]# kubectl get svc
    NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
    kubernetes           ClusterIP   10.1.0.1       <none>        443/TCP        5d4h
    nginx-svc            NodePort    10.1.224.211   <none>        80:31231/TCP   4h21m
    nginx-svc-external   ClusterIP   10.1.63.181    <none>  80/TCP         35s
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    5.1.4 创建endpoints资源

    [root@k8s-master ~]# kubectl create -f nginx-ed-external.yaml
    endpoints/nginx-svc-external created
    
    
    [root@k8s-master ~]# kubectl get ep
    NAME                 ENDPOINTS                   AGE
    kubernetes           10.10.10.100:6443           5d4h
    nginx-svc            10.2.1.55:80,10.2.2.31:80   4h22m
    nginx-svc-external   47.110.152.250:80           20s
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    5.1.5 查看endpoints和service描述信息

    [root@k8s-master ~]# kubectl describe ep  nginx-svc-external
    Name:         nginx-svc-external
    Namespace:    default
    Labels:       app=nginx
    Annotations:  <none>
    Subsets:
      Addresses:          47.110.152.250
      NotReadyAddresses:  <none>
      Ports:
        Name  Port  Protocol
        ----  ----  --------
        web   80    TCP
    
    Events:  <none>
    
    
    [root@k8s-master ~]# kubectl describe  svc nginx-svc-external
    Name:              nginx-svc-external
    Namespace:         default
    Labels:            app=nginx
    Annotations:       <none>
    Selector:          <none>
    Type:              ClusterIP
    IP Family Policy:  SingleStack
    IP Families:       IPv4
    IP:                10.1.63.181
    IPs:               10.1.63.181
    Port:              web  80/TCP
    TargetPort:        80/TCP
    Endpoints:         47.110.152.250:80
    Session Affinity:  None
    Events:            <none>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    5.1.6 通过busybox容器测试 ????

    [root@k8s-master ~]# kubectl get pod
    NAME                           READY   STATUS    RESTARTS      AGE
    dns-test                       1/1     Running   1 (23h ago)   24h
    fluentd-59k8k                  1/1     Running   0             6h29m
    fluentd-hhtls                  1/1     Running   0             6h25m
    nginx-deploy-fdd948cf4-69b85   1/1     Running   0             135m
    nginx-deploy-fdd948cf4-r8ktj   1/1     Running   0             5h43m
    
    
    [root@k8s-master ~]# kubectl exec -it dns-test -- sh
    / # wget http://nginx-svc-external
    Connecting to nginx-svc (47.110.152.250:80)
    index.html           100% |*************************************************************************************************************************************************************|   612   0:00:00 ETA
    / # cat  index.html
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
            width: 35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    5.1.7 k8s集群中的pod访问外部服务的流程

    在这里插入图片描述

    5.2 service反向代理外部域名(通过域名访问 )

    5.2.1 创建service的配置文件

    apiVersion: v1
    kind: Service  # 资源类型为 Service
    metadata:
      name: test-svc-external-domian   # service 名字
      labels:
        app: test-svc-external-domian   # service 的标签
    spec:
      type: ExternalName
      externalName: www.lan-he.com.cn
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    5.2.2 创建service

    [root@k8s-master ~]# kubectl create -f test-svc-external-domian.yaml
    
    service/test-svc-external-domian created
    
    • 1
    • 2
    • 3

    5.2.3 查看service信息

    service/csdn-svc-external-domian edited
    [root@k8s-master ~]# kubectl get svc
    NAME                       TYPE           CLUSTER-IP     EXTERNAL-IP         PORT(S)        AGE
    test-svc-external-domian   ExternalName   <none>         www.lan-he.com.cn   <none>         100s
    kubernetes                 ClusterIP      10.1.0.1       <none>              443/TCP        5d15h
    nginx-svc                  NodePort       10.1.224.211   <none>              80:31231/TCP   14h
    nginx-svc-external         ClusterIP      10.1.63.181    <none>              80/TCP         10h
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    5.2.4 通过busybox容器测试

    [root@k8s-master ~]# kubectl exec -it dns-test -- sh
    / # wget http://www.lan-he.com.cn
    Connecting to www.lan-he.com.cn (47.110.152.250:80)
    index.html           100% |*************************************************************************************************************************************************************|   612   0:00:00 ETA
    / # cat index.html
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
            width: 35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    6、service中spec中的type常用类型

    • ClusterIP: 只能在集群内部使用,不配置类型的话默认就是ClusterlP
    • ExternalName:返回定义的CNAME别名,可以配置为域名
    • NodePort:
      • 会在所有安装了kube-proxy的节点都绑定一个端口,此端口可以代理至对应的Pod,集群外部可以使用任意节点ip+NodePort的端口号访问到集群中对应Pod中的服务。
      • 当类型设置为NodePort后,可以在ports配置中增加nodePort配置指定端口,需要在下方的端口范围内,如果不指定会随机指定端口。
      • 端口范围:30000~32767
      • 端口范围配置在/usr/lib/systemd/system/kube-apiserver.service文件中
    • LoadBalance: 使用云服务商(阿里云、腾讯云等)提供的负载均衡器服务
  • 相关阅读:
    Scotch: Combining SGX and SMM to Monitor Cloud Resource Usage【TEE的应用】
    嵌入式学习笔记(55)LCD简介
    Qt多线程的多种方法之一 QThread
    [手写spring](4)实现后置处理器
    useInfiniteScroll --- react滚动加载
    Jmeter压测监控体系搭建Docker+Influxdb+Grafana
    有道字典案例
    删除 Windows Server IIS 10 和 ASP.NET 中的 HTTP 响应标头
    Linux多线程控制:深入理解与应用(万字详解!)
    怎样认识量化交易?
  • 原文地址:https://blog.csdn.net/u011709380/article/details/136277569