WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Kubernetes · Advanced · question 47 of 100

Discuss the key security features in Kubernetes, such as Role-Based Access Control (RBAC), Network Policies, and Pod Security Policies (PSP).?

📕 Buy this interview preparation book: 100 Kubernetes questions & answers — PDF + EPUB for $5

Kubernetes provides several security features to ensure the safety and security of the cluster, such as Role-Based Access Control (RBAC), Network Policies, and Pod Security Policies (PSP).

Role-Based Access Control (RBAC): RBAC is a method of regulating access to resources in a Kubernetes cluster. It allows administrators to define specific roles and permissions for users and groups within the cluster. Roles can be defined for specific namespaces or for the entire cluster. Users can be granted different levels of access, such as read-only, read-write, or full control. RBAC also allows for easy management of access permissions, as roles and users can be easily added, modified, or removed.

To create a RoleBinding that grants a user read-only access to a specific namespace, we can use the following example YAML:

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: read-only-access
  namespace: my-namespace
subjects:
- kind: User
  name: john
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: read-only-role
  apiGroup: rbac.authorization.k8s.io

This RoleBinding grants user "john" read-only access to the "my-namespace" namespace using the "read-only-role" Role.

Network Policies: Network Policies in Kubernetes are used to control the network traffic flow between Pods and/or from external sources. They define rules that allow or deny traffic based on various criteria such as source and destination IP addresses, ports, and protocols. Network Policies can be defined for specific Pods or for entire namespaces.

To create a Network Policy that allows traffic only from a specific IP address range to a set of Pods, we can use the following example YAML:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-from-ip-range
  namespace: my-namespace
spec:
  podSelector:
    matchLabels:
      app: my-app
  policyTypes:
  - Ingress
  ingress:
  - from:
    - ipBlock:
        cidr: 10.0.0.0/8
    ports:
    - protocol: TCP
      port: 80

This Network Policy allows traffic only from the IP address range "10.0.0.0/8" to Pods with the label "app=my-app" in the "my-namespace" namespace on port 80.

Pod Security Policies (PSP): Pod Security Policies are used to restrict the actions that can be performed by Pods within a Kubernetes cluster. They define a set of security constraints that must be met before a Pod can be scheduled to run. PSPs can be used to limit the use of privileged containers, restrict the use of host namespaces and devices, and control the use of security contexts.

To create a Pod Security Policy that restricts the use of privileged containers, we can use the following example YAML:

apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
  name: restrict-privileged-containers
spec:
  privileged: false
  allowPrivilegeEscalation: false
  allowedCapabilities:
  - "NET\_ADMIN"
  - "SYS\_TIME"
  seLinux:
    rule: RunAsAny
  supplementalGroups:
    rule: RunAsAny
  fsGroup:
    rule: RunAsAny
  runAsUser:
    rule: RunAsAny

This Pod Security Policy restricts the use of privileged containers by setting "privileged" and "allowPrivilegeEscalation" to false. It also allows only the "NET_ADMIN"

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Kubernetes interview — then scores it.
📞 Practice Kubernetes — free 15 min
📕 Buy this interview preparation book: 100 Kubernetes questions & answers — PDF + EPUB for $5

All 100 Kubernetes questions · All topics