Ingress Controller

发布时间: 更新时间: 总字数:2799 阅读时间:6m 作者:IP:上海 网址

本文介绍 Ingress/Ingress Controller 的作用、架构,并提供 ingress-nginx 的安装部署和示例。Service 工作在 TCP 四层,HTTPS 工作在七层,因此不能在 Service 实现该功能。Ingress 应运而生,它提供从 Kubernetes 集群外部到集群内服务的 HTTP 和 HTTPS 路由,支持负载均衡等。

介绍

使用 Ingress 使 kubernetes 内部服务暴露到集群外部,并节省静态 IP 资源,且不需要多个 LoadBalancer 服务

架构

ingress nginx arch

代理架构:

client --> extLBaaS(四层) --> Ingress Controller(Deployment Pod 共享宿主机 net namespace,工作在七层,可管理 HTTPS 回话) --proxy(通过 Service 分组)--> Pod

说明:

  • Ingress 通过 Service 获取后端 Pod 对应的 Endpoints

什么是 Ingress

Ingress(入站) 是一个规则的集合,允许入站连接到达后端的端点(如Service 的 Endpoints)。Ingress 的功能如下:

  • 给服务提供外部可访问的URL
  • 负载平衡流量
  • 卸载 SSL
  • 提供基于名称的 虚拟主机(virtual hosting)

Ingress 资源定义,查看帮助:

kubectl explain ingress

Ingress Controllers

Ingress Controller 实质上可以理解为是个监视器,Ingress Controller 通过不断地跟 kubernetes API 打交道,实时的感知后端 servicepod 等变化,比如新增和减少 podservice 增加与减少等;当得到这些变化信息后,Ingress Controller 再结合 Ingress 生成配置,然后更新反向代理负载均衡器,并刷新其配置,达到服务发现的作用。

与其他类型的 controllers 不同,Ingress Controller 不是 kube-controller-manager 的一部分,Ingress Controller 默认是不启动的。

Ingress Controller 的实现

Kubernetes 作为一个 CNCF 的开源项目,目前支持和维护的控制器如下:

IngressClass

IngressClass 指定当前实现该控制器名称,用于区分一个 Kubernetes 环境中部署的多个 Ingress Controller。示例:

$ kubectl -n ingress-nginx get ingressclasses nginx -o yaml
apiVersion: networking.k8s.io/v1
kind: IngressClass
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"networking.k8s.io/v1","kind":"IngressClass","metadata":{"annotations":{},"labels":{"app.kubernetes.io/component":"controller","app.kubernetes.io/instance":"ingress-nginx","app.kubernetes.io/managed-by":"Helm","app.kubernetes.io/name":"ingress-nginx","app.kubernetes.io/version":"1.1.1","helm.sh/chart":"ingress-nginx-4.0.15"},"name":"nginx"},"spec":{"controller":"k8s.io/ingress-nginx"}}
  labels:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/version: 1.1.1
    helm.sh/chart: ingress-nginx-4.0.15
  name: nginx
spec:
  controller: k8s.io/ingress-nginx

部署 ingress-nginx

示例

  • 后端 deployment:deployment-hello-app.yaml
---
apiVersion: v1
kind: Service
metadata:
  name: hello-app
  namespace: default
spec:
  selector:
    app: hello-app
    release: canary
  type: ClusterIP
  ports:
  - name: port-80
    port: 80
    targetPort: 8080
    protocol: TCP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-app-dp
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: hello-app
      release: canary
  template:
    metadata:
      name: hello-app-pod
      labels:
        app: hello-app
        release: canary
    spec:
      containers:
      - name: hello-app
        image: gcriogooglesamples/hello-app:1.0
        ports:
        - name: http
          containerPort: 8080

准备自签发 SSL

openssl genrsa -out tls.key 2048
openssl req -new -x509 -key tls.key -out tls.crt -subj /C=CN/ST=SH/L=SH/O=IT/CN=hello-app.xiexianbin.cn/

创建 Secret

create-secret

Ingress 定义

  • ingress-hello-app.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-hello-app
  namespace: default
spec:
  ingressClassName: nginx
  tls:
  - hosts:
    - hello-app.xiexianbin.cn
    secretName: hello-app-secret
  rules:
  - host: hello-app.xiexianbin.cn
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: hello-app
            port:
              number: 8080

说明:

  • 通过 ingressClassName: nginx 指定 Ingress 为刚才创建的 nginx。
  • pathType
    • ImplementationSpecific:对于这种路径类型,匹配方法取决于 IngressClass
    • Exact:精确匹配 URL 路径,且区分大小写
    • Prefix:基于以 / 分隔的 URL 路径前缀匹配。匹配区分大小写,并且对路径中的元素逐个完成

创建 Ingress

$ kubectl apply -f deployment-hello-app.yaml
service/hello-app created
$ kubectl apply -f ingress-hello-app.yaml
ingress.networking.k8s.io/ingress-hello-app created

查询相关信息

$ kubectl get ingress
NAME                CLASS   HOSTS                     ADDRESS   PORTS     AGE
ingress-hello-app   nginx   hello-app.xiexianbin.cn             80, 443   34s
$ kubectl describe ingress ingress-hello-app
Name:             ingress-hello-app
Labels:           <none>
Namespace:        default
Address:          172.20.0.200
Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
TLS:
  hello-app-secret terminates hello-app.xiexianbin.cn
Rules:
  Host                     Path  Backends
  ----                     ----  --------
  hello-app.xiexianbin.cn
                           /   hello-app:8080 ()
Annotations:               <none>
Events:
  Type    Reason  Age                From                      Message
  ----    ------  ----               ----                      -------
  Normal  Sync    40h (x2 over 40h)  nginx-ingress-controller  Scheduled for sync

进入 pod 查看 nginx 配置

nginx-conf

访问

$ curl -k https://hello-app.xiexianbin.cn
Hello, world!
Version: 1.0.0
Hostname: hello-app-dp-74f5d67978-8ghmx

$ curl -k --resolve hello-app.xiexianbin.cn:443:172.20.0.200 https://hello-app.xiexianbin.cn
本文总阅读量 次 本站总访问量 次 本站总访客数