{"id":30583,"date":"2022-07-13T21:01:58","date_gmt":"2022-07-13T21:01:58","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=30583"},"modified":"2025-10-09T06:44:48","modified_gmt":"2025-10-09T06:44:48","slug":"kubernetes-cks-network-policy-example-code","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/kubernetes-cks-network-policy-example-code\/","title":{"rendered":"Kubernetes Tutorials: What is Network Policy &amp; Example Code"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>If you want to control traffic flow at the IP address or port level (OSI layer 3 or 4), then you might consider using Kubernetes NetworkPolicies for particular applications in your cluster. NetworkPolicies are an application-centric construct which allow you to specify how a&nbsp;<a href=\"https:\/\/kubernetes.io\/docs\/concepts\/workloads\/pods\/\" target=\"_blank\" rel=\"noreferrer noopener\">pod<\/a>&nbsp;is allowed to communicate with various network &#8220;entities&#8221; (we use the word &#8220;entity&#8221; here to avoid overloading the more common terms such as &#8220;endpoints&#8221; and &#8220;services&#8221;, which have specific Kubernetes connotations) over the network. NetworkPolicies apply to a connection with a pod on one or both ends, and are not relevant to other connections.<\/p>\n\n\n\n<p>The entities that a Pod can communicate with are identified through a combination of the following 3 identifiers:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Other pods that are allowed (exception: a pod cannot block access to itself)<\/li><li>Namespaces that are allowed<\/li><li>IP blocks (exception: traffic to and from the node where a Pod is running is always allowed, regardless of the IP address of the Pod or the node)<\/li><\/ol>\n\n\n\n<p>When defining a pod- or namespace- based NetworkPolicy, you use a&nbsp;<a href=\"https:\/\/kubernetes.io\/docs\/concepts\/overview\/working-with-objects\/labels\/\" target=\"_blank\" rel=\"noreferrer noopener\">selector<\/a>&nbsp;to specify what traffic is allowed to and from the Pod(s) that match the selector.<\/p>\n\n\n\n<p>Meanwhile, when IP based NetworkPolicies are created, we define policies based on IP blocks (CIDR ranges).<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Sample Code &#8211; https:\/\/github.com\/devopsschool-sample-programs\/kubernetes-network-policy-recipes<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"549\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/kubernetes-network-policy-1-1024x549.png\" alt=\"\" class=\"wp-image-30600\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/kubernetes-network-policy-1-1024x549.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/kubernetes-network-policy-1-300x161.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/kubernetes-network-policy-1-768x412.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/kubernetes-network-policy-1.png 1518w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"519\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/image-1024x519.png\" alt=\"\" class=\"wp-image-30601\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/image-1024x519.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/image-300x152.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/image-768x389.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/07\/image.png 1375w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-use-cases\">Example Use Cases<\/h3>\n\n\n\n<p>Below is a brief list of common use cases for Network Policies. You can find more use case examples with sample manifests at the&nbsp;<a href=\"https:\/\/github.com\/ahmetb\/kubernetes-networkpolicy-tutorial\" target=\"_blank\" rel=\"noopener\">kubernetes-networkpolicy-tutorial<\/a>&nbsp;on GitHub.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/github.com\/ahmetb\/kubernetes-networkpolicy-tutorial\/blob\/master\/01-deny-all-traffic-to-an-application.md\" target=\"_blank\" rel=\"noopener\">DENY all traffic to an application<\/a><img decoding=\"async\" src=\"https:\/\/ahmet.im\/blog\/images\/2017\/08\/networkpolicy\/1.gif\" alt=\"\"><\/li><li><a href=\"https:\/\/github.com\/ahmetb\/kubernetes-networkpolicy-tutorial\/blob\/master\/02-limit-traffic-to-an-application.md\" target=\"_blank\" rel=\"noopener\">LIMIT traffic to an application<\/a><img decoding=\"async\" src=\"https:\/\/ahmet.im\/blog\/images\/2017\/08\/networkpolicy\/2.gif\" alt=\"\"><\/li><li><a href=\"https:\/\/github.com\/ahmetb\/kubernetes-networkpolicy-tutorial\/blob\/master\/03-deny-all-non-whitelisted-traffic-in-the-namespace.md\" target=\"_blank\" rel=\"noopener\">DENY all non-whitelisted traffic in a namespace<\/a><img decoding=\"async\" src=\"https:\/\/ahmet.im\/blog\/images\/2017\/08\/networkpolicy\/3.gif\" alt=\"\"><\/li><li><a href=\"https:\/\/github.com\/ahmetb\/kubernetes-networkpolicy-tutorial\/blob\/master\/04-deny-traffic-from-other-namespaces.md\" target=\"_blank\" rel=\"noopener\">DENY all traffic from other namespaces<\/a><img decoding=\"async\" src=\"https:\/\/ahmet.im\/blog\/images\/2017\/08\/networkpolicy\/4.gif\" alt=\"\"><\/li><li><a href=\"https:\/\/github.com\/ahmetb\/kubernetes-networkpolicy-tutorial\/blob\/master\/06-allow-traffic-from-a-namespace.md\" target=\"_blank\" rel=\"noopener\">ALLOW traffic from other namespaces<\/a><img decoding=\"async\" src=\"https:\/\/ahmet.im\/blog\/images\/2017\/08\/networkpolicy\/5.gif\" alt=\"\"><\/li><li><a href=\"https:\/\/github.com\/ahmetb\/kubernetes-networkpolicy-tutorial\/blob\/master\/08-allow-external-traffic.md\" target=\"_blank\" rel=\"noopener\">ALLOW traffic from external clients<\/a><img decoding=\"async\" src=\"https:\/\/ahmet.im\/blog\/images\/2017\/08\/networkpolicy\/8.gif\" alt=\"\"><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/3c5de103195d04b9a00920c5ef2ed8c9.js\"><\/script>\n","protected":false},"excerpt":{"rendered":"<p>If you want to control traffic flow at the IP address or port level (OSI layer 3 or 4), then you might consider using Kubernetes NetworkPolicies for particular applications in&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[4859],"tags":[],"class_list":["post-30583","post","type-post","status-publish","format-standard","hentry","category-kubernetes"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30583","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=30583"}],"version-history":[{"count":4,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30583\/revisions"}],"predecessor-version":[{"id":53840,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30583\/revisions\/53840"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=30583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=30583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=30583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}