【问题标题】:How can i configure ingress and nginx ingress controller to send http traffic to port 80 and https traffic to 443 port, with the same host and path如何配置入口和 nginx 入口控制器以将 http 流量发送到端口 80 并将 https 流量发送到 443 端口,具有相同的主机和路径
【发布时间】:2019-08-13 08:55:41
【问题描述】:

我的应用有一项服务:

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    io.kompose.service: app
  name: app
spec:
  type: ClusterIP
  ports:
    - name: "443"
      port: 443
      targetPort: 443
    - name: "80"
      port: 80
      targetPort: 80
  selector:
    io.kompose.service: app

和入口:

  apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: app-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-passthrough: "true"
    nginx.ingress.kubernetes.io/proxy-body-size: 200m
spec:
  rules:
  - host: test.app.com
    http:
      paths:
        - backend:
            serviceName: app
            servicePort: 443

和入口控制器:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-ingress-controller
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: ingress-nginx
      app.kubernetes.io/part-of: ingress-nginx
  template:
    metadata:
      labels:
        app.kubernetes.io/name: ingress-nginx
        app.kubernetes.io/part-of: ingress-nginx
      annotations:
        prometheus.io/port: "10254"
        prometheus.io/scrape: "true"
    spec:
      serviceAccountName: nginx-ingress-serviceaccount
      containers:
        - name: nginx-ingress-controller
          image: quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.25.0
          args:
            - /nginx-ingress-controller
            - --configmap=$(POD_NAMESPACE)/nginx-configuration
            - --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services
            - --udp-services-configmap=$(POD_NAMESPACE)/udp-services
            - --publish-service=$(POD_NAMESPACE)/ingress-nginx
            - --annotations-prefix=nginx.ingress.kubernetes.io
            - --enable-ssl-passthrough
          securityContext:
            allowPrivilegeEscalation: true
            capabilities:
              drop:
                - ALL
              add:
                - NET_BIND_SERVICE
            # www-data -> 33
            runAsUser: 33
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
          ports:
            - name: http
              containerPort: 80
            - name: https
              containerPort: 443
          livenessProbe:
            failureThreshold: 3
            httpGet:
              path: /healthz
              port: 10254
              scheme: HTTP
            initialDelaySeconds: 10
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 10
          readinessProbe:
            failureThreshold: 3
            httpGet:
              path: /healthz
              port: 10254
              scheme: HTTP
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 10

问题在于入口控制器将 http 和 https 流量都发送到端口 443。 我想将其配置为能够将 http 流量发送到端口 80 并将 https 流量发送到端口 443。我该怎么做?还是有什么我不理解的地方?

【问题讨论】:

    标签: nginx kubernetes nginx-ingress


    【解决方案1】:

    你能把问题更新得更精确一点吗?

    我可以在两种情况下扣除。

    1) 如果您想将相同的流量发送到端口 80 和 443?

    2) 您希望在入口的 443 端口接收到的流量发送到端口 80 到 pod。

    不过,简而言之,我会放弃。

    案例一

    这是不可能的。你必须选择提供流量镜像的 istio 等。

    案例二

    它是有效的并且使用了很多称为 SSL 卸载。为此,您必须删除下面的注释并更新入口以在端口 80 上发送流量而不是 443。

    nginx.ingress.kubernetes.io/ssl-passthrough: "true"
    

    【讨论】:

    • 我想将 http 流量发送到端口 80,将 https 流量发送到端口 443。即使我知道这是首选方式,我也不希望入口执行 SSL 卸载。我在服务后面有一个 Web 服务器,应该执行 SSL 卸载。
    • http 到 80 和 https 到 433,这很容易。你有一个 443 的规则添加 80 的规则它会起作用。 ``` 规则:-主机:test.app.com http:路径:-后端:服务名称:应用服务端口:80 ``
    • 它不起作用。仍然将流量发送到 443。因为它是第一个匹配的支持。
    • 可以添加nginx服务入口吗?如果可能的话,来自 pod 的 ngix.conf
    【解决方案2】:
    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      name: test
      annotations:
        ingress.kubernetes.io/rewrite-target: /
        nginx.ingress.kubernetes.io/ssl-redirect: "false"
        nginx.ingress.kubernetes.io/ssl-passthrough: "true"
    spec:
      rules:
      - host: my.url.com
        http:
          paths:
          - path: /
            backend:
              serviceName: servicea
              servicePort: 80
          - path: /
            backend:
              serviceName: servicea
              servicePort: 443
    

    【讨论】:

    • 我问如何使用相同的主机和路径将 https 传输到 443 和 http 传输到 80。您的解决方案使用不同的路径,并且它们都被路由到端口 80。
    • 尝试注释:nginx.ingress.kubernetes.io/ssl-redirect: "false"
    猜你喜欢
    • 2019-08-30
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 2021-09-17
    • 1970-01-01
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多