Setting up Kyverno & Kyverno Policies
The Kyverno
To integrate Kyverno & Kyverno Policies into Managed Service for Kubernetes:
- Install the Kyverno & Kyverno Policies application.
- Check how the policy works for the baseline profile.
- Create and test your own Kyverno policy.
If you no longer need the resources you created, delete them.
Getting started
-
Create a Managed Service for Kubernetes cluster and node group.
ManuallyTerraform-
If you do not have a network yet, create one.
-
If you do not have any subnets yet, create them in the availability zones where your Kubernetes cluster and node group will be created.
-
Create security groups for the Managed Service for Kubernetes cluster and its node groups.
Warning
The configuration of security groups determines the performance and availability of the cluster and the services and applications running in it.
-
Create a Managed Service for Kubernetes cluster and a node group in any suitable configuration. When creating them, specify the security groups prepared earlier.
-
If you do not have Terraform yet, install it.
-
Get the authentication credentials. You can add them to environment variables or specify them later in the provider configuration file.
-
Configure and initialize a provider. There is no need to create a provider configuration file manually, you can download it
. -
Place the configuration file in a separate working directory and specify the parameter values. If you did not add the authentication credentials to environment variables, specify them in the configuration file.
-
Download the k8s-cluster.tf
cluster configuration file to the same working directory. The file describes:-
Managed Service for Kubernetes cluster.
-
Service account required for the Managed Service for Kubernetes cluster and node group.
-
Security groups which contain rules required for the Managed Service for Kubernetes cluster and its node groups.
Warning
The configuration of security groups determines the performance and availability of the cluster and the services and applications running in it.
-
Specify the following in the configuration file:
- Folder ID.
- Kubernetes version for the Managed Service for Kubernetes cluster and node groups.
- Kubernetes cluster CIDR.
- Service account name. It must be unique within the folder.
-
Check that the Terraform configuration files are correct using this command:
terraform validate
If there are any errors in the configuration files, Terraform will point them out.
-
Create the required infrastructure:
-
Run the command to view planned changes:
terraform plan
If the resource configuration descriptions are correct, the terminal will display a list of the resources to modify and their parameters. This is a test step. No resources are updated.
-
If you are happy with the planned changes, apply them:
-
Run the command:
terraform apply
-
Confirm the update of resources.
-
Wait for the operation to complete.
-
All the required resources will be created in the specified folder. You can check resource availability and their settings in the management console
. -
-
-
Install kubectl
and configure it to work with the created cluster.
Install the Kyverno & Kyverno Policies application
Follow this guide to install Kyverno & Kyverno Policies with the following settings:
- Pod Security Standard profile:
baseline
. - Validation failure action:
enforce
.
The baseline
Pod Security Standard profile
Check how the policy works for the baseline profile
-
Create a pod named
nginx
with standard parameters:kubectl run nginx --image nginx
Result:
pod/nginx created
Such a pod will satisfy the policy requirements for the
baseline
profile. -
Create a pod named
nginx
in privileged mode:kubectl run nginx --image nginx --privileged=true
Result:
Error from server: admission webhook "validate.kyverno.svc-fail" denied the request: policy Pod/default/nginx for resource violation: disallow-privileged-containers: privileged-containers: 'validation error: Privileged mode is disallowed. The fields spec.containers[*].securityContext.privileged and spec.initContainers[*].securityContext.privileged must be unset or set to `false`. rule privileged-containers failed at path /spec/containers/0/securityContext/privileged/'
The policy rules for the
baseline
profile prohibit creating pods in privileged mode.
Note
Although the policies are designed for pods, Kyverno applies them to any resources able to create pods.
Create and test your own Kyverno policy
-
Create a policy that will require all pods to have the
app.kubernetes.io/name
label:-
Save the
ClusterPolicy
creation specification to a YAML file namedpolicy.yaml
:apiVersion: kyverno.io/v1 kind: ClusterPolicy metadata: name: require-labels spec: validationFailureAction: Enforce rules: - name: check-for-labels match: any: - resources: kinds: - Pod validate: message: "label 'app.kubernetes.io/name' is required" pattern: metadata: labels: app.kubernetes.io/name: "?*"
-
Run this command:
kubectl apply -f policy.yaml
Result:
clusterpolicy.kyverno.io/require-labels created
-
-
(Optional) Install Policy Reporter in your Managed Service for Kubernetes cluster to be able to save and process policy results.
Test Kyverno & Kyverno Policies
-
Create a pod named
nginx
with noapp.kubernetes.io/name
Kubernetes label:kubectl run nginx --image nginx
Result:
Error from server: admission webhook "validate.kyverno.svc-fail" denied the request: resource Pod/default/nginx was blocked due to the following policies require-labels: check-for-labels: 'validation error: label ''app.kubernetes.io/name'' is required. Rule check-for-labels failed at path /metadata/labels/app.kubernetes.io/name/'
-
Create a pod named
nginx
with theapp.kubernetes.io/name
label:kubectl run nginx --image nginx --labels app.kubernetes.io/name=nginx
Result:
pod/nginx created
Delete the resources you created
Some resources are not free of charge. To avoid paying for them, delete the resources you no longer need:
-
In the terminal window, go to the directory containing the infrastructure plan.
Warning
Make sure the directory has no Terraform manifests with the resources you want to keep. Terraform deletes all resources that were created using the manifests in the current directory.
-
Delete resources:
-
Run this command:
terraform destroy
-
Confirm deleting the resources and wait for the operation to complete.
All the resources described in the Terraform manifests will be deleted.
-