---
apiVersion: v1
kind: ConfigMap
metadata:
  name: timescaledb-config
  namespace: wdd-rmdc
data:
  postgresql.conf: |
    # 内存优化配置
    shared_buffers = 4GB
    effective_cache_size = 12GB
    maintenance_work_mem = 1GB
    checkpoint_completion_target = 0.9
    wal_buffers = 16MB
    default_statistics_target = 100
    random_page_cost = 1.1
    effective_io_concurrency = 200
    work_mem = 128MB
    min_wal_size = 2GB
    max_wal_size = 8GB
    max_worker_processes = 8
    max_parallel_workers_per_gather = 4
    max_parallel_workers = 8
    max_parallel_maintenance_workers = 4
    
    # TimescaleDB 优化
    timescaledb.max_background_workers = 8
    shared_preload_libraries = 'timescaledb'
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: timescaledb
  namespace: wdd-rmdc
spec:
  serviceName: timescaledb
  replicas: 1
  selector:
    matchLabels:
      app: timescaledb
  template:
    metadata:
      labels:
        app: timescaledb
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: kubernetes.io/hostname
                    operator: In
                    values:
                      - worker-2-192.168.40.62
      imagePullSecrets:
        - name: harborsecret
      containers:
        - name: timescaledb
          image: harbor.cdcyy.com.cn/cmii/timescaledb:2.24.0-pg17
          ports:
            - containerPort: 5432
              name: postgresql
          env:
            - name: POSTGRES_PASSWORD
              value: "admin@123"
            - name: POSTGRES_USER
              value: "postgres"
            - name: POSTGRES_DB
              value: "timescaledb"
            - name: PGDATA
              value: /var/lib/postgresql/data/pgdata
            - name: TS_TUNE_MEMORY
              value: "16GB"
            - name: TS_TUNE_NUM_CPUS
              value: "8"
          resources:
            requests:
              memory: "8Gi"
              cpu: "4"
            limits:
              memory: "16Gi"
              cpu: "4"
          volumeMounts:
            - name: data
              mountPath: /var/lib/postgresql/data
            - name: config
              mountPath: /etc/postgresql/postgresql.conf
              subPath: postgresql.conf
          livenessProbe:
            exec:
              command:
                - /bin/sh
                - -c
                - pg_isready -U postgres
            initialDelaySeconds: 30
            periodSeconds: 10
          readinessProbe:
            exec:
              command:
                - /bin/sh
                - -c
                - pg_isready -U postgres
            initialDelaySeconds: 5
            periodSeconds: 5
      volumes:
        - name: config
          configMap:
            name: timescaledb-config
        - name: data
          hostPath:
            path: /var/lib/docker/postgresql_data
---
apiVersion: v1
kind: Service
metadata:
  name: timescaledb
  namespace: wdd-rmdc
spec:
  type: NodePort
  ports:
    - port: 5432
      targetPort: 5432
      nodePort: 35432
      protocol: TCP
      name: postgresql
  selector:
    app: timescaledb
---
apiVersion: v1
kind: Service
metadata:
  name: timescaledb-headless
  namespace: wdd-rmdc
spec:
  clusterIP: None
  ports:
    - port: 5432
      targetPort: 5432
      protocol: TCP
      name: postgresql
  selector:
    app: timescaledb
