%3CLINGO-SUB%20id%3D%22lingo-sub-1463923%22%20slang%3D%22en-US%22%3ENetwork%20Policy%20in%20Kubernetes%20using%20Calico%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1463923%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3EIntroduction%20to%20the%20problem%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EAll%20pods%20in%20Kubernetes%20can%20reach%20each%20other.%20For%20example%2C%20the%20frontend%20can%20reach%20the%20backend%20and%20the%20backend%20can%20reach%20the%20database.%20That%20is%20expected%20and%20normal.%20But%20this%20openness%20can%20make%20problems%20like%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSPAN%3EThe%20frontend%20can%20reach%20the%20database!%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3EApplications%20from%20different%20namespaces%20can%20talk%20to%20each%20other!%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3EThis%20means%2C%20if%20a%20container%20gets%20hacked%2C%20it%20will%20have%20access%20to%20all%20the%20containers!%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSPAN%3EThat%20would%20be%20a%20security%20issue.%20We%20want%20to%20apply%20%3CSTRONG%3Ethe%20principle%20of%20least%20privilege%20from%20The%20Zero%20Trust%20Network%20model%3C%2FSTRONG%3E%3A%20each%20pod%20should%20get%20access%20to%20the%20strict%20minimum%20and%20only%20the%20pods%20it%20is%20supposed%20to%20access%20to.%20If%20it%20doesn%E2%80%99t%20need%20access%20to%20other%20pods%2C%20there%20should%20be%20a%20rule%20to%20deny%20that%20possible%20access.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIn%20this%20workshop%2C%20we%20will%20learn%20how%20to%20setup%20network%20policies%20in%20Kubernetes%20to%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSPAN%3EDeny%20pods%20to%20reach%20pods%20within%20all%20other%20namespaces.%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3EDeny%20pods%20to%20reach%20any%20pods%20except%20specific%20ones.%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fkubernetes.io%2Fdocs%2Fconcepts%2Fservices-networking%2Fnetwork-policies%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3ENetworkPolicy%3C%2FA%3E%20is%20defined%20in%20Kubernetes%20API%20as%20a%20specification.%20But%20it%20is%20not%20implemented.%20It%20is%20up%20to%20us%20to%20choose%20the%20implementation.%20There%20are%20multiple%20solutions%20available%2C%20like%20Calico%2C%20Azure%20Network%20Policy%2C%20Weave%2C%20Cilium%2C%20Romana%2C%20Kube-router%E2%80%A6%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThis%20workshop%20is%20also%20available%20as%20%3CA%20href%3D%22https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D1TsORarOEXc%26amp%3Blist%3DPLpbcUe4chE79sB7Jg7B4z3HytqUUEwcNE%26amp%3Bindex%3D23%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ea%20video%20on%20youtube%3C%2FA%3E%3A%20%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22calico.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F198889i7D3CFB709C12A9B7%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22calico.png%22%20alt%3D%22calico.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EWe%20need%20to%20choose%20one%20for%20this%20demo%2C%20so%20we%E2%80%99ll%20go%20for%20Calico%20as%20it%20is%20one%20of%20the%20widely%20used.%20But%20we%20can%20still%20choose%20another%20implementation%20and%20the%20demo%20still%20applicable.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThen%20we%20need%20to%20have%20a%20Kubernetes%20cluster.%20We%20can%20choose%20between%20Minikube%2C%20GKE%2C%20EKS%2C%20PKS%20and%20all%20things%20xKy.%20We%E2%80%99ll%20choose%20here%20AKS%20(Azure%20Kubernetes%20Service).%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIn%20AKS%2C%20we%20can%20choose%20between%20Calico%20and%20Azure%20Network%20Policy.%20Until%20now%2C%20Calico%20could%20be%20enabled%20only%20on%20new%20clusters%2C%20not%20existing%20ones.%20Follow%20the%20following%20instructions%20to%20create%20a%20new%20AKS%20cluster%20and%20enable%20Calico.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E1.%20Enabling%20Calico%20in%20AKS%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EEnabling%20Calico%20from%20the%20Azure%20Portal%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSPAN%3EWhen%20we%20create%20a%20new%20AKS%20cluster%20in%20the%20Azure%20Portal%2C%20under%20the%20Networking%20tab%2C%20we%E2%80%99ll%20have%20the%20option%20to%20select%20Calico.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22calico-azure.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F198890i4A0F4E4FD5E82C74%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22calico-azure.png%22%20alt%3D%22calico-azure.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EEnabling%20Calico%20from%20the%20Azure%20CLI%3A%20az%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSPAN%3EFrom%20the%20az%20command%20line%2C%20when%20we%20create%20a%20new%20AKS%20cluster%2C%20we%20can%20add%20the%20parameter%20%E2%80%93network-policy.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%20az%20aks%20create%20--resource-group%20%3CRG%3E%20--name%20%3CNAME%3E%20--network-policy%20calico%20%3C%2FNAME%3E%3C%2FRG%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EEnabling%20Calico%20from%20Terraform%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSPAN%3EIn%20Terraform%2C%20we%20can%20add%20the%20network_policy%20with%20value%20set%20to%20%E2%80%9Ccalico%E2%80%9D%20inside%20%E2%80%9Cazurerm_kubernetes_cluster%E2%80%9D%20as%20described%20in%20the%20following%20link%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fwww.terraform.io%2Fdocs%2Fproviders%2Fazurerm%2Fr%2Fkubernetes_cluster.html%23network_policy%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.terraform.io%2Fdocs%2Fproviders%2Fazurerm%2Fr%2Fkubernetes_cluster.html%23network_policy%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%26nbsp%3B%20network_profile%26nbsp%3B%7B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bnetwork_plugin%26nbsp%3B%3D%26nbsp%3B%22kubenet%22%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bnetwork_policy%26nbsp%3B%3D%26nbsp%3B%22calico%22%0A%26nbsp%3B%26nbsp%3B%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIn%20the%20following%20scenarios%2C%20we%E2%80%99ll%20define%20a%20network%20policy%20and%20we%E2%80%99ll%20test%20how%20it%20works.%20So%2C%20lets%20create%20a%20Nginx%20Pod%20that%20will%20act%20as%20the%20backend%20for%20the%20application.%20Then%20we%E2%80%99ll%20simulate%20an%20Alpine%20Pod%20that%20acts%20as%20the%20frontend%20who%20tries%20to%20reach%20the%20backend.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EFirst%2C%20let%E2%80%99s%20create%20a%20namespace%20with%20labels.%20Create%20a%20new%20file%20with%20the%20following%20content%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%23%26nbsp%3B1-namespace-development.yaml%0AapiVersion%3A%26nbsp%3Bv1%0Akind%3A%26nbsp%3BNamespace%0Ametadata%3A%0A%26nbsp%3B%26nbsp%3Bname%3A%26nbsp%3Bdevelopment%0A%26nbsp%3B%26nbsp%3Blabels%3A%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bpurpose%3A%26nbsp%3Bdevelopment%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThen%20create%20the%20namespace%20with%20the%20following%20command%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%26nbsp%3Bkubectl%20create%20-f%201-namespace-development.yaml%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EAfter%20that%2C%20create%20the%20backend%20Nginx%20Pod%20with%20a%20Service%20with%20the%20following%20command%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3Ekubectl%20run%20backend%20--image%3Dnginx%20--labels%20app%3Dwebapp%2Crole%3Dbackend%20--namespace%20development%20--expose%20--port%2080%20--generator%3Drun-pod%2Fv1%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThis%20command%20will%20generate%20a%20Pod%20and%20Service%20like%20the%20following%20config%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FHoussemDellai%2FNetworkPolicy-Calico%2Fblob%2Fmaster%2F1-pod-svc-nginx-backend.yaml%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%3C%2FA%3E%3C%2FSPAN%3E.%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EScenario%201%3A%20Deny%20inbound%20traffic%20from%20all%20Pods%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ELet%E2%80%99s%20create%20a%20policy%20to%20deny%20access%20to%20our%20backend%20Pod.%20%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%23%26nbsp%3B1-network-policy-deny-all.yaml%0Akind%3A%26nbsp%3BNetworkPolicy%0AapiVersion%3A%26nbsp%3Bnetworking.k8s.io%2Fv1%0Ametadata%3A%0A%26nbsp%3B%26nbsp%3Bname%3A%26nbsp%3Bbackend-policy%0A%26nbsp%3B%26nbsp%3Bnamespace%3A%26nbsp%3Bdevelopment%0Aspec%3A%0A%26nbsp%3B%26nbsp%3BpodSelector%3A%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BmatchLabels%3A%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bapp%3A%26nbsp%3Bwebapp%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Brole%3A%26nbsp%3Bbackend%0A%26nbsp%3B%26nbsp%3Bingress%3A%26nbsp%3B%5B%5D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CSPAN%3ENote%3A%3C%2FSPAN%3E%3C%2FSTRONG%3E%3CSPAN%3E%20we%20are%20not%20referencing%20the%20Pod%20with%20its%20name%2C%20but%20with%20its%20labels.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThen%20we%20apply%20this%20policy%20into%20Kubernetes%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%26nbsp%3Bkubectl%20apply%20-f%201-network-policy-deny-all.yaml%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EWe%20create%20and%20run%20an%20Alpine%20Pod%20in%20interactive%20mode%20(-it)%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3Ekubectl%20run%20--rm%20-it%20--image%3Dalpine%20network-policy%20--namespace%20development%20--generator%3Drun-pod%2Fv1%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20command%20will%20give%20us%20access%20to%20run%20a%20command%20within%20the%20alpine%20pod.%20We%20try%20to%20connect%20to%20the%20backend%20pod%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%26nbsp%3Bwget%20-qO-%20--timeout%3D2%20http%3A%2F%2Fbackend%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20output%20would%20look%20like%20the%20following%20screenshot%2C%20where%20we%20see%20the%20access%20fails%20(%E2%80%98wget%3A%20download%20timed%20out%E2%80%99)%20as%20we%20denied%20access.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3Ekubectl%20apply%20-f%201-network-policy-deny-all.yaml%0Anetworkpolicy.networking.k8s.io%2Fbackend-policy%20configured%0Akubectl%20run%20--rm%20-it%20--image%3Dalpine%20network-policy%20--namespace%20development%20--generator%3Drun-pod%2Fv1%0AIf%20you%20don't%20see%20a%20command%20prompt%2C%20try%20pressing%20enter.%0A%2F%20%23%20wget%20-qO-%20--timeout%3D2%20http%3A%2F%2Fbackend%0Awget%3A%20download%20timed%20out%0A%2F%20%23%20exit%0ASession%20ended%2C%20resume%20using%20'kubectl%20attach%20network-policy%20-c%20network-policy%20-i%20-t'%20command%20when%20the%20pod%20is%20running%0Apod%20%22network-policy%22%20deleted%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAt%20the%20end%2C%20we%20type%20%E2%80%98exit%E2%80%99%20to%20exit%20the%20current%20alpine%20pod.%20That%20will%20delete%20the%20pod%20from%20the%20cluster.%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EScenario%202%3A%20Allow%20inbound%20traffic%20from%20pods%20with%20matching%20labels%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3ENow%20let%E2%80%99s%20update%20the%20policy%20to%20allow%20access%20to%20the%20backend%20pod%20to%20only%20specific%20pods%20with%20matching%20labels.%20Note%20here%20how%20we%20are%20accepting%20inbound%20(ingress)%20traffic%20from%20any%20Pod%20with%20specific%20labels%2C%20in%20any%20namespace.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%23%26nbsp%3B2-network-policy-allow-pod.yaml%0Akind%3A%26nbsp%3BNetworkPolicy%0AapiVersion%3A%26nbsp%3Bnetworking.k8s.io%2Fv1%0Ametadata%3A%0A%26nbsp%3B%26nbsp%3Bname%3A%26nbsp%3Bbackend-policy%0A%26nbsp%3B%26nbsp%3Bnamespace%3A%26nbsp%3Bdevelopment%0Aspec%3A%0A%26nbsp%3B%26nbsp%3BpodSelector%3A%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BmatchLabels%3A%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bapp%3A%26nbsp%3Bwebapp%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Brole%3A%26nbsp%3Bbackend%0A%26nbsp%3B%26nbsp%3Bingress%3A%0A%26nbsp%3B%26nbsp%3B-%26nbsp%3Bfrom%3A%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B-%26nbsp%3BnamespaceSelector%3A%26nbsp%3B%7B%7D%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BpodSelector%3A%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BmatchLabels%3A%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bapp%3A%26nbsp%3Bwebapp%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Brole%3A%26nbsp%3Bfrontend%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELet%E2%80%99s%20deploy%20this%20policy%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%26nbsp%3Bkubectl%20apply%20-f%202-network-policy-allow-pod.yaml%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThen%20let%E2%80%99s%20run%20interactively%20an%20alpine%20pod%20with%20labels%20matching%20the%20ones%20required%20by%20the%20policy%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%26nbsp%3Bkubectl%20run%20--rm%20-it%20frontend%20--image%3Dalpine%20--labels%20app%3Dwebapp%2Crole%3Dfrontend%20--namespace%20development%20--generator%3Drun-pod%2Fv1%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20within%20the%20console%20of%20the%20alpine%20pod%2C%20we%20try%20to%20connect%20from%20it%20to%20the%20backend%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%26nbsp%3Bwget%20-qO-%20http%3A%2F%2Fbackend%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20final%20output%20should%20be%20like%20the%20following%2C%20where%20access%20succeed%20(Nginx%20is%20returning%20it%E2%80%99s%20HTML%20hello%20page)%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3Ekubectl%20apply%20-f%202-network-policy-allow-pod.yaml%0Anetworkpolicy.networking.k8s.io%2Fbackend-policy%20configured%0Akubectl%20run%20--rm%20-it%20frontend%20--image%3Dalpine%20--labels%20app%3Dwebapp%2Crole%3Dfrontend%20--namespace%20development%20--generator%3Drun-pod%2Fv1%0AIf%20you%20don't%20see%20a%20command%20prompt%2C%20try%20pressing%20enter.%0A%2F%20%23%20wget%20-qO-%20http%3A%2F%2Fbackend%0A%0A%0A%0A%3CTITLE%3EWelcome%20to%20nginx!%3C%2FTITLE%3E%0A%E2%80%A6%20removed%20for%20brievety%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20prove%20pods%20without%20matching%20labels%20cannot%20access%20the%20backend%20pod%2C%20lets%20run%20the%20following%20experiment%20showing%20its%20failure%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3Ekubectl%20run%20--rm%20-it%20--image%3Dalpine%20network-policy%20--namespace%20development%20--generator%3Drun-pod%2Fv1%0AIf%20you%20don't%20see%20a%20command%20prompt%2C%20try%20pressing%20enter.%0A%2F%20%23%20wget%20-qO-%20--timeout%3D2%20http%3A%2F%2Fbackend%0Awget%3A%20download%20timed%20out%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EScenario%203%3A%20Allow%20inbound%20traffic%20from%20pods%20with%20matching%20labels%20and%20within%20a%20defined%20namespace%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EIn%20this%20scenario%2C%20we%20want%20to%20restrict%20access%20to%20the%20backend%20pod%20not%20only%20to%20pods%20with%20matching%20labels%2C%20but%20also%20to%20pods%20within%20the%20same%20namespace.%20Which%20generally%20makes%20sense.%20For%20example%2C%20we%20don%E2%80%99t%20want%20the%20backend%20from%20production%20environment%20to%20connect%20to%20our%20database%20in%20development%20environment.%20Which%20can%20happen%20accidentally%20by%20selecting%20the%20wrong%20service%20name.%3C%2FP%3E%0A%3CP%3ELet%E2%80%99s%20first%20show%20that%20there%E2%80%99s%20no%20restriction%20for%20communications%20between%20pods%20in%20different%20namespaces.%20We%20create%20a%20new%20namespace%20called%20production.%20Then%20we%20add%20labels%20to%20it.%20And%20create%20a%20new%20pod%20from%20that%20namespace%20trying%20t%20connect%20to%20backend%20pod%20in%20development%20namespace.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3Ekubectl%20create%20namespace%20production%0Anamespace%2Fproduction%20created%0Akubectl%20label%20namespace%2Fproduction%20purpose%3Dproduction%0Anamespace%2Fproduction%20labeled%0Akubectl%20run%20--rm%20-it%20frontend%20--image%3Dalpine%20--labels%20app%3Dwebapp%2Crole%3Dfrontend%20--namespace%20production%20--generator%3Drun-pod%2Fv1%0AIf%20you%20don't%20see%20a%20command%20prompt%2C%20try%20pressing%20enter.%0A%2F%20%23%20wget%20-qO-%20http%3A%2F%2Fbackend.development%0A%0A%0A%0A%3CTITLE%3EWelcome%20to%20nginx!%3C%2FTITLE%3E%0A%E2%80%A6%20removed%20for%20brievety%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ENote%3A%3C%2FSTRONG%3E%20Note%20here%20how%20we%20are%20attaching%20the%20backend%20service%20name%20to%20its%20namespace%20name%20to%20reach%20the%20backend%20pod%3A%20%3CA%20href%3D%22http%3A%2F%2Fbackend.development%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fbackend.development%3C%2FA%3E%3C%2FP%3E%0A%3CP%3ENow%20let%E2%80%99s%20update%20the%20policy%20to%20accept%20inbound%20traffic%20from%20pods%20with%20matching%20labels%20and%20within%20specific%20namespace%20using%20the%20following%20template%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%23%26nbsp%3B3-network-policy-allow-pod-namespace.yaml%0Akind%3A%26nbsp%3BNetworkPolicy%0AapiVersion%3A%26nbsp%3Bnetworking.k8s.io%2Fv1%0Ametadata%3A%0A%26nbsp%3B%26nbsp%3Bname%3A%26nbsp%3Bbackend-policy%0A%26nbsp%3B%26nbsp%3Bnamespace%3A%26nbsp%3Bdevelopment%0Aspec%3A%0A%26nbsp%3B%26nbsp%3BpodSelector%3A%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BmatchLabels%3A%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bapp%3A%26nbsp%3Bwebapp%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Brole%3A%26nbsp%3Bbackend%0A%26nbsp%3B%26nbsp%3Bingress%3A%0A%26nbsp%3B%26nbsp%3B-%26nbsp%3Bfrom%3A%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B-%26nbsp%3BnamespaceSelector%3A%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BmatchLabels%3A%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bpurpose%3A%26nbsp%3Bdevelopment%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BpodSelector%3A%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BmatchLabels%3A%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bapp%3A%26nbsp%3Bwebapp%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Brole%3A%26nbsp%3Bfrontend%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ENote%3A%3C%2FSTRONG%3E%20The%20namespaceSelector%20relies%20on%20labels%20to%20identify%20namespaces%2C%20not%20the%20namespace%20name%20itself.%3C%2FP%3E%0A%3CP%3EWe%20apply%20the%20policy%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3Ekubectl%20apply%20-f%203-network-policy-allow-pod-namespace.yaml%0Anetworkpolicy.networking.k8s.io%2Fbackend-policy%20configured%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20run%20first%20test%20with%20pod%20with%20matching%20labels%20and%20within%20production%20namespace%20(not%20matching%20labels).%20Then%20we%20try%20to%20connect%20from%20that%20pod%20to%20the%20backend%20pod%20sitting%20in%20development%20namespace.%20Of%20course%2C%20that%20will%20fail.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3Ekubectl%20run%20--rm%20-it%20frontend%20--image%3Dalpine%20--labels%20app%3Dwebapp%2Crole%3Dfrontend%20--namespace%20production%20--generator%3Drun-pod%2Fv1%0AIf%20you%20don't%20see%20a%20command%20prompt%2C%20try%20pressing%20enter.%0A%2F%20%23%20wget%20-qO-%20--timeout%3D2%20http%3A%2F%2Fbackend.development%0Awget%3A%20download%20timed%20out%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20we%20run%20another%20test%20with%20pod%20with%20matching%20labels%20and%20within%20matching%20namespace.%20That%20will%20succeed.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3Ekubectl%20run%20--rm%20-it%20frontend%20--image%3Dalpine%20--labels%20app%3Dwebapp%2Crole%3Dfrontend%20--namespace%20development%20--generator%3Drun-pod%2Fv1%0AIf%20you%20don't%20see%20a%20command%20prompt%2C%20try%20pressing%20enter.%0A%2F%20%23%20wget%20-qO-%20http%3A%2F%2Fbackend%0A%0A%0A%0A%3CTITLE%3EWelcome%20to%20nginx!%3C%2FTITLE%3E%0A%E2%80%A6%20removed%20for%20brievety%E2%80%A6%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ENote%3A%3C%2FSTRONG%3E%20Kubernetes%20Network%20Policies%20Viewer%20is%20a%20tool%20for%20%E2%80%98explaining%E2%80%99%20the%20network%20policy%20using%20a%20designer%20view%3A%20%3CA%20href%3D%22https%3A%2F%2Forca.tufin.io%2Fnetpol%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Forca.tufin.io%2Fnetpol%2F%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22calico-designer.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F198897i7E67887BC5C9B5FB%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22calico-designer.png%22%20alt%3D%22calico-designer.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EScenario%204%3A%20Allow%20outbound%20traffic%20to%20pods%20with%20matching%20labels%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EIn%20all%20the%20above%20samples%2C%20we%20have%20worked%20only%20with%20ingress%2Finbound%20traffic%20to%20the%20pod.%20So%20that%20only%20the%20pod%20is%20reachable%20by%20only%20other%20matching%20pods.%20But%20we%20can%20also%20add%20rules%20to%20the%20egress%2Foutbound%20traffic%20to%20the%20pod.%20This%20will%20make%20it%20possible%20to%20the%20pod%20to%20talk%20only%20to%20other%20matching%20pods.%3C%2FP%3E%0A%3CP%3EThe%20syntax%20for%20adding%20an%20egress%20rule%20is%20like%20the%20following%2C%20note%20how%20we%20are%20using%20%E2%80%98egress%E2%80%99%20and%20%E2%80%98to%E2%80%99%20the%20same%20way%20we%20used%20%E2%80%98ingress%E2%80%99%20and%20%E2%80%98from%E2%80%99.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%26nbsp%3B%26nbsp%3Begress%3A%0A%26nbsp%3B%26nbsp%3B-%26nbsp%3Bto%3A%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B-%26nbsp%3BnamespaceSelector%3A%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BmatchLabels%3A%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bpurpose%3A%26nbsp%3Bdevelopment%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BpodSelector%3A%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3BmatchLabels%3A%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Bapp%3A%26nbsp%3Bwebapp%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3Brole%3A%26nbsp%3Bdatabase%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EScenario%205%3A%20allow%20inbound%20and%20outbound%20traffic%20from%20and%20to%20pods%20within%20the%20same%20namespace%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EA%20good%20practice%20and%20easy%20to%20implement%20rule%20is%20to%20start%20by%20denying%20pods%20to%20reach%20other%20pods%20from%20a%20different%20namespace.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%23%26nbsp%3B5-network-policy-allow-within-namespace.yaml%0AapiVersion%3A%26nbsp%3Bnetworking.k8s.io%2Fv1%0Akind%3A%26nbsp%3BNetworkPolicy%0Ametadata%3A%0A%26nbsp%3B%26nbsp%3Bname%3A%26nbsp%3Ballow-same-namespace%0Aspec%3A%0A%26nbsp%3B%26nbsp%3BpodSelector%3A%26nbsp%3B%7B%7D%0A%26nbsp%3B%26nbsp%3BpolicyTypes%3A%0A%26nbsp%3B%26nbsp%3B-%26nbsp%3BIngress%0A%26nbsp%3B%26nbsp%3Bingress%3A%0A%26nbsp%3B%26nbsp%3B-%26nbsp%3Bfrom%3A%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B-%26nbsp%3BpodSelector%3A%26nbsp%3B%7B%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EConclusion%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThis%20was%20how%20to%20get%20started%20creating%20basic%20network%20policies%20to%20limit%20inbound%20and%20outbound%20traffic%20between%20pods.%20However%2C%20there%20are%20many%20other%20concepts%26nbsp%3B%20and%20features%20not%20covered%20here%20like%20creating%20-ERR%3AREF-NOT-FOUND-rules%20based%20on%20IP%20address%20and%20Port%20numbers.%20Also%20-ERR%3AREF-NOT-FOUND-creating%20rules%20based%20on%20HTTP%20methods%20like%20Get%2C%20Post%2C%20Delete%2C%20Put%2C%20etc.%20And%20also%20%3CA%20href%3D%22https%3A%2F%2Fdocs.projectcalico.org%2Fv3.9%2Fsecurity%2Fservice-accounts%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ecreating%20rules%20for%20Service%20Accounts%3C%2FA%3E.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EMore%20resources%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EOfficial%20Calico%20documentation%3A%20-ERR%3AREF-NOT-FOUND-%3CA%20href%3D%22https%3A%2F%2Fdocs.projectcalico.org%2Fv3.9%2Fsecurity%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.projectcalico.org%2Fv3.9%2Fsecurity%2F%3C%2FA%3E%3C%2FP%3E%0A%3CP%3EThis%20is%20the%20article%20that%20inspired%20me%20to%20write%20this%20blog%3A%20-ERR%3AREF-NOT-FOUND-%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Faks%2Fuse-network-policies%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Faks%2Fuse-network-policies%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CSTRONG%3EDisclaimer%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CFONT%20size%3D%222%22%3E%3C%2FFONT%3E%3C%2FSTRONG%3E%3CFONT%3E%3CBR%20%2F%3E%3CFONT%20size%3D%222%22%3EThe%20sample%20scripts%20are%20not%20supported%20under%20any%20Microsoft%20standard%20support%20program%20or%20service.%20The%20sample%20scripts%20are%20provided%20AS%20IS%20without%20warranty%20of%20any%20kind.%20Microsoft%20further%20disclaims%20all%20implied%20warranties%20including%2C%20without%20limitation%2C%20any%20implied%20warranties%20of%20merchantability%20or%20of%20fitness%20for%20a%20particular%20purpose.%20The%20entire%20risk%20arising%20out%20of%20the%20use%20or%20performance%20of%20the%20sample%20scripts%20and%20documentation%20remains%20with%20you.%20In%20no%20event%20shall%20Microsoft%2C%20its%20authors%2C%20or%20anyone%20else%20involved%20in%20the%20creation%2C%20production%2C%20or%20delivery%20of%20the%20scripts%20be%20liable%20for%20any%20damages%20whatsoever%20(including%2C%20without%20limitation%2C%20damages%20for%20loss%20of%20business%20profits%2C%20business%20interruption%2C%20loss%20of%20business%20information%2C%20or%20other%20pecuniary%20loss)%20arising%20out%20of%20the%20use%20of%20or%20inability%20to%20use%20the%20sample%20scripts%20or%20documentation%2C%20even%20if%20Microsoft%20has%20been%20advised%20of%20the%20possibility%20of%20such%20damages.%3C%2FFONT%3E%3C%2FFONT%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1463923%22%20slang%3D%22en-US%22%3E%3CP%3ESet%20up%20network%20communication%20rules%20in%20Kubernetes.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22calico.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F198889i7D3CFB709C12A9B7%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22calico.png%22%20alt%3D%22calico.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1463923%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EHoussemDellai%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

Introduction to the problem

All pods in Kubernetes can reach each other. For example, the frontend can reach the backend and the backend can reach the database. That is expected and normal. But this openness can make problems like:

  • The frontend can reach the database!
  • Applications from different namespaces can talk to each other!
  • This means, if a container gets hacked, it will have access to all the containers!

That would be a security issue. We want to apply the principle of least privilege from The Zero Trust Network model: each pod should get access to the strict minimum and only the pods it is supposed to access to. If it doesn’t need access to other pods, there should be a rule to deny that possible access.

In this workshop, we will learn how to setup network policies in Kubernetes to:

  • Deny pods to reach pods within all other namespaces.
  • Deny pods to reach any pods except specific ones.

NetworkPolicy is defined in Kubernetes API as a specification. But it is not implemented. It is up to us to choose the implementation. There are multiple solutions available, like Calico, Azure Network Policy, Weave, Cilium, Romana, Kube-router…

This workshop is also available as a video on youtube:

 

calico.png

 

We need to choose one for this demo, so we’ll go for Calico as it is one of the widely used. But we can still choose another implementation and the demo still applicable.

Then we need to have a Kubernetes cluster. We can choose between Minikube, GKE, EKS, PKS and all things xKy. We’ll choose here AKS (Azure Kubernetes Service).

In AKS, we can choose between Calico and Azure Network Policy. Until now, Calico could be enabled only on new clusters, not existing ones. Follow the following instructions to create a new AKS cluster and enable Calico.

1. Enabling Calico in AKS

  • Enabling Calico from the Azure Portal

When we create a new AKS cluster in the Azure Portal, under the Networking tab, we’ll have the option to select Calico.

calico-azure.png

  • Enabling Calico from the Azure CLI: az

From the az command line, when we create a new AKS cluster, we can add the parameter –network-policy.

 

 

 az aks create --resource-group <RG> --name <NAME> --network-policy calico 

 

 

  • Enabling Calico from Terraform

In Terraform, we can add the network_policy with value set to “calico” inside “azurerm_kubernetes_cluster” as described in the following link:

https://www.terraform.io/docs/providers/azurerm/r/kubernetes_cluster.html#network_policy

 

 

  network_profile {
    network_plugin = "kubenet"
    network_policy = "calico"
  }

 

 

In the following scenarios, we’ll define a network policy and we’ll test how it works. So, lets create a Nginx Pod that will act as the backend for the application. Then we’ll simulate an Alpine Pod that acts as the frontend who tries to reach the backend.

First, let’s create a namespace with labels. Create a new file with the following content:

 

 

# 1-namespace-development.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: development
  labels:
    purpose: development

 

 

Then create the namespace with the following command:

 

 

 kubectl create -f 1-namespace-development.yaml

 

 

After that, create the backend Nginx Pod with a Service with the following command:

 

 

kubectl run backend --image=nginx --labels app=webapp,role=backend --namespace development --expose --port 80 --generator=run-pod/v1

 

 

This command will generate a Pod and Service like the following config here.

Scenario 1: Deny inbound traffic from all Pods

Let’s create a policy to deny access to our backend Pod.

 

 

# 1-network-policy-deny-all.yaml
kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
  name: backend-policy
  namespace: development
spec:
  podSelector:
    matchLabels:
      app: webapp
      role: backend
  ingress: []

 

 

Note: we are not referencing the Pod with its name, but with its labels.

Then we apply this policy into Kubernetes:

 

 

 kubectl apply -f 1-network-policy-deny-all.yaml

 

 

We create and run an Alpine Pod in interactive mode (-it):

 

 

kubectl run --rm -it --image=alpine network-policy --namespace development --generator=run-pod/v1

 

 

The command will give us access to run a command within the alpine pod. We try to connect to the backend pod:

 

 

 wget -qO- --timeout=2 http://backend

 

 

The output would look like the following screenshot, where we see the access fails (‘wget: download timed out’) as we denied access.

 

 

kubectl apply -f 1-network-policy-deny-all.yaml
networkpolicy.networking.k8s.io/backend-policy configured
kubectl run --rm -it --image=alpine network-policy --namespace development --generator=run-pod/v1
If you don't see a command prompt, try pressing enter.
/ # wget -qO- --timeout=2 http://backend
wget: download timed out
/ # exit
Session ended, resume using 'kubectl attach network-policy -c network-policy -i -t' command when the pod is running
pod "network-policy" deleted

 

 

At the end, we type ‘exit’ to exit the current alpine pod. That will delete the pod from the cluster.

Scenario 2: Allow inbound traffic from pods with matching labels

Now let’s update the policy to allow access to the backend pod to only specific pods with matching labels. Note here how we are accepting inbound (ingress) traffic from any Pod with specific labels, in any namespace.

 

 

# 2-network-policy-allow-pod.yaml
kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
  name: backend-policy
  namespace: development
spec:
  podSelector:
    matchLabels:
      app: webapp
      role: backend
  ingress:
  - from:
    - namespaceSelector: {}
      podSelector:
        matchLabels:
          app: webapp
          role: frontend

 

 

Let’s deploy this policy:

 

 

 kubectl apply -f 2-network-policy-allow-pod.yaml

 

 

Then let’s run interactively an alpine pod with labels matching the ones required by the policy:

 

 

 kubectl run --rm -it frontend --image=alpine --labels app=webapp,role=frontend --namespace development --generator=run-pod/v1

 

 

Now within the console of the alpine pod, we try to connect from it to the backend:

 

 

 wget -qO- http://backend

 

 

The final output should be like the following, where access succeed (Nginx is returning it’s HTML hello page):

 

 

kubectl apply -f 2-network-policy-allow-pod.yaml
networkpolicy.networking.k8s.io/backend-policy configured
kubectl run --rm -it frontend --image=alpine --labels app=webapp,role=frontend --namespace development --generator=run-pod/v1
If you don't see a command prompt, try pressing enter.
/ # wget -qO- http://backend
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
… removed for brievety
</html>

 

 

To prove pods without matching labels cannot access the backend pod, lets run the following experiment showing its failure:

 

 

kubectl run --rm -it --image=alpine network-policy --namespace development --generator=run-pod/v1
If you don't see a command prompt, try pressing enter.
/ # wget -qO- --timeout=2 http://backend
wget: download timed out

 

 

Scenario 3: Allow inbound traffic from pods with matching labels and within a defined namespace

In this scenario, we want to restrict access to the backend pod not only to pods with matching labels, but also to pods within the same namespace. Which generally makes sense. For example, we don’t want the backend from production environment to connect to our database in development environment. Which can happen accidentally by selecting the wrong service name.

Let’s first show that there’s no restriction for communications between pods in different namespaces. We create a new namespace called production. Then we add labels to it. And create a new pod from that namespace trying t connect to backend pod in development namespace.

 

 

kubectl create namespace production
namespace/production created
kubectl label namespace/production purpose=production
namespace/production labeled
kubectl run --rm -it frontend --image=alpine --labels app=webapp,role=frontend --namespace production --generator=run-pod/v1
If you don't see a command prompt, try pressing enter.
/ # wget -qO- http://backend.development
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
… removed for brievety
</html>

 

 

Note: Note here how we are attaching the backend service name to its namespace name to reach the backend pod: http://backend.development

Now let’s update the policy to accept inbound traffic from pods with matching labels and within specific namespace using the following template:

 

 

# 3-network-policy-allow-pod-namespace.yaml
kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
  name: backend-policy
  namespace: development
spec:
  podSelector:
    matchLabels:
      app: webapp
      role: backend
  ingress:
  - from:
    - namespaceSelector:
        matchLabels:
          purpose: development
      podSelector:
        matchLabels:
          app: webapp
          role: frontend

 

 

Note: The namespaceSelector relies on labels to identify namespaces, not the namespace name itself.

We apply the policy:

 

 

kubectl apply -f 3-network-policy-allow-pod-namespace.yaml
networkpolicy.networking.k8s.io/backend-policy configured

 

 

We run first test with pod with matching labels and within production namespace (not matching labels). Then we try to connect from that pod to the backend pod sitting in development namespace. Of course, that will fail.

 

 

kubectl run --rm -it frontend --image=alpine --labels app=webapp,role=frontend --namespace production --generator=run-pod/v1
If you don't see a command prompt, try pressing enter.
/ # wget -qO- --timeout=2 http://backend.development
wget: download timed out

 

 

Now we run another test with pod with matching labels and within matching namespace. That will succeed.

 

 

kubectl run --rm -it frontend --image=alpine --labels app=webapp,role=frontend --namespace development --generator=run-pod/v1
If you don't see a command prompt, try pressing enter.
/ # wget -qO- http://backend
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
… removed for brievety…
</html>

 

 

Note: Kubernetes Network Policies Viewer is a tool for ‘explaining’ the network policy using a designer view: https://orca.tufin.io/netpol/

 

calico-designer.png

Scenario 4: Allow outbound traffic to pods with matching labels

In all the above samples, we have worked only with ingress/inbound traffic to the pod. So that only the pod is reachable by only other matching pods. But we can also add rules to the egress/outbound traffic to the pod. This will make it possible to the pod to talk only to other matching pods.

The syntax for adding an egress rule is like the following, note how we are using ‘egress’ and ‘to’ the same way we used ‘ingress’ and ‘from’.

 

 

  egress:
  - to:
    - namespaceSelector:
        matchLabels:
          purpose: development
      podSelector:
        matchLabels:
          app: webapp
          role: database

 

 

Scenario 5: allow inbound and outbound traffic from and to pods within the same namespace

A good practice and easy to implement rule is to start by denying pods to reach other pods from a different namespace.

 

 

# 5-network-policy-allow-within-namespace.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-same-namespace
spec:
  podSelector: {}
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector: {}

 

 

Conclusion

This was how to get started creating basic network policies to limit inbound and outbound traffic between pods. However, there are many other concepts  and features not covered here like creating rules based on IP address and Port numbers. Also creating rules based on HTTP methods like Get, Post, Delete, Put, etc. And also creating rules for Service Accounts.

More resources

Official Calico documentation: https://docs.projectcalico.org/v3.9/security/

This is the article that inspired me to write this blog: https://docs.microsoft.com/en-us/azure/aks/use-network-policies

 

Disclaimer
The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.