After this lesson, you will be able to: Use namespaces for env separation, configure RBAC, set resource quotas, and enable horizontal pod autoscaling.
Production K8s is more than YAML. This lesson covers the four practices that turn a learning cluster into a real one.
Use namespaces to isolate environments inside ONE cluster (staging + production) or teams (team-a + team-b). Each namespace has its own quotas, RBAC, network policies. Pods in different namespaces still share the cluster network by default. Convention: kebab-case names (`prod`, `staging`, `team-foo`). Default namespace exists; use it only for one-off testing. Switch namespaces: `kubectl config set-context --current --namespace=prod` or use `kubens` (a one-liner switcher).
ServiceAccounts for apps, Roles for permissions, RoleBindings to glue them together.
# A read-only role for the staging namespaceapiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata:namespace: stagingname: read-onlyrules:- apiGroups: [""]resources: [pods, services, configmaps]verbs: [get, list, watch]---apiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata:namespace: stagingname: alice-readsubjects:- kind: Username: alice@example.com- kind: ServiceAccountname: cicdnamespace: stagingroleRef:apiGroup: rbac.authorization.k8s.iokind: Rolename: read-only# Cluster-wide variants: ClusterRole + ClusterRoleBinding (use sparingly)
Cap CPU, memory, object counts per namespace.
apiVersion: v1kind: ResourceQuotametadata:name: staging-quotanamespace: stagingspec:hard:requests.cpu: "10" # total CPU requests across all Pods in this nsrequests.memory: 20Gilimits.cpu: "20"limits.memory: 40Gipersistentvolumeclaims: "10"services.loadbalancers: "2" # cap public LBspods: "50"
Scale Pods up/down based on CPU or custom metrics.
apiVersion: autoscaling/v2kind: HorizontalPodAutoscalermetadata:name: myapp-hpaspec:scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: myappminReplicas: 2maxReplicas: 20metrics:- type: Resourceresource:name: cputarget:type: UtilizationaverageUtilization: 70 # scale up when avg CPU > 70%# Verifykubectl get hpakubectl top pods # current CPU/memory (needs metrics-server installed)# Custom metrics (requests-per-second, queue depth):# Install Prometheus + KEDA for event-driven autoscaling.
By default, every Pod can talk to every other Pod. NetworkPolicy locks that down. `Deny all` policy: deny ingress by default in a namespace, then explicitly allow what should flow. Massive defense-in-depth win: a compromised web Pod can't reach the DB without explicit allow. Requires a CNI that supports policies (Calico, Cilium). Not enforced by default in kind/minikube.
Everyone has cluster-admin. Audit; most engineers need namespace-scoped Roles, not ClusterRoles. Skipping ResourceQuota. One bad team / app starves everyone. HPA without resource requests on the Pod. K8s can't compute utilization without a baseline. Setting maxReplicas too high. Autoscaling can rack up cloud bills fast on a bad metric. Forgetting NetworkPolicies. Most clusters are flat networks; compromise of one Pod = compromise of all.
Pick the canonical bug.
Sign in and purchase access to unlock this lesson.