· 5 years ago · Jun 30, 2020, 09:12 AM
1---
2apiVersion: v1
3kind: Service
4metadata:
5 name: postgres
6 namespace: italian-coffee-bar
7spec:
8 selector:
9 db: postgres
10 ports:
11 - name: "postgres-port"
12 port: 5432
13 targetPort: 5432
14---
15apiVersion: v1
16kind: PersistentVolume
17metadata:
18 name: postgres-pv-volume
19 namespace: italian-coffee-bar
20 labels:
21 name: postgres-pv-volume
22spec:
23 storageClassName: manual
24 capacity:
25 storage: 1Gi
26 accessModes:
27 - ReadWriteMany
28 hostPath:
29 path: "/mnt/data"
30---
31apiVersion: v1
32kind: PersistentVolumeClaim
33metadata:
34 name: postgres-volume
35 namespace: italian-coffee-bar
36spec:
37 selector:
38 matchLabels:
39 name: postgres-pv-volume
40 storageClassName: manual
41 accessModes:
42 - ReadWriteMany
43 resources:
44 requests:
45 storage: 1Gi
46---
47apiVersion: v1
48kind: ConfigMap
49metadata:
50 name: postgres-init-sql
51 namespace: italian-coffee-bar
52 labels:
53 db: postgres-init-sql
54data:
55 init-sql: |
56 CREATE USER account WITH PASSWORD 'account';
57 CREATE DATABASE account;
58 GRANT ALL PRIVILEGES ON DATABASE account TO account;
59
60 \c account;
61
62 CREATE TABLE IF NOT EXISTS account (
63 id SERIAL PRIMARY KEY,
64 product VARCHAR(255),
65 price SMALLINT,
66 items_sold BIGINT
67 );
68 CREATE UNIQUE INDEX IF NOT EXISTS product ON account (product);
69
70 GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO account;
71
72 INSERT INTO account (product, price, items_sold) VALUES ('espresso', 2, 0)
73 ON CONFLICT (product) DO NOTHING;
74---
75apiVersion: apps/v1
76kind: Deployment
77metadata:
78 name: postgres
79 namespace: italian-coffee-bar
80spec:
81 selector:
82 matchLabels:
83 db: postgres
84 replicas: 1
85 strategy:
86 type: Recreate
87 template:
88 metadata:
89 labels:
90 db: postgres
91 spec:
92 containers:
93 - image: postgres:9.6.2-alpine
94 name: postgres
95 ports:
96 - containerPort: 5432
97 name: postgres
98 env:
99 - name: POSTGRES_HOST_AUTH_METHOD
100 value: trust
101 volumeMounts:
102 - name: postgres-init-script
103 mountPath: /docker-entrypoint-initdb.d/init.sql
104 subPath: init.sql
105 - name: postgres-storage
106 mountPath: /var/lib/postgresql/data
107 hostname: postgres
108 restartPolicy: Always
109 volumes:
110 - name: postgres-storage
111 persistentVolumeClaim:
112 claimName: postgres-volume
113 - configMap:
114 name: postgres-init-sql
115 items:
116 - key: init-sql
117 path: init.sql
118 name: postgres-init-script