GKE 和 kubernetes 的新手,只是试图启动和运行一个简单的项目。以下是我在 GKE 中尝试在单个集群、单个节点池和单个命名空间中完成的任务:
LoadBalancer 服务后面的 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
---