我尝试了解 Kubernetes 中的 hostIP 和 hostPort。
这是我的集群配置:
3个流浪节点:
nodes = [
{ :hostname => 'k8s-master', :ip => '192.168.150.200', :ram => 4096 },
{ :hostname => 'k8s-minion1', :ip => '192.168.150.201', :ram => 4096 },
{ :hostname => 'k8s-minion2', :ip => '192.168.150.202', :ram => 4096 },
]
我写了以下 list 来测试它:
apiVersion: v1
kind: Pod
metadata:
name: firstpod
spec:
containers:
- name: container
image: nginx
ports:
- containerPort: 80
hostIP: 10.0.0.1
hostPort: 8080
我使用 kubectl apply -f port.yml
进行部署
pod 在 k8s-minion2 上运行
kubectl get pods -o wide gives :
NAME READY STATUS RESTARTS AGE IP NODE
firstpod 1/1 Running 0 2m 10.38.0.3 k8s-minion2
我可以从集群内部 curl ngnix,如下所示:
#ssh inside the cluster
vagrant ssh k8s-master
#curl the containerPort on the pod ip
curl 10.38.0.3:80
但是,我不知道如何使用 hostIp 和 hostPort。 curl 10.0.0.1:8080 给出:
curl: (7) Failed to connect to 10.0.0.1 port 80: Connection timed out
curl 节点或集群 Ip 给出:
curl: (7) Failed to connect to 10.38.0.3 port 8080: Connection refused
那么 8080 端口在哪里开放,hostIp 的用途是什么?
谢谢
最佳答案
如果您查看 kubernetes API reference你会发现hostIP
是在将 pod 调度到节点后分配的 IP。
hostIP
(string) - IP address of the host to which the pod is assigned. Empty if not yet scheduled.
这可以更进一步exposed如果需要,作为 pod 内的环境 (spec.hostIP
)
与 hostPort
您可以在地址 <hostIP>:<hostPort>
处将容器端口暴露给外部网络,其中hostIP是运行容器的Kubernetes节点的IP地址,hostPort是用户请求的端口。您可以阅读有关 here 的更多信息.
如果您想到达您的 pod,还有其他方法可以做到这一点,例如 ClusterIP
或 NodePort
取决于请求是来自内部还是外部。这个article通过它们和它们的差异。
关于Kubernetes : What is hostPort and hostIp used for?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63691946/