-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcron.yaml
90 lines (88 loc) · 2.99 KB
/
cron.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
---
apiVersion: v1
kind: Secret
metadata:
name: postgres-secret
data:
username: YWRtaW4= #admin
database: cG9zdGdyZXM= #postgres
hostname: cG9zdGdyZXM= #postgres
password: YWRtaW4= #admin
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: postgres-backup
spec:
schedule: "0 */3 * * *" # Every 3 hr
jobTemplate:
spec:
template:
metadata:
labels:
app: backup
spec:
containers:
- name: update-backup
image: postgres:12
command: ["/bin/sh"]
args: ["-c", 'PGPASSWORD="$POSTGRES_PASSWORD" psql -U $POSTGRES_USERNAME -h $HOSTNAME -d $DATABASE -p 5432 -f /var/lib/postgresql/backup.sql']
env:
- name: POSTGRES_USERNAME
valueFrom:
secretKeyRef:
name: postgres-secret
key: username
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: postgres-secret
key: password
- name: POSTGRES_HOSTNAME
valueFrom:
secretKeyRef:
name: postgres-secret
key: hostname
- name: POSTGRES_DATABASE
valueFrom:
secretKeyRef:
name: postgres-secret
key: database
volumeMounts:
- mountPath: "/var/lib/postgresql"
name: postgres-storage
initContainers:
- name: postgres-backup
image: postgres:12
command: ["/bin/sh"]
args: ["-c", 'PGPASSWORD="$POSTGRES_PASSWORD" pg_dump -U $POSTGRES_USERNAME -h $HOSTNAME -d $DATABASE -p 5432 > /var/lib/postgresql/backup.sql']
env:
- name: POSTGRES_USERNAME
valueFrom:
secretKeyRef:
name: postgres-secret
key: username
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: postgres-secret
key: password
- name: POSTGRES_HOSTNAME
valueFrom:
secretKeyRef:
name: postgres-secret
key: hostname
- name: POSTGRES_DATABASE
valueFrom:
secretKeyRef:
name: postgres-secret
key: database
volumeMounts:
- mountPath: "/var/lib/postgresql"
name: postgres-storage
restartPolicy: OnFailure
securityContext:
runAsUser: 999
volumes:
- name: postgres-storage
emptyDir: {} #store backup data temporarily on worker node. once cron completes it will get deleted.