k8s中使用cloudnativepg部署postgresql集群,主从故障自动切换
部署sc,我这里部署的local-path-provisioner
将sc设置为默认
kubectl patch storageclass local-path -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'部署cloudnativepg
部署postgresql
在k8s集群中用来部署postgresql的节点打上污点2台或以上,如果不指定哪个节点单独部署postgresql,直接执行下一步
kubectl taint node nodename node-role.kubernetes.io/postgres=run:NoSchedule在k8s集群中用来部署postgresql的节点打上标签2台或以上
kubectl label node nodename node-role.kubernetes.io/postgres=run部署postgresql,instances: 2表示部署一主一从,根据自己的环境更改,storage、shared_buffers、max_connections 根据自己的环境更改
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: cluster-postgres
spec:
instances: 2
postgresql:
parameters:
shared_buffers: "256MB"
max_connections: "4000"
storage:
size: 1000Gi
imageName: ghcr.io/cloudnative-pg/postgresql:16.13
imagePullPolicy: IfNotPresent
affinity:
enablePodAntiAffinity: true
topologyKey: kubernetes.io/hostname
podAntiAffinityType: required
nodeSelector:
node-role.kubernetes.io/postgres: run
tolerations:
- key: node-role.kubernetes.io/postgres
operator: Equal
value: run
effect: NoSchedule