prvng_kcl/examples/kubernetes_deployment.k
2025-10-07 11:17:54 +01:00

325 lines
8.6 KiB
Plaintext

# Kubernetes Deployment Example
# Shows complete K8s deployment with services, volumes, and monitoring
import ..main
# Web application deployment in Kubernetes
webapp_k8s: main.K8sDeploy = main.K8sDeploy {
name: "webapp"
namespace: "production"
create_ns: True
# Deployment specification
spec: main.K8sDeploySpec {
replicas: 3
containers: [
main.K8sContainers {
name: "webapp"
image: "nginx:1.21-alpine"
# Port configuration
ports: [
main.K8sPort {
name: "http"
container: 80
target: 8080
}
]
# Resource requirements
resources_requests: main.K8sResources {
memory: "64Mi"
cpu: "50m"
}
resources_limits: main.K8sResources {
memory: "128Mi"
cpu: "100m"
}
# Environment variables
env: [
main.K8sKeyVal {
key: "NODE_ENV"
value: "production"
},
main.K8sKeyVal {
key: "LOG_LEVEL"
value: "info"
}
]
# Mount configuration volume
volumeMounts: [
main.K8sVolumeMount {
name: "config"
mountPath: "/etc/nginx/conf.d"
readOnly: True
}
]
}
]
# Volume configuration
volumes: [
main.K8sVolume {
name: "config"
typ: "configMap"
configMap: main.K8sConfigMap {
name: "webapp-config"
}
}
]
# Node selection for production workloads
nodeSelector: [
main.K8sKeyVal {
key: "node-type"
value: "production"
}
]
# Anti-affinity to spread pods across nodes
affinity: main.K8sAffinity {
antiAffinity: main.K8sAntyAffinityLabelSelector {
typ: "preferredDuringSchedulingIgnoredDuringExecution"
weight: 100
labelSelector: [
main.K8sAffinityMatch {
key: "app"
operator: "In"
values: ["webapp"]
}
]
topologyKey: "kubernetes.io/hostname"
}
}
}
# Service configuration
service: main.K8sService {
name: "webapp-service"
typ: "ClusterIP"
ports: [
main.K8sPort {
name: "http"
target: 80
nodePort: 30080
}
]
selector: [
main.K8sKeyVal {
key: "app"
value: "webapp"
}
]
}
# Labels for the deployment
labels: [
main.K8sKeyVal {
key: "app"
value: "webapp"
},
main.K8sKeyVal {
key: "version"
value: "v1.0.0"
},
main.K8sKeyVal {
key: "environment"
value: "production"
}
]
}
# Database deployment with persistent storage
database_k8s: main.K8sDeploy = main.K8sDeploy {
name: "postgres"
namespace: "production"
spec: main.K8sDeploySpec {
replicas: 1 # Database typically runs single instance
containers: [
main.K8sContainers {
name: "postgres"
image: "postgres:15-alpine"
ports: [
main.K8sPort {
name: "postgres"
container: 5432
target: 5432
}
]
# Database needs more resources
resources_requests: main.K8sResources {
memory: "256Mi"
cpu: "100m"
}
resources_limits: main.K8sResources {
memory: "512Mi"
cpu: "500m"
}
# Database environment
env: [
main.K8sKeyVal {
key: "POSTGRES_DB"
value: "webapp"
},
main.K8sKeyVal {
key: "POSTGRES_USER"
value: "webapp"
},
main.K8sKeyVal {
key: "POSTGRES_PASSWORD"
value: "changeme" # Use secrets in production
}
]
# Persistent data volume
volumeMounts: [
main.K8sVolumeMount {
name: "postgres-data"
mountPath: "/var/lib/postgresql/data"
readOnly: False
}
]
}
]
# Persistent volume for database
volumes: [
main.K8sVolume {
name: "postgres-data"
typ: "volumeClaim"
persitentVolumeClaim: main.K8sVolumeClaim {
name: "postgres-pvc"
storageClassName: "manual"
storage: "10Gi"
modes: ["ReadWriteOnce"]
reclaimPolicy: "Retain"
}
}
]
}
# Internal service for database
service: main.K8sService {
name: "postgres-service"
typ: "ClusterIP"
ports: [
main.K8sPort {
name: "postgres"
target: 5432
}
]
selector: [
main.K8sKeyVal {
key: "app"
value: "postgres"
}
]
}
labels: [
main.K8sKeyVal {
key: "app"
value: "postgres"
},
main.K8sKeyVal {
key: "component"
value: "database"
}
]
}
# Monitoring deployment using Prometheus
monitoring_k8s: main.K8sDeploy = main.K8sDeploy {
name: "prometheus"
namespace: "monitoring"
create_ns: True
spec: main.K8sDeploySpec {
replicas: 1
containers: [
main.K8sContainers {
name: "prometheus"
image: "prom/prometheus:v2.40.0"
ports: [
main.K8sPort {
name: "web"
container: 9090
target: 9090
}
]
resources_requests: main.K8sResources {
memory: "512Mi"
cpu: "200m"
}
resources_limits: main.K8sResources {
memory: "1Gi"
cpu: "500m"
}
volumeMounts: [
main.K8sVolumeMount {
name: "prometheus-config"
mountPath: "/etc/prometheus"
readOnly: True
},
main.K8sVolumeMount {
name: "prometheus-data"
mountPath: "/prometheus"
readOnly: False
}
]
}
]
volumes: [
main.K8sVolume {
name: "prometheus-config"
typ: "configMap"
configMap: main.K8sConfigMap {
name: "prometheus-config"
}
},
main.K8sVolume {
name: "prometheus-data"
typ: "volumeClaim"
persitentVolumeClaim: main.K8sVolumeClaim {
name: "prometheus-pvc"
storage: "20Gi"
storageClassName: "manual"
modes: ["ReadWriteOnce"]
}
}
]
}
service: main.K8sService {
name: "prometheus-service"
typ: "NodePort"
ports: [
main.K8sPort {
name: "web"
target: 9090
nodePort: 30090
}
]
}
labels: [
main.K8sKeyVal {
key: "app"
value: "prometheus"
},
main.K8sKeyVal {
key: "component"
value: "monitoring"
}
]
}