Introduction to Kubernetes Security
Security in Kubernetes is critical for protecting your applications, data, and infrastructure. Kubernetes provides a range of tools and mechanisms to ensure a secure and robust environment, including Role-Based Access Control (RBAC), Network Policies, Pod Security Standards, and more.
Why is Security Important?
- Prevent Unauthorized Access: Ensure that only authorized users and services access resources.
- Protect Sensitive Data: Secure secrets and configurations.
- Minimize Attack Surface: Limit the scope of potential vulnerabilities.
- Compliance: Meet industry standards and regulations.
Key Security Concepts in Kubernetes
- Authentication: Verifying the identity of users or services.
- Authorization: Determining access permissions for authenticated users or services.
- RBAC: Fine-grained access control using roles and bindings.
- Secrets Management: Secure storage of sensitive data.
- Pod Security Standards (PSS): Guidelines for configuring Pods securely.
Step-by-Step Implementation
Step 1: Understanding Authentication
Kubernetes Authentication Mechanisms
- Client Certificates: Used by kubelets and users.
- Bearer Tokens: Service account tokens.
- External Authentication: OAuth2, LDAP, or custom webhook authenticators.
Verify current authentication methods:
kubectl config view
Authenticate using a token:
kubectl --token=<your-token> get pods
Step 2: Setting Up RBAC
RBAC allows you to define roles with specific permissions and bind them to users or service accounts.
RBAC Components
- Role: Defines permissions within a namespace.
- ClusterRole: Defines permissions cluster-wide.
- RoleBinding: Assigns a Role to a user or group in a namespace.
- ClusterRoleBinding: Assigns a ClusterRole to a user or group cluster-wide.
Example 1: Creating a Role
Create a Role YAML (role-example.yaml):
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch", "list"]
Apply the Role:
kubectl apply -f role-example.yaml
Example 2: Creating a RoleBinding
Create a RoleBinding YAML (rolebinding-example.yaml):
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods
namespace: default
subjects:
- kind: User
name: jane
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
Apply the RoleBinding:
kubectl apply -f rolebinding-example.yaml
Verify Access
Switch to the user jane and test:
kubectl auth can-i list pods --namespace=default
Example 3: Using a ClusterRole
ClusterRoles are useful for granting cluster-wide permissions.
Create a ClusterRole YAML (clusterrole-example.yaml):
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: cluster-admin-reader
rules:
- apiGroups: [""]
resources: ["*"]
verbs: ["get", "list"]
Apply the ClusterRole:
kubectl apply -f clusterrole-example.yaml
Create a ClusterRoleBinding:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: read-all-cluster
subjects:
- kind: User
name: admin
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: cluster-admin-reader
apiGroup: rbac.authorization.k8s.io
Apply the ClusterRoleBinding:
kubectl apply -f clusterrolebinding-example.yaml
Step 3: Securing Secrets
Kubernetes Secrets store sensitive data such as passwords and tokens securely.
Create a Secret
kubectl create secret generic db-password --from-literal=password=supersecret
View the Secret (base64-encoded):
kubectl get secret db-password -o yaml
Decode the Secret:
echo "c3VwZXJzZWNyZXQ=" | base64 --decode
Use the Secret in a Pod:
apiVersion: v1
kind: Pod
metadata:
name: db-app
spec:
containers:
- name: app
image: nginx
env:
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db-password
key: password
Step 4: Implementing Pod Security Standards (PSS)
Enforcing Pod Security
Use Kubernetes admission controllers or policies to enforce security.
Example Policy: Deny Privileged Containers
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
name: restricted
spec:
privileged: false
runAsUser:
rule: MustRunAsNonRoot
seLinux:
rule: RunAsAny
supplementalGroups:
rule: MustRunAs
fsGroup:
rule: MustRunAs
Apply the policy:
kubectl apply -f psp-restricted.yaml
Monitoring and Troubleshooting Security
Audit Logs: Analyze cluster activity.
kubectl logs -n kube-system <audit-log-pod>
RBAC Diagnostics:
kubectl auth can-i --list
Check Unauthorized Access:
kubectl describe rolebinding <rolebinding-name>
Best Practices
- Follow Least Privilege Principle: Grant minimal permissions.
- Rotate Secrets Regularly: Avoid long-lived credentials.
- Enable Network Policies: Restrict Pod communication.
- Monitor User Actions: Use tools like Falco for real-time alerts.
- Review RBAC Rules: Periodically audit permissions.
Production Example: Secure a Multi-Tier Application
Scenario
Deploy a secure multi-tier web application with RBAC and secrets.
Deployment Example
- Frontend: Exposes a public endpoint.
- Backend: Access restricted via RBAC.
Apply an RBAC Role for the backend:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: backend
name: backend-access
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list"]
Create the RoleBinding for the backend:
subjects:
- kind: ServiceAccount
name: backend-sa
roleRef:
kind: Role
name: backend-access
apiGroup: rbac.authorization.k8s.io
Conclusion
In this chapter, you learned:
- How to use RBAC to secure cluster resources.
- How to manage sensitive data with Secrets.
- How to enforce Pod Security Standards.