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

How to configure nginx deployment to pass traffic to front end deployment in Google Kubernetes Engine?

GKE 和 kubernetes 的新手,只是想启动一个简单的项目 运行。以下是我在 GKE 中尝试在单个集群、单个节点池和单个命名空间中完成的任务:

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

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

前端已成功与 StatefulSet 运行 Postgres 数据库通信。在我将它的服务从 LoadBalancer 切换到 ClusterIP 之前,可以看到前端成功地为 Http (gunicorn) 提供服务。

我不知道如何正确设置 Nginx 配置以将流量传递到前端部署的 ClusterIP 服务。我的东西不工作。

任何advice/suggestions将不胜感激。以下是设置文件:

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 deployment/service

---
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;"

前端deployment/service

---
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 规则即可部署 8 nginx 并将 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 ingress 是解决这个问题的方法。 GKE 在后台使用 Google 云负载均衡器来配置您的 Kubernetes 入口资源;因此,当您创建 Ingress 对象时,GKE ingress 控制器会创建一个 Google Cloud HTTP(S) 负载平衡器,并根据 Ingress 及其关联服务中的信息对其进行配置。

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

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

这个教程也很有帮助 -

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