Traceback (most recent call last): File "/Users/medivh/github/box/test_eureka-service.py", line 19, in <module> res = eureka_client.do_service("O2O-SERVICE", "/", File "/Users/medivh/github/box/venv/lib/python3.8/site-packages/py_eureka_client/eureka_client.py", line 1736, in do_service return cli.do_service(app_name=app_name, service=service, return_type=return_type, File "/Users/medivh/github/box/venv/lib/python3.8/site-packages/py_eureka_client/eureka_client.py", line 1467, in do_service return self.walk_nodes(app_name, service, prefer_ip, prefer_https, walk_using_urllib) File "/Users/medivh/github/box/venv/lib/python3.8/site-packages/py_eureka_client/eureka_client.py", line 1415, in walk_nodes raise http_client.URLError("Try all up instances in registry, but all fail") urllib.error.URLError: <urlopen error Try all up instances in registry, but all fail> [2022-04-27 17:32:39]-[eureka_client]-[line:1409] -WARNING: do service / in node [o2o-service-287077292-hj7q7:o2o-service:8080] error, use next node. Error: <urlopen error timed out>
# 在路由器上需要做的配置: ip route add -net 10.200.0.0/16 gw 192.168.1.109 ip route add -net 10.96.0.0/12 gw 192.168.1.109
setps 3
这个时候在办公网主机上ping 集群中的Pod或者service的IP,理论上就是通的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Pod ➜ ~ traceroute 10.200.96.148 traceroute to 10.200.96.148 (10.200.96.148), 64 hops max, 52 byte packets 1 10.1.3.1 (10.1.3.1) 2.781 ms 2.422 ms 2.388 ms 2 bogon (192.168.1.109) 2.723 ms 3.352 ms 3.647 ms 3 bogon (10.200.96.128) 3.724 ms 3.485 ms 3.417 ms 4 10.200.96.148 (10.200.96.148) 3.551 ms 3.440 ms 3.390 ms
# service
➜ ~ traceroute 10.96.230.195 traceroute to 10.96.230.195 (10.96.230.195), 64 hops max, 52 byte packets 1 10.1.3.1 (10.1.3.1) 5.698 ms 2.482 ms 2.375 ms 2 10.96.230.195 (10.96.230.195) 2.774 ms 2.807 ms 2.834 ms
问题总结
如果以上操作都执行了,但是还是不通,请检查以下项目:
转发是否开启
iptables的规则是否正确
iptables规则是否有MASQUERADE all -- 192.168.1.0/24 0.0.0.0/0 这么一条
至此,解决了开发人员无法在办公网访问集群环境的IP问题。
跨集群访问
如果我们有多个集群,这个时候如果实现网络打通呢,比如新旧集群迁移的时候?
以IP形式访问
其实我们依然可以采用之前的方案,添加静态路由。
网段:
网段名称
网段范围
办公网段
10.1.3.0/24
集群A Pod地址池
10.200.0.0/16
集群A Svc地址池
10.96.0.0/12
集群B Pod地址池
10.100.0.0/16
集群B Svc地址池
10.254.0.0/16
主要实现逻辑是每个集群选择一个节点,开启路由转发,实现办公网段可以访问集群中的IP。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
~ iptables -t nat -A POSTROUTING -s 10.1.3.0/24 -d 10.254.0.0/16 -j MASQUERADE -w ~ iptables -t nat -A POSTROUTING -s 10.1.3.0/24 -d 10.100.0.0/16 -j MASQUERADE -w ~ iptables -t nat -L -n --line-numbers | grep -A 10 "Chain POSTROUTING" Chain POSTROUTING (policy ACCEPT) num target prot opt source destination 1 MASQUERADE all -- 10.100.72.0/24 0.0.0.0/0 2 KUBE-POSTROUTING all -- 0.0.0.0/0 0.0.0.0/0 /* kubernetes postrouting rules */ 3 MASQUERADE all -- 10.1.3.0/24 10.254.0.0/16 4 MASQUERADE all -- 10.1.3.0/24 10.100.0.0/16
Chain DOCKER (2 references) num target prot opt source destination 1 RETURN all -- 0.0.0.0/0 0.0.0.0/0
# 办公网 ➜ ~ traceroute 10.100.5.7 traceroute to 10.100.5.7 (10.100.5.7), 64 hops max, 52 byte packets 1 bogon (10.1.3.1) 2.699 ms 2.201 ms 2.140 ms 2 *.demo.yourdomain.com (192.168.1.243) 2.213 ms 7.957 ms 7.389 ms 3 bogon (10.100.5.0) 2.902 ms 4.637 ms 2.616 ms 4 bogon (10.100.5.7) 4.109 ms 3.944 ms 2.782 ms
~ kubectl get svc -n kube-system -l k8s-app=kube-dns NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 34d
~ traceroute eureka.default.svc.cluster.local traceroute to eureka.default.svc.cluster.local (10.200.229.155), 30 hops max, 60 byte packets 1 192.168.1.1 (192.168.1.1) 0.151 ms 0.145 ms 0.137 ms 2 new.eureka.com (192.168.1.109) 0.622 ms 0.595 ms * 3 bogon (10.200.229.128) 1.311 ms 1.972 ms 2.019 ms 4 bogon (10.200.229.155) 2.075 ms 2.743 ms 2.741 ms