# Performance Optimization

> 🚀 **High Performance**: Panduan lengkap untuk mengoptimalkan Kubernetes performance, resource usage, dan cost efficiency.

***

## 📋 **Daftar Isi**

### **⚡ Resource Optimization**

* [CPU Optimization](#cpu-optimization)
* [Memory Optimization](#memory-optimization)
* [Storage Optimization](#storage-optimization)
* [Network Optimization](#network-optimization)
* [Pod Density Optimization](#pod-density-optimization)

### **💰 Cost Management**

* [Cost Monitoring](#cost-monitoring)
* [Resource Right-Sizing](#resource-right-sizing)
* [Spot Instance Strategy](#spot-instance-strategy)
* [Autoscaling Optimization](#autoscaling-optimization)
* [Multi-Region Cost Strategy](#multi-region-cost-strategy)

### **📊 Performance Tuning**

* [Cluster Performance](#cluster-performance)
* [Application Performance](#application-performance)
* [Database Performance](#database-performance)
* [Cache Optimization](#cache-optimization)
* [Load Balancer Optimization](#load-balancer-optimization)

### **🔧 Advanced Optimization**

* [Kubernetes Scheduler Optimization](#kubernetes-scheduler-optimization)
* [Container Runtime Optimization](#container-runtime-optimization)
* [Operating System Tuning](#operating-system-tuning)
* [Cloud Provider Optimization](#cloud-provider-optimization)
* [Monitoring & Metrics](#monitoring--metrics)

### **🎯 Best Practices**

* [Performance Testing](#performance-testing)
* [Benchmarking Strategy](#benchmarking-strategy)
* [Continuous Optimization](#continuous-optimization)
* [Performance SLAs](#performance-slas)

***

## ⚡ **Resource Optimization**

### CPU Optimization

**🚀 CPU Resource Management**

**Understanding CPU Requests and Limits**

```yaml
# CPU optimization example
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: app
    image: my-app:latest
    resources:
      requests:
        cpu: "100m"  # Minimum guaranteed CPU (0.1 core)
      limits:
        cpu: "500m"  # Maximum CPU limit (0.5 core)
    env:
    - name: GOMAXPROCS
      value: "2"     # Number of CPU cores Go can use
    - name: JAVA_OPTS
      value: "-XX:+UseG1GC -XX:MaxGCPauseMillis=200"
```

**CPU Optimization Strategies**

```yaml
# Multi-container pod optimization
apiVersion: v1
kind: Pod
metadata:
  name: optimized-app
spec:
  containers:
  - name: main-app
    image: my-app:latest
    resources:
      requests:
        cpu: "250m"
      limits:
        cpu: "500m"
    env:
    - name: WORKER_THREADS
      value: "2"
  - name: sidecar
    image: sidecar:latest
    resources:
      requests:
        cpu: "50m"
      limits:
        cpu: "100m"
```

**Advanced CPU Tuning**

```bash
# Check CPU usage patterns
kubectl top pod --sort-by=cpu
kubectl describe node <node-name> | grep -A 10 "Allocated resources"

# CPU pinning for performance-critical workloads
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: performance-app
    image: my-app:latest
    resources:
      requests:
        cpu: "2"
      limits:
        cpu: "2"
    env:
    - name: GOMAXPROCS
      value: "2"
  nodeSelector:
    node-role.kubernetes.io/performance: "true"
  tolerations:
  - key: "performance"
    operator: "Equal"
    value: "true"
    effect: "NoSchedule"
```

### Memory Optimization

**💾 Memory Resource Management**

**Memory Allocation Best Practices**

```yaml
# Memory optimization example
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: app
    image: my-app:latest
    resources:
      requests:
        memory: "256Mi"  # Minimum guaranteed memory
      limits:
        memory: "512Mi"  # Maximum memory limit
    env:
    - name: JAVA_OPTS
      value: "-Xms256m -Xmx512m -XX:+UseG1GC -XX:MaxGCPauseMillis=200"
    - name: NODE_OPTIONS
      value: "--max-old-space-size=256"
```

**Memory Leak Prevention**

```yaml
# Memory optimization with monitoring
apiVersion: apps/v1
kind: Deployment
metadata:
  name: memory-optimized-app
spec:
  replicas: 3
  template:
    spec:
      containers:
      - name: app
        image: my-app:latest
        resources:
          requests:
            memory: "128Mi"
          limits:
            memory: "256Mi"
        env:
        - name: MEMORY_LIMIT
          valueFrom:
            resourceFieldRef:
              containerName: app
              resource: limits.memory
        lifecycle:
          preStop:
            exec:
              command: ["/bin/sh", "-c", "kill -TERM 1; while kill -0 1; do sleep 1; done"]
```

**Advanced Memory Tuning**

```bash
# Memory usage analysis
kubectl top pod --sort-by=memory
kubectl exec -it <pod-name> -- cat /proc/meminfo
kubectl exec -it <pod-name> -- free -h

# Java application memory tuning
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: java-app
    image: my-java-app:latest
    resources:
      requests:
        memory: "512Mi"
      limits:
        memory: "1Gi"
    env:
    - name: JAVA_OPTS
      value: >-
        -Xms512m
        -Xmx768m
        -XX:+UseG1GC
        -XX:MaxGCPauseMillis=200
        -XX:+UnlockExperimentalVMOptions
        -XX:+UseCGroupMemoryLimitForHeap
        -XX:MaxRAMFraction=2
```

### Storage Optimization

**💾 Storage Performance Tuning**

**Storage Class Optimization**

```yaml
# High-performance storage class
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: high-performance-ssd
provisioner: kubernetes.io/aws-ebs
parameters:
  type: gp3
  iops: "3000"
  throughput: "125"
  fsType: ext4
allowVolumeExpansion: true
reclaimPolicy: Retain
volumeBindingMode: WaitForFirstConsumer

# Cost-effective storage class
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: cost-effective-hdd
provisioner: kubernetes.io/aws-ebs
parameters:
  type: sc1
  fsType: ext4
allowVolumeExpansion: true
reclaimPolicy: Delete
volumeBindingMode: Immediate
```

**Volume Optimization**

```yaml
# Local storage for high-performance workloads
apiVersion: v1
kind: PersistentVolume
metadata:
  name: high-performance-pv
spec:
  capacity:
    storage: 100Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: high-performance-ssd
  local:
    path: /mnt/fast-ssd
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: node-type
          operator: In
          values: ["high-performance"]

# Optimized volume mounting
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: app
    image: my-app:latest
    volumeMounts:
    - name: cache-volume
      mountPath: /tmp/cache
    - name: data-volume
      mountPath: /data
  volumes:
  - name: cache-volume
    emptyDir:
      medium: Memory  # Use RAM for cache
      sizeLimit: 1Gi
  - name: data-volume
    persistentVolumeClaim:
      claimName: high-performance-pvc
```

### Network Optimization

**🌐 Network Performance Tuning**

**Network Policy Optimization**

```yaml
# Optimized network policies
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: optimized-network-policy
spec:
  podSelector:
    matchLabels:
      app: web-app
  policyTypes:
  - Ingress
  - Egress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app: frontend
    - ipBlock:
        cidr: 10.0.0.0/8
    ports:
    - protocol: TCP
      port: 8080
  egress:
  - to:
    - podSelector:
        matchLabels:
          app: database
    - ipBlock:
        cidr: 0.0.0.0/0
        except:
        - 169.254.169.254/32  # Block metadata service
    ports:
    - protocol: TCP
      port: 5432
    - protocol: UDP
      port: 53
    - protocol: TCP
      port: 443
```

**Service Optimization**

```yaml
# Optimized service configuration
apiVersion: v1
kind: Service
metadata:
  name: optimized-service
  annotations:
    service.beta.kubernetes.io/aws-load-balancer-type: "nlb"  # Use NLB for better performance
    service.beta.kubernetes.io/aws-load-balancer-backend-protocol: "tcp"
    service.beta.kubernetes.io/aws-load-balancer-ssl-ports: "443"
    service.beta.kubernetes.io/aws-load-balancer-cross-zone-load-balancing-enabled: "true"
spec:
  type: LoadBalancer
  selector:
    app: web-app
  ports:
  - name: http
    port: 80
    targetPort: 8080
    protocol: TCP
  - name: https
    port: 443
    targetPort: 8443
    protocol: TCP
  externalTrafficPolicy: Local  # Preserve client source IP
  sessionAffinity: None
```

### Pod Density Optimization

**📊 Increasing Pod Density**

**Resource Optimization for Higher Density**

```yaml
# High-density node configuration
apiVersion: v1
kind: Node
metadata:
  name: high-density-node
  labels:
    node-type: high-density
    density: high
spec:
  podCIDR: "10.244.0.0/24"
  maxPods: 110  # Increased from default 110 to 200

# Optimized pod for high density
apiVersion: v1
kind: Pod
metadata:
  name: high-density-app
spec:
  containers:
  - name: app
    image: my-app:alpine  # Use smaller base images
    resources:
      requests:
        cpu: "50m"      # Minimal CPU request
        memory: "64Mi"   # Minimal memory request
      limits:
        cpu: "100m"
        memory: "128Mi"
    env:
    - name: JAVA_OPTS
      value: "-Xms32m -Xmx64m -XX:+UseSerialGC"  # Use serial GC for small heaps
    - name: PYTHONUNBUFFERED
      value: "1"
    - name: PYTHONOPTIMIZE
      value: "2"
```

**Kubelet Configuration for High Density**

```yaml
# Kubelet configuration for high density
apiVersion: kubelet.config.k8s.io/v1beta1
kind: KubeletConfiguration
maxPods: 200
podsPerCore: 10
systemReserved:
  cpu: "500m"
  memory: "1Gi"
kubeReserved:
  cpu: "200m"
  memory: "512Mi"
evictionHard:
  memory.available: "200Mi"
  imagefs.available: "15%"
evictionSoft:
  memory.available: "300Mi"
  imagefs.available: "20%"
evictionSoftGracePeriod:
  memory.available: "30s"
  imagefs.available: "45s"
```

***

## 💰 **Cost Management**

### Cost Monitoring

**📊 Cost Tracking and Analysis**

**Prometheus Cost Metrics**

```yaml
# Cost monitoring configuration
apiVersion: v1
kind: ConfigMap
metadata:
  name: cost-monitoring
  namespace: monitoring
data:
  prometheus.yml: |
    global:
      scrape_interval: 15s
    scrape_configs:
    - job_name: 'kubernetes-pods'
      kubernetes_sd_configs:
      - role: pod
      relabel_configs:
      - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
        action: keep
        regex: true
      - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
        action: replace
        target_label: __metrics_path__
        regex: (.+)
      - action: labelmap
        regex: __meta_kubernetes_pod_label_(.+)

# Cost calculation queries
# Cost per namespace
sum by (namespace) (kube_pod_container_resource_requests{resource="cpu"}) * 0.01

# Cost per pod
sum by (pod, namespace) (kube_pod_container_resource_requests{resource="cpu"}) * 0.01

# Memory cost
sum by (namespace) (kube_pod_container_resource_requests{resource="memory"}) / 1024 / 1024 / 1024 * 0.01
```

**Grafana Cost Dashboard**

```yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: grafana-cost-dashboard
  labels:
    grafana_dashboard: "1"
data:
  cost-dashboard.json: |
    {
      "dashboard": {
        "title": "Kubernetes Cost Monitoring",
        "panels": [
          {
            "title": "Cost per Namespace",
            "type": "graph",
            "targets": [
              {
                "expr": "sum by (namespace) (kube_pod_container_resource_requests{resource=\"cpu\"}) * 0.01",
                "legendFormat": "{{namespace}}"
              }
            ],
            "yAxes": [
              {
                "label": "Cost ($/hour)",
                "min": 0
              }
            ]
          },
          {
            "title": "Resource Utilization",
            "type": "stat",
            "targets": [
              {
                "expr": "sum(rate(container_cpu_usage_seconds_total{container!=\"POD\",container!=\"\"}[5m])) / sum(kube_node_status_allocatable_cpu_cores)",
                "legendFormat": "CPU Utilization"
              }
            ]
          }
        ]
      }
    }
```

### Resource Right-Sizing

**🎯 Dynamic Resource Optimization**

**Vertical Pod Autoscaler**

```yaml
# VPA configuration
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: app-vpa
  namespace: production
spec:
  targetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: web-deployment
  updatePolicy:
    updateMode: "Auto"  # Automatic updates
  resourcePolicy:
    containerPolicies:
    - containerName: web
      minAllowed:
        cpu: 50m
        memory: 64Mi
      maxAllowed:
        cpu: 2
        memory: 4Gi
      controlledResources: ["cpu", "memory"]
      mode: "Auto"

# Custom VPA with update mode off
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: app-vpa-monitor
spec:
  targetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: api-deployment
  updatePolicy:
    updateMode: "Off"  # Monitor only
  resourcePolicy:
    containerPolicies:
    - containerName: api
      minAllowed:
        cpu: 100m
        memory: 128Mi
      maxAllowed:
        cpu: 4
        memory: 8Gi
      controlledResources: ["cpu", "memory"]
```

**Resource Recommendations**

```bash
# Get resource recommendations from VPA
kubectl describe vpa app-vpa-monitor

# View VPA recommendation details
kubectl get vpa app-vpa-monitor -o yaml

# Manual resource optimization based on metrics
kubectl top pod --all-namespaces --sort-by=cpu
kubectl top pod --all-namespaces --sort-by=memory

# Resource usage analysis script
#!/bin/bash
for pod in $(kubectl get pods --all-namespaces -o custom-columns=NAME:.metadata.namespace/.metadata.name --no-headers); do
  namespace=$(echo $pod | cut -d'/' -f1)
  podname=$(echo $pod | cut -d'/' -f2)
  cpu_usage=$(kubectl top pod -n $namespace $podname --no-headers | awk '{print $2}')
  memory_usage=$(kubectl top pod -n $namespace $podname --no-headers | awk '{print $3}')
  echo "$namespace/$podname: CPU=$cpu_usage, Memory=$memory_usage"
done
```

### Spot Instance Strategy

**💰 Cost-Effective Spot Instance Usage**

**Spot Node Group Configuration**

```yaml
# EKS Spot node group
apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig

metadata:
  name: cost-optimized-cluster
  region: us-west-2

managedNodeGroups:
  - name: spot-workers
    instanceTypes:
      - m5.large
      - m5.xlarge
      - c5.large
      - c5.xlarge
      - r5.large
      - r5.xlarge
    minSize: 2
    maxSize: 20
    desiredCapacity: 5
    spot: true
    capacityType: SPOT
    labels:
      role: spot-worker
      lifecycle: spot
    taints:
      spot-instance: "true:NoSchedule"
    iam:
      withAddonPolicies:
        autoScaler: true
        cloudWatch: true
        ebs: true
    volumeSize: 50
    volumeType: gp3

  - name: on-demand-critical
    instanceType: m5.large
    minSize: 1
    maxSize: 3
    desiredCapacity: 2
    labels:
      role: on-demand-critical
      lifecycle: on-demand
    taints:
      workload: "critical:NoSchedule"
    iam:
      withAddonPolicies:
        autoScaler: true
        cloudWatch: true
```

**Spot Instance Workload Scheduling**

```yaml
# Pod tolerations for spot instances
apiVersion: apps/v1
kind: Deployment
metadata:
  name: spot-workload
  namespace: batch-processing
spec:
  replicas: 5
  selector:
    matchLabels:
      app: batch-processor
  template:
    metadata:
      labels:
        app: batch-processor
        workload: batch
    spec:
      tolerations:
      - key: "spot-instance"
        operator: "Equal"
        value: "true"
        effect: "NoSchedule"
      nodeSelector:
        role: spot-worker
        lifecycle: spot
      containers:
      - name: processor
        image: batch-processor:latest
        resources:
          requests:
            cpu: "200m"
            memory: "256Mi"
          limits:
            cpu: "500m"
            memory: "512Mi"
        env:
        - name: WORKLOAD_TYPE
          value: "batch"
        - name: GRACEFUL_SHUTDOWN
          value: "120"  # Graceful shutdown time in seconds
        lifecycle:
          preStop:
            exec:
              command: ["/bin/sh", "-c", "echo 'Shutting down gracefully...'; sleep 30"]
```

### Autoscaling Optimization

**📈 Smart Autoscaling Configuration**

**Horizontal Pod Autoscaler with Custom Metrics**

```yaml
# Advanced HPA configuration
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: advanced-hpa
  namespace: production
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: web-deployment
  minReplicas: 2
  maxReplicas: 20
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 60
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 70
  - type: Pods
    pods:
      metric:
        name: http_requests_per_second
      target:
        type: AverageValue
        averageValue: 100
  - type: External
    external:
      metric:
        name: queue_messages_ready
      target:
        type: Value
        value: 30
  behavior:
    scaleUp:
      stabilizationWindowSeconds: 60
      policies:
      - type: Percent
        value: 100
        periodSeconds: 15
      - type: Pods
        value: 4
        periodSeconds: 15
      selectPolicy: Max
    scaleDown:
      stabilizationWindowSeconds: 300
      policies:
      - type: Percent
        value: 10
        periodSeconds: 60

# Cluster Autoscaler configuration
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cluster-autoscaler
  namespace: kube-system
spec:
  template:
    spec:
      containers:
      - image: k8s.gcr.io/autoscaling/cluster-autoscaler:v1.27.0
        name: cluster-autoscaler
        command:
        - ./cluster-autoscaler
        - --v=4
        - --stderrthreshold=info
        - --cloud-provider=aws
        - --skip-nodes-with-local-storage=false
        - --expander=least-waste
        - --node-group-auto-discovery=asg:tag=k8s.io/cluster-autoscaler/enabled,k8s.io/cluster-autoscaler/my-cluster
        - --balance-similar-node-groups
        - --skip-nodes-with-system-pods=false
        - --max-nodes-total=100
        - --max-total-unready-percentage=45
        - --scale-down-unneeded-time=10m
        - --scale-down-unready-time=20m
        - --scale-down-delay-after-add=10m
        - --scale-down-delay-after-delete=10m
```

### Multi-Region Cost Strategy

**🌍 Cross-Region Optimization**

**Multi-Region Deployment Strategy**

```yaml
# Global service using multiple regions
apiVersion: v1
kind: Service
metadata:
  name: global-service
  annotations:
    service.beta.kubernetes.io/aws-load-balancer-type: "nlb"
    service.beta.kubernetes.io/aws-load-balancer-backend-protocol: "tcp"
    service.beta.kubernetes.io/aws-load-balancer-cross-zone-load-balancing-enabled: "true"
spec:
  type: LoadBalancer
  selector:
    app: global-app
  ports:
  - port: 80
    targetPort: 8080

# Regional deployment with cost optimization
apiVersion: apps/v1
kind: Deployment
metadata:
  name: regional-app
  namespace: production
  labels:
    region: us-west-2
    cost-tier: optimized
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: regional-app
        region: us-west-2
        cost-tier: optimized
    spec:
      nodeSelector:
        region: us-west-2
        cost-tier: optimized
      containers:
      - name: app
        image: my-app:latest
        resources:
          requests:
            cpu: "100m"
            memory: "128Mi"
          limits:
            cpu: "500m"
            memory: "512Mi"
        env:
        - name: REGION
          value: "us-west-2"
        - name: COST_TIER
          value: "optimized"
```

***

## 📊 **Performance Tuning**

### Cluster Performance

**🚀 Kubernetes Cluster Optimization**

**API Server Performance Tuning**

```yaml
# API server configuration for high performance
apiVersion: v1
kind: ConfigMap
metadata:
  name: kube-apiserver-config
  namespace: kube-system
data:
  config.yaml: |
    apiVersion: kube-apiserver.config.k8s.io/v1beta1
    kind: KubeAPIServerConfiguration
    maxRequestsInFlight: 1600
    maxMutatingRequestsInFlight: 400
    enableAggregatorRouting: true
    enablePriorityAndFairness: true
    goawayChance: 0.01
    eventRecordQPS: 50
    eventBurst: 100
    watchCacheSizes:
      - group: ""
        resource: pods
        size: 1000
      - group: ""
        resource: services
        size: 10000
      - group: ""
        resource: endpoints
        size: 10000
    defaultWatchCacheSize: 1000

# etcd performance tuning
apiVersion: v1
kind: ConfigMap
metadata:
  name: etcd-config
  namespace: kube-system
data:
  config.yaml: |
    apiVersion: etcd.config.k8s.io/v1beta1
    kind: EtcdConfiguration
    quota-backend-bytes: 8589934592  # 8GB
    max-request-bytes: 1572864        # 1.5MB
    max-snapshots: 5
    max-wals: 5
    snapshot-count: 100000
    heartbeat-interval: 100
    election-timeout: 1000
    auto-compaction-retention: "8h"
```

**Scheduler Optimization**

```yaml
# Custom scheduler configuration
apiVersion: kubescheduler.config.k8s.io/v1beta3
kind: KubeSchedulerConfiguration
clientConnection:
  kubeconfig: /etc/kubernetes/scheduler.conf
profiles:
- schedulerName: performance-scheduler
  plugins:
    score:
      enabled:
      - name: NodeResourcesFit
      - name: NodeResourcesBalancedAllocation
      - name: PodTopologySpread
      - name: TaintToleration
      disabled:
      - name: NodeResourcesLeastAllocated
  pluginConfig:
  - name: NodeResourcesFit
    args:
      scoringStrategy:
        type: MostAllocated
        resources:
        - name: cpu
          weight: 1
        - name: memory
          weight: 1
  - name: PodTopologySpread
    args:
      defaultConstraints:
      - maxSkew: 1
        topologyKey: kubernetes.io/hostname
        whenUnsatisfiable: DoNotSchedule
        labelSelector:
          matchExpressions:
          - key: app
            operator: Exists
```

### Application Performance

**⚡ Application-Level Optimization**

**Application Performance Monitoring**

```yaml
# Application with performance monitoring
apiVersion: apps/v1
kind: Deployment
metadata:
  name: performance-optimized-app
  namespace: production
spec:
  replicas: 3
  template:
    metadata:
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "9090"
        prometheus.io/path: "/metrics"
        prometheus.io/scheme: "http"
    spec:
      containers:
      - name: app
        image: my-app:latest
        ports:
        - containerPort: 8080
          name: http
        - containerPort: 9090
          name: metrics
        resources:
          requests:
            cpu: "200m"
            memory: "256Mi"
          limits:
            cpu: "1000m"
            memory: "1Gi"
        env:
        - name: JAVA_OPTS
          value: >-
            -Xms256m
            -Xmx768m
            -XX:+UseG1GC
            -XX:MaxGCPauseMillis=200
            -XX:+PrintGCDetails
            -XX:+PrintGCTimeStamps
            -XX:+UseStringDeduplication
        - name: PROFILING_ENABLED
          value: "true"
        - name: METRICS_ENABLED
          value: "true"
        livenessProbe:
          httpGet:
            path: /health
            port: 8080
          initialDelaySeconds: 30
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /ready
            port: 8080
          initialDelaySeconds: 5
          periodSeconds: 5
```

**Performance Tuning for Specific Applications**

```yaml
# Node.js application optimization
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: nodejs-app
    image: my-nodejs-app:latest
    resources:
      requests:
        cpu: "100m"
        memory: "128Mi"
      limits:
        cpu: "500m"
        memory: "512Mi"
    env:
    - name: NODE_OPTIONS
      value: >-
        --max-old-space-size=256
        --max-new-space-size=64
        --optimize-for-size
        --gc-interval=100
    - name: UV_THREADPOOL_SIZE
      value: "4"

# Java application optimization
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: java-app
    image: my-java-app:latest
    resources:
      requests:
        cpu: "500m"
        memory: "512Mi"
      limits:
        cpu: "2000m"
        memory: "2Gi"
    env:
    - name: JAVA_OPTS
      value: >-
        -Xms512m
        -Xmx1536m
        -XX:+UseG1GC
        -XX:MaxGCPauseMillis=200
        -XX:+UnlockExperimentalVMOptions
        -XX:+UseCGroupMemoryLimitForHeap
        -XX:MaxRAMFraction=2
        -XX:+PrintGCDetails
        -XX:+PrintGCTimeStamps
        -XX:+UseCompressedOops
        -XX:+UseCompressedClassPointers
```

### Database Performance

**🗄️ Database Optimization in Kubernetes**

**MySQL Performance Optimization**

```yaml
# MySQL StatefulSet with performance tuning
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql-performance
spec:
  serviceName: mysql
  replicas: 1
  template:
    spec:
      containers:
      - name: mysql
        image: mysql:8.0
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: password
        - name: MYSQL_DATABASE
          value: myapp
        resources:
          requests:
            cpu: "500m"
            memory: "1Gi"
          limits:
            cpu: "2000m"
            memory: "4Gi"
        volumeMounts:
        - name: mysql-storage
          mountPath: /var/lib/mysql
        - name: mysql-config
          mountPath: /etc/mysql/conf.d
      volumes:
      - name: mysql-config
        configMap:
          name: mysql-performance-config
  volumeClaimTemplates:
  - metadata:
      name: mysql-storage
    spec:
      accessModes: ["ReadWriteOnce"]
      storageClassName: high-performance-ssd
      resources:
        requests:
          storage: 100Gi

# MySQL performance configuration
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-performance-config
data:
  my.cnf: |
    [mysqld]
    # Connection settings
    max_connections = 500
    connect_timeout = 10
    wait_timeout = 600
    max_allowed_packet = 64M

    # Buffer settings
    innodb_buffer_pool_size = 2G
    innodb_log_file_size = 256M
    innodb_log_buffer_size = 16M
    innodb_flush_log_at_trx_commit = 2

    # Query cache
    query_cache_type = 1
    query_cache_size = 128M

    # Performance schema
    performance_schema = ON

    # Binary log
    log_bin = /var/lib/mysql/mysql-bin.log
    binlog_format = ROW
    expire_logs_days = 7
```

**PostgreSQL Performance Optimization**

```yaml
# PostgreSQL StatefulSet with performance tuning
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgresql-performance
spec:
  serviceName: postgresql
  replicas: 1
  template:
    spec:
      containers:
      - name: postgresql
        image: postgres:15
        ports:
        - containerPort: 5432
        env:
        - name: POSTGRES_DB
          value: myapp
        - name: POSTGRES_USER
          value: appuser
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: postgres-secret
              key: password
        - name: POSTGRES_INITDB_ARGS
          value: "--encoding=UTF-8 --lc-collate=C --lc-ctype=C"
        resources:
          requests:
            cpu: "500m"
            memory: "1Gi"
          limits:
            cpu: "2000m"
            memory: "4Gi"
        volumeMounts:
        - name: postgresql-storage
          mountPath: /var/lib/postgresql/data
        - name: postgresql-config
          mountPath: /etc/postgresql/postgresql.conf
          subPath: postgresql.conf
      volumes:
      - name: postgresql-config
        configMap:
          name: postgresql-performance-config
  volumeClaimTemplates:
  - metadata:
      name: postgresql-storage
    spec:
      accessModes: ["ReadWriteOnce"]
      storageClassName: high-performance-ssd
      resources:
        requests:
          storage: 100Gi

# PostgreSQL performance configuration
apiVersion: v1
kind: ConfigMap
metadata:
  name: postgresql-performance-config
data:
  postgresql.conf: |
    # Memory settings
    shared_buffers = 1GB
    effective_cache_size = 3GB
    work_mem = 16MB
    maintenance_work_mem = 128MB

    # Connection settings
    max_connections = 200
    superuser_reserved_connections = 3

    # WAL settings
    wal_buffers = 16MB
    checkpoint_completion_target = 0.9
    wal_writer_delay = 200ms

    # Query planner
    random_page_cost = 1.1
    effective_io_concurrency = 200

    # Logging
    log_min_duration_statement = 1000
    log_checkpoints = on
    log_connections = on
    log_disconnections = on
    log_lock_waits = on

    # Autovacuum
    autovacuum = on
    autovacuum_max_workers = 3
    autovacuum_naptime = 30s
```

### Cache Optimization

**⚡ Caching Strategies for Performance**

**Redis Performance Optimization**

```yaml
# Redis StatefulSet with performance tuning
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis-performance
spec:
  serviceName: redis
  replicas: 3
  template:
    spec:
      containers:
      - name: redis
        image: redis:7-alpine
        ports:
        - containerPort: 6379
        command:
        - redis-server
        - /etc/redis/redis.conf
        resources:
          requests:
            cpu: "200m"
            memory: "256Mi"
          limits:
            cpu: "1000m"
            memory: "1Gi"
        volumeMounts:
        - name: redis-storage
          mountPath: /data
        - name: redis-config
          mountPath: /etc/redis
      volumes:
      - name: redis-config
        configMap:
          name: redis-performance-config
  volumeClaimTemplates:
  - metadata:
      name: redis-storage
    spec:
      accessModes: ["ReadWriteOnce"]
      storageClassName: high-performance-ssd
      resources:
        requests:
          storage: 10Gi

# Redis performance configuration
apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-performance-config
data:
  redis.conf: |
    # Memory management
    maxmemory 512mb
    maxmemory-policy allkeys-lru

    # Persistence
    save 900 1
    save 300 10
    save 60 10000
    rdbcompression yes
    rdbchecksum yes

    # Networking
    tcp-keepalive 300
    timeout 0

    # Performance
    tcp-backlog 511
    databases 16

    # Logging
    loglevel notice

    # Security
    protected-mode no

    # Client connections
    maxclients 10000

    # Slow log
    slowlog-log-slower-than 10000
    slowlog-max-len 128

# Redis cluster configuration
apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-cluster-config
data:
  redis-cluster.conf: |
    cluster-enabled yes
    cluster-config-file nodes.conf
    cluster-node-timeout 5000
    cluster-announce-ip ${POD_IP}
    cluster-announce-port 6379
    cluster-announce-bus-port 16379
    appendonly yes
    appendfsync everysec
```

**Application-Level Caching**

```yaml
# Application with caching
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cached-app
spec:
  replicas: 3
  template:
    spec:
      containers:
      - name: app
        image: my-app:latest
        env:
        - name: REDIS_URL
          value: "redis://redis-service:6379"
        - name: CACHE_TTL
          value: "300"
        - name: CACHE_STRATEGY
          value: "write-through"
        - name: MEMCACHED_SERVERS
          value: "memcached-service:11211"
        - name: LOCAL_CACHE_SIZE
          value: "1000"
        resources:
          requests:
            cpu: "200m"
            memory: "256Mi"
          limits:
            cpu: "500m"
            memory: "512Mi"
```

### Load Balancer Optimization

**⚖️ Advanced Load Balancing Configuration**

**High-Performance Load Balancer**

```yaml
# NGINX Ingress Controller with performance tuning
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-ingress-controller
  namespace: ingress-nginx
spec:
  replicas: 3
  template:
    spec:
      containers:
      - name: nginx-ingress-controller
        image: k8s.gcr.io/ingress-nginx/controller:v1.8.1
        args:
        - /nginx-ingress-controller
        - --publish-service=$(POD_NAMESPACE)/ingress-nginx-controller
        - --election-id=ingress-controller-leader
        - --controller-class=k8s.io/ingress-nginx
        - --configmap=$(POD_NAMESPACE)/ingress-nginx-controller
        - --validating-webhook=:8443
        - --enable-ssl-passthrough
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        ports:
        - name: http
          containerPort: 80
        - name: https
          containerPort: 443
        resources:
          requests:
            cpu: "100m"
            memory: "90Mi"
          limits:
            cpu: "500m"
            memory: "512Mi"

# NGINX performance configuration
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-configuration
  namespace: ingress-nginx
data:
  nginx.conf: |
    worker_processes auto;
    worker_cpu_affinity auto;
    worker_rlimit_nofile 1048576;

    events {
        worker_connections 16384;
        use epoll;
        multi_accept on;
    }

    http {
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        client_max_body_size 1m;

        # Gzip compression
        gzip on;
        gzip_vary on;
        gzip_min_length 1024;
        gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        # Caching
        proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;

        # Connection pooling
        upstream my_backend {
            least_conn;
            server app-service:8080 max_fails=3 fail_timeout=30s;
            keepalive 32;
        }

        # SSL optimization
        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384;
        ssl_prefer_server_ciphers off;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 10m;
    }
```

***

## 🔧 **Advanced Optimization**

### Kubernetes Scheduler Optimization

**📊 Custom Scheduling Strategies**

**Performance-Optimized Scheduler**

```yaml
# Custom scheduler configuration
apiVersion: v1
kind: Pod
metadata:
  name: custom-scheduler
spec:
  containers:
  - name: kube-scheduler
    image: k8s.gcr.io/kube-scheduler:v1.28.0
    command:
    - kube-scheduler
    - --config=/etc/kubernetes/scheduler-config.yaml
    - --v=3
    volumeMounts:
    - name: scheduler-config
      mountPath: /etc/kubernetes/scheduler-config.yaml
      subPath: scheduler-config.yaml
  volumes:
  - name: scheduler-config
    configMap:
      name: custom-scheduler-config

# Custom scheduler configuration
apiVersion: kubescheduler.config.k8s.io/v1beta3
kind: KubeSchedulerConfiguration
clientConnection:
  kubeconfig: /etc/kubernetes/scheduler.conf
profiles:
- schedulerName: performance-scheduler
  plugins:
    queueSort:
      enabled:
      - name: PrioritySort
      disabled:
      - name: QueueSort
    preFilter:
      enabled:
      - name: NodeResourcesFit
      - name: NodeAffinity
      - name: TaintToleration
    filter:
      enabled:
      - name: NodeUnschedulable
      - name: NodeName
      - name: NodePort
      - name: NodeAffinity
      - name: TaintToleration
      - name: NodeResourcesFit
    score:
      enabled:
      - name: NodeResourcesBalancedAllocation
        weight: 5
      - name: NodeResourcesFit
        weight: 3
      - name: PodTopologySpread
        weight: 2
      - name: TaintToleration
        weight: 1
    bind:
      enabled:
      - name: DefaultBinder
  pluginConfig:
  - name: NodeResourcesFit
    args:
      scoringStrategy:
        type: MostAllocated
        resources:
        - name: cpu
          weight: 1
        - name: memory
          weight: 1
  - name: PodTopologySpread
    args:
      defaultConstraints:
      - maxSkew: 1
        topologyKey: kubernetes.io/hostname
        whenUnsatisfiable: DoNotSchedule
        labelSelector:
          matchExpressions:
          - key: app
            operator: Exists
```

**Node Affinity and Anti-Affinity**

```yaml
# Pod with advanced affinity rules
apiVersion: v1
kind: Pod
metadata:
  name: performance-critical-pod
spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: node-type
            operator: In
            values: ["high-performance"]
          - key: cpu-type
            operator: In
            values: ["intel-xeon"]
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 100
        preference:
          matchExpressions:
          - key: zone
            operator: In
            values: ["us-west-2a", "us-west-2b"]
      - weight: 50
        preference:
          matchExpressions:
          - key: instance-type
            operator: In
            values: ["m5.4xlarge", "m5.8xlarge"]
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: app
            operator: In
            values: ["database"]
        topologyKey: kubernetes.io/hostname
    podAntiAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 100
        podAffinityTerm:
          labelSelector:
            matchExpressions:
            - key: app
              operator: In
              values: ["web-server"]
          topologyKey: kubernetes.io/hostname
  containers:
  - name: app
    image: my-app:latest
    resources:
      requests:
        cpu: "2000m"
        memory: "4Gi"
      limits:
        cpu: "4000m"
        memory: "8Gi"
```

### Container Runtime Optimization

**🐳 Container Performance Tuning**

**Containerd Configuration**

```yaml
# containerd configuration for performance
apiVersion: v1
kind: ConfigMap
metadata:
  name: containerd-config
  namespace: kube-system
data:
  config.toml: |
    version = 2

    [plugins]
      [plugins."io.containerd.grpc.v1.cri"]
        [plugins."io.containerd.grpc.v1.cri".containerd]
          [plugins."io.containerd.grpc.v1.cri".containerd.runtimes]
            [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
              runtime_type = "io.containerd.runc.v2"
              [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
                SystemdCgroup = true
                IoUid = 0
                IoGid = 0
                CgroupPath = ""
        [plugins."io.containerd.grpc.v1.cri".registry]
          [plugins."io.containerd.grpc.v1.cri".registry.mirrors]
            [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
              endpoint = ["https://registry-1.docker.io"]
            [plugins."io.containerd.grpc.v1.cri".registry.mirrors."my-registry.com"]
              endpoint = ["https://my-registry.com"]
        [plugins."io.containerd.grpc.v1.cri".cni]
          bin_dir = "/opt/cni/bin"
          conf_dir = "/etc/cni/net.d"
      [plugins."io.containerd.grpc.v1.cri".containerd]
        snapshotter = "overlayfs"
        disable_snapshot_annotations = false

    [metrics]
      address = "127.0.0.1:1338"
      grpc_histogram = true

    [debug]
      level = "info"
      format = "text"
```

**Docker Runtime Optimization**

```yaml
# Docker daemon configuration for performance
apiVersion: v1
kind: ConfigMap
metadata:
  name: docker-daemon-config
  namespace: kube-system
data:
  daemon.json: |
    {
      "log-driver": "json-file",
      "log-opts": {
        "max-size": "10m",
        "max-file": "3"
      },
      "storage-driver": "overlay2",
      "storage-opts": [
        "overlay2.override_kernel_check=true",
        "overlay2.size=20G"
      ],
      "live-restore": true,
      "userland-proxy": false,
      "experimental": false,
      "metrics-addr": "127.0.0.1:9323",
      "exec-opts": ["native.cgroupdriver=systemd"],
      "bridge": "none",
      "ip-forward": true,
      "iptables": true,
      "default-ulimits": {
        "nofile": {
          "Name": "nofile",
          "Hard": 64000,
          "Soft": 64000
        }
      }
    }
```

### Operating System Tuning

**🖥️ Host OS Optimization**

**Node Kernel Parameters**

```yaml
# System optimization via DaemonSet
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: sysctl-tuner
  namespace: kube-system
spec:
  selector:
    matchLabels:
      app: sysctl-tuner
  template:
    metadata:
      labels:
        app: sysctl-tuner
    spec:
      hostPID: true
      hostNetwork: true
      containers:
      - name: sysctl-tuner
        image: busybox:latest
        securityContext:
          privileged: true
        command:
        - /bin/sh
        - -c
        - |
          # Network optimization
          sysctl -w net.core.rmem_max=134217728
          sysctl -w net.core.wmem_max=134217728
          sysctl -w net.ipv4.tcp_rmem="4096 87380 134217728"
          sysctl -w net.ipv4.tcp_wmem="4096 65536 134217728"
          sysctl -w net.core.netdev_max_backlog=5000
          sysctl -w net.ipv4.tcp_congestion_control=bbr

          # File system optimization
          sysctl -w fs.file-max=2097152
          sysctl -w fs.inotify.max_user_instances=8192
          sysctl -w fs.inotify.max_user_watches=524288

          # Memory optimization
          sysctl -w vm.swappiness=1
          sysctl -w vm.dirty_ratio=15
          sysctl -w vm.dirty_background_ratio=5
          sysctl -w vm.dirty_expire_centisecs=12000

          # Process optimization
          sysctl -w kernel.pid_max=4194304
          sysctl -w kernel.threads-max=4194304

          sleep infinity
```

**CPU Governor Optimization**

```bash
#!/bin/bash
# CPU governor optimization script

# Set CPU governor to performance mode
for cpu in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do
  echo "performance" > $cpu
done

# Disable CPU idle states
for cpu in /sys/devices/system/cpu/cpu*/cpuidle/state*/disable; do
  echo "1" > $cpu 2>/dev/null || true
done

# Optimize CPU frequency scaling
echo "performance" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
echo "0" > /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us
```

### Cloud Provider Optimization

**☁️ Cloud-Specific Optimizations**

**AWS EKS Optimization**

```yaml
# EKS cluster with performance optimizations
apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig

metadata:
  name: high-performance-eks
  region: us-west-2
  version: "1.28"

iam:
  withOIDC: true

managedNodeGroups:
  - name: compute-optimized
    instanceType: c5.2xlarge
    minSize: 2
    maxSize: 10
    desiredCapacity: 3
    iam:
      withAddonPolicies:
        autoScaler: true
        cloudWatch: true
        ebs: true
    volumeSize: 100
    volumeType: gp3
    volumeIOPS: 3000
    volumeThroughput: 125
    labels:
      node-type: compute-optimized
    taints:
      - key: workload-type
        value: compute-intensive
        effect: NoSchedule

  - name: memory-optimized
    instanceType: r5.2xlarge
    minSize: 2
    maxSize: 10
    desiredCapacity: 3
    iam:
      withAddonPolicies:
        autoScaler: true
        cloudWatch: true
        ebs: true
    volumeSize: 200
    volumeType: gp3
    volumeIOPS: 3000
    volumeThroughput: 125
    labels:
      node-type: memory-optimized
    taints:
      - key: workload-type
        value: memory-intensive
        effect: NoSchedule

cloudWatch:
  clusterLogging:
    enable: ["api", "audit", "authenticator", "controllerManager", "scheduler"]
```

**GCP GKE Optimization**

```yaml
# GKE cluster with performance optimizations
apiVersion: container.cnrm.cloud.google.com/v1beta1
kind: ContainerCluster
metadata:
  name: high-performance-gke
  namespace: gke
spec:
  location: us-central1
  initialNodeCount: 1
  nodeConfig:
    machineType: n2-standard-4
    diskSizeGb: 100
    diskType: pd-ssd
    oauthScopes:
    - "https://www.googleapis.com/auth/cloud-platform"
    labels:
      node-type: high-performance
    preemptible: false
    spot: false
    taints:
    - key: workload-type
      value: performance
      effect: NoSchedule
  addonsConfig:
    horizontalPodAutoscaling:
      enabled: true
    httpLoadBalancing:
      enabled: true
    networkPolicyConfig:
      enabled: true
    rayOperator:
      enabled: false
  networkPolicy:
    enabled: true
  ipAllocationPolicy:
    useIpAliases: true
    clusterIpv4CidrBlock: "/16"
    servicesIpv4CidrBlock: "/22"
  masterAuthorizedNetworksConfig:
    cidrBlocks:
    - cidrBlock: "0.0.0.0/0"
      displayName: "All networks"
  binaryAuthorization:
    enabled: false
  podSecurityPolicyConfig:
    enabled: false
  privateClusterConfig:
    enablePrivateNodes: true
    enablePrivateEndpoint: false
    masterIpv4CidrBlock: "172.16.0.0/28"
```

### Monitoring & Metrics

**📊 Comprehensive Performance Monitoring**

**Prometheus Configuration for Performance Metrics**

```yaml
# Prometheus configuration for performance monitoring
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-config
  namespace: monitoring
data:
  prometheus.yml: |
    global:
      scrape_interval: 15s
      evaluation_interval: 15s

    rule_files:
      - "performance_rules.yml"

    alerting:
      alertmanagers:
        - static_configs:
            - targets:
              - alertmanager:9093

    scrape_configs:
      - job_name: 'kubernetes-nodes'
        kubernetes_sd_configs:
          - role: node
        relabel_configs:
          - source_labels: [__address__]
            regex: '(.*):10250'
            target_label: __address__
            replacement: '${1}:9100'
          - action: labelmap
            regex: __meta_kubernetes_node_label_(.+)

      - job_name: 'kubernetes-pods'
        kubernetes_sd_configs:
          - role: pod
        relabel_configs:
          - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
            action: keep
            regex: true
          - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
            action: replace
            target_label: __metrics_path__
            regex: (.+)
          - action: labelmap
            regex: __meta_kubernetes_pod_label_(.+)

      - job_name: 'kube-state-metrics'
        kubernetes_sd_configs:
          - role: service
        relabel_configs:
          - source_labels: [__meta_kubernetes_service_label_k8s_app]
            action: keep
            regex: kube-state-metrics

  performance_rules.yml: |
    groups:
    - name: performance.rules
      rules:
      - alert: HighCPUUsage
        expr: 100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 80
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "High CPU usage on {{ $labels.instance }}"
          description: "CPU usage is above 80% for more than 5 minutes"

      - alert: HighMemoryUsage
        expr: (node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes * 100 > 85
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "High memory usage on {{ $labels.instance }}"
          description: "Memory usage is above 85% for more than 5 minutes"

      - alert: DiskSpaceUsage
        expr: (node_filesystem_size_bytes - node_filesystem_avail_bytes) / node_filesystem_size_bytes * 100 > 90
        for: 5m
        labels:
          severity: critical
        annotations:
          summary: "Disk space usage on {{ $labels.instance }}"
          description: "Disk usage is above 90% on {{ $labels.device }}"

      - alert: PodHighCPUUsage
        expr: rate(container_cpu_usage_seconds_total{container!="POD",container!=""}[5m]) * 100 > 80
        for: 10m
        labels:
          severity: warning
        annotations:
          summary: "High CPU usage in pod {{ $labels.pod }}"
          description: "Pod {{ $labels.pod }} CPU usage is above 80%"

      - alert: PodHighMemoryUsage
        expr: container_memory_usage_bytes{container!="POD",container!=""} / container_spec_memory_limit_bytes * 100 > 85
        for: 10m
        labels:
          severity: warning
        annotations:
          summary: "High memory usage in pod {{ $labels.pod }}"
          description: "Pod {{ $labels.pod }} memory usage is above 85%"
```

**Grafana Performance Dashboard**

```yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: grafana-performance-dashboard
  labels:
    grafana_dashboard: "1"
data:
  performance-dashboard.json: |
    {
      "dashboard": {
        "id": null,
        "title": "Kubernetes Performance Dashboard",
        "tags": ["kubernetes", "performance"],
        "timezone": "browser",
        "panels": [
          {
            "title": "Cluster CPU Usage",
            "type": "graph",
            "targets": [
              {
                "expr": "100 - (avg by (instance) (irate(node_cpu_seconds_total{mode=\"idle\"}[5m])) * 100)",
                "legendFormat": "{{instance}}"
              }
            ],
            "yAxes": [
              {
                "label": "CPU Usage (%)",
                "min": 0,
                "max": 100
              }
            ]
          },
          {
            "title": "Cluster Memory Usage",
            "type": "graph",
            "targets": [
              {
                "expr": "(node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes * 100",
                "legendFormat": "{{instance}}"
              }
            ],
            "yAxes": [
              {
                "label": "Memory Usage (%)",
                "min": 0,
                "max": 100
              }
            ]
          },
          {
            "title": "Pod Performance Metrics",
            "type": "table",
            "targets": [
              {
                "expr": "rate(container_cpu_usage_seconds_total{container!=\"POD\",container!=\"\"}[5m])",
                "legendFormat": "{{pod}} - {{container}}"
              }
            ],
            "transformations": [
              {
                "id": "organize",
                "options": {
                  "excludeByName": {
                    "Time": true
                  },
                  "indexByName": {
                    "pod": 0,
                    "container": 1,
                    "Value": 2
                  },
                  "renameByName": {
                    "pod": "Pod",
                    "container": "Container",
                    "Value": "CPU Usage"
                  }
                }
              }
            ]
          }
        ],
        "time": {
          "from": "now-1h",
          "to": "now"
        },
        "refresh": "30s"
      }
    }
```

***

## 🎯 **Best Practices**

### **🔍 Performance Testing**

**🧪 Load Testing Strategy**

```yaml
# Load testing with K6
apiVersion: batch/v1
kind: Job
metadata:
  name: load-test
spec:
  template:
    spec:
      containers:
      - name: k6
        image: loadimpact/k6:latest
        command:
        - k6
        - run
        - --vus
        - "100"
        - --duration
        - "5m"
        - --out
        - influxdb=http://influxdb.monitoring.svc.cluster.local:8086/k6
        - /scripts/load-test.js
        volumeMounts:
        - name: test-script
          mountPath: /scripts
      volumes:
      - name: test-script
        configMap:
          name: load-test-script
      restartPolicy: Never

# Load test script
apiVersion: v1
kind: ConfigMap
metadata:
  name: load-test-script
data:
  load-test.js: |
    import http from 'k6/http';
    import { check, sleep } from 'k6';

    export let options = {
      stages: [
        { duration: '2m', target: 50 },
        { duration: '5m', target: 50 },
        { duration: '2m', target: 100 },
        { duration: '5m', target: 100 },
        { duration: '2m', target: 200 },
        { duration: '5m', target: 200 },
        { duration: '2m', target: 0 },
      ],
    };

    export default function () {
      let response = http.get('http://my-app.production.svc.cluster.local:8080/health');
      check(response, {
        'status is 200': (r) => r.status === 200,
        'response time < 200ms': (r) => r.timings.duration < 200,
      });
      sleep(1);
    }
```

### **📊 Benchmarking Strategy**

**🔍 Performance Benchmarking**

```yaml
# Benchmarking with SysBench
apiVersion: batch/v1
kind: Job
metadata:
  name: sysbench-cpu
spec:
  template:
    spec:
      containers:
      - name: sysbench
        image: severalnines/sysbench
        command:
        - sysbench
        - cpu
        - --cpu-max-prime=20000
        - --threads=4
        - --time=60
        - run
        resources:
          requests:
            cpu: "1000m"
            memory: "1Gi"
          limits:
            cpu: "2000m"
            memory: "2Gi"
      restartPolicy: Never
```

### **🔄 Continuous Optimization**

**📈 Automated Performance Optimization**

```yaml
# Performance optimization CronJob
apiVersion: batch/v1
kind: CronJob
metadata:
  name: performance-optimizer
spec:
  schedule: "0 */6 * * *"  # Every 6 hours
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: optimizer
            image: performance-optimizer:latest
            command:
            - python3
            - optimize.py
            - --cluster
            - my-cluster
            - --threshold
            - "80"
            env:
            - name: PROMETHEUS_URL
              value: "http://prometheus.monitoring.svc.cluster.local:9090"
            - name: KUBECONFIG
              value: "/etc/kubeconfig/config"
            volumeMounts:
            - name: kubeconfig
              mountPath: /etc/kubeconfig
          volumes:
          - name: kubeconfig
            secret:
              secretName: kubeconfig
          restartPolicy: OnFailure
```

### **📋 Performance SLAs**

**🎯 Service Level Objectives**

```yaml
# SLO configuration
apiVersion: v1
kind: ConfigMap
metadata:
  name: slo-config
  namespace: monitoring
data:
  slo.yaml: |
    service_level_objectives:
      web_service:
        availability:
          target: 99.9%
          window: 30d
        latency:
          p95_target: 200ms
          p99_target: 500ms
          window: 1d
        error_rate:
          target: 0.1%
          window: 1d

      api_service:
        availability:
          target: 99.95%
          window: 30d
        latency:
          p95_target: 100ms
          p99_target: 300ms
          window: 1d
        throughput:
          target: 1000 rps
          window: 1h
```

***

## 🔗 **Referensi**

### **📚 Dokumentasi Resmi**

* [Kubernetes Performance Tuning](https://kubernetes.io/docs/tasks/debug-application-cluster/resource-usage-monitoring/)
* [Cluster Autoscaler](https://github.com/kubernetes/autoscaler/blob/master/cluster-autoscaler/FAQ.md)
* [Vertical Pod Autoscaler](https://github.com/kubernetes/autoscaler/tree/master/vertical-pod-autoscaler)

### **🛠️ Performance Tools**

* [K6 Load Testing](https://k6.io/)
* [SysBench](https://github.com/akopytov/sysbench)
* [Prometheus](https://prometheus.io/)
* [Grafana](https://grafana.com/)

### **📖 Learning Resources**

* [Kubernetes Performance Best Practices](https://kubernetes.io/docs/concepts/cluster-administration/manage-deployment/)
* [Cloud Provider Optimization Guides](https://kubernetes.io/docs/setup/production-environment/)
* [Application Performance Monitoring](https://kubernetes.io/docs/tasks/debug-application-cluster/resource-usage-monitoring/)

***

\*⚡ \**Performance optimization adalah proses berkelanjutan yang memerlukan monitoring dan tuning secara teratur*
