【发布时间】:2020-05-25 08:50:45
【问题描述】:
刚接触 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
---
【问题讨论】:
-
查看 Ingress Controller kubernetes.io/docs/concepts/services-networking/… 不要使用默认入口来限制自己。在我看来,您至少应该检查 Nginx ingress Controller cloud.google.com/community/tutorials/nginx-ingress-gke、Traefik containo.us/traefik 和 Istio istio.io。另外,这篇文章可能会有所帮助medium.com/@vdboor/…
标签: nginx google-kubernetes-engine gke-networking