{"id":1022,"date":"2021-04-10T15:43:26","date_gmt":"2021-04-10T07:43:26","guid":{"rendered":"https:\/\/www.linuxdevops.cn\/?p=1022"},"modified":"2023-04-07T10:02:37","modified_gmt":"2023-04-07T02:02:37","slug":"graphical-kubernetes-network-3","status":"publish","type":"post","link":"https:\/\/www.linuxdevops.cn\/2021\/04\/graphical-kubernetes-network-3\/","title":{"rendered":"\u56fe\u89e3Kubernetes\u7f51\u7edc\uff08\u4e09\uff09"},"content":{"rendered":"
\u3010\u7f16\u8005\u7684\u8bdd\u3011\u672c\u6587\u4e3b\u8981\u6307\u51faKubernetes\u4f5c\u4e3a\u4e00\u4e2a\u5206\u5e03\u5f0f\u7cfb\u7edf\uff0c\u662f\u5982\u4f55\u901a\u8fc7Service\u8fd9\u4e2a\u62bd\u8c61\u6982\u5ff5\u8fdb\u884c\u670d\u52a1\u53d1\u73b0\u7684\uff0c\u5e76\u8fdb\u4e00\u6b65\u9610\u660e\u4e86\u51fa\u7ad9\u6d41\u91cf\u548c\u5165\u7ad9\u6d41\u91cf\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u5e2e\u52a9\u7528\u6237\u66f4\u52a0\u6e05\u6670\u5f97\u4f7f\u7528Kubernetes\u3002<\/p>\n
\u672c\u6587\u662fKubernetes\u7f51\u7edc\u7cfb\u5217\u7684\u7b2c\u4e09\u90e8\u5206\uff0c\u5982\u679c\u4f60\u8fd8\u6ca1\u770b\u8fc7\u7b2c\u4e00\u90e8\u5206<\/a>\u548c\u7b2c\u4e8c\u90e8\u5206<\/a>\uff0c\u6211\u5efa\u8bae\u4f60\u5148\u82b1\u70b9\u65f6\u95f4\u8bfb\u4e00\u4e0b\u3002<\/em><\/p>\n \u7531\u4e8eKubernetes\uff08\u66f4\u901a\u7528\u7684\u8bf4\u6cd5\u662f\u5206\u5e03\u5f0f\u7cfb\u7edf\uff09\u5929\u751f\u5177\u6709\u4e0d\u65ad\u53d8\u5316\u7684\u7279\u6027\uff0c\u56e0\u6b64\u5b83\u7684Pod\uff08\u4ee5\u53caPod\u7684IP\uff09\u603b\u662f\u5728\u6539\u53d8\u3002\u53d8\u5316\u7684\u539f\u56e0\u53ef\u4ee5\u662f\u9488\u5bf9\u4e0d\u53ef\u9884\u6d4b\u7684Pod\u6216\u8282\u70b9\u5d29\u6e83\u800c\u8fdb\u884c\u7684\u6eda\u52a8\u5347\u7ea7\u548c\u6269\u5c55\u3002\u8fd9\u4f7f\u5f97Pod IP\u4e0d\u80fd\u76f4\u63a5\u7528\u4e8e\u901a\u4fe1\u3002<\/p>\n \u6211\u4eec\u770b\u4e00\u4e0bKubernetes Service\uff0c\u5b83\u662f\u4e00\u4e2a\u865a\u62dfIP\uff0c\u5e76\u4f34\u968f\u7740\u4e00\u7ec4Pod IP\u4f5c\u4e3aEndpoint\uff08\u901a\u8fc7\u6807\u7b7e\u9009\u62e9\u5668\u8bc6\u522b\uff09\u3002\u5b83\u4eec\u5145\u5f53\u865a\u62df\u8d1f\u8f7d\u5747\u8861\u5668\uff0c\u5176IP\u4fdd\u6301\u4e0d\u53d8\uff0c\u800c\u540e\u7aefPod IP\u53ef\u80fd\u4f1a\u4e0d\u65ad\u53d8\u5316\u3002<\/p>\n <\/p>\n Kubernetes Srvice\u5bf9\u8c61\u7684\u6807\u7b7e\u9009\u62e9\u5668<\/em><\/p>\n \u6574\u4e2a\u865a\u62dfIP\u7684\u5b9e\u73b0\u5b9e\u9645\u4e0a\u662f\u4e00\u7ec4iptables\uff08\u6700\u65b0\u7248\u672c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528IPVS\uff0c\u4f46\u8fd9\u662f\u53e6\u4e00\u4e2a\u8ba8\u8bba\uff09\u89c4\u5219\uff0c\u7531Kubernetes\u7ec4\u4ef6kube-proxy\u7ba1\u7406\u3002 \u8fd9\u4e2a\u540d\u5b57\u73b0\u5728\u5b9e\u9645\u4e0a\u662f\u8bef\u5bfc\u6027\u7684\u3002 \u5b83\u5728v 1.0\u4e4b\u524d\u786e\u5b9e\u662f\u4e00\u4e2a\u4ee3\u7406\uff0c\u5e76\u4e14\u7531\u4e8e\u5176\u5b9e\u73b0\u662f\u5185\u6838\u7a7a\u95f4\u548c\u7528\u6237\u7a7a\u95f4\u4e4b\u95f4\u7684\u4e0d\u65ad\u590d\u5236\uff0c\u5b83\u53d8\u5f97\u975e\u5e38\u8017\u8d39\u8d44\u6e90\u5e76\u4e14\u901f\u5ea6\u8f83\u6162\u3002 \u73b0\u5728\uff0c\u5b83\u53ea\u662f\u4e00\u4e2a\u63a7\u5236\u5668\uff0c\u5c31\u50cfKubernetes\u4e2d\u7684\u8bb8\u591a\u5176\u5b83\u63a7\u5236\u5668\u4e00\u6837\uff0c\u5b83watch api server\u7684endpoint\u7684\u66f4\u6539\u5e76\u76f8\u5e94\u5730\u66f4\u65b0iptables\u89c4\u5219\u3002<\/p>\n \u6709\u4e86\u8fd9\u4e9biptables\u89c4\u5219\uff0c\u6bcf\u5f53\u6570\u636e\u5305\u53d1\u5f80Service IP\u65f6\uff0c\u5b83\u5c31\u8fdb\u884cDNAT\uff08DNAT=\u76ee\u6807\u7f51\u7edc\u5730\u5740\u8f6c\u6362\uff09\u64cd\u4f5c\uff0c\u8fd9\u610f\u5473\u7740\u76ee\u6807IP\u4eceService IP\u66f4\u6539\u4e3a\u5176\u4e2d\u4e00\u4e2aEndpoint - Pod IP - \u7531iptables\u968f\u673a\u9009\u62e9\u3002\u8fd9\u53ef\u786e\u4fdd\u8d1f\u8f7d\u5747\u5300\u5206\u5e03\u5728\u540e\u7aefPod\u4e2d\u3002<\/p>\n <\/p>\n iptables DNAT<\/em><\/p>\n \u5f53\u8fd9\u4e2aDNAT\u53d1\u751f\u65f6\uff0c\u8fd9\u4e2a\u4fe1\u606f\u5b58\u50a8\u5728conntrack\u4e2d\u2014\u2014\u5b83\u662fLinux\u8fde\u63a5\u8ddf\u8e2a\u8868\uff08\u5b58\u50a8iptables\u5df2\u5b8c\u6210\u76845\u5143\u7ec4\u7ffb\u8bd1\uff1aprotocol\uff0csrcIP\uff0csrcPort\uff0cdstIP\uff0cdstPort\uff09\u3002 \u8fd9\u6837\u5f53\u8bf7\u6c42\u56de\u6765\u65f6\uff0c\u5b83\u53ef\u4ee5\u53d6\u6d88DNAT\uff0c\u8fd9\u610f\u5473\u7740\u5c06\u6e90IP\u4ecePod IP\u66f4\u6539\u4e3aService IP\u3002 \u8fd9\u6837\uff0c\u5ba2\u6237\u7aef\u5c31\u4e0d\u7528\u5173\u5fc3\u540e\u53f0\u5982\u4f55\u5904\u7406\u6570\u636e\u5305\u6d41\u3002<\/p>\n <\/p>\n conntrack\u8868\u4e2d\u76845\u5143\u7ec4\u5b9e\u4f8b<\/em><\/p>\n \u56e0\u6b64\u901a\u8fc7\u4f7f\u7528Kubernetes Service\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u7aef\u53e3\u800c\u4e0d\u4f1a\u53d1\u751f\u4efb\u4f55\u51b2\u7a81\uff08\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u5c06\u7aef\u53e3\u91cd\u65b0\u6620\u5c04\u5230Endpoint\uff09\u3002 \u8fd9\u4f7f\u670d\u52a1\u53d1\u73b0\u53d8\u5f97\u975e\u5e38\u5bb9\u6613\u3002 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5185\u90e8DNS\u5e76\u5bf9\u670d\u52a1\u4e3b\u673a\u540d\u8fdb\u884c\u786c\u7f16\u7801\u3002 \u6211\u4eec\u751a\u81f3\u53ef\u4ee5\u4f7f\u7528Kubernetes\u9884\u8bbe\u7684\u670d\u52a1\u4e3b\u673a\u548c\u7aef\u53e3\u73af\u5883\u53d8\u91cf\u3002<\/p>\n \u5c0f\u5efa\u8bae<\/strong>\uff1a\u91c7\u53d6\u7b2c\u4e8c\u79cd\u65b9\u6cd5\uff0c\u4f60\u53ef\u8282\u7701\u5927\u91cf\u4e0d\u5fc5\u8981\u7684DNS\u8c03\u7528\uff01<\/p>\n \u5230\u76ee\u524d\u4e3a\u6b62\u6211\u4eec\u8ba8\u8bba\u7684Kubernetes Service\u662f\u5728\u4e00\u4e2a\u96c6\u7fa4\u5185\u5de5\u4f5c\u3002\u4f46\u662f\uff0c\u5728\u5927\u591a\u6570\u5b9e\u9645\u60c5\u51b5\u4e2d\uff0c\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u8bbf\u95ee\u4e00\u4e9b\u5916\u90e8api \/\u7f51\u7ad9\u3002<\/p>\n \u901a\u5e38\uff0c\u8282\u70b9\u53ef\u4ee5\u540c\u65f6\u5177\u6709\u79c1\u6709IP\u548c\u516c\u5171IP\u3002\u5bf9\u4e8e\u4e92\u8054\u7f51\u8bbf\u95ee\uff0c\u8fd9\u4e9b\u516c\u5171\u548c\u79c1\u6709IP\u5b58\u5728\u67d0\u79cd1\uff1a1\u7684NAT\uff0c\u7279\u522b\u662f\u5728\u4e91\u73af\u5883\u4e2d\u3002<\/p>\n \u5bf9\u4e8e\u4ece\u8282\u70b9\u5230\u67d0\u4e9b\u5916\u90e8IP\u7684\u666e\u901a\u901a\u4fe1\uff0c\u6e90IP\u4ece\u8282\u70b9\u7684\u4e13\u7528IP\u66f4\u6539\u4e3a\u5176\u51fa\u7ad9\u6570\u636e\u5305\u7684\u516c\u5171IP\uff0c\u5165\u7ad9\u7684\u54cd\u5e94\u6570\u636e\u5305\u5219\u521a\u597d\u76f8\u53cd\u3002\u4f46\u662f\uff0c\u5f53Pod\u53d1\u51fa\u4e0e\u5916\u90e8IP\u7684\u8fde\u63a5\u65f6\uff0c\u6e90IP\u662fPod IP\uff0c\u4e91\u63d0\u4f9b\u5546\u7684NAT\u673a\u5236\u4e0d\u77e5\u9053\u8be5IP\u3002\u56e0\u6b64\u5b83\u5c06\u4e22\u5f03\u5177\u6709\u9664\u8282\u70b9IP\u4e4b\u5916\u7684\u6e90IP\u7684\u6570\u636e\u5305\u3002<\/p>\n \u56e0\u6b64\u4f60\u53ef\u80fd\u4e5f\u731c\u5bf9\u4e86\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u66f4\u591a\u7684iptables\uff01\u8fd9\u4e9b\u89c4\u5219\u4e5f\u7531kube-proxy\u6dfb\u52a0\uff0c\u6267\u884cSNAT\uff08\u6e90\u7f51\u7edc\u5730\u5740\u8f6c\u6362\uff09\uff0c\u5373IP MASQUERADE\u3002\u5b83\u544a\u8bc9\u5185\u6838\u4f7f\u7528\u6b64\u6570\u636e\u5305\u53d1\u51fa\u7684\u7f51\u7edc\u63a5\u53e3\u7684IP\uff0c\u4ee3\u66ff\u6e90Pod IP\u3002\u540c\u65f6\u4fdd\u7559conntrack\u6761\u76ee\u4ee5\u8fdb\u884c\u53cdSNAT\u64cd\u4f5c\u3002<\/p>\n \u5230\u76ee\u524d\u4e3a\u6b62\u4e00\u5207\u90fd\u5f88\u597d\u3002Pod\u53ef\u4ee5\u4e92\u76f8\u4ea4\u8c08\uff0c\u4e5f\u53ef\u4ee5\u8bbf\u95ee\u4e92\u8054\u7f51\u3002\u4f46\u6211\u4eec\u4ecd\u7136\u7f3a\u5c11\u5173\u952e\u90e8\u5206 - \u4e3a\u7528\u6237\u8bf7\u6c42\u6d41\u91cf\u63d0\u4f9b\u670d\u52a1\u3002\u622a\u81f3\u76ee\u524d\uff0c\u6709\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\uff1a<\/p>\n \u5c06\u670d\u52a1\u7c7b\u578b\u8bbe\u7f6e\u4e3a \u4e91\u73af\u5883\u4e2d\u7684LoadBalancer\u670d\u52a1\u7c7b\u578b\u5c06\u5728\u6240\u6709\u8282\u70b9\u4e4b\u524d\u521b\u5efa\u4e91\u8d1f\u8f7d\u5747\u8861\u5668\uff08\u4f8b\u5982ELB\uff09\uff0c\u547d\u4e2d\u76f8\u540c\u7684nodePort\u3002<\/p>\n \u8bb8\u591a\u4e0d\u540c\u7684\u5de5\u5177\uff0c\u5982Nginx\uff0cTraefik\uff0cHAProxy\u7b49\uff0c\u4fdd\u7559\u4e86http\u4e3b\u673a\u540d\/\u8def\u5f84\u548c\u5404\u81ea\u540e\u7aef\u7684\u6620\u5c04\u3002\u901a\u5e38\u8fd9\u662f\u57fa\u4e8e\u8d1f\u8f7d\u5747\u8861\u5668\u548cnodePort\u7684\u6d41\u91cf\u5165\u53e3\u70b9\uff0c\u5176\u4f18\u70b9\u662f\u6211\u4eec\u53ef\u4ee5\u6709\u4e00\u4e2a\u5165\u53e3\u5904\u7406\u6240\u6709\u670d\u52a1\u7684\u5165\u7ad9\u6d41\u91cf\uff0c\u800c\u4e0d\u9700\u8981\u591a\u4e2anodePort\u548c\u8d1f\u8f7d\u5747\u8861\u5668\u3002<\/p>\n \u53ef\u4ee5\u628a\u5b83\u60f3\u8c61\u4e3aPod\u7684\u5b89\u5168\u7ec4\/ ACL\u3002 NetworkPolicy\u89c4\u5219\u5141\u8bb8\/\u62d2\u7edd\u8de8Pod\u7684\u6d41\u91cf\u3002\u786e\u5207\u7684\u5b9e\u73b0\u53d6\u51b3\u4e8e\u7f51\u7edc\u5c42\/CNI\uff0c\u4f46\u5927\u591a\u6570\u53ea\u4f7f\u7528iptables\u3002<\/p>\n \u76ee\u524d\u4e3a\u6b62\u5c31\u8fd9\u6837\u4e86\u3002 \u5728\u524d\u9762<\/a>\u7684\u90e8\u5206<\/a>\u4e2d\uff0c\u6211\u4eec\u7814\u7a76\u4e86Kubernetes\u7f51\u7edc\u7684\u57fa\u7840\u4ee5\u53caoverlay\u7f51\u7edc\u7684\u5de5\u4f5c\u539f\u7406\u3002 \u73b0\u5728\u6211\u4eec\u77e5\u9053Service\u62bd\u8c61\u662f\u5982\u4f55\u5728\u4e00\u4e2a\u52a8\u6001\u96c6\u7fa4\u5185\u8d77\u4f5c\u7528\u5e76\u4f7f\u670d\u52a1\u53d1\u73b0\u53d8\u5f97\u975e\u5e38\u5bb9\u6613\u3002\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86\u51fa\u7ad9\u548c\u5165\u7ad9\u6d41\u91cf\u7684\u5de5\u4f5c\u539f\u7406\u4ee5\u53ca\u7f51\u7edc\u7b56\u7565\u5982\u4f55\u5bf9\u7fa4\u96c6\u5185\u7684\u5b89\u5168\u6027\u8d77\u4f5c\u7528\u3002<\/p>\n \u539f\u6587\u94fe\u63a5\uff1aAn illustrated guide to Kubernetes Networking [Part 3]<\/a>\uff08\u7ffb\u8bd1\uff1a\u6c60\u5251\u950b\uff09<\/strong><\/p>\n\u96c6\u7fa4\u52a8\u529b\u5b66<\/h3>\n
\u51fa\u7ad9\u6d41\u91cf<\/h3>\n
\u5165\u7ad9\u6d41\u91cf<\/h3>\n
NodePort \/\u4e91\u8d1f\u8f7d\u5747\u8861\u5668\uff08L4 - IP\u548c\u7aef\u53e3\uff09<\/h3>\n
NodePort<\/code>\u5c06\u4f1a\u4e3a\u670d\u52a1\u5206\u914d\u8303\u56f4\u4e3a
30000-33000<\/code>\u7684
nodePort<\/code>\u3002\u5373\u4f7f\u5728\u7279\u5b9a\u8282\u70b9\u4e0a\u6ca1\u6709\u8fd0\u884cPod\uff0c\u6b64
nodePort<\/code>\u4e5f\u4f1a\u5728\u6bcf\u4e2a\u8282\u70b9\u4e0a\u6253\u5f00\u3002\u6b64NodePort\u4e0a\u7684\u5165\u7ad9\u6d41\u91cf\u5c06\u518d\u6b21\u4f7f\u7528iptables\u53d1\u9001\u5230\u5176\u4e2d\u4e00\u4e2aPod\uff08\u8be5Pod\u751a\u81f3\u53ef\u80fd\u5728\u5176\u5b83\u8282\u70b9\u4e0a\uff01\uff09\u3002<\/p>\n
Ingress\uff08L7 - HTTP \/ TCP\uff09<\/h3>\n
\u7f51\u7edc\u7b56\u7565<\/h3>\n