硬件环境:
两台物理机:
172.16.114.210(master主机)
172.16.114.211(node主机)
软件环境:
kubernetes 1.5.2
flannel 0.5.5
问题描述:
部署了一个mysql的NodePort服务,指定nodePort端口为30010,telnet 172.16.114.211 30010是通的,但是telnet 172.16.114.210 30010是不通的。
问题分析
查看172.16.114.210主机的情况如下,
1、30010端口已经监听
2、iptables -S -t nat | grep mysql,nat表规则均正常
3、iptables -S -t filter 如下:
[root@localhost ~]# iptables -S -t filter -P INPUT ACCEPT -P FORWARD DROP -P OUTPUT ACCEPT -N DOCKER -N DOCKER-ISOLATION -N KUBE-SERVICES -A INPUT -i virbr0 -p udp -m udp --dport 53 -j ACCEPT -A INPUT -i virbr0 -p tcp -m tcp --dport 53 -j ACCEPT -A INPUT -i virbr0 -p udp -m udp --dport 67 -j ACCEPT -A INPUT -i virbr0 -p tcp -m tcp --dport 67 -j ACCEPT -A FORWARD -j DOCKER-ISOLATION -A FORWARD -o docker0 -j DOCKER -A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -A FORWARD -i docker0 ! -o docker0 -j ACCEPT -A FORWARD -i docker0 -o docker0 -j ACCEPT -A FORWARD -d 192.168.122.0/24 -o virbr0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -A FORWARD -s 192.168.122.0/24 -i virbr0 -j ACCEPT -A FORWARD -i virbr0 -o virbr0 -j ACCEPT -A FORWARD -o virbr0 -j REJECT --reject-with icmp-port-unreachable -A FORWARD -i virbr0 -j REJECT --reject-with icmp-port-unreachable -A OUTPUT -m comment --comment "kubernetes service portals" -j KUBE-SERVICES -A OUTPUT -o virbr0 -p udp -m udp --dport 68 -j ACCEPT -A DOCKER-ISOLATION -j RETURN
有一条规则-P FORWARD DROP,意思是本机默认的转发规则是丢弃(DROP),因为nodeport服务本身是部署在node主机上的,telnet master主机时,会根据iptables的规则,将该请求转发给node主机上的pod,所以当master主机的默认转发规则是丢弃时,就出现了telnet master主机 加nodeport端口不通的情况。
解决办法
修改master主机上的iptables规则,将默认转发的action修改为accept,执行命令为iptables -P FORWARD ACCEPT,修改后规则如下:
[root@localhost ~]# iptables -P FORWARD ACCEPT [root@localhost ~]# iptables -S -t filter -P INPUT ACCEPT -P FORWARD ACCEPT -P OUTPUT ACCEPT -N DOCKER -N DOCKER-ISOLATION -N KUBE-SERVICES -A INPUT -i virbr0 -p udp -m udp --dport 53 -j ACCEPT -A INPUT -i virbr0 -p tcp -m tcp --dport 53 -j ACCEPT -A INPUT -i virbr0 -p udp -m udp --dport 67 -j ACCEPT -A INPUT -i virbr0 -p tcp -m tcp --dport 67 -j ACCEPT -A FORWARD -j DOCKER-ISOLATION -A FORWARD -o docker0 -j DOCKER -A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -A FORWARD -i docker0 ! -o docker0 -j ACCEPT -A FORWARD -i docker0 -o docker0 -j ACCEPT -A FORWARD -d 192.168.122.0/24 -o virbr0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -A FORWARD -s 192.168.122.0/24 -i virbr0 -j ACCEPT -A FORWARD -i virbr0 -o virbr0 -j ACCEPT -A FORWARD -o virbr0 -j REJECT --reject-with icmp-port-unreachable -A FORWARD -i virbr0 -j REJECT --reject-with icmp-port-unreachable -A OUTPUT -m comment --comment "kubernetes service portals" -j KUBE-SERVICES -A OUTPUT -o virbr0 -p udp -m udp --dport 68 -j ACCEPT -A DOCKER-ISOLATION -j RETURN
此时在telnet master主机加nodeport端口,就通了