nginx: [emerg] host not found in upstream "udagram-users:8080" in /etc/nginx/nginx.conf:11

nginx: [emerg] host not found in upstream "udagram-users:8080" in /etc/nginx/nginx.conf:11

部署到 AWS EKS 后我收到此错误

Gtihub 回购:https://github.com/oussamabouchikhi/udagram-microservices


重现步骤

  1. 创建 AWS EKS 个集群和节点组
  2. 配置 EKS 集群 kubectl
  3. 部署到EKS集群(首先是secrets,然后是其他服务,然后是reverserproxy)
    kubectl apply -f env-secret.yaml
    kubectl apply -f aws-secret.yaml
    kubectl apply -f env-configmap.yaml
    。 ...
    kubectl apply -f reverseproxy-deployment.yaml
    kubectl apply -f reverseproxy-service.yaml

nginx 配置

worker_processes 1;
  
events { worker_connections 1024; }
error_log /dev/stdout debug;

http {

    sendfile on;

    upstream users {
        server udagram-users:8080;
    }

    upstream feed {
        server udagram-feed:8080;
    }
    
    proxy_set_header   Host $host;
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Host $server_name;
    
    server {
        listen 8080;
        location /api/v0/feed {
            resolver           8.8.8.8;
            proxy_pass         http://feed;
        }
        location /api/v0/users {
            resolver           8.8.8.8;
            proxy_pass         http://users;
        }            
    }

}

docker-撰写

version: '3'
services:
  reverseproxy:
    image: oussamabouchikhi/reverseproxy
    ports:
      - 8080:8080
    restart: always
    depends_on:
      - udagram-users
      - udagram-feed
    networks:
      - example-net
  udagram-users:
    image: oussamabouchikhi/udagram-api-users
    volumes:
      - $HOME/.aws:/root/.aws
    environment:
      POSTGRES_USERNAME: $POSTGRES_USERNAME
      POSTGRES_PASSWORD: $POSTGRES_PASSWORD
      POSTGRES_DB: $POSTGRES_DB
      POSTGRES_HOST: $POSTGRES_HOST
      AWS_REGION: $AWS_REGION
      AWS_PROFILE: $AWS_PROFILE
      AWS_MEDIA_BUCKET: $AWS_BUCKET
      JWT_SECRET: $JWT_SECRET
      URL: $URL
      networks:
        - example-net
  udagram-feed:
    image: oussamabouchikhi/udagram-api-feed
    volumes:
      - $HOME/.aws:/root/.aws
    environment:
      POSTGRES_USERNAME: $POSTGRES_USERNAME
      POSTGRES_PASSWORD: $POSTGRES_PASSWORD
      POSTGRES_DB: $POSTGRES_DB
      POSTGRES_HOST: $POSTGRES_HOST
      AWS_REGION: $AWS_REGION
      AWS_PROFILE: $AWS_PROFILE
      AWS_MEDIA_BUCKET: $AWS_BUCKET
      JWT_SECRET: $JWT_SECRET
      URL: $URL
    networks:
      - example-net
  udagram-frontend:
    image: oussamabouchikhi/udagram-frontend
    ports:
      - '8100:80'
    networks:
      - example-net
networks:
  example-net:
    external: true

反向代理部署

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    service: reverseproxy
  name: reverseproxy
spec:
  replicas: 1
  selector:
    matchLabels:
      service: reverseproxy
  template:
    metadata:
      labels:
        service: reverseproxy
    spec:
      containers:
        - image: oussamabouchikhi/reverseproxy:latest
          name: reverseproxy
          imagePullPolicy: Always
          resources:
            requests:
              memory: '64Mi'
              cpu: '250m'
            limits:
              memory: '1024Mi'
              cpu: '500m'
          ports:
            - containerPort: 8080
      restartPolicy: Always

反向代理服务

apiVersion: v1
kind: Service
metadata:
  labels:
    service: reverseproxy
  name: reverseproxy
spec:
  ports:
  - name: "8080"
    port: 8080
    targetPort: 8080
  selector:
    service: reverseproxy
  type: LoadBalancer

nginx配置中使用resolver

需要 nginx resolver 指令。

Nginx 是一个多路复用服务器(多个连接在一个 OS 进程中),因此系统解析器的每次调用都会停止处理所有连接,直到收到解析器应答。这就是 Nginx 实现自己的内部非阻塞解析器的原因。

如果您的配置文件具有静态 DNS 名称(未生成),并且您不关心没有 nginx reload 的跟踪 IP 更改,则不需要 nginx解析器。在这种情况下,所有 DNS 名称都将在启动时解析。 Nginx 的解析器

Nginx resolver 指令应该被使用,如果你想在运行时解析域名而不需要 nginx 重新加载。

例如:

location /my_uri {
  resolver kube-dns.kube-system valid=10s;
  ...
}
location /my_uri {
  resolver 127.0.0.1:53 ipv6=off valid=10s;
  ...
}

使用相同的网络(不是你的情况,但仍然值得注意)

您尝试 link 的容器可能不在同一网络上。 您可能希望将它们全部放在同一个网络上。

在你的情况下子网是相同的,没关系:

docker-compose

version: '3'
services:
  reverseproxy:
    ...
    networks:
      - example-net
  udagram-users:
    ...
      networks:
        - example-net
  udagram-feed:
    ...
    networks:
      - example-net
  udagram-frontend:
    ...
    networks:
      - example-net
networks:
  example-net:
    external: true