master和node在同一台虚拟机,外部网络无法访问k8s配置好的service暴露的端口
现象
环境:
| 12
 3
 
 | CentOS Linux release 7.9.2009 (Core)Kubernetes v1.5.2
 Docker version 1.13.1, build 0be3e21/1.13.1
 
 | 
单机搭建master加node
tomcat-deployment.yaml内容:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 
 | apiVersion: extensions/v1beta1kind: Deployment
 metadata:
 name: tomcat
 spec:
 replicas: 1
 selector:
 matchLabels:
 tier: tomcat
 matchExpressions:
 - {key: tier, operator: In, values: [tomcat]}
 template:
 metadata:
 labels:
 app: tomcat-demo
 tier: tomcat
 spec:
 containers:
 - name: tomcat-demo
 image: tomcat
 imagePullPolicy: IfNotPresent
 ports:
 - containerPort: 8080
 
 | 
tomcat-service.yaml内容:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | apiVersion: v1kind: Service
 metadata:
 name: tomcat-service
 spec:
 type: NodePort
 ports:
 - port: 8080
 nodePort: 31002
 selector:
 tier: tomcat
 app: tomcat-demo
 
 | 
用kubectl get pods看状态正常,kubectl describe pod xxx也正常,就是docker exec -it xxx /bin/bash进容器看日志也正常,虚拟机内直接curl http://127.0.0.1:31002也是正常有访问的,并且虚拟机的防火墙也是关着的(用docker run -d -p 31002:8080 --name xxx tomcat直接起容器就可以访问)
解决
排查一圈之后,实在没办法,上网搜。解决办法,虚拟机内执行:
| 1
 | iptables -P FORWARD ACCEPT
 | 
完了就全正常了。。
参考链接