如何配置 nginx 部署以将流量传递到 Google Kubernetes Engine 中的前端部署?



GKE 和 Kubernetes 的新手,只是试图启动并运行一个简单的项目。以下是我试图在 GKE 中在单个集群、单节点池和单个命名空间中完成的工作:

负载均衡器服务后面的 nginx 部署接受端口 80 上的 Http 流量,并在端口 8000 上将其传递给

ClusterIP 服务后面的前端部署(python Django(接受端口 8000 上的流量。

前端已经成功地与运行 Postgres 数据库的 StatefulSet 通信。在我将其服务从LoadBalancer切换到ClusterIP之前,前端已成功服务于Http(gunicorn(。

我不知道如何正确设置 Nginx 配置以将流量传递到 ClusterIP 服务以进行前端部署。我所拥有的不起作用。

任何意见/建议将不胜感激。以下是安装文件:

nginx - etc/nginx/conf.d/nginx.conf

upstream front-end {
server front-end:8000;
}
server {
listen 80;
client_max_body_size 2M;
location / {
proxy_pass http://front-end;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /static/ {
alias /usr/src/app/static/;
}
}

nginx部署/服务

---
apiVersion: v1
kind: Service
metadata:
name: "web-nginx"
labels:
app: "nginx"
spec:
type: "LoadBalancer"
ports:
- port: 80
name: "web"
selector:
app: "nginx"
---
apiVersion: "apps/v1"
kind: "Deployment"
metadata:
name: "nginx"
namespace: "default"
labels:
app: "nginx"
spec:
replicas: 1
selector:
matchLabels:
app: "nginx"
template:
metadata:
labels:
app: "nginx"
spec:
containers:
- name: "my-nginx"
image: "us.gcr.io/my_repo/my_nginx_image"  # this is nginx:alpine + my staicfiles & nginx.conf
ports:
- containerPort: 80
args:
- /bin/sh 
- -c
- while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g "daemon off;"

前端部署/服务

---
apiVersion: v1
kind: Service
metadata:
name: "front-end"
labels:
app: "front-end"
spec:
type: "ClusterIP"
ports:
- port: 8000
name: "django"
targetPort: 8000
selector:
app: "front-end"
---
apiVersion: "apps/v1"
kind: "Deployment"
metadata:
name: "front-end"
namespace: "default"
labels:
app: "front-end"
spec:
replicas: 1
selector:
matchLabels:
app: "front-end"
template:
metadata:
labels:
app: "front-end"
spec:
containers:
- name: "myApp"
image: "us.gcr.io/my_repo/myApp"
ports:
- containerPort: 8000
args:
- /bin/sh 
- -c
- python manage.py migrate && gunicorn smokkr.wsgi:application --bind 0.0.0.0:8000
---

最好使用ingress将流量转发到 Kubernetes 中的服务。

你可以在这里找到更多的去区:https://www.digitalocean.com/community/tutorials/how-to-set-up-an-nginx-ingress-with-cert-manager-on-digitalocean-kubernetes

在 Kubernetes 官方文档上:https://kubernetes.io/docs/concepts/services-networking/ingress/

只需部署 nginx 控制器并在后端应用 nginx 规则,它部署 nginx 8 并将 YAML 规则转换为 nginx conf。

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: test-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /testpath
backend:
serviceName: test
servicePort: 80

Kubernetes 入口是解决这个问题的方法。GKE 在幕后使用 Google 云负载均衡器来配置您的 Kubernetes 入口资源;因此,当您创建入口对象时,GKE 入口控制器会创建一个 Google Cloud HTTP(S( 负载均衡器,并根据入口及其相关服务中的信息对其进行配置。

通过这种方式,您可以从 Google 访问一些自定义资源类型,例如ManagedCertificatesstaticIP地址,这些资源类型可能与 kubernetes 中的入口相关联,以实现服务之间或客户端与服务之间的负载均衡。

按照此处的文档了解如何使用 K8s 入口与 GKE 设置 HTTP(s( 负载平衡 - https://cloud.google.com/kubernetes-engine/docs/concepts/ingress

本教程也非常有帮助 -

https://cloud.google.com/kubernetes-engine/docs/tutorials/http-balancer

最新更新