docker - 连接失败 envoyproxy grpc 转码 HTTP
问题描述
我正在关注本教程,以便将 grcp 服务转码为 HTTP(它在 Linux 上运行)。envoy API 更新到 v3,所以我也跟着这个例子。我已将 envoy_config 更新为 API v3,因此它符合此版本的新要求。
但是,当我部署 docker 映像时,我在尝试访问 API 端点时收到错误消息。例如,当我运行此命令curl http://localhost:51051/greeting
(这是生成的 API 的公开端点)时,我收到以下错误:upstream connect error or disconnect/reset before headers. reset reason: connection failure
,状态代码为503
.
当我运行以下命令sudo docker ps
时,我可以看到列中没有显示端口Port
。因此,我尝试通过命令docker run -p ...
或 来映射/公开它们docker run --expose ...
,但它们仍然没有出现。
我现在有点迷茫,因为我根本不知道 docker,我什至不知道我的问题是否与端口暴露有关。
但无论如何,我不得不说 grpc 服务在 java Server/Client 上运行良好。只是 Web API (envoy) 不工作。
如果你想要更多的材料,我已经在这里上传了整个项目,所以你可以自己重现问题。
否则,这里是envoy-config.yml
(暴露的 API 端点是地址 #2 : 0.0.0.0:51051
):
admin:
access_log_path: /tmp/admin_access.log
address:
socket_address: { address: 0.0.0.0, port_value: 9901 } #1
static_resources:
listeners:
- name: main-listener
address:
socket_address: { address: 0.0.0.0, port_value: 51051 } #2
filter_chains:
- filters:
- name: envoy.filters.network.http_connection_manager
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager # Explicit v3
stat_prefix: grpc_json
codec_type: AUTO
route_config:
name: local_route
virtual_hosts:
- name: local_service
domains: ["*"]
routes:
- match: { prefix: "/" , grpc: {}} # 3a grpc:{} means that requests are only forwarded if they are found in the grpc service definition, returning 404 for others
route: { cluster: grpc-backend-services, timeout: { seconds: 60 } } #3b
http_filters:
- name: envoy.filters.http.grpc_json_transcoder
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.grpc_json_transcoder.v3.GrpcJsonTranscoder
proto_descriptor: "/data/greeting_service_definition.pb" #4
services: ["helloworld.HelloService"] #5
print_options:
add_whitespace: true
always_print_primitive_fields: true
always_print_enums_as_ints: false
preserve_proto_field_names: false #6
- name: envoy.filters.http.router
clusters:
- name: grpc-backend-services #7
connect_timeout: 1.25s
type: logical_dns
lb_policy: round_robin
dns_lookup_family: V4_ONLY
typed_extension_protocol_options:
envoy.extensions.upstreams.http.v3.HttpProtocolOptions:
"@type": type.googleapis.com/envoy.extensions.upstreams.http.v3.HttpProtocolOptions
explicit_http_config:
http2_protocol_options: {}
load_assignment:
cluster_name: grpc-backend-services
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: 127.0.0.1 #8
port_value: 53000
这是运行的脚本protoc
和docker
:
#!/usr/bin/env bash
if ! [ -x "$(command -v protoc)" ] ; then
echo "you do not seem to have the protoc executable on your path"
echo "we need protoc to generate a service defintion (*.pb file) that envoy can understand"
echo "download the precompiled protoc executable and place it in somewhere in your systems PATH!"
echo "goto: https://github.com/protocolbuffers/protobuf/releases/latest"
echo "choose:"
echo " for linux: protoc-3.6.1-linux-x86_64.zip"
echo " for windows: protoc-3.6.1-win32.zip"
echo " for mac: protoc-3.6.1-osx-x86_64.zip"
exit 1
fi
# generate the greeting_service_definition.pb file that we can pass to envoy so that knows the grpc service
# we want to expose
protoc -I. -Isrc/main/proto --include_imports \
--include_source_info \
--descriptor_set_out=greeting_service_definition.pb \
src/main/proto/HelloService.proto
if ! [ $? -eq 0 ]; then
echo "protobuf compilation failed"
exit 1
fi
# now we can start envoy in a docker container and map the configuration and service definition inside
# we use --network="host" so that envoy can access the grpc service at localhost:<port>
# the envoy-config.yml has configured envoy to run at port 51051, so you can access the HTTP/JSON
# api at localhost:51051
if ! [ -x "$(command -v docker)" ] ; then
echo "docker command is not available, please install docker"
echo "Install docker: https://store.docker.com/search?offering=community&type=edition"
exit 1
fi
# check if sudo is required to run docker
if [ "$(groups | grep -c docker)" -gt "0" ]; then
echo "Envoy will run at port 51051 (see envoy-config.yml)"
docker run -it --rm --name envoy --network="host" \
-v "$(pwd)/greeting_service_definition.pb:/data/greeting_service_definition.pb:ro" \
-v "$(pwd)/envoy-config.yml:/etc/envoy/envoy.yaml:ro" \
envoyproxy/envoy:v1.18.2
else
echo "you are not in the docker group, running with sudo"
echo "Envoy will run at port 51051 (see envoy-config.yml)"
sudo docker run -it --rm --name envoy --network="host"\
-v "$(pwd)/greeting_service_definition.pb:/data/greeting_service_definition.pb:ro" \
-v "$(pwd)/envoy-config.yml:/etc/envoy/envoy.yaml:ro" \
envoyproxy/envoy:v1.18.2
fi
解决方案
该503
状态是因为转码操作失败 - 看起来 envoy 无法连接到 gRPC 服务。
您已将 envoy 配置为连接到 gRPC - 如果您在 linux 上使用而不是在 Mac/Windows上使用,host.docker.internal:53000
这可能是一个问题。docker
docker-desktop
但是java服务器也监听端口8080,而不是53000,所以我认为你只需要改变它。
[编辑]
我确认“host.docker.internal”适用于您在 Ubuntu 18.04 上的项目,像这样调用 docker
docker run -it --rm --name envoy --add-host=host.docker.internal:host-bridge -p 51051:5105
:. 在我测试的版本(f9e7c1)中,需要的端口lb_endpoints
从 53000 更改为 8080。
请注意,这是使用默认的“桥接”网络模式,因此需要端口映射curl
以访问转码后的特使端点。
您可以在“主机”模式网络中运行 envoy 容器——这更简单,因为您可以curl
通过 localhost(无端口映射)来访问 envoy 的转码端点,并且 envoy 也可以通过 localhost IP 访问 java 服务器。
有关处理此类容器间依赖关系的方法的详细摘要,请参阅此答案。
推荐阅读
- database - 如何在刷新时重新渲染 Flatlist?
- arduino - esp8266继电器实时读取继电器状态
- consul - 带有 ocelot 和 consul 的动态服务名称
- java - HttpUrlConnection connect() 方法返回 IOException,仅来自 android 9(饼图)
- objective-c - 替换现有执行程序时调度程序附加错误!!!编号:88
- excel - vba excel 2010中意外的自主更改a1 / r1c1表示法
- php - 带有重音字符的 SQL 查询
- python - 无法在同一个 django 视图中使用两个模型
- python - 如果第 N 个索引应大于前一个索引,如何检查索引
- batch-file - 如何在cmd中为命令设置超时