mirror of
https://github.com/outbackdingo/patroni.git
synced 2026-08-25 14:53:37 +00:00
When running on K8s Patroni is communicating with API via the `kubernetes` service, which is address is exposed via the `KUBERNETES_SERVICE_HOST` environment variable. Like any other service, the `kubernetes` service is handled by `kube-proxy`, that depending on configuration is either relying on userspace program or `iptables` for traffic routing. During K8s upgrade, when master nodes are replaced, it is possible that `kube-proxy` doesn't update the service configuration in time and as a result Patroni fails to update the leader lock and demotes postgres. In order to improve the user experience and get more control on the problem we make it possible to bypass the `kubernetes` service and connect directly to API nodes. The strategy is very simple: 1. Resolve list IPs of API nodes from the kubernetes endpoint on every iteration of HA loop. 2. Stick to one of these IPs for API requests 3. Switch to a different IP if connected to IP is not from the list 4. If the request fails, switch to another IP and retry Such a strategy is already used for Etcd and proven to work quite well. In order to enable the feature, you need either to set to `true` `kubernetes.bypass_api_service` in the Patroni configuration file or `PATRONI_KUBERNETES_BYPASS_API_SERVICE` environment variable. If for some reason `GET /default/endpoints/kubernetes` isn't allowed Patroni will disable the feature.
278 lines
6.0 KiB
YAML
278 lines
6.0 KiB
YAML
# headless service to avoid deletion of patronidemo-config endpoint
|
|
apiVersion: v1
|
|
kind: Service
|
|
metadata:
|
|
name: patronidemo-config
|
|
labels:
|
|
application: patroni
|
|
cluster-name: patronidemo
|
|
spec:
|
|
clusterIP: None
|
|
|
|
---
|
|
apiVersion: apps/v1
|
|
kind: StatefulSet
|
|
metadata:
|
|
name: &cluster_name patronidemo
|
|
labels:
|
|
application: patroni
|
|
cluster-name: *cluster_name
|
|
spec:
|
|
replicas: 3
|
|
serviceName: *cluster_name
|
|
template:
|
|
metadata:
|
|
labels:
|
|
application: patroni
|
|
cluster-name: *cluster_name
|
|
spec:
|
|
serviceAccountName: patronidemo
|
|
containers:
|
|
- name: *cluster_name
|
|
image: patroni # docker build -t patroni .
|
|
imagePullPolicy: IfNotPresent
|
|
readinessProbe:
|
|
httpGet:
|
|
scheme: HTTP
|
|
path: /readiness
|
|
port: 8008
|
|
initialDelaySeconds: 3
|
|
periodSeconds: 10
|
|
timeoutSeconds: 5
|
|
successThreshold: 1
|
|
failureThreshold: 3
|
|
ports:
|
|
- containerPort: 8008
|
|
protocol: TCP
|
|
- containerPort: 5432
|
|
protocol: TCP
|
|
volumeMounts:
|
|
- mountPath: /home/postgres/pgdata
|
|
name: pgdata
|
|
env:
|
|
- name: PATRONI_KUBERNETES_POD_IP
|
|
valueFrom:
|
|
fieldRef:
|
|
fieldPath: status.podIP
|
|
- name: PATRONI_KUBERNETES_NAMESPACE
|
|
valueFrom:
|
|
fieldRef:
|
|
fieldPath: metadata.namespace
|
|
- name: PATRONI_KUBERNETES_BYPASS_API_SERVICE
|
|
value: 'true'
|
|
- name: PATRONI_KUBERNETES_USE_ENDPOINTS
|
|
value: 'true'
|
|
- name: PATRONI_KUBERNETES_LABELS
|
|
value: '{application: patroni, cluster-name: patronidemo}'
|
|
- name: PATRONI_SUPERUSER_USERNAME
|
|
value: postgres
|
|
- name: PATRONI_SUPERUSER_PASSWORD
|
|
valueFrom:
|
|
secretKeyRef:
|
|
name: *cluster_name
|
|
key: superuser-password
|
|
- name: PATRONI_REPLICATION_USERNAME
|
|
value: standby
|
|
- name: PATRONI_REPLICATION_PASSWORD
|
|
valueFrom:
|
|
secretKeyRef:
|
|
name: *cluster_name
|
|
key: replication-password
|
|
- name: PATRONI_SCOPE
|
|
value: *cluster_name
|
|
- name: PATRONI_NAME
|
|
valueFrom:
|
|
fieldRef:
|
|
fieldPath: metadata.name
|
|
- name: PATRONI_POSTGRESQL_DATA_DIR
|
|
value: /home/postgres/pgdata/pgroot/data
|
|
- name: PATRONI_POSTGRESQL_PGPASS
|
|
value: /tmp/pgpass
|
|
- name: PATRONI_POSTGRESQL_LISTEN
|
|
value: '0.0.0.0:5432'
|
|
- name: PATRONI_RESTAPI_LISTEN
|
|
value: '0.0.0.0:8008'
|
|
terminationGracePeriodSeconds: 0
|
|
volumes:
|
|
- name: pgdata
|
|
emptyDir: {}
|
|
# volumeClaimTemplates:
|
|
# - metadata:
|
|
# labels:
|
|
# application: spilo
|
|
# spilo-cluster: *cluster_name
|
|
# annotations:
|
|
# volume.alpha.kubernetes.io/storage-class: anything
|
|
# name: pgdata
|
|
# spec:
|
|
# accessModes:
|
|
# - ReadWriteOnce
|
|
# resources:
|
|
# requests:
|
|
# storage: 5Gi
|
|
|
|
---
|
|
apiVersion: v1
|
|
kind: Endpoints
|
|
metadata:
|
|
name: &cluster_name patronidemo
|
|
labels:
|
|
application: patroni
|
|
cluster-name: *cluster_name
|
|
subsets: []
|
|
|
|
---
|
|
apiVersion: v1
|
|
kind: Service
|
|
metadata:
|
|
name: &cluster_name patronidemo
|
|
labels:
|
|
application: patroni
|
|
cluster-name: *cluster_name
|
|
spec:
|
|
type: ClusterIP
|
|
ports:
|
|
- port: 5432
|
|
targetPort: 5432
|
|
|
|
---
|
|
apiVersion: v1
|
|
kind: Service
|
|
metadata:
|
|
name: patronidemo-repl
|
|
labels:
|
|
application: patroni
|
|
cluster-name: &cluster_name patronidemo
|
|
role: replica
|
|
spec:
|
|
type: ClusterIP
|
|
selector:
|
|
application: patroni
|
|
cluster-name: *cluster_name
|
|
role: replica
|
|
ports:
|
|
- port: 5432
|
|
targetPort: 5432
|
|
|
|
---
|
|
apiVersion: v1
|
|
kind: Secret
|
|
metadata:
|
|
name: &cluster_name patronidemo
|
|
labels:
|
|
application: patroni
|
|
cluster-name: *cluster_name
|
|
type: Opaque
|
|
data:
|
|
superuser-password: emFsYW5kbw==
|
|
replication-password: cmVwLXBhc3M=
|
|
|
|
---
|
|
apiVersion: v1
|
|
kind: ServiceAccount
|
|
metadata:
|
|
name: patronidemo
|
|
|
|
---
|
|
apiVersion: rbac.authorization.k8s.io/v1
|
|
kind: Role
|
|
metadata:
|
|
name: patronidemo
|
|
rules:
|
|
- apiGroups:
|
|
- ""
|
|
resources:
|
|
- configmaps
|
|
verbs:
|
|
- create
|
|
- get
|
|
- list
|
|
- patch
|
|
- update
|
|
- watch
|
|
# delete and deletecollection are required only for 'patronictl remove'
|
|
- delete
|
|
- deletecollection
|
|
- apiGroups:
|
|
- ""
|
|
resources:
|
|
- endpoints
|
|
verbs:
|
|
- get
|
|
- patch
|
|
- update
|
|
# the following three privileges are necessary only when using endpoints
|
|
- create
|
|
- list
|
|
- watch
|
|
# delete and deletecollection are required only for for 'patronictl remove'
|
|
- delete
|
|
- deletecollection
|
|
- apiGroups:
|
|
- ""
|
|
resources:
|
|
- pods
|
|
verbs:
|
|
- get
|
|
- list
|
|
- patch
|
|
- update
|
|
- watch
|
|
# The following privilege is only necessary for creation of headless service
|
|
# for patronidemo-config endpoint, in order to prevent cleaning it up by the
|
|
# k8s master. You can avoid giving this privilege by explicitly creating the
|
|
# service like it is done in this manifest (lines 2..10)
|
|
- apiGroups:
|
|
- ""
|
|
resources:
|
|
- services
|
|
verbs:
|
|
- create
|
|
|
|
---
|
|
apiVersion: rbac.authorization.k8s.io/v1
|
|
kind: RoleBinding
|
|
metadata:
|
|
name: patronidemo
|
|
roleRef:
|
|
apiGroup: rbac.authorization.k8s.io
|
|
kind: Role
|
|
name: patronidemo
|
|
subjects:
|
|
- kind: ServiceAccount
|
|
name: patronidemo
|
|
|
|
# Following privileges are only required if deployed not in the "default"
|
|
# namespace and you want Patroni to bypass kubernetes service
|
|
# (PATRONI_KUBERNETES_BYPASS_API_SERVICE=true)
|
|
---
|
|
apiVersion: rbac.authorization.k8s.io/v1
|
|
kind: ClusterRole
|
|
metadata:
|
|
name: patroni-k8s-ep-access
|
|
rules:
|
|
- apiGroups:
|
|
- ""
|
|
resources:
|
|
- endpoints
|
|
resourceNames:
|
|
- kubernetes
|
|
verbs:
|
|
- get
|
|
|
|
---
|
|
apiVersion: rbac.authorization.k8s.io/v1
|
|
kind: ClusterRoleBinding
|
|
metadata:
|
|
name: patroni-k8s-ep-access
|
|
roleRef:
|
|
apiGroup: rbac.authorization.k8s.io
|
|
kind: ClusterRole
|
|
name: patroni-k8s-ep-access
|
|
subjects:
|
|
- kind: ServiceAccount
|
|
name: patronidemo
|
|
# The namespace must be specified explicitly.
|
|
# If deploying to the different namespace you have to change it.
|
|
namespace: default
|