This chapter covers security for Azure Kubernetes Service (AKS), focusing on three critical areas: Kubernetes RBAC, network policies for pod traffic control, and Pod Identity for accessing Azure resources. These topics are central to the Compute Security domain (Objective 2.2) of the AZ-500 exam, and together they represent approximately 15-20% of exam questions. Mastering these concepts is essential for securing containerized workloads in Azure, as misconfigurations are a leading cause of breaches in production clusters.
Jump to a section
Imagine a large apartment building (the AKS cluster) with many residents (pods). The building has a main entrance (API server) with a security guard who checks IDs (Azure AD for RBAC). Each resident has a keycard that grants access only to certain floors or rooms (Kubernetes RBAC roles and role bindings). Inside the building, there are internal doors between apartments (network policies) that can be locked to prevent residents from entering each other's units unless explicitly allowed. Some residents need to pick up packages from the lobby using a shared mailbox (Pod Identity). The mailbox is accessed by presenting a special token (Azure AD pod identity) that proves the resident's identity to the mailroom clerk (Azure Instance Metadata Service). The clerk then retrieves the package (accesses Azure resources like Key Vault) and hands it to the resident. Critically, the resident never handles the master key to the mailroom; the identity is bound to the pod's existence. If a resident moves out (pod terminates), the token is revoked automatically. This analogy maps directly: RBAC controls who can enter and what they can do, network policies control pod-to-pod traffic, and Pod Identity gives pods a verifiable identity to access Azure services without embedding secrets.
1. Kubernetes RBAC in AKS
Kubernetes RBAC (Role-Based Access Control) is the primary mechanism for controlling access to the Kubernetes API server. In AKS, RBAC is enabled by default and integrated with Azure Active Directory (Azure AD) for user authentication, but Kubernetes RBAC governs authorization for both users and service accounts.
How it works:
- Roles and ClusterRoles: A Role defines a set of permissions (verbs on resources) within a specific namespace. A ClusterRole is similar but cluster-scoped (non-namespaced resources like nodes, or can be used across all namespaces).
- RoleBindings and ClusterRoleBindings: These bind a Role/ClusterRole to a subject (user, group, or service account). RoleBindings are namespace-scoped; ClusterRoleBindings are cluster-scoped.
- Verbs: Typical verbs include get, list, watch, create, update, patch, delete. The exam tests knowledge of which verbs apply to which resources.
- Resources: Examples: pods, deployments, services, secrets, configmaps, nodes, namespaces. Note that secrets and configmaps are often tested.
Default values and timers: There are no timers in RBAC; changes are effective immediately upon applying the binding. However, cached authorization decisions may take up to 5 minutes to refresh in some configurations, but this is not a default behavior.
Configuration example:
Create a Role that allows reading pods in the default namespace:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]Bind this role to a user:
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods
namespace: default
subjects:
- kind: User
name: "alice@contoso.com"
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.ioVerification commands:
kubectl get roles --all-namespaces
kubectl get rolebindings --all-namespaces
kubectl describe role pod-reader -n default
kubectl auth can-i get pods --as alice@contoso.com -n defaultInteraction with Azure AD: For human users, AKS integrates Azure AD to authenticate. The user's Azure AD token is presented to the API server, which validates it via Azure AD. Then Kubernetes RBAC authorizes the request. For service accounts (used by pods), Kubernetes issues its own tokens, but Pod Identity (discussed later) can be used to integrate with Azure AD for pod-level identities.
2. Network Policies
Network policies in Kubernetes are firewall rules that control traffic between pods at the IP address or port level. They are implemented by a network plugin (e.g., Azure CNI, Calico). In AKS, network policies can be enforced using Azure Network Policies (built-in) or Calico (third-party).
How it works:
- Network policies are namespace-scoped and are applied to pods via label selectors.
- A policy consists of podSelector (target pods), policyTypes (Ingress, Egress, or both), ingress rules (allowed inbound traffic), and egress rules (allowed outbound traffic).
- By default, all pod-to-pod traffic is allowed. Once any network policy selects a pod, that pod becomes isolated: only traffic explicitly allowed by the policy is permitted.
- Rules can specify from/to using podSelector, namespaceSelector, ipBlock, or a combination.
Key components and defaults:
- PodSelector: If empty, the policy applies to all pods in the namespace.
- PolicyTypes: If not specified, defaults to Ingress if any ingress rules exist, similarly for egress. But best practice is to specify both.
- ipBlock: Used for traffic to/from outside the cluster (e.g., Azure SQL database). Must specify CIDR and optional except.
- Ports: Can specify port number and protocol (TCP, UDP, SCTP). Default protocol is TCP if not specified.
Example network policy:
Allow only frontend pods to communicate with backend pods on port 6379:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: backend-policy
namespace: default
spec:
podSelector:
matchLabels:
app: backend
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
ports:
- port: 6379Verification commands:
kubectl get networkpolicy
kubectl describe networkpolicy backend-policyHow it interacts with related technologies: - In AKS, network policies require a network policy engine. Azure Network Policies is the default for clusters using Azure CNI. Calico can be used as an alternative, offering more advanced features like global network policies and egress access controls. - Network policies do not affect pod-to-service traffic (the service IP is virtual; the policy applies to the actual pod IPs). - For DNS resolution, pods need egress access to the DNS service IP (typically 10.0.0.10 in AKS). A common misconfiguration is blocking DNS, causing name resolution failures.
3. Pod Identity
Azure AD Pod Identity enables pods in AKS to authenticate to Azure services (e.g., Key Vault, Storage, SQL Database) using a managed identity assigned to the pod, without embedding credentials. It is an add-on that must be enabled on the AKS cluster.
How it works:
- The Pod Identity add-on deploys two components: the Node Management Identity (NMI) daemon and the Managed Identity Controller (MIC).
- A pod is assigned an Azure AD identity through a custom resource called AzureIdentity and AzureIdentityBinding.
- When a pod makes a request to Azure Instance Metadata Service (IMDS) endpoint (169.254.169.254), NMI intercepts the request, checks if the pod has a matching identity, and acquires a token from Azure AD on behalf of the pod.
- The token is then returned to the pod, which can use it to access Azure resources.
Key components: - AzureIdentity: Defines the Azure AD managed identity (user-assigned) that will be used by pods. It specifies the resource ID of the managed identity. - AzureIdentityBinding: Links the AzureIdentity to a pod based on a selector label. Pods with a matching label are allowed to use that identity. - Managed Identity Controller (MIC): Watches for changes to identities and bindings, and updates the NMI daemon with the mapping. - Node Management Identity (NMI): Runs as a DaemonSet on each node, intercepts IMDS requests, and acquires tokens.
Configuration example:
Create a user-assigned managed identity in Azure.
Deploy the Pod Identity add-on (if not enabled during cluster creation):
az aks enable-addons --addons pod-identity --name myAKSCluster --resource-group myResourceGroupCreate AzureIdentity and AzureIdentityBinding:
apiVersion: "aadpodidentity.k8s.io/v1"
kind: AzureIdentity
metadata:
name: my-identity
spec:
type: 0 # User-assigned managed identity
resourceID: /subscriptions/<sub>/resourcegroups/<rg>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/my-identity
clientID: <client-id-of-identity>
---
apiVersion: "aadpodidentity.k8s.io/v1"
kind: AzureIdentityBinding
metadata:
name: my-identity-binding
spec:
azureIdentity: my-identity
selector: my-labelDeploy a pod with label aadpodidbinding: my-label:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
labels:
aadpodidbinding: my-label
spec:
containers:
- name: my-container
image: my-imageThe pod can now request a token using the IMDS endpoint.
Verification commands:
kubectl get azureidentity
kubectl get azureidentitybinding
kubectl describe pod my-podInteraction with related technologies: - Pod Identity is often used with Azure Key Vault via the Key Vault FlexVolume driver or CSI driver, allowing pods to mount secrets as files. - It replaces the need for service principal credentials in container images. - The pod identity is bound to the pod's lifetime; if the pod is deleted, the identity assignment is removed.
Common pitfalls: - The managed identity must have appropriate RBAC permissions on the target Azure resource (e.g., Key Vault access policy). - Pod Identity does not work with system-assigned managed identities; only user-assigned. - The NMI modifies iptables rules on the node to intercept IMDS traffic; this can conflict with other network plugins if not properly configured.
Enable Azure AD Integration
For Kubernetes RBAC with Azure AD, first, create an Azure AD application and service principal for the AKS cluster. Configure the cluster with `--enable-aad` flag during creation or enable it later. This integrates Azure AD as the authentication source. The API server validates tokens by querying Azure AD. Once enabled, users authenticate with `kubectl` using their Azure AD credentials. The AKS cluster returns a kubeconfig with the user's token. This step is foundational because without it, Kubernetes RBAC cannot use Azure AD identities.
Define Kubernetes RBAC Roles
Create Roles or ClusterRoles that specify allowed verbs on resources. For example, a developer may need `get`, `list`, `watch` on pods in their namespace. Use `kubectl create role` or YAML manifests. Roles are namespace-scoped; ClusterRoles are cluster-scoped. Apply the manifest with `kubectl apply -f role.yaml`. After creation, verify with `kubectl get roles`. The exam often tests the difference between Role and ClusterRole, especially for resources like `nodes` that are cluster-scoped.
Bind Roles to Azure AD Users/Groups
Create RoleBindings or ClusterRoleBindings to associate the Role/ClusterRole with an Azure AD user or group. For example, bind the `pod-reader` Role to the group `developers@contoso.com`. Use `kubectl create rolebinding` or YAML. The subject must specify `apiGroup: rbac.authorization.k8s.io`. After binding, users in that group can perform the allowed actions. Test with `kubectl auth can-i --as <user>`.
Apply Network Policies
Define network policies to restrict pod-to-pod traffic. Start by identifying which pods need to communicate. Create a YAML manifest with `podSelector`, `policyTypes`, and `ingress`/`egress` rules. Apply using `kubectl apply -f networkpolicy.yaml`. The policy takes effect immediately. Verify with `kubectl describe networkpolicy` and by testing connectivity from a test pod. Remember that once a policy selects a pod, all traffic not explicitly allowed is dropped.
Deploy Pod Identity Add-on
Enable the Pod Identity add-on on the AKS cluster using `az aks enable-addons --addons pod-identity`. This deploys NMI and MIC. Next, create a user-assigned managed identity in Azure and grant it permissions (e.g., Key Vault access). Then create `AzureIdentity` and `AzureIdentityBinding` custom resources in the cluster. Finally, deploy pods with the label matching the selector. The pod can now request tokens via IMDS.
Scenario 1: Microservices with Strict Isolation
A financial services company runs a microservices application on AKS where each microservice is in its own namespace. They need to ensure that only the frontend service can talk to the backend service, and the backend service can only talk to the database. They implement Kubernetes network policies: a policy in the frontend namespace allows egress to backend namespace on port 8080, and a policy in the backend namespace allows ingress from frontend on port 8080 and egress to database service IP on port 5432. They also use Azure Network Policies for simplicity. In production, they run 50 namespaces with hundreds of pods. Performance is not impacted because network policies are enforced at the node level using iptables. Common issues: forgetting to allow DNS egress (port 53) causes pod startup failures. They monitor network policy logs using Azure Monitor for containers.
Scenario 2: Accessing Azure Key Vault from Pods
A healthcare startup stores patient data in Azure Key Vault. Their AKS pods need to retrieve secrets (API keys, connection strings) without embedding them in images. They enable Pod Identity add-on and create a user-assigned managed identity with Key Vault Secrets Officer role. They deploy AzureIdentity and AzureIdentityBinding for each microservice. The application code uses the Azure SDK to request a token from IMDS (169.254.169.254) and then calls Key Vault. They use the Key Vault CSI driver to mount secrets as volumes, which simplifies code. In production, they have 20 identities and 200 pods. Issues: if the NMI daemon fails, pods cannot get tokens; they run multiple replicas of NMI to ensure availability. They also set --enable-pod-identity-pod-identity-exception to allow certain pods to bypass the interception.
Scenario 3: Multi-tenant AKS with RBAC
An enterprise runs a shared AKS cluster for multiple teams. Each team has its own namespace. They integrate Azure AD and use Azure AD groups for access control. The platform team has cluster-admin via a ClusterRoleBinding. Each team has a custom Role that allows full control within their namespace. They use kubectl with Azure AD authentication. They enforce that no one can create RoleBindings that grant cluster-admin. They audit RBAC changes using Azure Policy for Kubernetes. In production, they have 500 users across 10 teams. Issues: users often request more permissions than needed; they use kubectl auth can-i to verify before granting. They also use Azure AD Privileged Identity Management (PIM) for temporary elevation.
What AZ-500 Tests on This Topic (Objective 2.2)
The exam focuses on:
- Kubernetes RBAC: Difference between Role and ClusterRole, RoleBinding and ClusterRoleBinding, verbs and resources, integration with Azure AD. Be able to read a YAML manifest and identify if it grants cluster-scoped or namespace-scoped access.
- Network Policies: Default behavior (allow all), effect of selecting a pod (deny all except allowed), syntax of podSelector, namespaceSelector, ipBlock. Know that network policies are namespace-scoped and that they do not affect traffic to Services.
- Pod Identity: Components (NMI, MIC), custom resources (AzureIdentity, AzureIdentityBinding), how token acquisition works, requirement for user-assigned managed identity, and that it does not work with system-assigned identities.
Common Wrong Answers and Why
"Network policies apply to Services." Wrong: Network policies apply to pod IPs, not service IPs. Traffic to a service is load-balanced to pod IPs, and the policy is evaluated on the actual pod IP.
"Pod Identity uses system-assigned managed identity." Wrong: Only user-assigned managed identities are supported. System-assigned identities are tied to the VMSS node, not the pod.
"RBAC with Azure AD requires a ClusterRoleBinding for every user." Wrong: You can bind to Azure AD groups, and users inherit permissions via group membership.
"Network policies deny all traffic by default." Wrong: The default is allow all. Deny only occurs after a policy selects a pod.
Specific Numbers and Terms
IMDS endpoint: 169.254.169.254
Default DNS service IP in AKS: 10.0.0.10
Pod Identity add-on name: pod-identity
Verbs: get, list, watch, create, update, patch, delete
Resources: pods, secrets, configmaps, deployments, nodes
Edge Cases
If a pod is selected by multiple network policies, the union of all allowed traffic is permitted (logical OR).
ClusterRoles can be used in RoleBindings to grant cluster-level permissions within a namespace (e.g., cluster-admin RoleBinding in a namespace grants full control over that namespace).
Pod Identity does not support pods using hostNetwork: true.
How to Eliminate Wrong Answers
If a question asks about restricting traffic between pods, look for "network policy" not "RBAC" or "Pod Identity".
If a question involves accessing Azure resources without secrets, think Pod Identity.
If a question involves controlling what kubectl commands a user can run, think RBAC.
Remember: RBAC controls API access, network policies control pod traffic, Pod Identity controls Azure resource access.
Kubernetes RBAC in AKS is integrated with Azure AD for user authentication but uses Kubernetes RBAC for authorization.
Roles are namespace-scoped; ClusterRoles are cluster-scoped. RoleBindings bind to namespaces; ClusterRoleBindings bind cluster-wide.
Network policies are namespace-scoped and only affect pod-to-pod traffic, not Service IPs.
Default pod-to-pod traffic is allow-all; once a policy selects a pod, only explicitly allowed traffic is permitted.
Pod Identity requires user-assigned managed identities and the `pod-identity` add-on.
Pod Identity uses NMI to intercept IMDS requests (169.254.169.254) and acquires tokens on behalf of pods.
AzureIdentity and AzureIdentityBinding custom resources define the identity mapping to pods.
Network policies can use `ipBlock` for traffic to/from external IPs, e.g., Azure SQL.
The exam tests the difference between Role and ClusterRole, and which resources are cluster-scoped (e.g., nodes).
Pod Identity does not support pods with hostNetwork: true.
These come up on the exam all the time. Here's how to tell them apart.
Azure Network Policies
Built into Azure CNI, no extra installation
Supports basic ingress/egress rules with podSelector, namespaceSelector, ipBlock
Limited to standard Kubernetes NetworkPolicy API
Managed by Azure, less flexibility
No support for global network policies or advanced features like egress access controls
Calico Network Policies
Requires installation as a third-party add-on
Supports extended API with global network policies, host endpoints, and network sets
More granular control including policy tiers and order of precedence
Open source, community-supported
Supports egress access controls and application layer policies (L7)
Mistake
Network policies can restrict traffic to Services.
Correct
Network policies apply to pod IPs, not Service virtual IPs. Traffic to a Service is redirected to pod IPs via kube-proxy, and the policy is evaluated on the destination pod IP.
Mistake
Pod Identity works with system-assigned managed identities.
Correct
Pod Identity only supports user-assigned managed identities. System-assigned identities are tied to the VMSS node and cannot be assigned to individual pods.
Mistake
By default, Kubernetes denies all pod-to-pod traffic.
Correct
The default is allow all. Only after a network policy selects a pod does it become isolated, and only explicitly allowed traffic is permitted.
Mistake
RBAC with Azure AD requires each user to have a separate RoleBinding.
Correct
You can bind a Role to an Azure AD group, and all group members inherit the permissions. This is more scalable.
Mistake
Network policies are cluster-scoped.
Correct
Network policies are namespace-scoped. They only apply to pods within the same namespace as the policy.
Reveal each answer, then mark whether you got it right. Score 60%+ to unlock the next chapter.
A Role grants permissions within a specific namespace. A ClusterRole grants permissions cluster-wide (non-namespaced resources like nodes) or can be used in RoleBindings across all namespaces. For example, a Role can grant access to pods in the 'dev' namespace, while a ClusterRole can grant access to nodes or to pods in any namespace via a RoleBinding. On the exam, remember that non-namespaced resources (nodes, persistentvolumes) require a ClusterRole.
Use Kubernetes NetworkPolicy resources. Create a YAML manifest with `podSelector` to target pods, `policyTypes` (Ingress/Egress), and rules specifying allowed sources/destinations and ports. Apply with `kubectl apply`. Ensure a network policy engine (Azure Network Policies or Calico) is enabled. Remember that the default is allow all, and once a policy selects a pod, only explicitly allowed traffic is permitted.
No, Pod Identity only supports user-assigned managed identities. System-assigned identities are tied to the Azure VMSS underlying the node, not to individual pods. You must create a user-assigned managed identity, grant it permissions on target resources, and reference it in the AzureIdentity custom resource.
The Azure Instance Metadata Service (IMDS) endpoint is at 169.254.169.254. Pod Identity's NMI daemon intercepts requests to this endpoint from pods that have an associated identity. NMI then acquires an Azure AD token on behalf of the pod using the assigned managed identity and returns it to the pod. The pod can then use the token to authenticate to Azure services.
Use the `kubectl auth can-i` command. For example: `kubectl auth can-i get pods --as alice@contoso.com -n default`. This checks whether the user is authorized for the action. You can also use `kubectl describe rolebinding` to see bindings. On the exam, know that this command is the primary verification tool.
No, network policies apply to pod-to-pod traffic at the IP level. Traffic to a Service's ClusterIP or LoadBalancer IP is not subject to network policies. However, once the traffic is forwarded to a backend pod, the network policy on that pod applies. So, if a pod only allows ingress from a specific pod, traffic from a LoadBalancer Service will be dropped unless the source IP matches (which it won't, because it's the node's IP).
The policies are additive: the pod will allow traffic that matches any of the ingress/egress rules across all policies that select it. It's a logical OR of all allowed traffic. There is no priority; all policies are evaluated together. This is a common exam point: multiple policies do not override each other; they combine.
You've just covered AKS Security: RBAC, Network Policies, Pod Identity — now see how well it sticks with free AZ-500 practice questions. Full explanations included, no account needed.
Done with this chapter?