minikube 服务 URL 在 mac os Monterey 上给出 ECONNREFUSED

minikube service URL gives ECONNREFUSED on mac os Monterey

我有一个 spring-boot postgres 设置,我正在尝试将其容器化并部署到 minikube 中。我的 pods 和服务显示它们已启动。

$ kubectl get pods
NAME                                          READY   STATUS    RESTARTS   AGE
server-deployment-5bc57dcd4f-zrwzs            1/1     Running   0          14m
postgres-7f887f4d7d-5b8v5                     1/1     Running   0          25m
$ kubectl get svc
NAME               TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
server-service     NodePort    10.100.21.232   <none>        8080:31457/TCP   15m
postgres           ClusterIP   10.97.19.125    <none>        5432/TCP         26m
$ minikube service list
|-------------|------------------|--------------|-----------------------------|
|  NAMESPACE  |       NAME       | TARGET PORT  |             URL             |
|-------------|------------------|--------------|-----------------------------|
| default     | kubernetes       | No node port |
| kube-system | kube-dns         | No node port |
| custom      | server-service   | http/8080    | http://192.168.59.106:31457 |
| custom      | postgres         | No node port |
|-------------|------------------|--------------|-----------------------------|

但是当我尝试使用邮递员访问我的任何端点时,我得到:

Could not send request. Error: connect ECONNREFUSED 192.168.59.106:31457

我不知道我哪里错了。我尝试直接在 docker 中部署各个容器(我不得不修改一些 application.properties 以使其余服务器与 db 容器通信)并且工作没有问题所以很明显我的服务器端代码应该不是问题。

这是 rest-server 的 yml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: server-deployment
  namespace: custom
spec:
  selector:
    matchLabels:
      app: server-deployment
  template:
    metadata:
      name: server-deployment
      labels:
        app: server-deployment
    spec:
      containers:
      - name: server-deployment
        image: aruns1494/rest-server-k8s:latest
        env:
        - name: POSTGRES_USER
          valueFrom:
            configMapKeyRef:
              name: postgres-config
              key: postgres_user
        - name: POSTGRES_PASSWORD
          valueFrom:
            configMapKeyRef:
              name: postgres-config
              key: postgres_password
        - name: POSTGRES_SERVICE
          valueFrom:
            configMapKeyRef:
              name: postgres-config
              key: postgres_service
---
apiVersion: v1
kind: Service
metadata:
  name: server-service
  namespace: custom
spec:
  selector:
    name: server-deployment
  ports:
  - name: http
    port: 8080
  type: NodePort

我没有更改 spring 启动的默认端口,所以我希望它可以在 8080 上工作。我尝试通过 chrome 和 Firefox 连接到那个 URL,我得到了同样的结果错误信息。当我尝试访问 / 端点时,我希望它会返回到默认错误消息页面。

我确实查阅了几篇在线文章,但其中 none 似乎有帮助。如果有帮助,我还附加了我的 kube-system pods:

$ kubectl get pods -n kube-system
NAME                               READY   STATUS    RESTARTS      AGE
coredns-78fcd69978-x6mv6           1/1     Running   0             39m
etcd-minikube                      1/1     Running   0             40m
kube-apiserver-minikube            1/1     Running   0             40m
kube-controller-manager-minikube   1/1     Running   0             40m
kube-proxy-dnr8p                   1/1     Running   0             39m
kube-scheduler-minikube            1/1     Running   0             40m
storage-provisioner                1/1     Running   1 (39m ago)   40m

可能是 MacOS 防火墙阻止了连接。您可以尝试导航至 系统偏好设置 > 安全和隐私 并查看端口是否在 常规 选项卡中被阻止?您还可以在 防火墙 选项卡中禁用防火墙。

我的提议是检查所提供的 Deployment 和 Service 是否具有相同的标签和选择器,因为现在在 Deployment 配置 pod 标签是 app: server-deployment,但在服务配置选择器是 name: server-deployment

如果我们想为服务使用 name: server-deployment 选择器,那么我们需要更新部署,如下所示(matchLabelslabels 字段):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: server-deployment
  namespace: custom
spec:
  selector:
    matchLabels:
      name: server-deployment
  template:
    metadata:
      name: server-deployment
      labels:
        name: server-deployment
    spec:
      containers:
      ...