Skip to content

Commit 0b11792

Browse files
committed
feat(postgres): Add Component for HA multi-cluster replication
ref: #18
1 parent fcb9687 commit 0b11792

File tree

5 files changed

+289
-0
lines changed

5 files changed

+289
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
apiVersion: postgresql.cnpg.io/v1
3+
kind: Cluster
4+
metadata:
5+
labels:
6+
app.kubernetes.io/component: ha
7+
app.kubernetes.io/instance: production
8+
app.kubernetes.io/name: postgres
9+
app.kubernetes.io/part-of: production-ha
10+
name: main-02
11+
spec:
12+
affinity:
13+
additionalPodAntiAffinity:
14+
preferredDuringSchedulingIgnoredDuringExecution:
15+
- weight: 50
16+
podAffinityTerm:
17+
labelSelector:
18+
matchExpressions:
19+
- key: kubernetes.io/hostname
20+
operator: Exists
21+
topologyKey: kubernetes.io/hostname
22+
23+
enablePodAntiAffinity: true
24+
nodeAffinity:
25+
preferredDuringSchedulingIgnoredDuringExecution:
26+
- weight: 100
27+
preference:
28+
matchExpressions:
29+
- key: node-role.kubernetes.io/prime
30+
operator: Exists
31+
topologyKey: kubernetes.io/hostname
32+
33+
bootstrap:
34+
pg_basebackup:
35+
source: main-01
36+
37+
description: "Cluster PostgreSQL Instance"
38+
39+
enableSuperuserAccess: false
40+
41+
externalClusters: []
42+
43+
imageName: ghcr.io/cloudnative-pg/postgresql:-updated_by_patch-
44+
45+
inheritedMetadata:
46+
labels:
47+
app.kubernetes.io/component: ha
48+
app.kubernetes.io/instance: production
49+
app.kubernetes.io/name: postgres
50+
app.kubernetes.io/part-of: production-ha
51+
52+
instances: 3
53+
54+
monitoring: {}
55+
56+
nodeMaintenanceWindow:
57+
inProgress: false
58+
reusePVC: false
59+
60+
primaryUpdateStrategy: supervised
61+
62+
postgresql:
63+
parameters:
64+
max_parallel_workers: '34'
65+
max_replication_slots: '32'
66+
max_worker_processes: '34'
67+
68+
replica:
69+
primary: main-01
70+
source: main-01
71+
72+
resources:
73+
requests:
74+
cpu: "100m"
75+
memory: "100Mi"
76+
limits:
77+
cpu: "2"
78+
memory: "1Gi"
79+
80+
startDelay: 300
81+
82+
stopDelay: 300
83+
84+
storage:
85+
resizeInUseVolumes: false
86+
size: 10Gi
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
apiVersion: v1
3+
kind: Service
4+
metadata:
5+
labels:
6+
app.kubernetes.io/component: ha
7+
app.kubernetes.io/instance: production
8+
app.kubernetes.io/name: postgres
9+
app.kubernetes.io/part-of: production-ha
10+
name: main-r
11+
spec:
12+
ports:
13+
- name: postgres
14+
port: 5432
15+
protocol: TCP
16+
targetPort: 5432
17+
selector:
18+
cnpg.io/cluster: -updated_by_replacement-
19+
cnpg.io/podRole: instance
20+
sessionAffinity: None
21+
type: ClusterIP
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
apiVersion: v1
3+
kind: Service
4+
metadata:
5+
labels:
6+
app.kubernetes.io/component: ha
7+
app.kubernetes.io/instance: production
8+
app.kubernetes.io/name: postgres
9+
app.kubernetes.io/part-of: production-ha
10+
name: main-ro
11+
spec:
12+
ports:
13+
- name: postgres
14+
port: 5432
15+
protocol: TCP
16+
targetPort: 5432
17+
selector:
18+
cnpg.io/cluster: -updated_by_replacement-
19+
cnpg.io/instanceRole: replica
20+
sessionAffinity: None
21+
type: ClusterIP
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
apiVersion: v1
3+
kind: Service
4+
metadata:
5+
labels:
6+
app.kubernetes.io/component: ha
7+
app.kubernetes.io/instance: production
8+
app.kubernetes.io/name: postgres
9+
app.kubernetes.io/part-of: production-ha
10+
name: main-rw
11+
spec:
12+
ports:
13+
- name: postgres
14+
port: 5432
15+
protocol: TCP
16+
targetPort: 5432
17+
selector:
18+
cnpg.io/cluster: -updated_by_replacement-
19+
cnpg.io/instanceRole: primary
20+
sessionAffinity: None
21+
type: ClusterIP
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
---
2+
3+
apiVersion: kustomize.config.k8s.io/v1alpha1
4+
kind: Component
5+
6+
7+
resources:
8+
# - ../../base
9+
- Cluster-main-02.yaml
10+
- Service-main-r.yaml
11+
- Service-main-ro.yaml
12+
- Service-main-rw.yaml
13+
14+
15+
patches:
16+
17+
# yamllint disable rule:indentation
18+
- patch: |-
19+
20+
- op: add
21+
path: /metadata/name
22+
value: main-01
23+
24+
- op: replace
25+
path: /spec/replica
26+
value:
27+
primary: main-01
28+
source: main-02
29+
30+
# yamllint enable rule:indentation
31+
target:
32+
group: postgresql.cnpg.io
33+
version: v1
34+
kind: Cluster
35+
name: main
36+
37+
# yamllint disable rule:indentation
38+
- patch: |-
39+
40+
- op: replace
41+
path: /spec/externalClusters
42+
value:
43+
44+
- name: main-01
45+
connectionParameters:
46+
host: main-01-rw.updated_by_replacement.svc
47+
user: streaming_replica
48+
dbname: postgres
49+
sslmode: verify-full
50+
sslKey:
51+
name: main-01-replication
52+
key: tls.key
53+
sslCert:
54+
name: main-01-replication
55+
key: tls.crt
56+
sslRootCert:
57+
name: main-01-ca
58+
key: ca.crt
59+
60+
- name: main-02
61+
connectionParameters:
62+
host: main-02-rw.updated_by_replacement.svc
63+
user: streaming_replica
64+
dbname: postgres
65+
sslmode: verify-full
66+
sslKey:
67+
name: main-02-replication
68+
key: tls.key
69+
sslCert:
70+
name: main-02-replication
71+
key: tls.crt
72+
sslRootCert:
73+
name: main-02-ca
74+
key: ca.crt
75+
76+
# yamllint enable rule:indentation
77+
target:
78+
group: postgresql.cnpg.io
79+
version: v1
80+
kind: Cluster
81+
82+
83+
84+
replacements:
85+
#
86+
# Add to Top-most Kustomization.yaml
87+
#
88+
# Updates extenal cluster DNS name octet that contains the namespace
89+
# - source:
90+
# kind: Cluster
91+
# name: main-01
92+
# fieldPath: metadata.namespace
93+
# targets:
94+
# - select:
95+
# kind: Cluster
96+
# fieldPaths:
97+
# - spec.externalClusters.[name=main-01].connectionParameters.host
98+
# - spec.externalClusters.[name=main-02].connectionParameters.host
99+
# options:
100+
# delimiter: "."
101+
# index: 1
102+
103+
104+
#
105+
# Add to Top-most Kustomization.yaml, if primary has be changed
106+
#
107+
# Updates each of the services to point the cluster that is denoted as primary
108+
# within cluster main-02
109+
- source:
110+
kind: Cluster
111+
name: main-02
112+
fieldPath: spec.replica.primary
113+
targets:
114+
- select:
115+
kind: Service
116+
name: main-r
117+
fieldPaths:
118+
- spec.selector.[cnpg.io/cluster]
119+
- select:
120+
kind: Service
121+
name: main-ro
122+
fieldPaths:
123+
- spec.selector.[cnpg.io/cluster]
124+
- select:
125+
kind: Service
126+
name: main-rw
127+
fieldPaths:
128+
- spec.selector.[cnpg.io/cluster]
129+
130+
# Dont change
131+
- source:
132+
kind: Cluster
133+
name: main-02
134+
fieldPath: metadata.labels.[app.kubernetes.io/part-of]
135+
targets:
136+
- select:
137+
kind: Cluster
138+
fieldPaths:
139+
- metadata.labels.[app.kubernetes.io/part-of]
140+
- spec.inheritedMetadata.labels.[app.kubernetes.io/part-of]

0 commit comments

Comments
 (0)