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



我正在尝试使用GLBC在ingress-gce中缺乏内置HTTP->HTTPS重定向的建议解决方法。我正在努力解决的是如何使用这个自定义后端,该后端建议作为克服此限制的一种选择(例如,在 如何在 GKE 上为 Kubernetes 入口强制 SSL 中)。

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

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

例如,在 https://github.com/kubernetes/ingress-gce#redirecting-http-to-https 中,"应用程序"负责 forwaring(它似乎是基于 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 月编辑:"HTTP(S) 负载平衡重写和重定向支持现已正式发布",如 https://issuetracker.google.com/issues/35904733#comment95 中所述,似乎意味着现在终于可以在 LB 本身中实现适当的 rediction 规则,而不必诉诸额外的 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"的规则需要更改,以便serviceName: apache-httpdLB 将流量定向到 Apache 而不是serviceName: myapp

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

快速更新:这里

您可以使用前端配置来配置用于重定向的入口。

相关内容

最新更新