Preview - Secure your cluster using pod security policies in Azure Kubernetes Service (AKS)
Warning
The feature described in this document, pod security policy (preview), is set for deprecation and will no longer be available after May 31st, 2021 in favor of Azure Policy for AKS. The deprecation date has been extended from the previous date of October 15th, 2020.
After pod security policy (preview) is deprecated, you must disable the feature on any existing clusters using the deprecated feature to perform future cluster upgrades and stay within Azure support.
It is highly recommended to begin testing scenarios with Azure Policy for AKS, which offers built-in policies to secure pods and built-in initiatives which map to pod security policies. Click here to learn about migrating to Azure Policy from pod security policy (preview).
To improve the security of your AKS cluster, you can limit what pods can be scheduled. Pods that request resources you don't allow can't run in the AKS cluster. You define this access using pod security policies. This article shows you how to use pod security policies to limit the deployment of pods in AKS.
Important
AKS preview features are available on a self-service, opt-in basis. Previews are provided "as is" and "as available," and they're excluded from the service-level agreements and limited warranty. AKS previews are partially covered by customer support on a best-effort basis. As such, these features aren't meant for production use. AKS preview features aren't available in Azure Government or Azure China 21Vianet clouds. For more information, see the following support articles:
Before you begin
This article assumes that you have an existing AKS cluster. If you need an AKS cluster, see the AKS quickstart using the Azure CLI or using the Azure portal.
You need the Azure CLI version 2.0.61 or later installed and configured. RunĀ az --version
to find the version. If you need to install or upgrade, seeĀ Install Azure CLI.
Install aks-preview CLI extension
To use pod security policies, you need the aks-preview CLI extension version 0.4.1 or higher. Install the aks-preview Azure CLI extension using the az extension add command, then check for any available updates using the az extension update command:
# Install the aks-preview extension
az extension add --name aks-preview
# Update the extension to make sure you have the latest version installed
az extension update --name aks-preview
Register pod security policy feature provider
This document and feature are set for deprecation on October 15th, 2020.
To create or update an AKS cluster to use pod security policies, first enable a feature flag on your subscription. To register the PodSecurityPolicyPreview feature flag, use the az feature register command as shown in the following example:
az feature register --name PodSecurityPolicyPreview --namespace Microsoft.ContainerService
It takes a few minutes for the status to show Registered. You can check on the registration status using the az feature list command:
az feature list -o table --query "[?contains(name, 'Microsoft.ContainerService/PodSecurityPolicyPreview')].{Name:name,State:properties.state}"
When ready, refresh the registration of the Microsoft.ContainerService resource provider using the az provider register command:
az provider register --namespace Microsoft.ContainerService
Overview of pod security policies
In a Kubernetes cluster, an admission controller is used to intercept requests to the API server when a resource is to be created. The admission controller can then validate the resource request against a set of rules, or mutate the resource to change deployment parameters.
PodSecurityPolicy is an admission controller that validates a pod specification meets your defined requirements. These requirements may limit the use of privileged containers, access to certain types of storage, or the user or group the container can run as. When you try to deploy a resource where the pod specifications don't meet the requirements outlined in the pod security policy, the request is denied. This ability to control what pods can be scheduled in the AKS cluster prevents some possible security vulnerabilities or privilege escalations.
When you enable pod security policy in an AKS cluster, some default policies are applied. These default policies provide an out-of-the-box experience to define what pods can be scheduled. However, cluster users may run into problems deploying pods until you define your own policies. The recommended approach is to:
- Create an AKS cluster
- Define your own pod security policies
- Enable the pod security policy feature
To show how the default policies limit pod deployments, in this article we first enable the pod security policies feature, then create a custom policy.
Enable pod security policy on an AKS cluster
You can enable or disable pod security policy using the az aks update command. The following example enables pod security policy on the cluster name myAKSCluster in the resource group named myResourceGroup.
Note
For real-world use, don't enable the pod security policy until you have defined your own custom policies. In this article, you enable pod security policy as the first step to see how the default policies limit pod deployments.
az aks update \
--resource-group myResourceGroup \
--name myAKSCluster \
--enable-pod-security-policy
Default AKS policies
When you enable pod security policy, AKS creates one default policy named privileged. Don't edit or remove the default policy. Instead, create your own policies that define the settings you want to control. Let's first look at what these default policies are how they impact pod deployments.
To view the policies available, use the kubectl get psp command, as shown in the following example
$ kubectl get psp
NAME PRIV CAPS SELINUX RUNASUSER FSGROUP SUPGROUP READONLYROOTFS VOLUMES
privileged true * RunAsAny RunAsAny RunAsAny RunAsAny false * configMap,emptyDir,projected,secret,downwardAPI,persistentVolumeClaim
The privileged pod security policy is applied to any authenticated user in the AKS cluster. This assignment is controlled by ClusterRoles and ClusterRoleBindings. Use the kubectl get rolebindings command and search for the default:privileged: binding in the kube-system namespace:
kubectl get rolebindings default:privileged -n kube-system -o yaml
As shown in the following condensed output, the psp:privileged ClusterRole is assigned to any system:authenticated users. This ability provides a basic level of privilege without your own policies being defined.
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
[...]
name: default:privileged
[...]
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: psp:privileged
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: Group
name: system:masters
It's important to understand how these default policies interact with user requests to schedule pods before you start to create your own pod security policies. In the next few sections, let's schedule some pods to see these default policies in action.
Create a test user in an AKS cluster
By default, when you use the az aks get-credentials command, the admin credentials for the AKS cluster are added to your kubectl
config. The admin user bypasses the enforcement of pod security policies. If you use Azure Active Directory integration for your AKS clusters, you could sign in with the credentials of a non-admin user to see the enforcement of policies in action. In this article, let's create a test user account in the AKS cluster that you can use.
Create a sample namespace named psp-aks for test resources using the kubectl create namespace command. Then, create a service account named nonadmin-user using the kubectl create serviceaccount command:
kubectl create namespace psp-aks
kubectl create serviceaccount --namespace psp-aks nonadmin-user
Next, create a RoleBinding for the nonadmin-user to perform basic actions in the namespace using the kubectl create rolebinding command:
kubectl create rolebinding \
--namespace psp-aks \
psp-aks-editor \
--clusterrole=edit \
--serviceaccount=psp-aks:nonadmin-user
Create alias commands for admin and non-admin user
To highlight the difference between the regular admin user when using kubectl
and the non-admin user created in the previous steps, create two command-line aliases:
- The kubectl-admin alias is for the regular admin user, and is scoped to the psp-aks namespace.
- The kubectl-nonadminuser alias is for the nonadmin-user created in the previous step, and is scoped to the psp-aks namespace.
Create these two aliases as shown in the following commands:
alias kubectl-admin='kubectl --namespace psp-aks'
alias kubectl-nonadminuser='kubectl --as=system:serviceaccount:psp-aks:nonadmin-user --namespace psp-aks'
Test the creation of a privileged pod
Let's first test what happens when you schedule a pod with the security context of privileged: true
. This security context escalates the pod's privileges. In the previous section that showed the default AKS pod security policies, the privilege policy should deny this request.
Create a file named nginx-privileged.yaml
and paste the following YAML manifest:
apiVersion: v1
kind: Pod
metadata:
name: nginx-privileged
spec:
containers:
- name: nginx-privileged
image: mcr.microsoft.com/oss/nginx/nginx:1.14.2-alpine
securityContext:
privileged: true
Create the pod using the kubectl apply command and specify the name of your YAML manifest:
kubectl-nonadminuser apply -f nginx-privileged.yaml
The pod fails to be scheduled, as shown in the following example output:
$ kubectl-nonadminuser apply -f nginx-privileged.yaml
Error from server (Forbidden): error when creating "nginx-privileged.yaml": pods "nginx-privileged" is forbidden: unable to validate against any pod security policy: []
The pod doesn't reach the scheduling stage, so there are no resources to delete before you move on.
Test creation of an unprivileged pod
In the previous example, the pod specification requested privileged escalation. This request is denied by the default privilege pod security policy, so the pod fails to be scheduled. Let's try now running that same NGINX pod without the privilege escalation request.
Create a file named nginx-unprivileged.yaml
and paste the following YAML manifest:
apiVersion: v1
kind: Pod
metadata:
name: nginx-unprivileged
spec:
containers:
- name: nginx-unprivileged
image: mcr.microsoft.com/oss/nginx/nginx:1.14.2-alpine
Create the pod using the kubectl apply command and specify the name of your YAML manifest:
kubectl-nonadminuser apply -f nginx-unprivileged.yaml
The pod fails to be scheduled, as shown in the following example output:
$ kubectl-nonadminuser apply -f nginx-unprivileged.yaml
Error from server (Forbidden): error when creating "nginx-unprivileged.yaml": pods "nginx-unprivileged" is forbidden: unable to validate against any pod security policy: []
The pod doesn't reach the scheduling stage, so there are no resources to delete before you move on.
Test creation of a pod with a specific user context
In the previous example, the container image automatically tried to use root to bind NGINX to port 80. This request was denied by the default privilege pod security policy, so the pod fails to start. Let's try now running that same NGINX pod with a specific user context, such as runAsUser: 2000
.
Create a file named nginx-unprivileged-nonroot.yaml
and paste the following YAML manifest:
apiVersion: v1
kind: Pod
metadata:
name: nginx-unprivileged-nonroot
spec:
containers:
- name: nginx-unprivileged
image: mcr.microsoft.com/oss/nginx/nginx:1.14.2-alpine
securityContext:
runAsUser: 2000
Create the pod using the kubectl apply command and specify the name of your YAML manifest:
kubectl-nonadminuser apply -f nginx-unprivileged-nonroot.yaml
The pod fails to be scheduled, as shown in the following example output:
$ kubectl-nonadminuser apply -f nginx-unprivileged-nonroot.yaml
Error from server (Forbidden): error when creating "nginx-unprivileged-nonroot.yaml": pods "nginx-unprivileged-nonroot" is forbidden: unable to validate against any pod security policy: []
The pod doesn't reach the scheduling stage, so there are no resources to delete before you move on.
Create a custom pod security policy
Now that you've seen the behavior of the default pod security policies, let's provide a way for the nonadmin-user to successfully schedule pods.
Let's create a policy to reject pods that request privileged access. Other options, such as runAsUser or allowed volumes, aren't explicitly restricted. This type of policy denies a request for privileged access, but otherwise lets the cluster run the requested pods.
Create a file named psp-deny-privileged.yaml
and paste the following YAML manifest:
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
name: psp-deny-privileged
spec:
privileged: false
seLinux:
rule: RunAsAny
supplementalGroups:
rule: RunAsAny
runAsUser:
rule: RunAsAny
fsGroup:
rule: RunAsAny
volumes:
- '*'
Create the policy using the kubectl apply command and specify the name of your YAML manifest:
kubectl apply -f psp-deny-privileged.yaml
To view the policies available, use the kubectl get psp command, as shown in the following example. Compare the psp-deny-privileged policy with the default privilege policy that was enforced in the previous examples to create a pod. Only the use of PRIV escalation is denied by your policy. There are no restrictions on the user or group for the psp-deny-privileged policy.
$ kubectl get psp
NAME PRIV CAPS SELINUX RUNASUSER FSGROUP SUPGROUP READONLYROOTFS VOLUMES
privileged true * RunAsAny RunAsAny RunAsAny RunAsAny false *
psp-deny-privileged false RunAsAny RunAsAny RunAsAny RunAsAny false *
Allow user account to use the custom pod security policy
In the previous step, you created a pod security policy to reject pods that request privileged access. To allow the policy to be used, you create a Role or a ClusterRole. Then, you associate one of these roles using a RoleBinding or ClusterRoleBinding.
For this example, create a ClusterRole that allows you to use the psp-deny-privileged policy created in the previous step. Create a file named psp-deny-privileged-clusterrole.yaml
and paste the following YAML manifest:
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: psp-deny-privileged-clusterrole
rules:
- apiGroups:
- extensions
resources:
- podsecuritypolicies
resourceNames:
- psp-deny-privileged
verbs:
- use
Create the ClusterRole using the kubectl apply command and specify the name of your YAML manifest:
kubectl apply -f psp-deny-privileged-clusterrole.yaml
Now create a ClusterRoleBinding to use the ClusterRole created in the previous step. Create a file named psp-deny-privileged-clusterrolebinding.yaml
and paste the following YAML manifest:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: psp-deny-privileged-clusterrolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: psp-deny-privileged-clusterrole
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: Group
name: system:serviceaccounts
Create a ClusterRoleBinding using the kubectl apply command and specify the name of your YAML manifest:
kubectl apply -f psp-deny-privileged-clusterrolebinding.yaml
Note
In the first step of this article, the pod security policy feature was enabled on the AKS cluster. The recommended practice was to only enable the pod security policy feature after you've defined your own policies. This is the stage where you would enable the pod security policy feature. One or more custom policies have been defined, and user accounts have been associated with those policies. Now you can safely enable the pod security policy feature and minimize problems caused by the default policies.
Test the creation of an unprivileged pod again
With your custom pod security policy applied and a binding for the user account to use the policy, let's try to create an unprivileged pod again. Use the same nginx-privileged.yaml
manifest to create the pod using the kubectl apply command:
kubectl-nonadminuser apply -f nginx-unprivileged.yaml
The pod is successfully scheduled. When you check the status of the pod using the kubectl get pods command, the pod is Running:
$ kubectl-nonadminuser get pods
NAME READY STATUS RESTARTS AGE
nginx-unprivileged 1/1 Running 0 7m14s
This example shows how you can create custom pod security policies to define access to the AKS cluster for different users or groups. The default AKS policies provide tight controls on what pods can run, so create your own custom policies to then correctly define the restrictions you need.
Delete the NGINX unprivileged pod using the kubectl delete command and specify the name of your YAML manifest:
kubectl-nonadminuser delete -f nginx-unprivileged.yaml
Clean up resources
To disable pod security policy, use the az aks update command again. The following example disables pod security policy on the cluster name myAKSCluster in the resource group named myResourceGroup:
az aks update \
--resource-group myResourceGroup \
--name myAKSCluster \
--disable-pod-security-policy
Next, delete the ClusterRole and ClusterRoleBinding:
kubectl delete -f psp-deny-privileged-clusterrolebinding.yaml
kubectl delete -f psp-deny-privileged-clusterrole.yaml
Delete the security policy using kubectl delete command and specify the name of your YAML manifest:
kubectl delete -f psp-deny-privileged.yaml
Finally, delete the psp-aks namespace:
kubectl delete namespace psp-aks
Next steps
This article showed you how to create a pod security policy to prevent the use of privileged access. There are lots of features that a policy can enforce, such as type of volume or the RunAs user. For more information on the available options, see the Kubernetes pod security policy reference docs.
For more information about limiting pod network traffic, see Secure traffic between pods using network policies in AKS.