k8s 中的 ingress 使用细节


  • ingress-nginx 是 v1.1.3

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

    $ kubectl apply -f deploy.yaml
    

    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
    

    在最下面放了 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
    

    访问

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

    hello world

    你好
    %

    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 对外暴露端口。

    参考

    【Kubernetes的Ingress是啥】
    【理解k8s 的 Ingress】https://www.jianshu.com/p/189fab1845c5
    【Ingress】https://www.huaweicloud.com/zhishi/Ingress.html
    【Ingress 控制器】https://kubernetes.io/zh-cn/docs/concepts/services-networking/ingress-controllers/
    【k8s中的ingress】https://boilingfrog.github.io/2022/11/05/k8s中的ingress/

k8s