Docker for Desktop 运行 Kubernetes - IP 地址无法工作

21

kubectl describe node docker-for-desktop 这将获取Docker桌面版Windows的IP地址。

但是,当我们在浏览器上使用ip:nodeport时,它无法工作。

nodeport是我们在Kubernetes集群的服务文件中提到的端口号。

请在代码部分查找myservice.yaml文件。

apiVersion: v1
kind: Service
metadata:
  name: xxxx

spec:
  # This defines which pods are going to be represented by this Service
  # The service becomes a network endpoint for either other services
  # or maybe external users to connect to (eg browser)
  selector:
    mykey: webapp
    release: "0-5"

  ports:
    - name: http
      port: 80
      nodePort: 30080
      #this is port number greater than 30000, and it is port of the Node where this cluster is present.

  type: NodePort

http://<dockerDesktopIdaddress>:<nodeport>

nodeport是kubernetes集群中服务文件中的端口号。

始终出现错误。


刚刚编辑了问题,使其更易读。也许你想描述一下你遇到了哪个错误,这样别人才能帮助你。 - fqxp
2
是的,请添加错误描述。Docker for Desktop在localhost上公开了所有内容,所以不应该有任何问题。 - Rafał Leszko
如果有人想知道错误信息: 我运行了命令:curl http://192.168.65.4:30008 然后得到了这个错误:curl: (28) 连接到 192.168.65.4 端口 30008 失败,超时时间为 75004 毫秒 - Eliyahu Machluf
4个回答

55

我也遇到了同样的问题,解决方法很简单。 在终端输入:curl localhost:$NODE_PORT

Docker for Mac 运行在相同的主机上,因此可以通过 localhost 调用服务。


2
你是我的英雄!我为了获取k8s节点的外部IP整整挣扎了一天,但我应该使用简单的“localhost”::facepalm::。之前我在使用Minikube时必须找到外部IP,在Windows上的Docker中则不需要!非常感谢! - kryski
2
谢谢。这个问题让我很困惑,因为使用Docker Desktop Kubernetes时,我可以使用localhost:<端口>进行访问,而使用Minikube时,我无法这样做。在Minikube中,我必须使用minikube service <my-service> --url来获取URL,然后才能访问它。 - 0xh8h

5

另一个好用的命令是 kubectl describe service 你的服务名称。这样可以查看所有Kubernetes网络对象。

kubectl describe service configuration-service-service

Name:                     configuration-service-service
Namespace:                default
Labels:                   none
Annotations:              none
Selector:                 app=configuration-service-deployment
Type:                     NodePort
IP:                       10.109.61.232
LoadBalancer Ingress:     localhost
Port:                     <unset>  8080/TCP
TargetPort:               8080/TCP
NodePort:                 <unset>  30001/TCP
Endpoints:                10.1.0.40:8080,10.1.0.41:8080
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

2

请仔细检查节点是否定义了外部IP。

kubectl get nodes -o wide

查找服务中定义的负载均衡器入口

kubectl describe svc service_name 

请将LoadBalancer Ingress记录下来,本例中为“localhost”。

最后,您可以尝试:

curl http://localhost:PORT 

-6
Docker For Windows、Docker For MAC或者其他任何使用minikube的kubernetes都需要获取minikube机器的外部IP地址。
你需要获取外部IP地址,而不是内部IP地址,因为kubectl只显示内部IP地址。
C02W84XMHTD5:test iahmad$ 
C02W84XMHTD5:test iahmad$ 
C02W84XMHTD5:test iahmad$ 
C02W84XMHTD5:test iahmad$ kubectl describe node minikube | grep -i ip
  InternalIP:  10.0.2.15
C02W84XMHTD5:test iahmad$ 
C02W84XMHTD5:test iahmad$ 
C02W84XMHTD5:test iahmad$ kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        30d
nginx        NodePort    10.107.66.154   <none>        80:32622/TCP   4m
C02W84XMHTD5:test iahmad$ 
C02W84XMHTD5:test iahmad$ 
C02W84XMHTD5:test iahmad$ curl 10.0.2.15:32622


çççç^C
C02W84XMHTD5:test iahmad$ 
C02W84XMHTD5:test iahmad$ 
C02W84XMHTD5:test iahmad$ minikube ssh
                         _             _            
            _         _ ( )           ( )           
  ___ ___  (_)  ___  (_)| |/')  _   _ | |_      __  
/' _ ` _ `\| |/' _ `\| || , <  ( ) ( )| '_`\  /'__`\
| ( ) ( ) || || ( ) || || |\`\ | (_) || |_) )(  ___/
(_) (_) (_)(_)(_) (_)(_)(_) (_)`\___/'(_,__/'`\____)

$ 
$ ip a s | grep eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic eth0
$ 
$ ip a s | grep eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    inet 192.168.99.105/24 brd 192.168.99.255 scope global dynamic eth1
$ 
$ logout
C02W84XMHTD5:test iahmad$ 
C02W84XMHTD5:test iahmad$ curl 192.168.99.105:32622
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

编辑:

如评论中所建议,如果您不想查看minikube机器内部,可以使用以下命令获取外部IP:

 minikube ip

1
难道没有minikube ip命令可以获取IP地址,这样就不必执行额外的步骤了吗?另外,由于原始问题似乎是在Windows上使用Docker,因此帮助显示该上下文的正确IP地址而不是minikube会更有意义。 - Andy Shinn
Docker for Windows 或者 Docker for anything 都一样,我使用 Docker for MAC 管理 minikube,Docker for anything 无论如何都需要使用 minikube 来进行 Kubernetes 的管理,这是获取正确 IP 地址的正确方式,与 Docker for anything 无关。 - Ijaz Ahmad
5
不,它们是使用不同的虚拟化和管理策略的两个不同的东西。至少在 macOS 上,Docker for Mac 使用 xhyve 运行底层虚拟机,而 minikube 使用 VirtualBox。两者都将以不同的方式管理和处理网络。 - Andy Shinn
是的,但对于用户来说它们是相同的,特别是在这个问题的背景下。我保证这个解决方案也适用于Windows上的Docker。 - Ijaz Ahmad
你已经在内部IP上运行了curl命令,如何在Windows机器上运行相同的命令?由于Windows中没有curl,如果可能的话,您能否帮助我获取docker-for-desktop的外部IP地址? - Rajasekar Murugesan
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接