无法在GCP Cloud Run中的服务之间进行通信。

huangapple go评论61阅读模式
英文:

unable to communication between serices with GCP cloud run

问题

我有两个服务,一个是 API,一个是 view。目前,这两个服务都没有使用 VPC,而是开放在互联网上。API 没有问题地启动了,我能够访问它的端点。

API 日志

Default STARTUP TCP probe succeeded after 1 attempt for container "api-1" on port 8080.

然而,view 在启动时失败,因为它无法连接到 api

2023/06/02 05:14:11 [emerg] 1#1: host not found in upstream "api:8080" in /etc/nginx/nginx.conf:44

nginx: [emerg] host not found in upstream "api:8080" in /etc/nginx/nginx.conf:44

我已经尝试了 API 的主机 URL、api-1api。此时我不确定还能尝试什么。

建议?

daemon off;
user nginx;

events {
    worker_connections 1024;
}

http {
    gzip on;
    gzip_comp_level 2;
    gzip_min_length 1024;
    gzip_types *;

    proxy_connect_timeout       300;
    proxy_send_timeout          300;
    proxy_read_timeout          300;
    send_timeout                300;
    keepalive_timeout 300;
    client_body_timeout 300;
    client_header_timeout 300;
    client_max_body_size 1024M;
    proxy_max_temp_file_size 0;
    proxy_buffering off;
    server_names_hash_bucket_size 256;

    include /etc/nginx/mime.types;
    sendfile on;
    server_tokens off;

    log_format  main '$remote_addr - $remote_user [$time_local] "$request" '
                     '$status $body_bytes_sent "$http_referer" '
                     '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    default_type application/octet-stream;

    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }

    upstream api {
        server ${API_HOST}:${API_PORT};
    }

    # ssl_session_cache   shared:SSL:10m;
    # ssl_session_timeout 10m;

    server {
        listen 8080;

        # SSL configuration
        listen 443 ssl;
        listen [::]:443 ssl;

        server_name localhost;

        keepalive_timeout   70;

        root /usr/share/nginx/html;

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        location ~* \.(js|css|png|jpg|jpeg|gif|svg|ico|woff|woff2|ttf)$ {
            expires 1y;
            add_header Cache-Control "public";
            access_log off;
        }

        location / {
            try_files $uri /index.html;
        }

        location ~ ^/(v1)/ {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;

            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;

            proxy_redirect off;
            proxy_pass http://api;

            add_header Cache-Control "no-store, no-cache, must-revalidate";
            expires off;
        }

    }

}

已更新的 nginx 文件。此链接在评论中提供。我在 nginx 配置中添加了环境变量,但遗憾的是没有效果。部署 React 和 Nginx 到 Cloud Run

server {
    listen ${PORT};

    # SSL configuration
    listen 443 ssl;
    listen [::]:443 ssl;
}

start.sh 用于 nginx

#!/usr/bin/env sh
set -eu

# Replace env vars in config template and save it as config file
envsubst '${API_HOST}','${API_PORT}','${PORT}' < /etc/nginx/etc/nginx/nginx.conf.template > /etc/nginx/nginx.conf
# Run nginx
exec /usr/sbin/nginx

问题仍然是

2023/06/04 09:51:50 [emerg] 1#1: host not found in upstream "api:8080" in /etc/nginx/nginx.conf:44

这是 nginx 文件中的 upstream 部分

upstream api {
    server ${API_HOST}:${API_PORT};
}

由于某种原因它无法连接到 api 服务。

更新 1:

我创建了一个具有 Cloud Run Invoker 和 Cloud Run Admin 权限的自定义服务帐户,并分配了这两个服务的帐户。我还创建了一个 VPC,但仍然无法连接。

对 nginx 文件进行了小的更改

upstream api {
    server ${API_HOST}:${API_PORT};
}

# ssl_session_cache   shared:SSL:10m;
# ssl_session_timeout 10m;

server {
    listen ${PORT};
    listen 80;

    # SSL configuration
    listen 443 ssl;
    listen [::]:443 ssl;

    server_name localhost ${API_HOST};

    keepalive_timeout   70;
}
英文:

I have two services, and API and a view. At the moment, neither service is using a VPC and are open to the internet. The API starts with no issue, and I am able to access its endpoints.

api logs

Default STARTUP TCP probe succeeded after 1 attempt for container &quot;api-1&quot; on port 8080.

However, the view fails on start-up because it cannot nginx cannot connect to the api

2023/06/02 05:14:11 [emerg] 1#1: host not found in upstream &quot;api:8080&quot; in /etc/nginx/nginx.conf:44

nginx: [emerg] host not found in upstream &quot;api:8080&quot; in /etc/nginx/nginx.conf:44

I have tried the api's host url, api-1, and api. At this point I am not sure what else to try.

无法在GCP Cloud Run中的服务之间进行通信。

Advice?


daemon off;
user nginx;

events {
    worker_connections 1024;
}

http {
    gzip on;
    gzip_comp_level 2;
    gzip_min_length 1024;
    gzip_types *;

    proxy_connect_timeout       300;
    proxy_send_timeout          300;
    proxy_read_timeout          300;
    send_timeout                300;
    keepalive_timeout 300;
    client_body_timeout 300;
    client_header_timeout 300;
    client_max_body_size 1024M;
    proxy_max_temp_file_size 0;
    proxy_buffering off;
    server_names_hash_bucket_size 256;

    include /etc/nginx/mime.types;
    sendfile on;
    server_tokens off;

    log_format  main &#39;$remote_addr - $remote_user [$time_local] &quot;$request&quot; &#39;
                     &#39;$status $body_bytes_sent &quot;$http_referer&quot; &#39;
                     &#39;&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot;&#39;;

    access_log  /var/log/nginx/access.log  main;

    default_type application/octet-stream;

    map $http_upgrade $connection_upgrade {
        default upgrade;
        &#39;&#39;      close;
    }

     upstream api {
        server ${API_HOST}:${API_PORT};
     }

     # ssl_session_cache   shared:SSL:10m;
     # ssl_session_timeout 10m;

     server {
         listen 8080;

         # SSL configuration
         listen 443 ssl;
         listen [::]:443 ssl;

         server_name localhost;

         keepalive_timeout   70;

         root /usr/share/nginx/html;

         access_log /var/log/nginx/access.log;
         error_log /var/log/nginx/error.log;

         location ~* \.(js|css|png|jpg|jpeg|gif|svg|ico|woff|woff2|ttf)$ {
             expires 1y;
             add_header Cache-Control &quot;public&quot;;
             access_log off;
         }

         location / {
            try_files $uri /index.html;
         }

         location ~ ^/(v1)/ {
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_set_header X-Forwarded-Proto $scheme;
             proxy_set_header Host $http_host;
             proxy_set_header X-NginX-Proxy true;

             proxy_http_version 1.1;
             proxy_set_header Upgrade $http_upgrade;
             proxy_set_header Connection $connection_upgrade;

             proxy_redirect off;
             proxy_pass http://api;

             add_header Cache-Control &quot;no-store, no-cache, must-revalidate&quot;;
             expires off;
         }

     }

}

Updated nginx file.. This link was provided in the comments. I was added the environmental variable to the nginx configuration but sadly it had no effect. Deploy React and Nginx to Cloud Run

 server {
     listen ${PORT};

     # SSL configuration
     listen 443 ssl;
     listen [::]:443 ssl;

start.sh for nginx

#!/usr/bin/env sh
set -eu

# Replace env vars in config template and save it as config file
envsubst &#39;${API_HOST}&#39;,&#39;${API_PORT}&#39;,&#39;${PORT}&#39;  &lt; /etc/nginx/etc/nginx/nginx.conf.template &gt; /etc/nginx/nginx.conf
# Run nginx
exec /usr/sbin/nginx

The issue is still

2023/06/04 09:51:50 [emerg] 1#1: host not found in upstream &quot;api:8080&quot; in /etc/nginx/nginx.conf:44

which is the upstream in the nginx file

 upstream api {
    server ${API_HOST}:${API_PORT};
 }

For some reason it cannot connect to the api service.

---- update 1 ----

I create a custom service account with cloud run invoker and cloud run admin and assigned both services the account.
I also created a VPC. Still unable to connect.

Small change to nginx file

 upstream api {
    server ${API_HOST}:${API_PORT};
 }

 # ssl_session_cache   shared:SSL:10m;
 # ssl_session_timeout 10m;

 server {
     listen ${PORT};
     listen 80;

     # SSL configuration
     listen 443 ssl;
     listen [::]:443 ssl;

     server_name localhost ${API_HOST};

     keepalive_timeout   70;

答案1

得分: 0

根据错误和您的nginx配置,看起来问题出在您的API_HOST变量上。能否向我们展示您的主机是什么,或者确保您的主机是由Cloud Run生成的API服务的完整URL(以.run.app结尾)。然后您的视图应该能正确解析。

英文:

Based on the error and your nginx config, it seems the problem is with your API_HOST variable. Can you show us what host you have or make your the host is the full url for the api service generated by cloud run (which ends with .run.app). Then your view should resolve correctly.

huangapple
  • 本文由 发表于 2023年6月2日 13:25:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76387342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定