Skip to content

Commit 76fff18

Browse files
committed
chore(postgresql-cluster): generate password random
1 parent 4efae11 commit 76fff18

5 files changed

Lines changed: 184 additions & 64 deletions

File tree

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,90 @@
1+
## CREATE PG INSTANCE
12

23
```
3-
dataname:
4-
dbname: dbn
5-
username: admin
6-
password: R3Dv0NEmwZkhhz
4+
# create or update pg instance template yaml
75
networkPolicy.allowNamespaces:
86
- mx-test1
97
service.type: ClusterIP
108
metrics.enabled: true
119
backup:
10+
# whether BackUP should be enabled
1211
enabled: true
12+
# Cron schedule for doing base backups
13+
scheduleCronJob: "20 0 * * 0"
14+
Amount of base backups to retain
15+
retainBackups: 2
1316
s3:
1417
awsAccessKeyID: DO9l771LqiwZkhhz
1518
awsSecretAccessKey: R3Dv0NEmJBo8JFdn1q8jz49ArWwpDjFn
1619
walGS3Prefix: mx-test
17-
```
20+
```
21+
22+
## Create app user and database
23+
24+
- Login PG with admin user & password
25+
26+
- CREATE APP USER
27+
```
28+
CREATE USER `my_user` WITH CONNECTION LIMIT `conn_limit` LOGIN ENCRYPTED PASSWORD 'password';
29+
```
30+
- CREATE APP DATABASE
31+
```
32+
CREATE DATABASE `my_db` OWNER `my_user`;
33+
```
34+
- CREATE EXTENSIONS
35+
```
36+
CREATE EXTENSION pg_buffercache;
37+
```
38+
39+
## Network Access
40+
41+
Default access allow policy: only namespace scope.
42+
43+
- allow `mx-test1` namespace access
44+
```
45+
networkPolicy.allowNamespaces:
46+
- mx-test1
47+
```
48+
49+
- Assign external network IP address
50+
```
51+
service.type: LoadBlance
52+
```
53+
54+
## Manger backup your data `Very important`
55+
56+
`Strongly recommend enabling this feature.`
57+
`Strongly recommend enabling this feature.`
58+
`Strongly recommend enabling this feature.`
59+
60+
PG data backup use S3 as backenp store. Choose an independent storage space `outside of the current environment` as your backup space.
61+
```
62+
backup:
63+
# whether BackUP should be enabled
64+
enabled: true
65+
# Cron schedule for doing base backups
66+
scheduleCronJob: "20 0 * * 0"
67+
Amount of base backups to retain
68+
retainBackups: 2
69+
s3:
70+
awsAccessKeyID: DO9l771LqiwZkhhz
71+
awsSecretAccessKey: R3Dv0NEmJBo8JFdn1q8jz49ArWwpDjFn
72+
walGS3Prefix: mx-test
73+
```
74+
75+
You can modify multiple content at once, there is no need to modify part of it each time.
76+
77+
# Plans
78+
79+
| Resource Specification | Cores | MEMORY | Storage SIZE |
80+
| :---: | :---: | :---: | :---: |
81+
| standard-10 | 1C | 2G | 10G |
82+
| standard-20 | 2C | 4G | 20G |
83+
| standard-50 | 2C | 8G | 50G |
84+
| standard-100 | 4C | 16G | 100G |
85+
| standard-200 | 8C | 32G | 200G |
86+
| standard-400 | 16C | 64G | 400G |
87+
| standard-800 | 32C | 128G | 800G |
88+
89+
In order to obtain a better experience, it is recommended not to exceed 80% usage of resource utilization for a long period of time. If there is a need for larger resource scale, please apply for private customization.
90+

addons/postgresql-cluster/15/chart/postgresql-cluster/templates/_helpers.tpl

Lines changed: 73 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ Create patroni envs.
107107
secretKeyRef:
108108
name: {{ template "patroni.fullname" . }}
109109
key: password-rewind
110+
- name: ADMIN_USER
111+
valueFrom:
112+
secretKeyRef:
113+
name: {{ template "patroni.fullname" . }}
114+
key: admin-user
115+
- name: ADMIN_PASSWORD
116+
valueFrom:
117+
secretKeyRef:
118+
name: {{ template "patroni.fullname" . }}
119+
key: admin-password
110120
- name: PATRONI_SCOPE
111121
value: {{ template "patroni.fullname" . }}
112122
- name: PATRONI_NAME
@@ -123,21 +133,6 @@ Create patroni envs.
123133
value: '0.0.0.0:5432'
124134
- name: PATRONI_RESTAPI_LISTEN
125135
value: '0.0.0.0:8008'
126-
- name: DATABASE_NAME
127-
valueFrom:
128-
secretKeyRef:
129-
name: {{ template "patroni.fullname" . }}
130-
key: data-name
131-
- name: DATABASE_USER
132-
valueFrom:
133-
secretKeyRef:
134-
name: {{ template "patroni.fullname" . }}
135-
key: data-user
136-
- name: DATABASE_PASSWORD
137-
valueFrom:
138-
secretKeyRef:
139-
name: {{ template "patroni.fullname" . }}
140-
key: data-password
141136
{{- end -}}
142137

143138
{{/*
@@ -178,3 +173,66 @@ Create walg envs.
178173
value: ""
179174
{{- end }}
180175
{{- end }}
176+
177+
{{/*
178+
Generate random password
179+
*/}}
180+
181+
{{/*
182+
Get the super user password ;
183+
*/}}
184+
{{- define "credentials.superuserValue" }}
185+
{{- if .Values.credentials.superuser }}
186+
{{- .Values.credentials.superuser -}}
187+
{{- else -}}
188+
{{- include "getValueFromSecret" (dict "Namespace" .Release.Namespace "Name" (include "common.names.fullname" .) "Length" 10 "Key" "password-superuser") -}}
189+
{{- end -}}
190+
{{- end }}
191+
192+
{{/*
193+
Get the rewind password ;
194+
*/}}
195+
{{- define "credentials.rewindValue" }}
196+
{{- if .Values.credentials.rewind }}
197+
{{- .Values.credentials.rewind -}}
198+
{{- else -}}
199+
{{- include "getValueFromSecret" (dict "Namespace" .Release.Namespace "Name" (include "common.names.fullname" .) "Length" 10 "Key" "password-rewind") -}}
200+
{{- end -}}
201+
{{- end }}
202+
203+
{{/*
204+
Get the replication password ;
205+
*/}}
206+
{{- define "credentials.replicationValue" }}
207+
{{- if .Values.credentials.replication }}
208+
{{- .Values.credentials.replication -}}
209+
{{- else -}}
210+
{{- include "getValueFromSecret" (dict "Namespace" .Release.Namespace "Name" (include "common.names.fullname" .) "Length" 10 "Key" "password-replication") -}}
211+
{{- end -}}
212+
{{- end }}
213+
214+
{{/*
215+
Get the administrator password ;
216+
*/}}
217+
{{- define "adminRole.passwordValue" }}
218+
{{- if .Values.adminRole.password }}
219+
{{- .Values.adminRole.password -}}
220+
{{- else -}}
221+
{{- include "getValueFromSecret" (dict "Namespace" .Release.Namespace "Name" (include "common.names.fullname" .) "Length" 10 "Key" "password-replication") -}}
222+
{{- end -}}
223+
{{- end }}
224+
225+
{{/*
226+
Returns the available value for certain key in an existing secret (if it exists),
227+
otherwise it generates a random value.
228+
*/}}
229+
{{- define "getValueFromSecret" }}
230+
{{- $len := (default 16 .Length) | int -}}
231+
{{- $obj := (lookup "v1" "Secret" .Namespace .Name).data -}}
232+
{{- if $obj }}
233+
{{- index $obj .Key | b64dec -}}
234+
{{- else -}}
235+
{{- randAlphaNum $len -}}
236+
{{- end -}}
237+
{{- end }}
238+

addons/postgresql-cluster/15/chart/postgresql-cluster/templates/sec.yaml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ metadata:
1010
heritage: {{ .Release.Service }}
1111
cluster-name: {{ template "patroni.fullname" . }}
1212
type: Opaque
13-
data:
14-
password-superuser: {{ .Values.credentials.superuser | b64enc }}
15-
password-rewind: {{ .Values.credentials.rewind | b64enc }}
16-
password-replication: {{ .Values.credentials.replication | b64enc }}
17-
data-user: {{ .Values.database.username | b64enc }}
18-
data-name: {{ .Values.database.dbname | b64enc }}
19-
data-password: {{ .Values.database.password | b64enc }}
13+
data:
14+
password-superuser: {{ include "credentials.superuserValue" . | b64enc | quote }}
15+
password-rewind: {{ include "credentials.rewindValue" . | b64enc | quote }}
16+
password-replication: {{ include "credentials.replicationValue" . | b64enc | quote }}
17+
admin-user: {{ .Values.adminRole.username | b64enc }}
18+
admin-password: {{ .Values.adminRole.password | b64enc }}

addons/postgresql-cluster/15/chart/postgresql-cluster/values.yaml

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ image:
1717
# https://github.com/zalando/patroni/blob/master/docs/SETTINGS.rst#postgresql
1818
# https://github.com/zalando/spilo/blob/master/ENVIRONMENT.rst
1919
credentials:
20-
superuser: tea
21-
rewind: cola
22-
replication: reppasswd
20+
superuser: ""
21+
rewind: ""
22+
replication: ""
2323

24-
database:
25-
dbname: db1
26-
username: us1
27-
password: 111w
24+
adminRole:
25+
username: administrator
26+
password: ""
2827

2928
# Distribution Configuration stores
3029
# Please note that only one of the following stores should be enabled.
@@ -126,22 +125,21 @@ postInitScript: |
126125
#!/bin/bash
127126
set -Eeu
128127
# Create monitor user
129-
psql -w -c "CREATE USER tea_mon WITH ROLE pg_monitor"
130-
# Create init database & user
131-
if [[( -n "$DATABASE_USER") && ( -n "$DATABASE_PASSWORD") && ( -n "$DATABASE_NAME")]]; then
132-
echo "Creating user ${DATABASE_USER}"
133-
psql -w -c "create user ${DATABASE_USER} WITH LOGIN ENCRYPTED PASSWORD '${DATABASE_PASSWORD}'"
134-
echo "Creating database ${DATABASE_NAME} "
135-
psql -w -c "CREATE DATABASE ${DATABASE_NAME} OWNER ${DATABASE_USER} CONNECTION LIMIT 1000"
136-
psql -w -d ${DATABASE_NAME} -c "create extension postgis ; create extension pg_stat_statements ; create extension pg_buffercache ;"
137-
psql -w -c "CHECKPOINT;CHECKPOINT;"
128+
psql -w -c "CREATE USER tea_mon WITH ROLE pg_monitor;create extension pg_stat_statements;create extension pg_buffercache ;"
129+
# Create admin user
130+
if [[( -n "$ADMIN_USER") && ( -n "$ADMIN_PASSWORD")]]; then
131+
echo "Creating user ${ADMIN_USER}"
132+
# psql -w -c "CREATE USER ${ADMIN_USER} WITH NOSUPERUSER CREATEDB CREATEROLE REPLICATION CONNECTION LIMIT 10 LOGIN ENCRYPTED PASSWORD '${ADMIN_PASSWORD}'"
138133
else
139-
echo "Skipping user creation"
140-
echo "Skipping database creation"
134+
echo "Skipping create admin user"
141135
fi
136+
psql -w -c "CHECKPOINT;CHECKPOINT;"
137+
#norm user
138+
#
139+
#
142140
postgresql:
143141
config: |-
144-
log_min_duration_statement = 1008
142+
log_min_duration_statement = 1000
145143
max_wal_size = 4GB
146144
min_wal_size = 4GB
147145
max_connections = 1005
@@ -249,25 +247,25 @@ metrics:
249247
memory: 512Mi
250248
backup:
251249
# Specifies whether Wal-G should be enabled
252-
enabled: true
250+
enabled: false
253251
# Cron schedule for doing base backups
254252
scheduleCronJob: "20 0 * * 0"
255253
# Amount of base backups to retain
256254
retainBackups: 2
257255
# Name of the secret that holds the credentials to the bucket
258256
kubernetesSecret:
259257
# Maximum size of the WAL segments accumulated after the base backup to
260-
# consider WAL-E restore instead of pg_basebackup
258+
# consider WAL-G restore instead of pg_basebackup
261259
backupThresholdMegabytes: 1024
262260
# Maximum ratio (in percents) of the accumulated WAL files to the base backup
263-
# to consider WAL-E restore instead of pg_basebackup
261+
# to consider WAL-G restore instead of pg_basebackup
264262
backupThresholdPercentage: 30
265263
s3:
266264
used: true
267-
awsAccessKeyID: "minioadmin"
268-
awsSecretAccessKey: "minioadmin"
269-
walGS3Prefix: "s3://pg1"
270-
awsEndpoint: "http://10.10.2.11:9000"
265+
awsAccessKeyID: ""
266+
awsSecretAccessKey: ""
267+
walGS3Prefix: "s3://xx"
268+
awsEndpoint: "http://xxxx:9000"
271269
awsS3ForcePathStyle: "true"
272270
awsRegion: dx-1
273271

addons/postgresql-cluster/15/meta.yaml

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,14 @@ tags: postgresql-cluster
1313
bindable: true
1414
instances_retrievable: true
1515
bindings_retrievable: true
16-
plan_updateable: false
16+
plan_updateable: true
1717
allow_parameters:
18-
- name: "dataname"
19-
description: "database config for values.yaml"
2018
- name: "networkPolicy.allowNamespaces"
2119
description: "networkPolicy allowNamespaces config for values.yaml"
2220
- name: "service.type"
2321
description: "service type config for values.yaml"
2422
- name: "metrics.enabled"
2523
description: "Whether to enable metrics. default true"
26-
- name: "backup.enabled"
27-
description: "Whether to use S3 for backup your data. default true . ps: Make sure there is a available S3 "
28-
- name: "backup.s3.awsAccessKeyID"
29-
description: "S3 awsAccessKeyID"
30-
- name: "backup.s3.awsSecretAccessKey"
31-
description: "S3 awsSecretAccessKey"
32-
- name: "backup.s3.walGS3Prefix"
33-
description: "S3 walGS3Prefix : etc . s3://bucket001"
24+
- name: "backup"
25+
description: "Whether to use S3 for backup your data. default false . ps: Make sure there is a available S3 "
3426
archive: false

0 commit comments

Comments
 (0)