gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:33:45 24 4
gpt4 key购买 nike

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

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

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

例如,在 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

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

最佳答案

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

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

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

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

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

关于kubernetes - 使用 GLBC 在 ingress-gce 中实现缺少 http-> https 重定向的解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49667738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com