• k8s 中的 ingress 使用细节


    k8s中的ingress

    什么是ingress

    k8s 中使用 Service 为相同业务的 Pod 对象提供一个固定、统一的访问接口及负载均衡的能力,那么这些 Service 如何被外部的应用访问,其中常用的就是借助于 Ingress对象。

    Ingress 是 Kubernetes 中的一个资源对象,用来管理集群外部访问集群内部服务的方式。

    Ingress 对象由 Ingress Controller 和 Ingress 策略设置来共同完成。

    • Ingress 策略:用来配置不同的转发规则;

    • Ingress Controller :Ingress 对象的域名解析都由 Ingress Controller 来完成,Ingress Controller 就是一个反向代理程序,它负责解析 Ingress 的反向代理规则,如果 Ingress 有增删改的变动,所有的 Ingress Controller 都会及时更新自己相应的转发规则,当 Ingress Controller 收到请求后就会根据这些规则将请求转发到对应的 Service。

    Ingress 如何使用

    这里来个简单的 demo 来看下 Ingress 如何使用

    1、部署ingress-controller

    首先来部署下 Ingress Controller 这是使用的是 ingress-nginx

    使用的 k8s 版本是 v1.19.9,所以这里选择的 ingress-nginx 是 v1.1.3

    里面的镜像是需要FQ的,这里打包了镜像到 docker-hub 安装脚本

    $ kubectl apply -f deploy.yaml
    
    
    • 1
    • 2

    2、部署应用

    cat <./go-web.yaml
    # deployment
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      creationTimestamp: null
      labels:
        app: go-web
      name: go-web
      namespace: study-k8s
    spec:
      replicas: 5
      selector:
        matchLabels:
          app: go-web
      strategy: {}
      template:
        metadata:
          creationTimestamp: null
          labels:
            app: go-web
        spec:
          containers:
            - image: liz2019/test-docker-go-hub
              name: go-app-container
              resources: {}
    status: {}
    
    ---
    # service
    apiVersion: v1
    kind: Service
    metadata:
      name: go-web-svc
      labels:
        run: go-web-svc
    spec:
      selector:
        app: go-web
      ports:
        - protocol: TCP
          port: 8000
          targetPort: 8000
          name: go-web-http
    
    ---
    # ingress
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: go-web-ingress
      annotations:
        kubernetes.io/ingress.class: nginx
    spec:
      rules:
        - host: www.go-web.com
          http:
            paths:
              - path: /index
                pathType: Prefix
                backend:
                  service:
                    name: go-web-svc
                    port:
                      number: 8000
    EOF
    
    
    • 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
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    在最下面放了 ingress 的配置,通过 path: /index 将 ingress 请求转发到 go-web-svc 的 service。

    ➜  ~ kubectl get ingress -n study-k8s
    NAME             CLASS    HOSTS            ADDRESS                       PORTS   AGE
    go-web-ingress      www.go-web.com   192.168.56.112,192.168.56.111   80      28m
    
    
    • 1
    • 2
    • 3
    • 4

    访问

    $ curl '192.168.56.111:80/index' \
    --header 'Host: www.go-web.com'
    
    

    hello world

    你好
    %
    • 1
    • 2
    • 3
    • 4
    • 5

    ingress 使用细节

    1、一个集群中可以有多个 Ingress Controller, 在 Ingress 中可以指定使用哪一个Ingress Controller

    2、多个 Ingress 规则可能出现竞争;

    3、Ingress 可以为多个命名空间服务;

    4、关于如何暴露 ingress 服务,让外面的服务访问到?

    1、Ingress Controller 用 Deployment 方式部署,给它添加一个 Service,类型为 LoadBalancer,这样会自动生成一个 IP 地址,通过这个 IP 就能访问到了,并且一般这个 IP 是高可用的(前提是集群支持 LoadBalancer,通常云服务提供商才支持,自建集群一般没有);

    2、使用 hostPort;

    • 1、Ingress Controller 用 DaemonSet 方式部署,使用集群内部的某个或某些节点作为边缘节点,给 node 添加 label 来标识,使用 nodeSelector 绑定到边缘节点,保证每个边缘节点启动一个 Ingress Controller 实例,用 hostPort 直接在这些边缘节点宿主机暴露端口,然后我们可以访问边缘节点中 Ingress Controller 暴露的端口,这样外部就可以访问到 Ingress Controller 了;

    • 2、使用亲和性调度策略,使需要部署 Ingress Controller 的节点,每个节点都有一个 Ingress Controller 部署,然后用 hostPort 直接在这些边缘节点宿主机暴露端口,我们就能通过这些节点的 IP 和 hostPort来访问 Ingress Controller 了。

    我们上面部署 Ingress Controller 的方式就是使用的 hostPort 对外暴露端口。

  • 相关阅读:
    荐书 | 为什么喜欢的女生这么难追?
    CSS的作用与各种样式
    python设计模式12:状态模式
    ArcGIS Enterprise + ArcGIS Pro 常用服务类型发布
    Codeforces Round #811 (Div. 3)
    ubuntu、linux in window安装docker教程
    vmlogin指纹浏览器中设置本地API进行常规自动化操作
    __attribute__ 高级运用
    springboot 使用多数据源 + 多事务管理器
    【常用正则大全】
  • 原文地址:https://blog.csdn.net/Huangjiazhen711/article/details/127713081