kubernetes - 服务在 Istio 服务网格中不通信
问题描述
我有两个服务(Service1 和 Service2)的服务网格。边车已启用。我使用了 bookinfo 官方 Istio 教程中的基本网关,并配置了 VirtualService:
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: bookinfo-gateway
spec:
selector:
istio: ingressgateway # use istio default controller
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "*"
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: virtual-template-service
spec:
gateways:
- bookinfo-gateway
hosts:
- "*"
http:
- match:
- uri:
prefix: /api/home/method1
- uri:
prefix: /api/home/method2
route:
- destination:
host: service1.service1-production.svc.cluster.local
port:
number: 80
- match:
- uri:
prefix: /api/home2
route:
- destination:
host: service2.service2-production.svc.cluster.local
port:
number: 80
apiVersion: v1
kind: Service
metadata:
labels:
app: {{ .Chart.Name }}
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
release: {{ .Release.Name }}
heritage: {{ .Release.Service }}
component: app
name: {{ .Release.Name }}
spec:
ports:
- name: http
port: {{ .Values.service.port }}
protocol: TCP
targetPort: {{ .Values.service.port }}
selector:
app: {{ .Chart.Name }}
release: {{ .Release.Name }}
component: app
sessionAffinity: None
type: ClusterIP
Service1 的 method1 不与 Service2 交互。当我请求 Service1/api/home/method1. 一切正常。
Service1 的 method2 确实与 Service2 交互。它通过 POST 方法发送了一个简单的异步 HTTP 请求。而且这种方法行不通。
在 Service1 的边车日志中,我看到请求已发送,但状态为 500。在应用程序日志中,我看到错误:No such address or device
。
在 Service2 sidecar 中,日志没有任何活动。
对 service2/api/home2 的请求也可以正常工作。
我不明白我做错了什么。你怎么看待这件事?请帮忙。
更新
这两个应用程序都基于 .net 核心。所以我使用 webclient 来发送请求。例子:
public static async Task<string> RequestToService2Async(string request)
{
using (var client = new WebClient())
{
client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
var response = await client.UploadStringTaskAsync(new Uri(http://service2/api/home2), "POST", request);
return response;
}
}
Service1 边车日志:
"GET /api/home/method2HTTP/1.1" 500 - 0 0 28 5 "20.100.3.1" "PostmanRuntime/7.3.0" "083fbcbc-9598-9973-b0c1-b897afdc9df0" "Service1" "127.0.0.1:80" inbound|80||Service1.Service1-production.svc.cluster.local - 20.100.3.110:80 20.100.3.1:0 outbound|80||Service1.Service1-production.svc.cluster.local
应用程序日志中的错误
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HLHJ7PTBHFRI", Request id "0HLHJ7PTBHFRI:00000002": An unhandled exception was thrown by the application.
System.Net.WebException: No such device or address No such device or address ---> System.Net.Http.HttpRequestException: No such device or address ---> System.Net.Sockets.SocketException: No such device or address
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask`1 creationTask)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at System.Net.HttpWebRequest.SendRequest()
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
更新 2.0
当我在 Uri MyService2ClusterIP/api/home2 中使用时 - 一切正常!所以,我不能用 uri 中的主机名请求。
解决方案
推荐阅读
- java - 使用 Apache Nutch 的 regex-urlfilter 语法
- java - 单元测试 java 可选与 mockito
- django - Django-ORM 代替 python for 循环
- javascript - 闪烁元素Javascript
- ruby-on-rails - 嵌套每个循环查询错误的列名...模型问题?
- javascript - 为什么 jquery .fail 没有故意接收参数触发错误
- php - Yii2 从 NavBar 中删除 div 标签
- coldfusion - 来自 QoQ 的 Coldfusion 图表多个系列
- powerbi - 如何格式化 Dial Gauge 中的值以在 Power BI Desktop 中仅显示数百万
- amazon-web-services - DynamoDB 未使用的写入预置容量