4

我正在尝试使用 GLBC 围绕 ingress-gce 中缺少内置 HTTP->HTTPS 重定向的建议解决方法展开思考。我正在苦苦挣扎的是如何使用这个被建议作为一个选项来克服这个限制的自定义后端(例如,在How to force SSL for Kubernetes Ingress on GKE 中)。

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

还是应该将 apache 设置为某种单独的后端,仅当客户端使用纯 HTTP 时才会获得流量?在这种情况下,我错过了 GCE 负载平衡器中按协议对后端的分离,虽然我可以看到如何手动完成,但需要为此配置入口,我似乎找不到任何资源解释如何实际做到这一点。

例如,在https://github.com/kubernetes/ingress-gce#redirecting-http-to-https中,“应用程序”负责转发(它似乎是在 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

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

4

2 回答 2

3

2020 年 5 月编辑:如https://issuetracker.google.com/issues/35904733#comment95中所述,“HTTP(S) 负载平衡重写和重定向支持现已全面上市”似乎意味着现在终于有可能了在 LB 本身中实施适当的改写规则,而不必求助于额外的 pod 或任何其他此类调整。但是,如果以下内容对某人有用,我会将其留在那里以供参考。

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

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”的规则需要更改,以便让 LBserviceName: myappserviceName: apache-httpd流量直接发送到 Apache。

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

于 2018-04-10T10:08:22.820 回答
0

快速更新:这里

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

于 2021-01-14T01:33:46.810 回答