在微服务之间进行通信时,我收到连接被拒绝。
微服务通过服务发现暴露为4004端口上的集群 IP
DNS 已正确解析:
/app # ping sms.obchod-uat.svc.cluster.local
PING sms.obchod-uat.svc.cluster.local (10.43.204.129): 56 data bytes
当我尝试 curl 到端口 80 时,它正在等待超时,因为微服务没有暴露这个端口 - 正确的行为,但是当我尝试 curl 到暴露的端口时,它给了我Connection refused。
/app # curl sms.obchod-uat.svc.cluster.local:4004
curl: (7) Failed to connect to sms.obchod-uat.svc.cluster.local port 4004 after 12 ms: Connection refused
所以这意味着服务已经正确暴露了端口,我可以向它请求。但由于某种原因,有些东西拒绝了它。
我在用:
RKE **2.5.7**
kubernetes **1.20.11**
CoreDNS **1.8.0**
服务.yaml
apiVersion: v1
kind: Service
metadata:
annotations:
field.cattle.io/creatorId: user-5srkl
field.cattle.io/ipAddresses: "null"
field.cattle.io/targetDnsRecordIds: "null"
field.cattle.io/targetWorkloadIds: '["deployment:obchod-uat:sms"]'
creationTimestamp: "2022-01-22T13:30:30Z"
labels:
cattle.io/creator: norman
managedFields:
- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:metadata:
f:annotations:
.: {}
f:field.cattle.io/creatorId: {}
f:field.cattle.io/ipAddresses: {}
f:field.cattle.io/targetDnsRecordIds: {}
f:field.cattle.io/targetWorkloadIds: {}
f:labels:
.: {}
f:cattle.io/creator: {}
f:spec:
f:ports:
.: {}
k:{"port":4004,"protocol":"TCP"}:
.: {}
f:port: {}
f:protocol: {}
f:targetPort: {}
f:publishNotReadyAddresses: {}
f:sessionAffinity: {}
f:type: {}
manager: rancher
operation: Update
time: "2022-01-22T13:30:29Z"
- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:spec:
f:selector:
.: {}
f:workloadID_sms: {}
manager: agent
operation: Update
time: "2022-01-22T13:30:30Z"
name: sms
namespace: obchod-uat
resourceVersion: "57096530"
uid: f6213174-dcad-4535-be2c-e933a03e2fa6
spec:
clusterIP: 10.43.204.129
clusterIPs:
- 10.43.204.129
ports:
- port: 4004
protocol: TCP
targetPort: 4004
publishNotReadyAddresses: true
selector:
workloadID_sms: "true"
sessionAffinity: None
type: ClusterIP
status:
loadBalancer: {}
有任何想法吗?
解决了
如果您使用的是嵌套微服务,请不要忘记将主机属性添加到选项中。
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
async function bootstrap() {
const app = await NestFactory.createMicroservice<MicroserviceOptions>(
AppModule,
{
transport: Transport.TCP,
options: {
host: '0.0.0.0',
port: 4004,
},
},
);
await app.listen();
}
bootstrap();
这花了我2天的时间。