腾讯云TKE-Ingress案例: TKE-Ingress与Nginx-Ingress共存

2020-10-24 19:32:58 浏览数 (1)

背景

用户场景: 既想使用腾讯云TKE Ingress的能力又想使用Nginx-Ingress的能力, 多个入口,不同的controller监听不同的带有kubernetes.io/ingress.class annotations 标签的资源. 架构图如下:

Untitled Diagram.pngUntitled Diagram.png

部署

官方网站地址(https://kubernetes.github.io/ingress-nginx/deploy/), 下载deploy.yaml文件wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.40.2/deploy/static/provider/cloud/deploy.yaml

下载完毕之后需要修改镜像地址(一下主要展示了deploy.yaml 文件的Deployment 内容,其他内容无需修改, 不要删除 ):

代码语言:txt复制
# Source: ingress-nginx/templates/controller-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    helm.sh/chart: ingress-nginx-3.4.1
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/version: 0.40.2
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/component: controller
  name: ingress-nginx-controller
  namespace: ingress-nginx
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: ingress-nginx
      app.kubernetes.io/instance: ingress-nginx
      app.kubernetes.io/component: controller
  revisionHistoryLimit: 10
  minReadySeconds: 0
  template:
    metadata:
      labels:
        app.kubernetes.io/name: ingress-nginx
        app.kubernetes.io/instance: ingress-nginx
        app.kubernetes.io/component: controller
    spec:
      dnsPolicy: ClusterFirst
      containers:
        - name: controller
          image: ruiqingzhu-test.tencentcloudcr.com/ingress-nginx/controller:v0.40.2
          imagePullPolicy: IfNotPresent
          lifecycle:
            preStop:
              exec:
                command:
                  - /wait-shutdown
          args:
            - /nginx-ingress-controller
            - --publish-service=$(POD_NAMESPACE)/ingress-nginx-controller
            - --election-id=ingress-controller-leader
            - --ingress-class=nginx
            - --configmap=$(POD_NAMESPACE)/ingress-nginx-controller
            - --validating-webhook=:8443
            - --validating-webhook-certificate=/usr/local/certificates/cert
            - --validating-webhook-key=/usr/local/certificates/key
          securityContext:
            capabilities:
              drop:
                - ALL
              add:
                - NET_BIND_SERVICE
            runAsUser: 101
            allowPrivilegeEscalation: true
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: LD_PRELOAD
              value: /usr/local/lib/libmimalloc.so
          livenessProbe:
            httpGet:
              path: /healthz
              port: 10254
              scheme: HTTP
            initialDelaySeconds: 10
            periodSeconds: 10
            timeoutSeconds: 1
            successThreshold: 1
            failureThreshold: 5
          readinessProbe:
            httpGet:
              path: /healthz
              port: 10254
              scheme: HTTP
            initialDelaySeconds: 10
            periodSeconds: 10
            timeoutSeconds: 1
            successThreshold: 1
            failureThreshold: 3
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
            - name: https
              containerPort: 443
              protocol: TCP
            - name: webhook
              containerPort: 8443
              protocol: TCP
          volumeMounts:
            - name: webhook-cert
              mountPath: /usr/local/certificates/
              readOnly: true
          resources:
            requests:
              cpu: 100m
              memory: 90Mi
      serviceAccountName: ingress-nginx
      terminationGracePeriodSeconds: 300
      volumes:
        - name: webhook-cert
          secret:
            secretName: ingress-nginx-admission

注意: 原来的镜像地址k8s.gcr.io/ingress-nginx/controller:v0.40.2@sha256:46ba23c3fbaafd9e5bd01ea85b2f921d9f2217be082580edc22e6c704a83f02f 需要修改为自己的镜像地址(因为google的镜像仓库无法访问需要修改)

需要注意的在Deployment的yaml中启动参数--ingress-class=nginx 这个不能其他的ingress controller

冲突

image.pngimage.png

部署: kubectl apply -f deploy.yaml

  • 检查服务状态*# kubectl get po -n ingress-nginx NAME READY STATUS RESTARTS AGE ingress-nginx-admission-create-42fxn 0/1 Completed 0 4h56m ingress-nginx-admission-patch-tjqzr 0/1 Completed 1 4h56m ingress-nginx-controller-578c565df6-vv2x9 1/1 Running 0 4h48m

验证

验证方法:

  1. 创建2个deployment:
  2. test-tke-ingress: 直接通过tke控制台创建,并创建service, 这个deployment 部署nginx
  3. test-nginx-ingress: 直接通过tke控制台创建,并创建service(svc 不打qcloud) , 这个deployment中部署app curl podip/ping 返回pong 用来区别服务undefined test-tke-ingress 创建
Xnip2020-10-24_19-11-17.pngXnip2020-10-24_19-11-17.png

注意: service创建的时候不能选择近集群内访问,不然在创建ingress的时候不能选择这个worload。

test-nginx-ingress 创建

Xnip2020-10-24_19-03-08.pngXnip2020-10-24_19-03-08.png

在创建完成后可以在服务与路由里面的ingress-nginx的命名空间下看到一个service 暴露了一个公网ip转发到了nginx上面

  1. 创建2个Ingress
  2. test-tke-ingress: TKE ingress的资源, 在控制台创建
  3. test-nginx-ingress: 只能通过命令行手动创建因为需要指定ingess.classundefined

test-tke-ingress 创建:

image.pngimage.png

域名为: test-tke-ingress.com

创建完成后需要写hosts 将创建完成后生成的公网ip与该域名绑定, 用来本地测试

test-nginx-ingress

创建 test-nginx-ingress.yaml 文件:

代码语言:txt复制
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: test-nginx-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: "test-nginx-ingress.com"
    http:
      paths:
      - path: /
        backend:
          serviceName: test-nginx-ingress
          servicePort: 80

注意: annotations 中的kubernetes.io/ingress.class: "nginx"

部署: kubectl apply -f test-nginx-ingress.yaml

验证

写host 绑定ip到域名

TKE-Ingress

image.pngimage.png

Nginx-Ingress

image.pngimage.png

0 人点赞