问题描述:在k8s集群中主机无法ping通service ip,但是pod ip是可以ping通
1.先看下serviceIP是怎么来的?
serviceIP是serviceController生成的,参数–service-cluster-ip-range string会配置在controller-manager上,serviceController会在这个参数指定的cidr范围内取一个IP

2.为什么不能ping?
serviceIP是虚拟的地址,没有分配给任何网络接口,当数据包传输时不会把这个IP作为数据包的源IP或目的IP。
kube-proxy在iptables模式下,这个IP没有被设置在任何的网络设备上,ping这个IP的时候,没有任何的网络协议栈会回应这个ping请求。
可以查看kube-proxy的服务的配置文件,核查kube-proxy的代理模式。mode处不配置,默认为iptables模式


kubectl edit configmap -n kube-system kube-proxy

在iptables模式时,clusterIp会在iptables的PREROUTING链里面用于nat转换规则中。
而在ipvs模式下,会使用在ipvs模块的ipvs规则转换中。
在ipvs模式下,所有的clusterIp会被设置在node上的kube-ipvs0的虚拟网卡上,这个时候去ping是可以通的。



完美解决
网友评论