CoAI LogoCoAI.Dev

Kubernetes Deployment

Deploy CoAI.Dev on Kubernetes for production-grade scalability, high availability, and enterprise features

Kubernetes Deployment

Deploy CoAI.Dev on Kubernetes for production-grade scalability, high availability, and enterprise features. This deployment method provides the most robust and scalable solution for large organizations and high-traffic deployments.

Overview

Kubernetes deployment offers:

  • 🚀 Auto-Scaling: Automatic horizontal and vertical scaling based on demand
  • 🔄 High Availability: Multi-node deployment with automatic failover
  • 📊 Load Balancing: Intelligent traffic distribution across instances
  • 🔄 Rolling Updates: Zero-downtime deployments and updates
  • 📈 Resource Management: Efficient resource allocation and optimization
  • 🔍 Monitoring: Built-in monitoring and observability features
  • 🔒 Security: Pod security policies and network isolation

Production Ready

Kubernetes deployment is recommended for production environments requiring high availability, scalability, and enterprise-grade features.

Prerequisites

System Requirements

Kubernetes Cluster:

  • Kubernetes version 1.20+
  • At least 3 worker nodes for high availability
  • Minimum 4 CPU cores and 8GB RAM per node
  • 100GB+ persistent storage
  • LoadBalancer or Ingress controller

Required Resources:

  • CPU: 2-4 cores minimum (8+ cores recommended)
  • Memory: 8GB minimum (16GB+ recommended)
  • Storage: 50GB+ persistent volumes
  • Network: High-speed internal networking
  • External Storage: For persistent data and backups

Prerequisites Installation

Verify Kubernetes Cluster

Ensure your Kubernetes cluster is properly configured:

kubectl cluster-info
kubectl get nodes
kubectl get storageclass

Verify you have:

  • Running Kubernetes cluster
  • Available storage classes
  • Ingress controller (nginx, traefik, etc.)
  • LoadBalancer support (cloud provider or MetalLB)

Install Required Tools

Install necessary tools for deployment:

# Install Helm (package manager)
curl https://get.helm.sh/helm-v3.12.0-linux-amd64.tar.gz | tar xz
sudo mv linux-amd64/helm /usr/local/bin/
 
# Install kubectl (if not already installed)
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
 
# Verify installations
helm version
kubectl version --client

Configure Storage

Set up persistent storage for your deployment:

# storageclass.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: chatnio-storage
provisioner: kubernetes.io/aws-ebs  # Adjust for your cloud provider
parameters:
  type: gp3
  fsType: ext4
allowVolumeExpansion: true
reclaimPolicy: Retain
kubectl apply -f storageclass.yaml

Deployment Methods

Using Helm Chart for Easy Deployment

Add the CoAI.Dev Helm repository:

# Add Chart Nio Helm repository
helm repo add chatnio https://charts.chatnio.net
helm repo update
 
# View available charts
helm search repo chatnio

Basic Helm Installation:

# Create namespace
kubectl create namespace chatnio
 
# Install with default values
helm install chatnio chatnio/chatnio \
  --namespace chatnio \
  --set global.domain=chatnio.yourdomain.com

Custom Installation with Values:

# Install with custom configuration
helm install chatnio chatnio/chatnio \
  --namespace chatnio \
  --values values.yaml

Manual YAML Deployment

For custom deployments without Helm:

Create Namespace and Secrets

# namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: chatnio
---
# secrets.yaml
apiVersion: v1
kind: Secret
metadata:
  name: chatnio-secrets
  namespace: chatnio
type: Opaque
data:
  mysql-root-password: <base64-encoded-password>
  mysql-password: <base64-encoded-password>
  redis-password: <base64-encoded-password>
  jwt-secret: <base64-encoded-jwt-secret>
kubectl apply -f namespace.yaml
kubectl apply -f secrets.yaml

Deploy Database Services

# mysql.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
  namespace: chatnio
spec:
  serviceName: mysql
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:8.0
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: chatnio-secrets
              key: mysql-root-password
        - name: MYSQL_DATABASE
          value: chatnio
        - name: MYSQL_USER
          value: chatnio
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: chatnio-secrets
              key: mysql-password
        ports:
        - containerPort: 3306
        volumeMounts:
        - name: mysql-data
          mountPath: /var/lib/mysql
  volumeClaimTemplates:
  - metadata:
      name: mysql-data
    spec:
      accessModes: ["ReadWriteOnce"]
      storageClassName: chatnio-storage
      resources:
        requests:
          storage: 50Gi
---
apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: chatnio
spec:
  ports:
  - port: 3306
  selector:
    app: mysql

Deploy CoAI.Dev Application

# chatnio.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: chatnio
  namespace: chatnio
spec:
  replicas: 3
  selector:
    matchLabels:
      app: chatnio
  template:
    metadata:
      labels:
        app: chatnio
    spec:
      containers:
      - name: chatnio
        image: programzmh/chatnio:latest
        env:
        - name: MYSQL_HOST
          value: mysql
        - name: MYSQL_PORT
          value: "3306"
        - name: MYSQL_DB
          value: chatnio
        - name: MYSQL_USER
          value: chatnio
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: chatnio-secrets
              key: mysql-password
        - name: REDIS_HOST
          value: redis
        - name: REDIS_PORT
          value: "6379"
        - name: SECRET
          valueFrom:
            secretKeyRef:
              name: chatnio-secrets
              key: jwt-secret
        ports:
        - containerPort: 8000
        resources:
          requests:
            memory: "1Gi"
            cpu: "500m"
          limits:
            memory: "2Gi"
            cpu: "1000m"
        livenessProbe:
          httpGet:
            path: /api/health
            port: 8000
          initialDelaySeconds: 30
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /api/health
            port: 8000
          initialDelaySeconds: 5
          periodSeconds: 5
---
apiVersion: v1
kind: Service
metadata:
  name: chatnio
  namespace: chatnio
spec:
  selector:
    app: chatnio
  ports:
  - port: 80
    targetPort: 8000
  type: ClusterIP

Configure Ingress

# ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: chatnio-ingress
  namespace: chatnio
  annotations:
    nginx.ingress.kubernetes.io/proxy-body-size: "100m"
    nginx.ingress.kubernetes.io/proxy-connect-timeout: "600"
    nginx.ingress.kubernetes.io/proxy-send-timeout: "600"
    nginx.ingress.kubernetes.io/proxy-read-timeout: "600"
    cert-manager.io/cluster-issuer: "letsencrypt-prod"
spec:
  ingressClassName: nginx
  tls:
  - hosts:
    - chatnio.yourdomain.com
    secretName: chatnio-tls
  rules:
  - host: chatnio.yourdomain.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: chatnio
            port:
              number: 80

Scaling and Performance

Horizontal Pod Autoscaling

Auto-scaling Configuration:

# hpa.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: chatnio-hpa
  namespace: chatnio
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: chatnio
  minReplicas: 3
  maxReplicas: 20
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 80
  behavior:
    scaleUp:
      stabilizationWindowSeconds: 60
      policies:
      - type: Pods
        value: 2
        periodSeconds: 60
    scaleDown:
      stabilizationWindowSeconds: 300
      policies:
      - type: Pods
        value: 1
        periodSeconds: 60

Vertical Pod Autoscaling

VPA Configuration:

# vpa.yaml
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: chatnio-vpa
  namespace: chatnio
spec:
  targetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: chatnio
  updatePolicy:
    updateMode: "Auto"
  resourcePolicy:
    containerPolicies:
    - containerName: chatnio
      maxAllowed:
        cpu: "4"
        memory: "8Gi"
      minAllowed:
        cpu: "100m"
        memory: "256Mi"

Monitoring and Observability

Prometheus and Grafana Setup

Monitoring Stack Installation:

# Add Prometheus Helm repository
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
 
# Install monitoring stack
helm install monitoring prometheus-community/kube-prometheus-stack \
  --namespace monitoring \
  --create-namespace \
  --set grafana.adminPassword=admin123

CoAI.Dev Metrics Configuration:

# servicemonitor.yaml
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: chatnio-metrics
  namespace: chatnio
spec:
  selector:
    matchLabels:
      app: chatnio
  endpoints:
  - port: metrics
    interval: 30s
    path: /metrics

Logging

Centralized Logging with ELK Stack:

# Install Elasticsearch
helm repo add elastic https://helm.elastic.co
helm install elasticsearch elastic/elasticsearch \
  --namespace logging \
  --create-namespace
 
# Install Kibana
helm install kibana elastic/kibana \
  --namespace logging
 
# Install Filebeat for log collection
helm install filebeat elastic/filebeat \
  --namespace logging

Security

Network Policies

Restrict Network Traffic:

# network-policy.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: chatnio-network-policy
  namespace: chatnio
spec:
  podSelector:
    matchLabels:
      app: chatnio
  policyTypes:
  - Ingress
  - Egress
  ingress:
  - from:
    - namespaceSelector:
        matchLabels:
          name: ingress-nginx
    ports:
    - protocol: TCP
      port: 8000
  egress:
  - to:
    - podSelector:
        matchLabels:
          app: mysql
    ports:
    - protocol: TCP
      port: 3306
  - to:
    - podSelector:
        matchLabels:
          app: redis
    ports:
    - protocol: TCP
      port: 6379

Pod Security Standards

Security Context Configuration:

# Security context in deployment
spec:
  template:
    spec:
      securityContext:
        runAsNonRoot: true
        runAsUser: 1000
        fsGroup: 1000
      containers:
      - name: chatnio
        securityContext:
          allowPrivilegeEscalation: false
          readOnlyRootFilesystem: true
          capabilities:
            drop:
            - ALL
        volumeMounts:
        - name: tmp
          mountPath: /tmp
        - name: var-tmp
          mountPath: /var/tmp
      volumes:
      - name: tmp
        emptyDir: {}
      - name: var-tmp
        emptyDir: {}

Backup and Recovery

Database Backup

Automated Backup CronJob:

# backup-cronjob.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
  name: mysql-backup
  namespace: chatnio
spec:
  schedule: "0 2 * * *"  # Daily at 2 AM
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: mysql-backup
            image: mysql:8.0
            command:
            - /bin/bash
            - -c
            - |
              mysqldump -h mysql -u root -p$MYSQL_ROOT_PASSWORD chatnio > /backup/chatnio-$(date +%Y%m%d).sql
              aws s3 cp /backup/chatnio-$(date +%Y%m%d).sql s3://your-backup-bucket/
            env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: chatnio-secrets
                  key: mysql-root-password
            volumeMounts:
            - name: backup-storage
              mountPath: /backup
          volumes:
          - name: backup-storage
            emptyDir: {}
          restartPolicy: OnFailure

Troubleshooting

Common Issues

Pod Startup Issues

Problem: Pods fail to start or crash frequently

Solutions:

  1. Check resource limits and requests
  2. Verify database connectivity
  3. Review application logs
  4. Check persistent volume availability
  5. Verify secrets and configuration

Diagnostic Commands:

kubectl describe pod <pod-name> -n chatnio
kubectl logs <pod-name> -n chatnio
kubectl get events -n chatnio --sort-by='.lastTimestamp'

Database Connection Issues

Problem: Application cannot connect to database

Solutions:

  1. Verify MySQL service is running
  2. Check network policies
  3. Validate database credentials
  4. Test connectivity from application pod
  5. Review database logs

Debug Steps:

kubectl exec -it deployment/chatnio -n chatnio -- mysql -h mysql -u chatnio -p
kubectl logs deployment/mysql -n chatnio

Performance Optimization

Resource Tuning:

  • Monitor CPU and memory usage patterns
  • Adjust resource requests and limits
  • Configure appropriate JVM settings
  • Optimize database configuration
  • Use connection pooling

Network Optimization:

  • Configure appropriate network policies
  • Use service mesh for advanced traffic management
  • Implement caching strategies
  • Optimize ingress configuration

Kubernetes deployment provides the most robust and scalable solution for CoAI.Dev in production environments. This setup supports automatic scaling, high availability, and enterprise-grade security features essential for large-scale deployments.