Scan images using admission controller
Understand how to scan images using admission controller.
We can use Trivy as an admission controller to scan images before they are deployed in a Kubernetes cluster. This helps ensure that only images that meet your security policies are allowed to run in your environment.
With this approach, it might delay the deployment process because the admission controller will scan the image everytime before it is deployed and if the image is not compliant, it will block the deployment. So, the alternative way is to have your own internal registry with all pre-scanned images and use that registry in your deployment process. This way, the admission controller will only scan the images that are not in your internal registry.
Steps to scan images using Trivy with an admission controller
Step 2: Create a Webhook Service
Create a simple Flask application that will act as a webhook server. This server will receive admission review requests from the Kubernetes API server and respond with whether the image is allowed or not.
Then, create a Dockerfile
for the webhook server.
Build the Docker image.
Step 5: Test the Webhook
If the webhook is working correctly, the pod creation should be blocked, and you should see an error message similar to: