{"id":3587,"date":"2022-03-04T21:29:56","date_gmt":"2022-03-04T13:29:56","guid":{"rendered":"\/?p=3587"},"modified":"2022-03-07T21:30:31","modified_gmt":"2022-03-07T13:30:31","slug":"19-k8s%e7%9a%84networkpolicy","status":"publish","type":"post","link":"http:\/\/xinblog.ltd\/?p=3587","title":{"rendered":"19.K8S\u7684NetworkPolicy"},"content":{"rendered":"<p>NetworkPolicy<\/p>\n<p>\u6307\u7684\u662f\u8bbe\u5b9aPod\u4e4b\u95f4\u7684\u7f51\u7edc\u9694\u79bb\u7b56\u7565,\u9ed8\u8ba4\u662f\u6240\u6709\u4e92\u901a,\u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e,\u7ed9\u4e0e\u4e00\u4e9b\u767d\u540d\u5355,\u89c4\u5b9a\u767d\u540d\u5355\u4e4b\u5916\u7684\u7f51\u7edc\u8bf7\u6c42\u90fd\u662f\u4e0d\u53ef\u53d6\u7684<\/p>\n<p>\u53ef\u4ee5\u8bbe\u7f6e\u7684\u5c42\u5ea6\u6709<\/p>\n<p>\u5141\u8bb8\u7684Pods,\u89c4\u5b9a\u67d0\u4e9bPods\u662f\u5426\u53ef\u4ee5\u88ab\u8bbf\u95ee\u6216\u8005\u53ef\u4ee5\u8bbf\u95ee<\/p>\n<p>\u67d0\u4e9bnamespaces,\u90a3\u4e9bnamespace\u53ef\u4ee5\u8bbf\u95ee\u6216\u8005\u4e0d\u53ef\u4ee5\u8bbf\u95ee<\/p>\n<p>IP\u7ec4\u4ef6,\u8bbe\u7f6eCIDR,\u6765\u63a7\u5236\u901a\u4fe1<\/p>\n<p>NetworkPolicy\u7684\u76f8\u5173\u5bf9\u8c61\u5982\u4e0b<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"1076\" height=\"19\" class=\"wp-image-3588\" src=\"\/wp-content\/uploads\/2022\/03\/unnamed-file.png\" alt=\"\u56fe\u7247\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2022\/03\/unnamed-file.png 1076w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2022\/03\/unnamed-file-300x5.png 300w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2022\/03\/unnamed-file-1024x18.png 1024w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2022\/03\/unnamed-file-768x14.png 768w\" sizes=\"(max-width: 1076px) 100vw, 1076px\" \/><\/p>\n<p>\u90a3\u4e48,\u6211\u4eec\u4f7f\u7528explain\u6765\u67e5\u770b\u4e00\u4e0b\u5bf9\u8c61\u4e2d\u53ef\u9009\u7684\u5b57\u6bb5<\/p>\n<p>kubectl explain netpol.spec<\/p>\n<table>\n<tbody>\n<tr>\n<td>KIND:\u00a0\u00a0\u00a0\u00a0 NetworkPolicy<\/p>\n<p>VERSION:\u00a0 networking.k8s.io\/v1<\/p>\n<p>RESOURCE: spec &lt;Object&gt;<\/p>\n<p>DESCRIPTION:<\/p>\n<p>Specification of the desired behavior for this NetworkPolicy.<\/p>\n<p>NetworkPolicySpec provides the specification of a NetworkPolicy<\/p>\n<p>FIELDS:<\/p>\n<p>egress\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;[]Object&gt;<\/p>\n<p>List of egress rules to be applied to the selected pods. Outgoing traffic<\/p>\n<p>is allowed if there are no NetworkPolicies selecting the pod (and cluster<\/p>\n<p>policy otherwise allows the traffic), OR if the traffic matches at least<\/p>\n<p>one egress rule across all of the NetworkPolicy objects whose podSelector<\/p>\n<p>matches the pod. If this field is empty then this NetworkPolicy limits all<\/p>\n<p>outgoing traffic (and serves solely to ensure that the pods it selects are<\/p>\n<p>isolated by default). This field is beta-level in 1.8<\/p>\n<p>ingress\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;[]Object&gt;<\/p>\n<p>List of ingress rules to be applied to the selected pods. Traffic is<\/p>\n<p>allowed to a pod if there are no NetworkPolicies selecting the pod (and<\/p>\n<p>cluster policy otherwise allows the traffic), OR if the traffic source is<\/p>\n<p>the pod&#8217;s local node, OR if the traffic matches at least one ingress rule<\/p>\n<p>across all of the NetworkPolicy objects whose podSelector matches the pod.<\/p>\n<p>If this field is empty then this NetworkPolicy does not allow any traffic<\/p>\n<p>(and serves solely to ensure that the pods it selects are isolated by<\/p>\n<p>default)<\/p>\n<p>podSelector\u00a0 &lt;Object&gt; -required-<\/p>\n<p>Selects the pods to which this NetworkPolicy object applies. The array of<\/p>\n<p>ingress rules is applied to any pods selected by this field. Multiple<\/p>\n<p>network policies can select the same set of pods. In this case, the ingress<\/p>\n<p>rules for each are combined additively. This field is NOT optional and<\/p>\n<p>follows standard label selector semantics. An empty podSelector matches all<\/p>\n<p>pods in this namespace.<\/p>\n<p>policyTypes\u00a0 &lt;[]string&gt;<\/p>\n<p>List of rule types that the NetworkPolicy relates to. Valid options are<\/p>\n<p>[&#8220;Ingress&#8221;], [&#8220;Egress&#8221;], or [&#8220;Ingress&#8221;, &#8220;Egress&#8221;]. If this field is not<\/p>\n<p>specified, it will default based on the existence of Ingress or Egress<\/p>\n<p>rules; policies that contain an Egress section are assumed to affect<\/p>\n<p>Egress, and all policies (whether or not they contain an Ingress section)<\/p>\n<p>are assumed to affect Ingress. If you want to write an egress-only policy,<\/p>\n<p>you must explicitly specify policyTypes [ &#8220;Egress&#8221; ]. Likewise, if you want<\/p>\n<p>to write a policy that specifies that no egress is allowed, you must<\/p>\n<p>specify a policyTypes value that include &#8220;Egress&#8221; (since such a policy<\/p>\n<p>would not include an Egress section and would otherwise default to just [<\/p>\n<p>&#8220;Ingress&#8221; ]). This field is beta-level in 1.8<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5176\u4e2d\u7684\u9996\u8981\u5b57\u6bb5\u662fpodSelector,\u8fd9\u662f\u4e00\u4e2a\u6807\u51c6\u7684PodSelector\u683c\u5f0f,\u53ea\u8981\u88ab\u8fd9\u4e2a\u9009\u62e9\u5668\u9009\u62e9\u4e86\u7684Pod,\u9ed8\u8ba4\u7f51\u7edc\u90fd\u5c06\u88ab\u5c01\u95ed,\u8fd9\u662f\u4e0d\u914d\u7f6eIngress\u548cEngress\u7684\u60c5\u51b5<\/p>\n<p>\u5176\u6b21\u662f\u5173\u4e8eIngress\u548cEngress,\u4e8c\u8005\u90fd\u662f\u6570\u7ec4,\u4ee3\u8868\u4e86\u5165\u65b9\u5411\u548c\u51fa\u65b9\u5411\u7684\u89c4\u5219<\/p>\n<p>\u6211\u4eec\u9996\u5148\u6765\u770b\u770bIngress\u7684\u4e66\u5199\u65b9\u5f0f<\/p>\n<p>Kubectl explain netpol.spec.ingress<\/p>\n<p>\u4f1a\u88ab\u544a\u77e5\u5305\u542b\u4e24\u4e2a\u5b57\u6bb5,\u5206\u522b\u662ffrom\u548cport<\/p>\n<p>\u5176\u4e2dfrom\u4ecd\u7136\u662f\u4e00\u4e2a\u5bf9\u8c61\u6570\u7ec4,\u6211\u4eec\u7ee7\u7eed\u6700\u7ec8\u4e0b\u53bb\u67e5\u770b<\/p>\n<p>\u5176\u4e2d\u6709\u4e09\u79cd\u7c7b\u578b,\u5206\u522b\u662fipBlock,namespaceSelector,podSelector<\/p>\n<p>IpBlock \u662f\u6307\u7684ip\u6765\u6e90,\u53ef\u4ee5\u8bbe\u7f6e\u8303\u56f4,\u548cexcept\u8868\u793a\u8303\u56f4\u5185\u4e0d\u88ab\u5141\u8bb8\u7684<\/p>\n<p>NamespaceSelector\u53ef\u4ee5\u5339\u914dnamespace\u7684\u6807\u7b7e,\u4ece\u800c\u8bbe\u7f6e\u5141\u8bb8\u7684namespace<\/p>\n<p>PodSelector,\u4e5f\u662f\u548c\u4e0a\u9762\u4e00\u76f4\u7684\u9009\u62e9\u5668,\u8303\u56f4\u4e3aPod<\/p>\n<p>\u5176\u6b21\u662fport,\u56fa\u5b9a\u4e86\u4e0a\u9762\u8bbe\u7f6e\u7684\u767d\u540d\u5355\u53ef\u4ee5\u8bbf\u95ee\u90a3\u4e9bport,\u4e0d\u5199\u9ed8\u8ba4\u4e0d\u9650\u5236<\/p>\n<p>\u5bf9\u5e94\u7684from\u5219\u662f\u76f8\u53cd,\u6307\u7684\u662f\u51fa\u9632\u7ebf\u7684\u89c4\u5219,\u914d\u7f6e\u9879\u4e00\u6837<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u7ed9\u51fa\u4e0b\u9762\u7684\u4e00\u4e2a\u4f8b\u5b50<\/p>\n<table>\n<tbody>\n<tr>\n<td>apiVersion: networking.k8s.io\/v1<\/p>\n<p>kind: NetworkPolicy<\/p>\n<p>metadata:<\/p>\n<p>name: test-network-policy<\/p>\n<p>namespace: default<\/p>\n<p>spec:<\/p>\n<p>podSelector: \u00a0## \u9009\u4e2d\u6307\u5b9aPod<\/p>\n<p>matchLabels:<\/p>\n<p>role: default-backend<\/p>\n<p>policyTypes: \u00a0## \u5b9a\u4e49\u4e0a\u9762Pod\u7684\u5165\u7ad9\u51fa\u7ad9\u89c4\u5219<\/p>\n<p>&#8211; Ingress<\/p>\n<p>&#8211; Egress<\/p>\n<p>ingress: \u00a0 \u00a0## \u5b9a\u4e49\u5165\u7ad9\u767d\u540d\u5355<\/p>\n<p>&#8211; from:<\/p>\n<p>&#8211; ipBlock:<\/p>\n<p>cidr: 192.168.0.0\/16<\/p>\n<p>except:<\/p>\n<p>&#8211; 192.168.10.0\/16<\/p>\n<p>&#8211; namespaceSelector:<\/p>\n<p>matchLabels:<\/p>\n<p>project: dev<\/p>\n<p>&#8211; podSelector:<\/p>\n<p>matchLabels:<\/p>\n<p>role: default-frontend<\/p>\n<p>ports:<\/p>\n<p>&#8211; protocol: TCP<\/p>\n<p>port: 80<\/p>\n<p>egress: \u00a0## \u5b9a\u4e49\u51fa\u7ad9\u767d\u540d\u5355<\/p>\n<p>&#8211; to:<\/p>\n<p>&#8211; ipBlock:<\/p>\n<p>cidr: 192.168.0.0\/16<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e0a\u9762\u7ed9\u51fa\u4e86\u4e00\u4e2a\u5177\u6709\u5927\u591a\u6570\u58f0\u660e\u7684\u4f8b\u5b50<\/p>\n<p>\u9700\u8981\u6ce8\u610f,\u56e0\u4e3aIngress\u548cEngress\u90fd\u58f0\u660e\u9700\u8981\u4f20\u5165\u4e00\u4e2a\u6570\u7ec4<\/p>\n<p>\u90a3\u4e48\u6211\u4eec\u7684\u7ec4\u5408\u65b9\u5f0f\u5c31\u5f88\u91cd\u8981\u4e86<\/p>\n<p>\u4e0b\u9762\u7684\u7ec4\u5408\u662f\u4e00\u4e2a\u6807\u51c6\u7684\u7ec4\u5408<\/p>\n<table>\n<tbody>\n<tr>\n<td>\u00a0 ingress:<\/p>\n<p>&#8211; from:<\/p>\n<p>&#8211; namespaceSelector:<\/p>\n<p>matchLabels:<\/p>\n<p>user: alice<\/p>\n<p>&#8211; podSelector:<\/p>\n<p>matchLabels:<\/p>\n<p>role: client<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e0a\u9762\u7684\u7ec4\u5408\u4e3a<\/p>\n<p>\u5e26\u6709user=alice\u6807\u7b7e\u7684namesapce\u4e2d\u7684\u5bf9\u8c61\u548c\u5e26\u6709role=client\u7684\u672cnamesapce\u7684pod\u90fd\u53ef\u4ee5\u8bbf\u95ee<\/p>\n<p>\u4e0b\u9762\u7684\u7ec4\u5408\u5219\u8868\u793a\u5fc5\u987b\u8981\u662fuser=alice\u7684namespace\u4e0b,\u5e26role=client\u6807\u7b7e\u7684pod\u624d\u53ef\u4ee5\u8bbf\u95ee<\/p>\n<table>\n<tbody>\n<tr>\n<td>\u00a0 ingress:<\/p>\n<p>&#8211; from:<\/p>\n<p>&#8211; namespaceSelector:<\/p>\n<p>matchLabels:<\/p>\n<p>user: alice<\/p>\n<p>podSelector:<\/p>\n<p>matchLabels:<\/p>\n<p>role: client<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u524d\u8005\u53d6\u5f97\u662f\u4ea4\u96c6\u5173\u7cfb,\u540e\u8005\u53d6\u7684\u65f6\u5e76\u96c6\u5173\u7cfb<\/p>\n","protected":false},"excerpt":{"rendered":"<p>NetworkPolicy \u6307 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"_links":{"self":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/3587"}],"collection":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3587"}],"version-history":[{"count":0,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/3587\/revisions"}],"wp:attachment":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3587"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}