Configuring allowed repositories
With Gatekeeper you can set a constraint that requires only allowed repositories to be used to download images. For example, this constraint can be useful if company policies require downloading images only from trusted repositories, so that cluster operators do not accidentally run an untrusted application from an untrusted source.
To demostrate how Gatekeeper works several objects will be created:
- A constraint template and its corresponding constraint. It will only allow images to be downloaded from the Docker Hub repository.
- A few Kubernetes resources to test how the constraint works.
-
Create a Kubernetes cluster of the latest version, if not done so already.
Choose the cluster parameters at your own discretion.
-
Make sure that you can connect to the cluster with
kubectl.
-
Create a constraint template:
-
Create a constraint template manifest.
Use the contents of this file. This is a ready-made template
K8sAllowedReposfrom the Gatekeeper library, which checks that images are only downloaded from trusted repositories. -
Create a constraint template based on the template manifest:
kubectl apply -f template.yaml
-
-
Create a constraint:
-
Create a constraint manifest constraint based on the template you created earlier:
-
Create a constraint based on the constraint manifest:
kubectl apply -f constraint.yaml
-
-
Make sure that the constraint template and the constraint have been successfully created by running the command:
kubectl get constraints,constrainttemplatesOutput should give you the similar information:
NAME ENFORCEMENT-ACTION TOTAL-VIOLATIONSk8sallowedrepos.constraints.gatekeeper.sh/require-docker-hub ... ...NAME AGEconstrainttemplate.templates.gatekeeper.sh/k8sallowedrepos ... -
Test the restriction by trying to create several pods:
Pod that satisfies the constraintPod that does not satisfy the constraint-
Create a manifest for the pod:
-
Try to create a pod based on the manifest:
kubectl apply -f example-allowed.yamlThe operation should be completed successfully.
-
Make sure that the pod has been successfully created by running the command:
kubectl get pod allowed-podOutput should give you the similar information:
NAME READY STATUS RESTARTS AGEallowed-pod 1/1 Running ... ...
-
A running cluster consumes computing resources and is charged accordingly. If you no longer need the Kubernetes resources you created to test constraints, delete them:
-
Delete the created
allowed-podpod, the constraint template, and the constraint itself:Linux/macOSWindowskubectl delete pod allowed-podkubectl delete k8sallowedrepos.constraints.gatekeeper.sh/require-docker-hubkubectl delete constrainttemplate.templates.gatekeeper.sh/k8sallowedrepos -
Stop the cluster you created to use it later or delete it permanently.