简体   繁体   English

使用 docker-compose 和 ZB76E98AF9AAA680979ZBF5A65B2D5A105 服务 API 时的 Nginx 上游服务器值

[英]Nginx upstream server values when serving an API using docker-compose and kubernetes

I'm trying to use docker-compose and kubernetes as two different solutions to setup a Django API served by Gunicorn (as the web server) and Nginx (as the reverse proxy). I'm trying to use docker-compose and kubernetes as two different solutions to setup a Django API served by Gunicorn (as the web server) and Nginx (as the reverse proxy). Here are the key files:以下是关键文件:

default.tmpl (nginx) - this is converted to default.conf when the environment variable is filled in: default.tmpl (nginx) - 填入环境变量时转换为 default.conf:

upstream api {
    server ${UPSTREAM_SERVER};
}

server {
    listen 80;

    location / {
        proxy_pass http://api;
    }

    location /staticfiles {
        alias /app/static/;
    }
}

docker-compose.yaml: docker-compose.yaml:

version: '3'
services:
  api-gunicorn:
    build: ./api
    command: gunicorn --bind=0.0.0.0:8000 api.wsgi:application
    volumes:
      - ./api:/app

  api-proxy:
    build: ./api-proxy
    command: /bin/bash -c "envsubst < /etc/nginx/conf.d/default.tmpl > /etc/nginx/conf.d/default.conf && exec nginx -g 'daemon off;'"
    environment:
      - UPSTREAM_SERVER=api-gunicorn:8000
    ports:
      - 80:80
    volumes:
      - ./api/static:/app/static
    depends_on:
      - api-gunicorn

api-deployment.yaml (kubernetes): api-deployment.yaml(kubernetes):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: release-name-myapp-api-proxy
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: myapp-api-proxy
  template:
    metadata:
      labels:
        app.kubernetes.io/name: myapp-api-proxy
    spec:
      containers:
        - name: myapp-api-gunicorn
          image: "helm-django_api-gunicorn:latest"
          imagePullPolicy: Never
          command:
            - "/bin/bash"
          args:
            - "-c"
            - "gunicorn --bind=0.0.0.0:8000 api.wsgi:application"
        - name: myapp-api-proxy
          image: "helm-django_api-proxy:latest"
          imagePullPolicy: Never
          command:
            - "/bin/bash"
          args:
            - "-c"
            - "envsubst < /etc/nginx/conf.d/default.tmpl > /etc/nginx/conf.d/default.conf && exec nginx -g 'daemon off;'"
          env:
            - name: UPSTREAM_SERVER
              value: 127.0.0.1:8000
          volumeMounts:
            - mountPath: /app/static
              name: api-static-assets-on-host-mount
      volumes:
        - name: api-static-assets-on-host-mount
          hostPath:
            path: /Users/jonathan.metz/repos/personal/code-demos/kubernetes-demo/helm-django/api/static

My question involves the UPSTREAM_SERVER environment variable.我的问题涉及UPSTREAM_SERVER环境变量。

For docker-compose.yaml , the following values have worked for me:对于docker-compose.yaml ,以下值对我有用:

  • Setting it to the name of the gunicorn service and the port it's running on (in this case api-gunicorn:8000 ).将其设置为 gunicorn 服务的名称及其运行的端口(在本例中api-gunicorn:8000 )。 This is the best way to do it (and how I've done it in the docker-compose file above) because I don't need to expose the 8000 port to the host machine.这是最好的方法(以及我在上面的 docker-compose 文件中是如何做到的),因为我不需要将8000端口暴露给主机。
  • Setting it to MY_IP_ADDRESS:8000 as described in this SO post .本文所述,将其设置为MY_IP_ADDRESS:8000 This method requires me to expose the 8000 port, which is not ideal.这种方法需要我暴露8000端口,不太理想。

For api-deployment.yaml , only the following value has worked for me:对于api-deployment.yaml ,只有以下值对我有用:

Are there any other values for UPSTREAM_SERVER that work here, especially in the kubernetes file? UPSTREAM_SERVER是否还有其他适用于此的值,尤其是在 kubernetes 文件中? I feel like I should be able to point to the container's name and that should work.我觉得我应该能够指向容器的名称,并且应该可以。

You could create a service to target container myapp-api-gunicorn but this will expose it outside of the pod :您可以创建一个服务来定位容器myapp-api-gunicorn ,但这会将其暴露在pod之外:

apiVersion: v1
kind: Service
metadata:
  name: api-gunicorn-service
spec:
  selector:
    app.kubernetes.io/name: myapp-api-proxy
  ports:
    - protocol: TCP
      port: 8000
      targetPort: 8000

You might also use hostname and subdomain fields inside a pod to take advantage of FQDN.您还可以在pod中使用主机名和子域字段来利用 FQDN。

Currently when a pod is created, its hostname is the Pod's metadata.name value.当前,当创建 Pod 时,其主机名是 Pod 的metadata.name值。

The Pod spec has an optional hostname field, which can be used to specify the Pod's hostname. Pod 规范有一个可选的hostname段,可用于指定 Pod 的主机名。 When specified, it takes precedence over the Pod's name to be the hostname of the pod.指定时,它优先于 Pod 的名称成为 pod 的主机名。 For example, given a Pod with hostname set to “ my-host ”, the Pod will have its hostname set to “ my-host ”.例如,给定一个hostname设置为“ my-host ”的 Pod,该 Pod 的主机名将设置为“ my-host ”。

The Pod spec also has an optional subdomain field which can be used to specify its subdomain. Pod 规范还有一个可选的subdomain字段,可用于指定其子域。 For example, a Pod with hostname set to “ foo ”, and subdomain set to “ bar ”, in namespace “ my-namespace ”, will have the fully qualified domain name (FQDN) “ foo.bar.my-namespace.svc.cluster-domain.example ”.例如,在命名空间“ my-namespace ”中, hostname设置为“ foo ”, subdomain设置为“ bar ”的 Pod 将具有完全限定域名(FQDN)“ foo.bar.my-namespace.svc.cluster-domain.example ”。

Also here is a nice article from Mirantis which talks about exposing multiple containers in a pod这里还有一篇来自Mirantis的好文章,它谈到了在一个 pod 中暴露多个容器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM