使用 GLBC 在 ingress-gce 中实现缺少 http->https 重定向的解决方法

Implementing workaround for missing http->https redirection in ingress-gce with GLBC

我正在努力思考建议的解决方法,因为在 ingress-gce 中缺少内置的 HTTP->HTTPS 重定向,使用 GLBC。我正在努力解决的是如何使用建议的自定义后端作为克服此限制的一种选择(例如 )。

在我的例子中,负载均衡器背后的应用程序本身没有 apache 或 nginx,我只是不知道如何包括例如安装程序中的 apache(我比 nginx 更了解它)。我应该在应用程序前面设置 apache 作为代理吗?在那种情况下,我想知道在代理配置中放置什么,因为在那里不能使用那些方便的 k8s 服务名称...

或者是否应该将 apache 设置为某种单独的后端,它只会在客户端使用普通 HTTP 时获取流量?在那种情况下,我错过了在 GCE 负载均衡器中通过协议分离后端,虽然我可以看到如何手动完成,但需要为此配置入口,而且我似乎找不到任何资源解释如何实际做到这一点。

例如,在 https://github.com/kubernetes/ingress-gce#redirecting-http-to-https 中,"application" 负责转发(它似乎是建立在 nginx 上的),虽然该示例工作得很好,但不可能做同样的事情使用我正在谈论的应用程序。

基本上,我目前的设置是这样的:

http://<public ip>:80    -\
                           >      GCE LB     ->  K8s pod running the application
https://<public_ip>:443  -/   (ingress-gce)

我知道我可以完全阻止 HTTP,但是当有人在浏览器中输入域名时,这会破坏用户体验。

目前我为 LB 设置了这些服务:

kind: Service
apiVersion: v1
metadata:
  name: myapp
spec:
  type: LoadBalancer
  ports:
  - name: http
    port: 80
    targetPort: myapp
    protocol: TCP
  selector:
    app: myapp

---
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
  name: myapp-ingress
  annotations:
    ingress.kubernetes.io/rewrite-target: /
    kubernetes.io/ingress.global-static-ip-name: "my-ip"
    ingress.gcp.kubernetes.io/pre-shared-cert: "my-cert"
spec:
  backend:
    serviceName: myapp
    servicePort: 80
  rules:
  - host: my.domain.name
    http:
      paths:
      - path: /
        backend:
          serviceName: myapp
          servicePort: 80

此外,我将 GLBC 与应用程序部署捆绑在一起:

apiVersion: v1
kind: ConfigMap
metadata:
  name: glbc-configmap
data:
  gce.conf: |
    [global]
    node-tags = myapp-k8s-nodepool
    node-instance-prefix = gke-myapp-k8s-cluster

---
kind: Deployment
apiVersion: apps/v1beta2
metadata:
  name: myapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      name: myapp
      labels:
        app: myapp
    spec:
      containers:
      # START application container
      - name: myapp
        image: eu.gcr.io/myproject/myapp:latest
        imagePullPolicy: Always
        readinessProbe:
          httpGet:
            path: /ping
            port: 8080
        ports:
        - name: myapp
          containerPort: 8080
      # END application container
      # START GLBC container
      - name: myapp-glbc
        image: gcr.io/google_containers/glbc:0.9.7
        livenessProbe:
          httpGet:
            path: /ping
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 30
          timeoutSeconds: 5
        volumeMounts:
        - mountPath: /etc/glbc-configmap
          name: cloudconfig
          readOnly: true
        args:
        - --apiserver-host=http://localhost:8080
        - --default-backend-service=myapp
        - --sync-period=300s
        - --config-file-path=/etc/glbc-configmap/gce.conf

除了更完整的解决方案之外,我将不胜感激。

2020 年 5 月编辑:https://issuetracker.google.com/issues/35904733#comment95 中所述的 "HTTP(S) Load Balancing Rewrites and Redirects support is now in General Availability" 似乎意味着现在终于可以在 LB 本身中实施适当的限制规则,而无需求助于拥有一个额外的吊舱或任何其他类似的调整。但是,如果以下内容对某人有用,我会留在那里供参考。

我找到了一个解决方案,其中 GCE LB 将流量定向到作为 K8s 集群中的部署运行的 Apache(当然这应该适用于任何代理)。在Apache config中,有一个基于X-Forwarded-Protoheader的重定向,以及一个指向集群中应用的反向代理规则。

apiVersion: v1
kind: ConfigMap
metadata:
  name: apache-httpd-configmap
data:
  httpd.conf: |
    # Apache httpd v2.4 minimal configuration
    # This can be reduced further if you remove the accees log and mod_log_config

    ServerRoot "/usr/local/apache2"

    # Minimum modules needed
    LoadModule mpm_event_module modules/mod_mpm_event.so
    LoadModule log_config_module modules/mod_log_config.so
    LoadModule mime_module modules/mod_mime.so
    LoadModule dir_module modules/mod_dir.so
    LoadModule authz_core_module modules/mod_authz_core.so
    LoadModule unixd_module modules/mod_unixd.so
    LoadModule alias_module modules/mod_alias.so
    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_http_module modules/mod_proxy_http.so

    TypesConfig conf/mime.types

    PidFile logs/httpd.pid

    # Comment this out if running httpd as a non root user
    User nobody

    # Port to Listen on
    Listen 8081

    # In a basic setup httpd can only serve files from its document root
    DocumentRoot "/usr/local/apache2/htdocs"

    # Default file to serve
    DirectoryIndex index.html

    # Errors go to stderr
    ErrorLog /proc/self/fd/2

    # Access log to stdout
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    CustomLog /proc/self/fd/1 common

    Mutex posixsem proxy

    # Never change this block
    <Directory />
      AllowOverride None
      Require all denied
    </Directory>

    # Deny documents to be served from the DocumentRoot
    <Directory "/usr/local/apache2/htdocs">
      Require all denied
    </Directory>

    <VirtualHost *:8081>
      ServerName my.domain.name
      # Redirect HTTP to load balancer HTTPS URL
      <If "%{HTTP:X-Forwarded-Proto} -strcmatch 'http'">
        Redirect / https://my.domain.name:443/
      </If>

      # Proxy the requests to the application
      # "myapp" in the rules relies a K8s cluster add-on for DNS aliases
      # see https://kubernetes.io/docs/concepts/services-networking/service/#dns
      ProxyRequests Off
      ProxyPass         "/"    "http://myapp:80/"
      ProxyPassReverse  "/"    "http://myapp:80/"
    </VirtualHost>

---
kind: Service
apiVersion: v1
metadata:
  name: apache-httpd
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    targetPort: apache-httpd
    protocol: TCP
  selector:
    app: apache-httpd

---
kind: Deployment
apiVersion: apps/v1beta2
metadata:
  name: apache-httpd
spec:
  replicas: 1
  selector:
    matchLabels:
      app: apache-httpd
  template:
    metadata:
      name: apache-httpd
      labels:
        app: apache-httpd
    spec:
      containers:
      # START apache httpd container
      - name: apache-httpd
        image: httpd:2.4-alpine
        imagePullPolicy: Always
        readinessProbe:
          httpGet:
            path: /
            port: 8081
        command: ["/usr/local/apache2/bin/httpd"]
        args: ["-f", "/etc/apache-httpd-configmap/httpd.conf", "-DFOREGROUND"]
        ports:
        - name: apache-httpd
          containerPort: 8081
        volumeMounts:
        - mountPath: /etc/apache-httpd-configmap
          name: apacheconfig
          readOnly: true
      # END apache container
      # END containers
      volumes:
        - name: apacheconfig
          configMap:
            name: apache-httpd-configmap
      # END volumes
    # END template spec
  # END template

除了上述新的清单 yaml 之外,"myapp-ingress" 的规则需要更改,以便它具有 serviceName: apache-httpd 而不是 serviceName: myapp 以使 LB 将流量直接发送到 Apache。

似乎这个相当小的 Apache 设置只需要很少的 CPU 和 RAM,因此它非常适合现有的集群,因此不会真正导致任何直接的额外成本。

快速更新:here

您可以使用 FrontEndConfig 为重定向配置 Ingress。