无法使用 NodePort 服务从浏览器访问 Microk8s 服务

Cannot access Microk8s service from browser using NodePort service

我根据这里的步骤在我的 ubuntu 机器上安装了 microk8s https://ubuntu.com/kubernetes/install#single-node

然后我按照kubernetes官方教程创建并暴露了这样一个部署

microk8s.kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1

microk8s.kubectl expose deployment/kubernetes-bootcamp --type=NodePort --port 8083

这是我的kubectl get services输出

akila@ubuntu:~$ microk8s.kubectl get services
NAME                  TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes            ClusterIP   10.152.183.1    <none>        443/TCP          25h
kubernetes-bootcamp   NodePort    10.152.183.11   <none>        8083:31695/TCP   17s

这是我的kubectl get pods输出

akila@ubuntu:~$ microk8s.kubectl get pods
NAME                                   READY   STATUS    RESTARTS   AGE
kubernetes-bootcamp-6f6656d949-rllgt   1/1     Running   0          51m

但我无法使用 http://localhost:8083http://10.152.183.11:31695

从我的浏览器访问该服务

当我尝试 http://localhost:31695 时,我得到 ERR_CONNECTION_REFUSED。

如何从我的浏览器访问这个“kubernetes-bootcamp”服务? 我错过了什么吗?

IP 10.152.183.11CLUSTER-IP 并且无法从集群外部访问,即从浏览器访问。您应该使用 http://localhost:31695,其中 31695 是在主机系统上打开的 NodePort

gcr.io/google-samples/kubernetes-bootcamp:v1 图像的容器需要在端口 8083 上侦听,因为您要在该端口上公开它。仔细检查,否则会导致 ERR_CONNECTION_REFUSED 错误。

如果容器正在侦听端口 8080 然后使用下面的命令来公开该端口

microk8s.kubectl expose deployment/kubernetes-bootcamp --type=NodePort --port 8080

试试这个

kubectl port-forward <pod_name> <local_port>:<pod_port> 

然后访问 http://localhost:<local_port>