Skip to content

Commit 42bf8cf

Browse files
authored
Merge pull request #11 from jianxiaoguo/main
feat(postgres): use patroni
2 parents c186962 + 91c8331 commit 42bf8cf

26 files changed

Lines changed: 341 additions & 488 deletions

Dockerfile

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,34 @@
11
FROM registry.drycc.cc/drycc/base:bullseye
22

3-
COPY rootfs/bin /bin/
4-
COPY rootfs/docker-entrypoint-initdb.d /docker-entrypoint-initdb.d/
5-
COPY rootfs/docker-entrypoint.sh /docker-entrypoint.sh
6-
ENV JQ_VERSION="1.6" \
7-
GOSU_VERSION="1.14" \
8-
MC_VERSION="2022.04.01.23.44.48" \
9-
WAL_G_VERSION="1.1" \
10-
PYTHON_VERSION="3.10.4" \
3+
COPY rootfs/usr /usr/
4+
COPY rootfs/entrypoint.sh /entrypoint.sh
5+
ENV PYTHON_VERSION="3.10.4" \
116
PG_MAJOR=14 \
127
POSTGRESQL_VERSION="14.2"
138

14-
159
ENV PGDATA /data/${PG_MAJOR}
16-
ENV WALG_ENVDIR /etc/wal-g.d/env
1710

18-
RUN mkdir -p $WALG_ENVDIR \
19-
&& install-stack jq $JQ_VERSION \
20-
&& install-stack gosu $GOSU_VERSION \
21-
&& install-stack mc $MC_VERSION \
22-
&& install-stack wal-g $WAL_G_VERSION \
23-
&& install-stack python $PYTHON_VERSION \
11+
RUN install-stack python $PYTHON_VERSION \
2412
&& install-stack postgresql $POSTGRESQL_VERSION && . init-stack \
2513
&& rm -rf \
26-
/usr/share/doc \
27-
/usr/share/man \
28-
/usr/share/info \
29-
/usr/share/locale \
30-
/var/lib/apt/lists/* \
31-
/var/log/* \
32-
/var/cache/debconf/* \
33-
/etc/systemd \
34-
/lib/lsb \
35-
/lib/udev \
36-
/usr/lib/`echo $(uname -m)`-linux-gnu/gconv/IBM* \
37-
/usr/lib/`echo $(uname -m)`-linux-gnu/gconv/EBC* \
14+
/usr/share/doc \
15+
/usr/share/man \
16+
/usr/share/info \
17+
/usr/share/locale \
18+
/var/lib/apt/lists/* \
19+
/var/log/* \
20+
/var/cache/debconf/* \
21+
/etc/systemd \
22+
/lib/lsb \
23+
/lib/udev \
24+
/usr/lib/`echo $(uname -m)`-linux-gnu/gconv/IBM* \
25+
/usr/lib/`echo $(uname -m)`-linux-gnu/gconv/EBC* \
3826
&& mkdir -p /usr/share/man/man{1..8} \
39-
&& mkdir -p /run/postgresql $PGDATA \
27+
&& mkdir -p $PGDATA \
4028
&& groupadd postgres && useradd -g postgres postgres \
41-
&& chown -R postgres:postgres /run/postgresql $PGDATA \
42-
&& set -eux; pip3 install --disable-pip-version-check --no-cache-dir envdir 2>/dev/null
29+
&& chown -R postgres:postgres /data \
30+
&& set -eux; pip3 install --disable-pip-version-check --no-cache-dir psycopg[binary] patroni[kubernetes] 2>/dev/null
4331

44-
ENTRYPOINT ["init-stack", "/docker-entrypoint.sh"]
45-
CMD ["postgres"]
46-
EXPOSE 5432
32+
USER postgres
33+
ENTRYPOINT ["init-stack", "/entrypoint.sh"]
34+
EXPOSE 5432 8008

Makefile

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ PLATFORM ?= linux/amd64,linux/arm64
99

1010
include versioning.mk
1111

12-
SHELL_SCRIPTS = $(wildcard _scripts/*.sh contrib/ci/*.sh rootfs/bin/*)
12+
SHELL_SCRIPTS = $(wildcard rootfs/usr/share/scripts/patroni/*)
1313

1414
# The following variables describe the containerized development environment
1515
# and other build options
@@ -29,14 +29,9 @@ docker-build:
2929
docker-buildx:
3030
docker buildx build --platform ${PLATFORM} -t ${IMAGE} . --push
3131

32-
test: test-style test-functional
32+
test: test-style
3333

3434
test-style:
3535
${DEV_ENV_CMD} shellcheck $(SHELL_SCRIPTS)
3636

37-
test-functional: test-functional-storage
38-
39-
test-functional-storage:
40-
contrib/ci/test-storage.sh ${IMAGE}
41-
4237
.PHONY: all docker-build docker-push test

charts/database/templates/_helper.tpl

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,77 @@
22
env:
33
- name: DATABASE_STORAGE
44
value: "{{.Values.global.storage}}"
5+
{{- if eq .Values.debug "true" }}
6+
- name: PATRONI_LOG_LEVEL
7+
value: DEBUG
8+
- name: PATRONI_LOG_TRACEBACK_LEVEL
9+
value: DEBUG
10+
{{- end }}
511
- name: PGCTLTIMEOUT
612
value: "{{.Values.timeout}}"
7-
- name: "DRYCC_DATABASE_USER"
13+
- name: PATRONI_KUBERNETES_POD_IP
814
valueFrom:
9-
secretKeyRef:
10-
name: database-creds
11-
key: user
12-
- name: "DRYCC_DATABASE_PASSWORD"
15+
fieldRef:
16+
fieldPath: status.podIP
17+
- name: PATRONI_KUBERNETES_NAMESPACE
18+
valueFrom:
19+
fieldRef:
20+
fieldPath: metadata.namespace
21+
- name: PATRONI_KUBERNETES_BYPASS_API_SERVICE
22+
value: 'true'
23+
- name: PATRONI_KUBERNETES_USE_ENDPOINTS
24+
value: 'true'
25+
- name: PATRONI_KUBERNETES_LABELS
26+
value: '{app: drycc-database, cluster-name: drycc-database}'
27+
- name: PATRONI_SCOPE
28+
value: drycc-database
29+
- name: PATRONI_NAME
30+
valueFrom:
31+
fieldRef:
32+
fieldPath: metadata.name
33+
- name: PATRONI_POSTGRESQL_PGPASS
34+
value: /tmp/pgpass
35+
- name: PATRONI_POSTGRESQL_LISTEN
36+
value: '0.0.0.0:5432'
37+
- name: PATRONI_RESTAPI_LISTEN
38+
value: '0.0.0.0:8008'
39+
- name: DRYCC_DATABASE_SUPERUSER
1340
valueFrom:
1441
secretKeyRef:
1542
name: database-creds
16-
key: password
17-
- name: "DRYCC_STORAGE_LOOKUP"
43+
key: superuser
44+
- name: DRYCC_DATABASE_SUPERUSER_PASSWORD
1845
valueFrom:
1946
secretKeyRef:
20-
name: storage-creds
21-
key: lookup
22-
- name: "DRYCC_STORAGE_HEALTH"
47+
name: database-creds
48+
key: superuser-password
49+
- name: DRYCC_DATABASE_REPLICATOR
2350
valueFrom:
2451
secretKeyRef:
25-
name: storage-creds
26-
key: health
27-
- name: "DRYCC_STORAGE_BUCKET"
52+
name: database-creds
53+
key: replicator
54+
- name: DRYCC_DATABASE_REPLICATOR_PASSWORD
2855
valueFrom:
2956
secretKeyRef:
30-
name: storage-creds
31-
key: database-bucket
32-
- name: "DRYCC_STORAGE_ENDPOINT"
57+
name: database-creds
58+
key: replicator-password
59+
- name: "DRYCC_DATABASE_USER"
3360
valueFrom:
3461
secretKeyRef:
35-
name: storage-creds
36-
key: endpoint
37-
- name: "DRYCC_STORAGE_ACCESSKEY"
62+
name: database-creds
63+
key: user
64+
- name: "DRYCC_DATABASE_PASSWORD"
3865
valueFrom:
3966
secretKeyRef:
40-
name: storage-creds
41-
key: accesskey
42-
- name: "DRYCC_STORAGE_SECRETKEY"
67+
name: database-creds
68+
key: password
69+
{{- if eq .Values.global.storageLocation "on-cluster" }}
70+
- name: "DRYCC_STORAGE_HEALTH"
4371
valueFrom:
4472
secretKeyRef:
4573
name: storage-creds
46-
key: secretkey
74+
key: health
75+
{{- end }}
4776
{{- end }}
4877

4978
{{/* Generate database deployment limits */}}
@@ -64,4 +93,4 @@ resources:
6493
hugepages-1Gi: {{.Values.limitsHugepages1Gi}}
6594
{{- end }}
6695
{{- end }}
67-
{{- end }}
96+
{{- end }}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{{- if eq .Values.global.databaseLocation "on-cluster" }}
2+
apiVersion: rbac.authorization.k8s.io/v1
3+
kind: ClusterRole
4+
metadata:
5+
name: drycc-database
6+
rules:
7+
- apiGroups:
8+
- ""
9+
resources:
10+
- endpoints
11+
resourceNames:
12+
- kubernetes
13+
verbs:
14+
- get
15+
{{- end }}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{{- if eq .Values.global.databaseLocation "on-cluster" }}
2+
apiVersion: rbac.authorization.k8s.io/v1
3+
kind: ClusterRoleBinding
4+
metadata:
5+
name: drycc-database
6+
roleRef:
7+
apiGroup: rbac.authorization.k8s.io
8+
kind: ClusterRole
9+
name: drycc-database
10+
subjects:
11+
- kind: ServiceAccount
12+
name: drycc-database
13+
namespace: {{ .Release.Namespace }}
14+
{{- end }}

charts/database/templates/database-pvc.yaml

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{{- if eq .Values.global.databaseLocation "on-cluster" }}
2+
apiVersion: rbac.authorization.k8s.io/v1
3+
kind: RoleBinding
4+
metadata:
5+
name: drycc-database
6+
roleRef:
7+
apiGroup: rbac.authorization.k8s.io
8+
kind: Role
9+
name: drycc-database
10+
subjects:
11+
- kind: ServiceAccount
12+
name: drycc-database
13+
{{- end }}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{{- if eq .Values.global.databaseLocation "on-cluster" }}
2+
apiVersion: rbac.authorization.k8s.io/v1
3+
kind: Role
4+
metadata:
5+
name: drycc-database
6+
rules:
7+
- apiGroups:
8+
- ""
9+
resources:
10+
- configmaps
11+
verbs:
12+
- create
13+
- get
14+
- list
15+
- patch
16+
- update
17+
- watch
18+
# delete and deletecollection are required only for 'patronictl remove'
19+
- delete
20+
- deletecollection
21+
- apiGroups:
22+
- ""
23+
resources:
24+
- endpoints
25+
verbs:
26+
- get
27+
- patch
28+
- update
29+
# the following three privileges are necessary only when using endpoints
30+
- create
31+
- list
32+
- watch
33+
# delete and deletecollection are required only for for 'patronictl remove'
34+
- delete
35+
- deletecollection
36+
- apiGroups:
37+
- ""
38+
resources:
39+
- pods
40+
verbs:
41+
- get
42+
- list
43+
- patch
44+
- update
45+
- watch
46+
# The following privilege is only necessary for creation of headless service
47+
# for drycc-database-config endpoint, in order to prevent cleaning it up by the
48+
# k8s master. You can avoid giving this privilege by explicitly creating the
49+
# service like it is done in this manifest (lines 2..10)
50+
- apiGroups:
51+
- ""
52+
resources:
53+
- services
54+
verbs:
55+
- create
56+
{{- end }}

charts/database/templates/database-secret-creds.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ metadata:
99
annotations:
1010
"helm.sh/hook": pre-install
1111
data:
12-
user: {{ if .Values.user | default "" | ne "" }}{{ .Values.user | b64enc }}{{ else }}{{ randAlphaNum 32 | b64enc }}{{ end }}
12+
superuser: {{ if .Values.superuser | default "" | ne "" }}{{ .Values.superuser | b64enc }}{{ else }}{{ randAlpha 32 | b64enc }}{{ end }}
13+
superuser-password: {{ if .Values.superuserPassword | default "" | ne "" }}{{ .Values.superuserPassword | b64enc }}{{ else }}{{ randAlphaNum 32 | b64enc }}{{ end }}
14+
replicator: {{ if .Values.replicator | default "" | ne "" }}{{ .Values.replicator | b64enc }}{{ else }}{{ randAlpha 32 | b64enc }}{{ end }}
15+
replicator-password: {{ if .Values.replicatorPassword | default "" | ne "" }}{{ .Values.replicatorPassword | b64enc }}{{ else }}{{ randAlphaNum 32 | b64enc }}{{ end }}
16+
user: {{ if .Values.user | default "" | ne "" }}{{ .Values.user | b64enc }}{{ else }}{{ randAlpha 32 | b64enc }}{{ end }}
1317
password: {{ if .Values.password | default "" | ne "" }}{{ .Values.password | b64enc }}{{ else }}{{ randAlphaNum 32 | b64enc }}{{ end }}
1418
{{- end }}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{{- if eq .Values.global.databaseLocation "on-cluster" }}
2+
apiVersion: v1
3+
kind: Service
4+
metadata:
5+
name: drycc-database-replica
6+
annotations:
7+
{{- with .Values.service.annotations }}
8+
{{- toYaml . | nindent 4 }}
9+
{{- end }}
10+
labels:
11+
heritage: drycc
12+
app: drycc-database
13+
role: replica
14+
spec:
15+
type: ClusterIP
16+
ports:
17+
- name: postgres
18+
port: 5432
19+
targetPort: 5432
20+
selector:
21+
app: drycc-database
22+
role: replica
23+
{{- end }}

0 commit comments

Comments
 (0)