有学员在使用 minikube 发布应用时遇到正常发布了Deployment和Service后,Service不能正常访问的问题。通过 kubeclt describe, kubectl logs 等命令检查,发现 pod 已经正常运行,但就是不能通过 localhost 进行访问。本文记录遇到问题的过程及解决方法。

发布应用

使用以下命令

1
kubectl run nginx --image=nginx --restart=Never --port=80

然后通过新建一个服务来暴露应用,选择 NodePort 类型,

1
kubectl expose po nginx --name=front-end-service --port=80 --target-port=80 --type=NodePort

通常建立这个服务后,就可以通过 localhost 加上 kubernetes 随机生成的端口访问到 nginx 服务了。

但在 minikuber 中使用下面的命令

1
curl http://locahost:<端口>

却不能访问,显示连接拒绝。

解决方法

问题出在 minikube 跑在自己的虚拟网络中,并不是在 localhost 中,因此我们首先需要找到 minikube 的 IP

1
minikuber ip

然后通过该 ip 就可以访问了。

1
curl http://192.168.64.2:<端口>