问题描述
# Create the namespace
kubectl create namespace one
# Create the pod (NOTE - Without a service)
kubectl run rest -n one --image Nginx --labels app=rest --expose --port 80
然后我创建了第二个命名空间和 pod(我将使用它来访问第一个)。
# Create the namespace
kubectl create namespace two
# Create the pod (NOTE - Without a service)
kubectl run call-rest -n two --image alpine -- sleep 3600
然后我抓取第一个pod的ip地址:
kubectl get pod rest -n one -o wide
# This gives me
# NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READInesS GATES
# rest 1/1 Running 0 10m 192.168.0.142 k3d-dev-server-0 <none> <none>
# This uses the format that K8s creates for DNS
kubectl exec -it call-rest -n two -- wget -qO- --timeout=2 http://192-168-0-142.one.pod.cluster.local
以上只是超时
为了验证,我检查了集群的域,我认为它是正确的:
# Run this inside the pod
kubectl exec -it call-rest -n two -- cat /etc/resolv.conf
# And it prints out:
#
# search two.svc.cluster.local svc.cluster.local cluster.local
# nameserver 10.43.0.10
# options ndots:5
如果 pod 位于同一个命名空间中,它们可以相互访问(但我不需要使用 IP 地址)。
知道如何让他们相互联系吗?
我正在使用:
- k3d 版本 v4.4.1
- k3s 版本 v1.20.5-k3s1(默认)
- 印花布
解决方法
事实证明这是因为我使用的是 Calico。使用 k3d 中使用的默认法兰绒,上述 确实 工作。但是,在 Calico 中,它为 pod 创建集群范围的 IP 地址,而不是每个命名空间内的 IP。所以以下工作:
kubectl exec -it call-rest -n two -- wget -qO- --timeout=2 http://192.168.0.142