Kubernetes — Очистка раздела пода Postgres при удалении

Задача следующая, удалять содержимое Persistent Storage для пода с Postgres базой данных при включенной опции при сборке в Jenkins.

Для удобства мое приложение называется: "myapplication"

manifest.yml

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: gp2-myapplication
parameters:
  fsType: ext4
  type: gp2
provisioner: kubernetes.io/aws-ebs
reclaimPolicy: Delete
volumeBindingMode: Immediate

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: myapplication-data
  namespace: staging
spec:
  storageClassName: gp2-myapplication
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

---

apiVersion: v1
kind: ConfigMap
metadata:
  name: myapplication-postgres-config
  namespace: staging
  labels:
    app: myapplication-postgres-app
data:
  POSTGRES_DB: myapplication_database
  POSTGRES_PASSWORD: PASSWORD
  POSTGRES_USER: myapplication_user

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapplication-postgres-app
  namespace: staging
  labels:
    app: myapplication-postgres-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myapplication-postgres-app
  strategy:
    type: RollingUpdate
  progressDeadlineSeconds: 300
  template:
    metadata:
      labels:
        app: myapplication-postgres-app
    spec:
      volumes:
      - name: storage-myapplication
        persistentVolumeClaim:
         claimName: myapplication-data
      containers:
      - image: postgres
        name: myapplication-postgres
        imagePullPolicy: "IfNotPresent"
        envFrom:
            - configMapRef:
                name: myapplication-postgres-config
        ports:
          - containerPort: 5432
        volumeMounts:
          - mountPath: "/usr/local/pgsql/data"
            name: storage-myapplication
      nodeSelector:
        nodegroup: staging

---

apiVersion: v1
kind: Service
metadata:
  name: postgres-db-myapplication
  namespace: staging
spec:
  ports:
  - port: 5432
    targetPort: 5432
  selector:
    app: myapplication-postgres-app
  type: ClusterIP

Применим созданный манифест:

kubectl create -f manifest.yml

В Jenkins в MultibrachPipeline добавляем параметр сборки:

pipeline {
    agent any
    parameters {
        booleanParam(name: 'reinitialize_psql', defaultValue: false, description: 'Allows you to reinitialize postgreSQL.')
    }

И собственно шаг очистки:

        stage ('Reinitialize postgreSQL.'){
            when { 
                allOf {
                    branch 'staging'                
                    expression { return params.reinitialize_psql } 
                }
            }
            steps {
                sh "${KUBECTL} -n staging delete -f .jenkins/database.yml"
                sh "sleep 30"
                sh "${KUBECTL} -n staging apply -f .jenkins/database.yml"
            }
        }

Содержимое файла "database.yml" в директории ".jenkins"

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: myapplication-data
  namespace: staging
spec:
  storageClassName: gp2-myapplication
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapplication-postgres-app
  namespace: staging
  labels:
    app: myapplication-postgres-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myapplication-postgres-app
  strategy:
    type: RollingUpdate
  progressDeadlineSeconds: 300
  template:
    metadata:
      labels:
        app: myapplication-postgres-app
    spec:
      volumes:
      - name: storage-myapplication
        persistentVolumeClaim:
         claimName: myapplication-data
      containers:
      - image: postgres
        name: myapplication-postgres
        imagePullPolicy: "IfNotPresent"
        envFrom:
            - configMapRef:
                name: myapplication-postgres-config
        ports:
          - containerPort: 5432
        volumeMounts:
          - mountPath: "/usr/local/pgsql/data"
            name: storage-myapplication
      nodeSelector:
        nodegroup: staging
0 0 vote
Article Rating

Метки: Метки

Подписаться
Уведомление о
guest
0 Комментарий
Inline Feedbacks
View all comments