连接拒绝调用 kubernetes ClusterIP 服务

Connection refused calling kubernetes ClusterIP service

我正在尝试在本地 k3d k8s 集群上部署两个应用程序。服务 A 公开一个由服务 B 使用的 REST 端点。这是配置:

服务-A

---
apiVersion: v1
kind: Service
metadata:
  labels:
    app.kubernetes.io/name: service-A
    app.kubernetes.io/version: 1.0.1-SNAPSHOT
  name: service-A
spec:
  ports:
  - name: http
    port: 8554
    targetPort: 8554
  selector:
    app.kubernetes.io/name: service-A
    app.kubernetes.io/version: 1.0.1-SNAPSHOT
  type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/version: 1.0.1-SNAPSHOT
    app.kubernetes.io/name: service-A
  name: service-A
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/version: 1.0.1-SNAPSHOT
      app.kubernetes.io/name: service-A
  template:
    metadata:
      labels:
        app.kubernetes.io/version: 1.0.1-SNAPSHOT
        app.kubernetes.io/name: service-A
    spec:
      containers:
      - env:
        - name: KUBERNETES_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        image: registry.localhost:12345/service-A
        imagePullPolicy: IfNotPresent
        name: service-A
        ports:
        - containerPort: 8554
          name: http
          protocol: TCP

服务-B

---
apiVersion: v1
kind: Service
metadata:
  labels:
    app.kubernetes.io/name: service-B
    app.kubernetes.io/version: 1.0.1-SNAPSHOT
  name: service-B
spec:
  ports:
  - name: http
    port: 8550
    targetPort: 8550
  selector:
    app.kubernetes.io/name: service-B
    app.kubernetes.io/version: 1.0.1-SNAPSHOT
  type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/version: 1.0.1-SNAPSHOT
    app.kubernetes.io/name: service-B
  name: service-B
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/version: 1.0.1-SNAPSHOT
      app.kubernetes.io/name: service-B
  template:
    metadata:
      labels:
        app.kubernetes.io/version: 1.0.1-SNAPSHOT
        app.kubernetes.io/name: service-B
    spec:
      containers:
      - env:
        - name: KUBERNETES_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: SERVICE_A_ADDR
          valueFrom:
            configMapKeyRef:
              key: service_a_addr
              name: app-configmap
        image: registry.localhost:12345/service-B-native
        imagePullPolicy: IfNotPresent
        name: service-B
        ports:
        - containerPort: 8550
          name: http
          protocol: TCP

配置图

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-configmap
data:
  service_a_addr: service-A

如您所见,服务 B 需要一个环境变量,其值是从包含服务 A 名称的配置映射中读取的。

但是,当服务 B 尝试调用服务 A 时,我收到连接被拒绝的异常:

Caused by: javax.ws.rs.ProcessingException: RESTEASY004655: Unable to invoke request: org.apache.http.conn.HttpHostConnectException: Connect to localhost:8554 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused)
    at org.jboss.resteasy.client.jaxrs.engines.ManualClosingApacheHttpClient43Engine.invoke(ManualClosingApacheHttpClient43Engine.java:297)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:491)
    at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invokeSync(ClientInvoker.java:152)
    at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:115)
    at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:76)
    at com.sun.proxy.$Proxy291.getLoginId(Unknown Source)
    at java.lang.reflect.Method.invoke(Method.java:566)
    at org.jboss.resteasy.microprofile.client.ProxyInvocationHandler.invoke(ProxyInvocationHandler.java:147)
    at com.sun.proxy.$Proxy292.getLoginId(Unknown Source)

知道会发生什么吗? Kubernetes YAML 文件由 java 框架 Quarks 自动生成。

Connect to localhost:8554 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused)

它没有被配置为连接到“service-A”,它被配置为连接到本地主机?此错误消息提供了为什么您的客户端连接到错误地址的线索。