无法连接到主机中的minikube服务

时间:2020-08-10 13:56:57

标签: docker kubernetes minikube

我已经阅读了很多stackoverflow帖子,但是都没有工作...所以这是我的问题:

我有一个简单的节点应用程序正在端口0.0.0.0的广播5000上运行,它在/处有一个简单的单一端点。

我有两个k8s对象,这是我的Deployment对象:


### pf deployment
apiVersion: apps/v1
kind: Deployment
metadata:
    # Unique key of the Deployment instance
    name: pf-deployment
spec:
    # 3 Pods should exist at all times.
    replicas: 1
    selector:
        matchLabels:
            app: public-facing
    template:
        metadata:
            labels:
                # Apply this label to pods and default
                # the Deployment label selector to this value
                app: public-facing
        spec:
            containers:
                - name: public-facing
                  # Run this image
                  image: pf:ale8k
                  ports:
                    - containerPort: 5000

接下来,这是我的服务对象:

### pf service
apiVersion: v1
kind: Service
metadata:
  name: pf-service
  labels:
    run: pf-service-label
spec:
  type: NodePort ### may be ommited as it is a default type
  selector:
    name: public-facing ### should match your labels defined for your angular pods
  ports:
    - protocol: TCP
      targetPort: 5000 ### port your app listens on
      port: 5000 ### port on which you want to expose it within your cluster

最后,一个非常简单的dockerfile:

### generic docker file
FROM node:12

WORKDIR /usr/src/app

COPY . .

RUN npm i

EXPOSE 5000

CMD ["npm", "run", "start"]

我的映像在minikubes本地docker注册表中,所以这不是问题...

当我尝试时: curl $(minikube service pf-service --url) 我得到: curl: (7) Failed to connect to 192.168.99.101 port 31753: Connection refused

当我尝试时: minikube service pf-service

我得到一些进一步的输出:

Most likely you need to configure your SUID sandbox correctly

我正在运行hello-minikube图像,这工作得很好。所以我想这不是我的鼻涕吗?

我对kubernetes还是很陌生,所以如果很简单,请提前道歉。

谢谢!

1 个答案:

答案 0 :(得分:2)

服务具有选择器name: public-facing,但吊舱具有标签app: public-facing。它们必须与Endpoints相同,才能使用Pod IP填充服务。

如果执行以下命令

kubectl describe svc pf-service

您会看到Endpoints没有IP,这是connection refused错误的原因。

如下更改服务中的选择器以使其起作用。

### pf service
apiVersion: v1
kind: Service
metadata:
  name: pf-service
  labels:
    run: pf-service-label
spec:
  type: NodePort ### may be ommited as it is a default type
  selector:
    app: public-facing ### should match your labels defined for your angular pods
  ports:
    - protocol: TCP
      targetPort: 5000 ### port your app listens on
      port: 5000 ### port on which you want to expose it within your cluster