{"id":1619,"date":"2021-05-12T15:52:53","date_gmt":"2021-05-12T22:52:53","guid":{"rendered":"http:\/\/blog.nillsf.com\/?p=1619"},"modified":"2021-05-12T17:14:19","modified_gmt":"2021-05-13T00:14:19","slug":"block-public-load-balancers-on-azure-kubernetes-service-using-azure-policy","status":"publish","type":"post","link":"https:\/\/blog.nillsf.com\/index.php\/2021\/05\/12\/block-public-load-balancers-on-azure-kubernetes-service-using-azure-policy\/","title":{"rendered":"Block public load balancers on Azure Kubernetes Service using Azure policy"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In this post, I&#8217;ll explain to you how you can use Azure Policy to restrict the creation of load balancers with a public IP in AKS using Azure policy.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Azure policy is a way to ensure your Azure deployments follow your standards.  Policies are a set of restrictive rules, that control what can be deployed in your Azure environment. They do not control who gets access to your environment, that is controlled through role-based access control.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Azure policy has a plugin available that allows you to configure Azure policy on top of Kubernetes as well. This gives you the ability the use Azure policy to manage the state of your resources inside a Kubernetes cluster. To make this work, Azure policy under the covers uses a combination of <a href=\"https:\/\/github.com\/open-policy-agent\/gatekeeper\">Gatekeeper<\/a>, <a href=\"https:\/\/www.openpolicyagent.org\/\">Open Policy Agent<\/a>, and <a href=\"https:\/\/kubernetes.io\/docs\/reference\/access-authn-authz\/extensible-admission-controllers\/\">admission webhooks<\/a>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To restrict the creation of public load balancers, we&#8217;ll use Azure policy on a new cluster. Let&#8217;s get this going:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Setup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For this small experiment, I created a new cluster with Azure policy enabled at the creation time:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>az group create -n aks-policy -l westus2\naz aks create -n aks-policy -g aks-policy --enable-managed-identity --enable-addons azure-policy --node-count 1\naz aks get-credentials -n aks-policy -g aks-policy<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">I then went into the portal to assign a new Azure policy to this cluster. The policy I was looking for was: &#8220;Kubernetes clusters should use internal load balancers&#8221;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once you open Azure policy in the portal, select the Kubernetes category and look for the keyword &#8220;internal&#8221;, as shown in the picture below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"471\" src=\"\/wp-content\/uploads\/2021\/05\/image-6-1024x471.png\" alt=\"\" class=\"wp-image-1620\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-6-1024x471.png 1024w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-6-300x138.png 300w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-6-768x353.png 768w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-6-1536x706.png 1536w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-6.png 1863w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Finding the right policy<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The next screen shows you the actual policy in JSON, and giving you the ability to assign it:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"431\" src=\"\/wp-content\/uploads\/2021\/05\/image-7-1024x431.png\" alt=\"\" class=\"wp-image-1621\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-7-1024x431.png 1024w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-7-300x126.png 300w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-7-768x323.png 768w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-7-1536x647.png 1536w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-7.png 1860w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Overview of the policy definition<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Next up, you have to select the scope for this policy. In my case, I&#8217;m only assigning it to the resource group of the cluster. In your case, you could apply it to your full subscription or management group if you want to have it apply everywhere:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"393\" src=\"\/wp-content\/uploads\/2021\/05\/image-8-1024x393.png\" alt=\"\" class=\"wp-image-1622\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-8-1024x393.png 1024w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-8-300x115.png 300w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-8-768x295.png 768w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-8-1536x590.png 1536w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-8.png 1786w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Assigning the policy to the right scope<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Next, you can configure certain inclusion\/exclusions. In this example, I&#8217;ll have it apply to all namespaces except the default 3 and the internet namespace:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"965\" height=\"661\" src=\"\/wp-content\/uploads\/2021\/05\/image-10.png\" alt=\"\" class=\"wp-image-1625\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-10.png 965w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-10-300x205.png 300w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-10-768x526.png 768w\" sizes=\"auto, (max-width: 965px) 100vw, 965px\" \/><figcaption>Exclusion of the internet namespace (along the default namespace exclusions)<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, we can review and create the policy:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"763\" height=\"1024\" src=\"\/wp-content\/uploads\/2021\/05\/image-11-763x1024.png\" alt=\"\" class=\"wp-image-1626\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-11-763x1024.png 763w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-11-224x300.png 224w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-11-768x1031.png 768w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-11.png 909w\" sizes=\"auto, (max-width: 763px) 100vw, 763px\" \/><figcaption>Creating the policy<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Once you hit the create button, you&#8217;ll get a popup confirming the policy is being applied. It can take up to 30 minutes for it to appear on your cluster:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"535\" height=\"188\" src=\"\/wp-content\/uploads\/2021\/05\/image-12.png\" alt=\"\" class=\"wp-image-1627\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-12.png 535w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-12-300x105.png 300w\" sizes=\"auto, (max-width: 535px) 100vw, 535px\" \/><figcaption>Verification that the policy was created<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">After those 30 minutes have passed, let&#8217;s try this out!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Trying out the policy<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To verify the policy, I&#8217;ll be using an example from my latest book: the <a href=\"https:\/\/github.com\/PacktPublishing\/Hands-on-Kubernetes-on-Azure-Third-Edition\/blob\/main\/Chapter05\/guestbook-all-in-one.yaml\">guestbook all-in-one example,<\/a> which includes a public load balancer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s start by trying it out in the default namespace:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl create ns blocked\nkubectl create -n blocked -f https:\/\/raw.githubusercontent.com\/PacktPublishing\/Hands-on-Kubernetes-on-Azure-Third-Edition\/main\/Chapter05\/guestbook-all-in-one.yaml<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This resulted in the (expected) error:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"292\" src=\"\/wp-content\/uploads\/2021\/05\/image-13-1024x292.png\" alt=\"\" class=\"wp-image-1628\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-13-1024x292.png 1024w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-13-300x86.png 300w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-13-768x219.png 768w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-13-1536x438.png 1536w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-13.png 1708w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>In the blocked namespace, we are getting the policy error<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s try the same using an internal load balancer:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>k create -f https:\/\/raw.githubusercontent.com\/NillsF\/blog\/master\/aks-policy\/ilb.yaml -n blocked<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Which does not result in an error, since this is allowed:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"66\" src=\"\/wp-content\/uploads\/2021\/05\/image-15-1024x66.png\" alt=\"\" class=\"wp-image-1632\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-15-1024x66.png 1024w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-15-300x19.png 300w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-15-768x50.png 768w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-15-1536x99.png 1536w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-15.png 1637w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Creating an internal load balancer in the ILB namespace doesn&#8217;t result in an error<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s try creating a public load balancer in the internet namespace:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl create ns internet\nkubectl create -n internet -f https:\/\/raw.githubusercontent.com\/PacktPublishing\/Hands-on-Kubernetes-on-Azure-Third-Edition\/main\/Chapter05\/guestbook-all-in-one.yaml<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Which &#8211; as expected &#8211; succeeded without the error:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"212\" src=\"\/wp-content\/uploads\/2021\/05\/image-14-1024x212.png\" alt=\"\" class=\"wp-image-1629\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-14-1024x212.png 1024w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-14-300x62.png 300w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-14-768x159.png 768w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-14-1536x318.png 1536w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/image-14.png 1696w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>In the internet namespace, we are not getting the error.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">This shows you how to use Azure policy to limit the creation of Azure public load balancers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This post explained to you how to use Azure policy to limit the creation of public services in AKS. This post showed you how to create a new cluster with Azure policy enabled, how to assign the &#8220;Kubernetes clusters should use internal load balancers&#8221; policy to your cluster. You then saw how the policy shows a visual error when public services get created. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, I&#8217;ll explain to you how you can use Azure Policy to restrict the creation of load balancers with a public IP in AKS using Azure policy. Azure policy is a way to ensure your Azure deployments follow your standards. Policies are a set of restrictive rules, that control what can be deployed [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1630,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2,3,58,4,36],"tags":[115,102,18,132,38],"class_list":["post-1619","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure","category-devops","category-kubernetes","category-management","category-networking","tag-azure-kubernetes-service","tag-azure-policy","tag-kubernetes","tag-load-balancing","tag-networking"],"jetpack_featured_media_url":"https:\/\/nillsfblog.blob.core.windows.net\/media\/2021\/05\/2021-05-12-15_48_30-PowerPoint-Slide-Show-Customize-core-dumps-in-Azure-Kubernetes.pptx.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/posts\/1619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/comments?post=1619"}],"version-history":[{"count":5,"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/posts\/1619\/revisions"}],"predecessor-version":[{"id":1635,"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/posts\/1619\/revisions\/1635"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/media\/1630"}],"wp:attachment":[{"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/media?parent=1619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/categories?post=1619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/tags?post=1619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}