Setting up Kyverno & Kyverno Policies
Kyverno
To integrate Kyverno & Kyverno Policies into Managed Service for Kubernetes:
- Install Kyverno & Kyverno Policies.
- 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.
Required paid resources
The support cost for this solution includes:
- Fee for using the master and outgoing traffic in a Managed Service for Kubernetes cluster (see Managed Service for Kubernetes pricing).
- Fee for using computing resources, OS, and storage in cluster nodes (VMs) (see Compute Cloud pricing).
- Fee for a public IP address assigned to cluster nodes (see Virtual Private Cloud pricing).
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 the new Kubernetes cluster and node group will reside.
-
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 node group with any suitable configuration. When creating, specify the preconfigured security groups.
-
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. This file describes:-
Managed Service for Kubernetes cluster.
-
Service account 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.
-
Make sure the Terraform configuration files are correct using this command:
terraform validateTerraform will show any errors found in your configuration files.
-
Create the required infrastructure:
-
Run this command to view the planned changes:
terraform planIf you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
-
If everything looks correct, apply the changes:
-
Run this command:
terraform apply -
Confirm updating the 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 kubect
and configure it to work with the new cluster.
Install Kyverno & Kyverno Policies
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 the
nginxpod with standard settings:kubectl run nginx --image nginxResult:
pod/nginx createdSuch a pod satisfies the policy requirements for the
baselineprofile. -
Create the
nginxpod in privileged mode:kubectl run nginx --image nginx --privileged=trueResult:
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
baselineprofile prohibit creating pods in privileged mode.
Note
Even though the policies target pods, Kyverno applies them to all resources that can create pods.
Create and test your own Kyverno policy
-
Create a policy that requires all pods to have the
app.kubernetes.io/namelabel:-
Save the
ClusterPolicyspecification 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.yamlResult:
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 the
nginxpod with noapp.kubernetes.io/nameKubernetes label:kubectl run nginx --image nginxResult:
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 the
nginxpod with theapp.kubernetes.io/namelabel:kubectl run nginx --image nginx --labels app.kubernetes.io/name=nginxResult:
pod/nginx created
Delete the resources you created
Some resources are not free of charge. Delete the resources you no longer need to avoid paying for them:
-
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.
-