VOOZH about

URL: https://dzone.com/articles/best-practices-managing-kubernetes-at-scale

โ‡ฑ 10 Best Practices for Managing Kubernetes at Scale


Related

  1. DZone
  2. Software Design and Architecture
  3. Containers
  4. 10 Best Practices for Managing Kubernetes at Scale

10 Best Practices for Managing Kubernetes at Scale

We will discuss some of the best practices for managing distributed workloads on Kubernetes clusters to simplify deploying and managing containers.

Likes
Comment
Save
21.2K Views

Join the DZone community and get the full member experience.

Join For Free

As organizations use microservices and cloud-native architectures, Kubernetes is becoming the norm for container orchestration. As much as Kubernetes simplifies deploying and managing containers, workloads at scale make life complex, and robust practices are necessary. 

In this article, I will cover technical strategies and best practices for workload management at scale in Kubernetes.

Knowing Challenges in Scaling Kubernetes

Scaling out in Kubernetes entails overcoming obstacles such as:

  • Cluster resource scheduling. Optimized CPU, memory, and disk usage across nodes.
  • Network complexity. Consistent service-to-service communications in big, distributed environments.
  • Fault and scalability. Handling availability during failures and during a scale-out/scale-in scenario.
  • Operational overheads. Removing repetitive operations such as scaling, monitoring, and balancing loads.
  • Security at scale. Role-based access controls (RBAC), secrets, and network policies in big clusters.

In this article, I will go through examples of overcoming such obstacles with a combination of native Kubernetes capabilities and complementary tools.

Capabilities and Tools

1. Efficient Scheduling of Cluster Resources

Performance at scale is determined directly by the distribution of resources at scale. There are a variety of capabilities in Kubernetes for optimized use of resources:

Requests and Limits

Declaring CPU and memory requests and limits will cause fair distribution of resources and will not permit noising neighbors to consume all resources.

YAML
apiVersion: v1
kind: Pod
metadata:
 name: resource-demo
spec:
 containers:
 - name: app
 image: nginx
 resources:
 requests:
 memory: "128Mi"
 cpu: "500m"
 limits:
 memory: "256Mi"
          cpu: "1"


Best practices:

  • Use quota for enforcing at the namespace level.
  • Periodically analyze usage with kubectl top and make any required tweaks to limits.

Cluster Autoscaler

The autoscaler scales your cluster's node count dynamically according to workload demand.

Shell
kubectl apply -f https://github.com/kubernetes/autoscaler/releases/download/cluster-autoscaler-<version>/cluster-autoscaler.yaml


Best practices:

  • Label your autoscaler operations appropriately for your nodes.
  • Scale behavior monitor to avert over-provisioning.

2. Horizontal and Vertical Pod Autoscaling

Horizontal Pod Autoscaler (HPA) and Vertical Pod Autoscaler (VPA) autoscaling capabilities are native in Kubernetes, but service meshes like Istio and Linkerd make and simplify inter-service communications easier and more efficient.

Horizontal Pod Autoscaler (HPA)

HPA scales replicas of pods according to CPU, memory, or custom metrics.

Example: CPU usage for autoscaling

YAML
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
 name: hpa-example
spec:
 scaleTargetRef:
 apiVersion: apps/v1
 kind: Deployment
 name: web-app
 minReplicas: 2
 maxReplicas: 10
 metrics:
 - type: Resource
 resource:
 name: cpu
 target:
 type: Utilization
          averageUtilization: 50


Vertical Pod Autoscaler (VPA)

Vertical Pod Autoscaler scales a run-time request and limit of a pod.

Shell
kubectl apply -f https://github.com/kubernetes/autoscaler/releases/download/vertical-pod-autoscaler-<version>/vpa.yaml


3. Optimizing Networking at Scale

Service Mesh

Service meshes like Istio and Linkerd make and simplify inter-service communications easier and more efficient through the abstraction of service loads, retries, and encryption concerns.

Example: Istio VirtualService for routing traffic

YAML
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
 name: example-route
spec:
 hosts:
 - example.com
 http:
 - route:
 - destination:
 host: service-v1
 weight: 80
 - destination:
 host: service-v2
          weight: 20


Network Policies

Use network policies for constraining traffic between pods for enhanced security.

YAML
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
 name: restrict-traffic
spec:
 podSelector:
 matchLabels:
 app: web-app
 policyTypes:
 - Ingress
 ingress:
 - from:
 - podSelector:
 matchLabels:
              role: backend


4. Observability Enhancement

Observability is critical in controlling Kubernetes at a larger level. Use tools like Prometheus, Grafana, and Jaeger for metrics, logs, and tracing.

Prometheus Metrics

Use Prometheus annotations for scrapping pod metrics.

YAML
apiVersion: v1
kind: Pod
metadata:
 annotations:
 prometheus.io/scrape: "true"
 prometheus.io/port: "8080"
 labels:
 app: monitored-app
spec:
 containers:
 - name: app
 image: monitored-app:latest
 ports:
        - containerPort: 8080


5. Building Resilience

Pod Disruption Budgets (PDB)

Use PDBs for maintaining a minimum availability of pods during maintenance and upgrades.

YAML
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
 name: pdb-example
spec:
 minAvailable: 2
 selector:
 matchLabels:
      app: web-app


Rolling Updates

Roll out updates in phases in a manner that does not cause any downtime.

Shell
kubectl set image deployment/web-app web-app=web-app:v2 --record
kubectl rollout status deployment/web-app


6. Securing Kubernetes at Scale

RBAC Configuration

Use RBAC for constraining the privileges of the user and app.

YAML
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
 namespace: default
 name: pod-reader
rules:
 - apiGroups: [""]
 resources: ["pods"]
    verbs: ["get", "list", "watch"]


Secrets Management

Utilize Kubernetes Secrets for secure management of sensitive information. Use Kubernetes Secrets to manage sensitive information securely.

YAML
apiVersion: v1
kind: Secret
metadata:
 name: db-credentials
type: Opaque
data:
 username: dXNlcg==
 password: cGFzc3dvcmQ=


7. GitOps for Automation

Utilize GitOps with tools such as ArgoCD and Flux. Version and store Kubernetes manifest in Git repos and have clusters auto-synced with them.

8. Testing at Scale

Mock out high-scale workloads with tools such as K6 and Locust. Verify configuration, resource assignments, and scaling in testing environments.

9. Handling Storage at Scale

Dynamic Persistent Volume Provisioning

Storage for applications is dynamically provisioned with automation.

YAML
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
 name: fast-storage
provisioner: kubernetes.io/aws-ebs
parameters:
 type: gp2
  fsType: ext4


10. Optimizing CI/CD Pipelines for Kubernetes

Build and Push a Docker Image

Streamline creating and publishing container images with CI/CD tools such as Jenkins, GitHub Actions, and GitLab CI.

Conclusion

To scale Kubernetes, one must have a combination of efficient use of resources, automation, observability, and strong security processes in place. By taking full use of Kubernetes-native capabilities and combining them with complementary tools, your workloads can be high-performance, secure, and resilient at any scale.

Kubernetes Scaling (geometry) cluster

Opinions expressed by DZone contributors are their own.

Related

  • Scaling Microservices With Docker and Kubernetes on Production
  • Can You Run a MariaDB Cluster on a $150 Kubernetes Lab? I Gave It a Shot
  • How Kubernetes Cluster Sizing Affects Performance and Cost Efficiency in Cloud Deployments
  • Mastering Advanced Traffic Management in Multi-Cloud Kubernetes: Scaling With Multiple Istio Ingress Gateways

Partner Resources

ร—

Comments

The likes didn't load as expected. Please refresh the page and try again.

Let's be friends: