
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
In this lab, you learn how to use Policy Controller, a Kubernetes dynamic admission controller that checks, audits, and enforces your clusters' compliance with policies related to security, regulations, or business rules.
Policy Controller enforces your clusters' compliance with policies called constraints. In this lab, you use the constraint library, a set of templates that can be easily configured to enforce and audit security and compliance policies. For example, you can require that namespaces have at least one label so that you use GKE Usage Metering and allocate costs to different teams. Or, you might want to enforce the same requirements as provided by PodSecurityPolicies, but with the ability to audit them before disrupting a deployment with enforcements.
In addition to working with the provided platform constraint templates, you will learn how to create your own. With constraint templates, a centralized team can define how a constraint works, and delegate defining the specifics of the constraint to an individual or group with subject-matter expertise.
Policy Controller is built from the Gatekeeper open source project and is integrated into Anthos Config Management v1.1+.
In this lab, you learn how to perform the following tasks:
In this task, you use Qwiklabs and perform initialization steps for your lab.
For each lab, you get a new Google Cloud project and set of resources for a fixed time at no cost.
Sign in to Qwiklabs using an incognito window.
Note the lab's access time (for example, 1:15:00
), and make sure you can finish within that time.
There is no pause feature. You can restart if needed, but you have to start at the beginning.
When ready, click Start lab.
Note your lab credentials (Username and Password). You will use them to sign in to the Google Cloud Console.
Click Open Google Console.
Click Use another account and copy/paste credentials for this lab into the prompts.
If you use other credentials, you'll receive errors or incur charges.
Accept the terms and skip the recovery resource page.
After you complete the initial sign-in steps, the project dashboard appears.
Google Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud.
Google Cloud Shell provides command-line access to your Google Cloud resources.
In Cloud console, on the top right toolbar, click the Open Cloud Shell button.
Click Continue.
It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. For example:
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
Output:
Example output:
Output:
Example output:
Set the Zone environment variable:
Set up the Cloud Shell environment for command-line access to your cluster:
Enable necessary APIs:
Create the Anthos Policy Controller config file:
Install the Anthos Policy Controller:
Poll the Config Management service to see if the Anthos Policy Controller has been installed:
When the output updates to match what is shown below, you can type
<CTRL>+C
to exit the polling and continue. It may take 4-5 minutes
(but not longer) for the controller to be installed. Sometimes the
Status and/or the Hierachy_Controller stays in PENDING. Ignore that and
continue with the next steps.
Output:
Verifying the constraint template library installation:
Desired output:
Let's try one of the constraints from the library.
Let's specify that new
namespaces must the have label geo
before they can be created:
Apply the constraint using kubectl
:
Try creating a namespace without the geo
key, and confirm that it
doesn't work:
You should see results that look like this:
If instead you see an error that looks like this:
wait for a minute or two and try again.
Try creating the same namespace with the geo
label:
Congratulations! You successfully configured a template from the Constraints Library, deployed it, and verified that prevents users from installing non compliant resources.
Policy Controller constraint objects enable you to enforce policies for your Kubernetes clusters. To help test your policies, you can add an enforcement action to your constraints. You can then view violations in constraint objects and logs. There are two enforcement actions: deny and dryrun.
deny is the default enforcement action. It's automatically enabled, even if you don't add an enforcement action in your constraint. Use deny to prevent a given cluster operation from occurring when there's a violation.
dryrun let's you monitor violations of your rules without actively blocking transactions. You can use it to test if your constraints are working as intended, prior to enabling active enforcement using the deny action. Testing constraints this way can prevent disruptions caused by an incorrectly configured constraint.
Audit the constraint violations using the following kubectl
command:
Notice that the type of enforcementAction
is deny as you have not
changed the default behavior. Notice as well that there are a total of 9
existing violations - these are namespaces that don't meet the policy, but
were created before the policy was applied.
Open the ns-must-have-geo.yaml
constraint file in the Cloud Shell editor and save the file. Add the constraint
enforcementAction
to dryrun
. That way you can validate policy constraints before enforcing them. Make sure that the yaml file looks as
follows:
Run kubectl apply
to deploy the changes into the cluster:
Try creating another namespace without the geo
label and observe that
the command completes this time:
Check the violations again, to see a new violation added of type dryrun
:
In this task, you will write a custom constraint template and use it to extend Policy Controller capabilities. This is great when you cannot find a pre-written constraint template that suits your needs for extending Policy Controller.
Policy Controller policies are described by using the OPA Constraint Framework and are written in Rego. A policy can evaluate any field of a Kubernetes object.
Writing policies using Rego is a specialized skill. For this reason, a library of common constraint templates is installed by default. Most users can leverages these constraint templates when creating constraints. If you have specialized needs, you can create your own constraint templates.
Constraint templates let you separate a policy's logic from its specific requirements, and this enables reuse and delegation. You can create constraints by using constraint templates developed by third parties, such as open source projects, software vendors, or regulatory experts.
Create the policy template that denies all resources whose name matches a value provided by the creator of the constraint:
Deploy the template in the kubernetes cluster:
cluster/
directory.Once the template is deployed, a team can reference it the same way it would reference a template from Google's library to create a constraint:
Deploy the constraint in the kubernetes cluster:
Try creating any resource with the name policy-violation
to verify that
it does not work:
Congratulations! You successfully created and deployed your own template. Then you created a constraint and verified that it prevents users from installing non compliant resources.
In this lab, you reviewed ACM's Policy Controller and explored some of its useful features. You created a constraint from the provided templates as well as creating your own templates. You verified that constraints can be used to enforce policies and to audit policy violations before enforcing them in a production environment.
When you have completed your lab, click End Lab. Google Cloud Skills Boost removes the resources you’ve used and cleans the account for you.
You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.
The number of stars indicates the following:
You can close the dialog box if you don't want to provide feedback.
For feedback, suggestions, or corrections, please use the Support tab.
Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
This content is not currently available
We will notify you via email when it becomes available
Great!
We will contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one