Kubernetes Setup¶
Overview¶
Security Emphasis
Proper Kubernetes setup is the foundation of secure container scanning. This task implements security best practices for cluster preparation, ensuring proper isolation, network security, and access controls for scanner operations.
This task guides you through setting up a Kubernetes environment for secure container scanning. It covers both existing cluster preparation and local testing environment setup, with a focus on security considerations throughout.
Time to complete: 30-45 minutes
Security risk: 🟡 Medium - Involves cluster configuration and service setup
Security approach: Implements proper namespace isolation, secure service account configuration, and network security controls for scanner operations
Security Architecture¶
Understanding Permission Layers
Kubernetes setup for secure scanning involves multiple security boundaries:
1. Cluster Administration Permissions * Control: Ability to create and manage namespaces, ClusterRoles, and system resources * Risk area: Excessive administrative access could compromise the entire cluster * Mitigation: Use dedicated service accounts with specific permissions for administrative tasks
2. Namespace-Level Permissions * Control: Access to resources within the scanner namespace * Risk area: Cross-namespace access could lead to privilege escalation * Mitigation: Implement proper namespace isolation with resource quotas and network policies
3. Pod Security Standards * Control: Security context and capabilities of deployed pods * Risk area: Insecure pod configurations could lead to container escapes * Mitigation: Enforce restrictive Pod Security Standards for all scanner components
Security Prerequisites¶
- Access to a Kubernetes cluster (existing or ability to create a local one)
- kubectl installed and configured
- Administrative access to create namespaces and RBAC resources
- Basic understanding of Kubernetes security principles
Step-by-Step Instructions¶
Step 1: Choose Your Environment Type¶
Security Consideration
Different environments have different security requirements. Choose the one most appropriate for your needs.
There are two main approaches to setting up your Kubernetes environment:
- Existing Cluster Setup: Configure an existing Kubernetes cluster for scanning
- Local Minikube Setup: Create a local testing environment using Minikube
Step 2: Existing Cluster Setup¶
Follow these steps if you're using an existing Kubernetes cluster:
- Verify cluster requirements:
- Create a dedicated namespace for scanning operations:
- Apply resource quotas for proper isolation:
- Apply network policies for enhanced security:
- Label the namespace for network policy targeting:
For more details, follow the Existing Cluster Requirements guide.
Step 3: Local Minikube Setup¶
Security Consideration
Even in local testing environments, proper security controls should be implemented.
If you don't have access to an existing cluster, or want a dedicated testing environment, you can set up Minikube:
- Install Minikube if not already installed:
- Start Minikube with appropriate resources:
- For distroless container scanning support, enable feature gates:
- Verify the setup:
- Create the scanner namespace:
For a more automated setup, you can use the provided setup script:
For detailed steps and more options, see the Minikube Setup Guide.
Step 4: Configure Service Accounts¶
Security Consideration
Using dedicated service accounts with minimal permissions is essential for secure scanner operations.
- Create a service account for scanner operations:
- Create a role with minimal required permissions:
- Bind the role to the service account:
Step 5: Deploy Test Pods for Scanning¶
Security Consideration
Test pods should follow security best practices even in testing environments.
- Deploy a standard container test pod:
- For testing with a distroless container:
- Verify test pods are running:
Step 6: Configure Kubeconfig for Scanning¶
Security Consideration
Using properly scoped kubeconfig files enhances security by limiting access.
- Generate a restricted kubeconfig for scanner operations:
- This script performs these actions:
- Creates a token for the service account
- Extracts cluster information
- Generates a kubeconfig file with minimal permissions
-
Sets proper file permissions
-
Alternatively, you can create the kubeconfig manually:
Security Best Practices¶
- Use dedicated namespaces for scanner operations
- Implement network policies to restrict pod communication
- Apply resource quotas to prevent resource exhaustion
- Use non-root users for all containers
- Implement least-privilege RBAC for service accounts
- Generate short-lived tokens for scanner operations
- Use appropriate Pod Security Standards
- Regularly rotate credentials and tokens
- Monitor and audit scanner operations
- Keep Kubernetes and all components up to date
- Use secure container images with minimal attack surface
Verification Steps¶
- Verify namespace isolation
- Test RBAC configuration
- Verify kubeconfig works as expected
Troubleshooting¶
Issue | Solution |
---|---|
Minikube fails to start | Increase allocated resources or try a different driver |
Permission denied errors | Verify RBAC configuration and service account permissions |
Network policy issues | Ensure namespace labels are correctly applied |
Ephemeral containers not working | Verify Kubernetes version (1.23+) and feature gates |
Service account token creation fails | Use an alternative method appropriate for your Kubernetes version |
Next Steps¶
After completing this task, consider:
- Deploy the scanner with scripts for a lightweight approach
- Deploy using Helm charts for a more structured setup
- Configure RBAC for more advanced security scenarios
- Set up token management for better credential security
- Run container scans to validate your setup