2

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
---
4

2 回答 2

2

在 Kubernetes中使用ingress转发流量到服务会更好。

您可以在此处找到更多内容: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 并将 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
于 2020-02-10T03:49:10.437 回答
2

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

通过这种方式,您可以访问来自 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

于 2020-02-10T12:29:07.637 回答