Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🌐 Introduction
While Kubernetes excels at automating deployment and
scaling, security and cost optimization often become afterthoughts —
until vulnerabilities are exploited or cloud bills spike.
In this final chapter, we focus on:
🔐 Section 1: Kubernetes
Security Essentials
Securing a Kubernetes cluster means hardening multiple
layers: infrastructure, network, pods, APIs, and CI/CD.
✅ 1.1 Role-Based Access Control
(RBAC)
RBAC regulates who can do what.
🔧 Sample Role &
RoleBinding
yaml
apiVersion:
rbac.authorization.k8s.io/v1
kind:
Role
metadata:
name: pod-reader
rules:
-
apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch",
"list"]
yaml
kind:
RoleBinding
apiVersion:
rbac.authorization.k8s.io/v1
metadata:
name: read-pods
subjects:
-
kind: User
name: dev-user
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
✅ 1.2 Pod Security Admission
(PSA)
Kubernetes 1.25+ replaces PodSecurityPolicies (PSP) with Pod
Security Admission.
Profile |
Description |
privileged |
Allows all settings
(not recommended) |
baseline |
Reasonable
defaults for apps |
restricted |
Strictest (no root,
host access, etc.) |
Apply via namespace labels:
bash
kubectl
label ns dev pod-security.kubernetes.io/enforce=restricted
✅ 1.3 Secrets & ConfigMaps
Example Secret:
yaml
apiVersion:
v1
kind:
Secret
metadata:
name: db-password
type:
Opaque
data:
password: bXktc2VjcmV0LXBhc3M= # base64 encoded
✅ 1.4 Network Policies
Restrict traffic between pods and namespaces.
Example: Deny all ingress by default
yaml
apiVersion:
networking.k8s.io/v1
kind:
NetworkPolicy
metadata:
name: deny-all
spec:
podSelector: {}
policyTypes:
- Ingress
✅ 1.5 Security Contexts
Run containers with least privilege.
yaml
securityContext:
runAsUser: 1000
runAsNonRoot: true
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
💰 Section 2: Cost
Optimization Strategies
Cost creeps up when:
🔍 2.1 Set CPU &
Memory Limits
Overprovisioning leads to wasted capacity.
yaml
resources:
requests:
memory: "256Mi"
cpu: "200m"
limits:
memory: "512Mi"
cpu: "500m"
📈 2.2 Use Autoscaling
Component |
Benefit |
HorizontalPodAutoscaler |
Scale apps based on
load |
Cluster Autoscaler |
Scale worker
nodes on demand |
VPA (initial mode) |
Suggest optimized
resource usage |
🧹 2.3 Clean Up Unused
Resources
Use:
bash
kubectl
get all --all-namespaces
kubectl
delete pod <pod-name>
kubectl
delete pvc <volume-name>
📦 2.4 Optimize Logging
& Monitoring
💰 2.5 Spot/Preemptible
Instances
In cloud environments:
🧠 Section 3: Kubernetes
Best Practices
🧩 3.1 Namespaces for
Organization
Separate workloads by team or environment:
bash
kubectl
create namespace dev
kubectl
create namespace prod
Use resource quotas to limit usage per namespace.
⚙️ 3.2 Liveness and Readiness
Probes
Ensure only healthy pods receive traffic.
yaml
readinessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
🔐 3.3 Use TLS and Secrets
for Communication
🧪 3.4 CI/CD & GitOps
Use:
🔍 3.5 Enable Audit
Logging
Track access and resource changes.
bash
--audit-log-path=/var/log/kubernetes/audit.log
--audit-policy-file=/etc/kubernetes/audit-policy.yaml
🧼 3.6 Delete Completed
Jobs & Orphaned PVCs
bash
kubectl
delete jobs --field-selector=status.successful=1
kubectl
get pvc --no-headers | awk '{print $1}' | xargs kubectl delete pvc
🛡️ Section 4: CI/CD
Security Tips
✅ Summary
Securing and scaling Kubernetes apps requires continuous guardrails,
not one-time configurations.
Key takeaways:
With security-first practices and proactive cost control,
your Kubernetes environment becomes resilient, affordable, and
production-ready.
Answer:
Kubernetes automates deployment, scaling, and management of containerized
applications. It offers built-in features like horizontal pod autoscaling,
load balancing, and self-healing, allowing applications to handle
traffic spikes and system failures efficiently.
Answer:
Answer:
HPA monitors metrics like CPU or memory usage and automatically adjusts the
number of pods in a deployment to meet demand. It uses the Kubernetes Metrics
Server or custom metrics APIs.
Answer:
Yes. The Cluster Autoscaler automatically adjusts the number of nodes in
a cluster based on resource needs, ensuring pods always have enough room to
run.
Answer:
Ingress manages external access to services within the cluster. It provides SSL
termination, routing rules, and load balancing, enabling
scalable and secure traffic management.
Answer:
Use Kubernetes Deployments to perform rolling updates with zero
downtime. You can also perform canary or blue/green deployments
using tools like Argo Rollouts or Flagger.
Answer:
Yes. Stateless apps are easier to scale and deploy. For stateful apps,
Kubernetes provides StatefulSets, persistent volumes, and storage
classes to ensure data consistency across pod restarts or migrations.
Answer:
Use tools like Prometheus for metrics, Grafana for dashboards, ELK
stack or Loki for logs, and Kubernetes probes
(liveness/readiness) to track application health and scalability trends.
Answer:
Yes. Kubernetes is cloud-agnostic. You can deploy apps on any provider (AWS,
Azure, GCP) or use multi-cloud/hybrid tools like Rancher, Anthos,
or KubeFed for federated scaling across environments.
Answer:
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)