无法在 Kubernetes (minikube) 的同一个 pod 中 ping 容器

Cannot ping containers in the same pod in Kubernetes(minikube)

在我的本地我 运行 一个 mysql 容器,然后从同一网络上的另一个容器 ping 它:

$ docker run -d tutum/mysql
$ docker run -it plumsempy/plum bash
    # ping MYSQL_CONTAINER_ID

      PING 67e35427d638 (198.105.244.24): 56 data bytes
      64 bytes from 198.105.244.24: icmp_seq=0 ttl=37 time=0.243 ms 
      ...

很好。然后,在本地使用 Kubernetes(minikube),我使用以下 YAML:

部署 tutum/mysql
...
- name: mysql
  image: tutum/mysql
...

mysql 容器没有其他内容。然后我部署它,ssh 到 minikube pod,启动一个随机容器,这次尝试 ping pod 内的 mysql 容器:

$ kubectl create -f k8s-deployment.yml
$ minikube ssh
    $ docker ps
    $ docker run -it plumsempy/plum bash
        # ping MYSQL_CONTAINER_ID_INSIDE_MINIKUBE

          PING mysql (198.105.244.24): 56 data bytes
          ^C--- mysql ping statistics ---
          10 packets transmitted, 0 packets received, 100% packet loss

        # traceroute MYSQL_CONTAINER_ID_INSIDE_MINIKUBE

          traceroute to aa7f7ed7af01 (198.105.244.24), 30 hops max, 60 byte packets
           1  172.17.0.1 (172.17.0.1)  0.031 ms  0.009 ms  0.007 ms
           2  10.0.2.2 (10.0.2.2)  0.156 ms  0.086 ms  0.050 ms
           3  * * *
           4  * * *
           5  dtr02gldlca-tge-0-2-0-1.gldl.ca.charter.com (96.34.102.201)  16.153 ms  16.107 ms  16.077 ms
           6  crr01lnbhca-bue-200.lnbh.ca.charter.com (96.34.98.188)  18.753 ms  18.011 ms  30.642 ms
           7  crr01mtpkca-bue-201.mtpk.ca.charter.com (96.34.96.63)  30.779 ms  30.523 ms  30.428 ms
           8  bbr01mtpkca-bue-2.mtpk.ca.charter.com (96.34.2.24)  24.089 ms  23.900 ms  23.814 ms
           9  bbr01ashbva-tge-0-1-0-1.ashb.va.charter.com (96.34.3.139)  26.061 ms  25.949 ms  36.002 ms
           10  10ge9-10.core1.lax1.he.net (65.19.189.177)  34.027 ms  34.436 ms  33.857 ms
           11  100ge12-1.core1.ash1.he.net (184.105.80.201)  107.873 ms  107.750 ms  104.078 ms
           12  100ge3-1.core1.nyc4.he.net (184.105.223.166)  100.554 ms  100.478 ms  100.393 ms
           13  xerocole-inc.10gigabitethernet12-4.core1.nyc4.he.net (216.66.41.242)  109.184 ms  111.122 ms  111.018 ms
           14  * * *
           15  * * * 
           ...(til it ends)

plumsempy/plum 可以是任何容器,因为它们都在同一个网络和同一个 pod 上,ping 应该可以通过。问题是 为什么我无法在 minikube 上达到 mysql,我该如何解决?

我猜容器 ID 不适用于 Kubernetes。您还可以看到,容器 ID 解析为 public IP 198.105.244.24,这看起来是错误的。

您可以通过多种方式联系此广告连播:

  • 通过kubectl describe -f k8s-deployment.yml
  • 获取pod IP
  • 为该 pod 创建一个 service 并执行以下操作之一(假设服务名称是 mysql):

来自k8s multi-container pod docs

Pods share fate, and share some resources, such as storage volumes and IP addresses.

因此 mysql 容器可以从 IP 地址 127.0.0.1plum 容器访问。

此外,由于 mysql 默认在端口 3306 上运行,您可能希望 telnet 127.0.0.1 3306 检查它是否可达(ping 使用没有端口概念的 ICMP ).