-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathstatefulset.yaml
346 lines (342 loc) · 14.3 KB
/
statefulset.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#
# Author: Hari Sekhon
# Date: [% DATE # 2019-11-28 18:51:59 +0000 (Thu, 28 Nov 2019) %]
#
# vim:ts=2:sts=2:sw=2:et
# lint: k8s
#
# https://github.com/HariSekhon/Kubernetes-configs
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn
# and optionally send me feedback to help improve or steer this or other code I publish
#
# https://www.linkedin.com/in/HariSekhon
#
# ============================================================================ #
# S t a t e f u l S e t
# ============================================================================ #
# https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/
#
# https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/stateful-set-v1/
#
# see pod anti-affinity here:
#
# https://kubernetes.io/docs/tutorials/stateful-application/zookeeper/
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: NAME
namespace: NAMESPACE
annotations:
# see also k8s_pod_disruption_budget.yaml
# XXX: set to true if this can be evicted, otherwise this can prevent cluster autoscaler from scaling down the nodepool
cluster-autoscaler.kubernetes.io/safe-to-evict: "false"
# adds ArgoCD link symbol in the UI next to object
#link.argocd.argoproj.io/external-link: http://www.domain.com/path
spec:
replicas: 3 # XXX: don't define replicas if using HPA, otherwise gets overridden and reset to this number every deploy which could cause load or data sharding issues
serviceName: NAME
selector:
matchLabels:
app: APP
template:
metadata:
labels:
app: APP
spec:
# for business critical workloads to get priority access to the stable node pool instead of preemptible node pool, will evict lower priority pods to preemptible node pool (default 0) if necessary. Requires: priorityclass.yaml
#
# priorityClassName: high-priority
#
#nodeSelector:
#myLabel: myFastNodes
#disktype: ssd
#kubernetes.io/arch: amd64
#kubernetes.io/os: linux
#node.kubernetes.io/instance-type: m3.medium # 1.17+
## for persistent disks limited to 2 zones combined with GKE regional clusters
#topology.kubernetes.io/zone: europe-west2-a # 1.17+
#topology.kubernetes.io/region: europe-west2 # 1.17+
#
# GKE specific - hard requirement - prefer affinity below is safer soft requirement to still allow scheduling if node pool becomes unavailable (eg. recreated by Terraform)
#
# cloud.google.com/gke-nodepool: POOL_NAME
#
# GKE Autopilot
#
# https://cloud.google.com/kubernetes-engine/docs/concepts/autopilot-compute-classes#when-to-use
#
# cloud.google.com/compute-class: Balanced
#
#
# https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#affinity-and-anti-affinity
# XXX: not recommended in clusters greater than several hundred nodes due to significant CPU overhead slowing down scheduling
affinity:
nodeAffinity:
# Node preemption:
# - shuts down pods ungracefully
# - ignores PodDisruptionBudget
# - invalidate Stateful guarantees and can lead to data loss !!
# https://cloud.google.com/kubernetes-engine/docs/how-to/preemptible-vms#kubernetes_constraint_violations
# https://cloud.google.com/kubernetes-engine/docs/how-to/preemptible-vms#best_practices
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
# GCP: https://cloud.google.com/kubernetes-engine/docs/how-to/preemptible-vms#best_practices
#
# gke-preemptible not valid on GKE AutoPilot clusters - remove in that case and leave only gke-spot
- key: cloud.google.com/gke-preemptible
operator: DoesNotExist
- key: cloud.google.com/gke-spot
operator: DoesNotExist
#
# AWS: https://docs.aws.amazon.com/eks/latest/userguide/managed-node-groups.html
- key: eks.amazonaws.com/capacityType
operator: NotIn
values:
- SPOT
#
# Azure: https://docs.microsoft.com/en-us/azure/aks/spot-node-pool
- key: kubernetes.azure.com/scalesetpriority
operator: NotIn
values:
- spot
#podAffinity:
podAntiAffinity:
# XXX: spread app across AZs
# XXX: use preferred if num_replicas > num_AZs
# preferredDuringSchedulingIgnoredDuringExecution:
# - weight: 100
# podAffinityTerm:
# topologyKey: topology.kubernetes.io/zone
# labelSelector:
# matchExpressions:
# - key: app
# operator: In
# values:
# - APP
requiredDuringSchedulingIgnoredDuringExecution:
# XXX: spread app across AZs
- topologyKey: topology.kubernetes.io/zone
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- APP
# XXX: spread app across hosts (default prefers spread, whereas this requires it)
- topologyKey: kubernetes.io/hostname
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- APP
#serviceAccountName: NAME
securityContext:
# see pod-security-policy.yaml for wider enforcement
runAsNonRoot: true
#runAsUser: 0 # not recommended
fsGroup: 1000 # group to own volumeMounts so they can be written to by the user-level process
# XXX: don't chgrp all the many small files unless the root mismatches, prevents long outages on pod creation hanging with multiple of these error messages:
#
# Unable to attach or mount volumes: unmounted volumes=[jenkins-home], unattached volumes=[kube-api-access-6q2kb jenkins-home]: timed out waiting for the condition
#
# https://docs.cloudbees.com/docs/cloudbees-ci-kb/latest/cloudbees-ci-on-modern-cloud-platforms/timeout-when-attaching-volumes-in-kubernetes
#
fsGroupChangePolicy: OnRootMismatch
containers:
- name: NAME
image: postgres:9.6
ports:
- name: postgres
containerPort: 5432
protocol: TCP
securityContext:
runAsNonRoot: true
privileged: false
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
env:
- name: HEAP_SIZE
value: 1G
- name: CASSANDRA_SEEDS
value: cassandra-0.cassandra.svc.cluster.local,cassandra-1.cassandra.svc.cluster.local,cassandra-2.cassandra.svc.cluster.local
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP # K8S API path
- name: CLOUD_SQL_INSTANCE
valueFrom:
configMapKeyRef:
key: cloud-sql-instance
name: my-configmap
- name: MYSECRET
valueFrom:
secretKeyRef:
name: my-secret
key: my-secret
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: "1"
memory: 2Gi
#
# Probe fields:
#
# https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/#configure-probes
#
# newer versions of Kubernetes
# use this for slow starting applications without compromising the responsiveness of readiness or liveness probes which do not start until after this
#startupProbe:
# # http, tcp or exec
# #httpGet:
# # path: /healthz
# # port: 8080 # or 'http' to use name from ports section
# # scheme: HTTPS
# # #httpHeaders:
# # # - name: Custom-Header
# # # value: Awesome
# # only uncomment if you can't use http check, must comment/delete http check otherwise will get this error:
# # invalid: spec.containers[0].livenessProbe.tcpSocket: Forbidden: may not specify more than 1 handler type
# tcpSocket:
# port: 5432
# #grpc: # Kubernetes 1.27+
# # port: 2379 # must be number, not port name or fqdn
# # #service: readiness # to use same grpc port to distinguish between checks
# initialDelaySeconds: 0 # default
# initialDelaySeconds: 0 # default
# successThreshold: 1 # default
# failureThreshold: 3 # default
# periodSeconds: 10 # default (interval)
# timeoutSeconds: 1 # default
readinessProbe:
# http, tcp or exec
#httpGet:
# path: /healthz
# port: 8080 # or 'http' to use name from ports section
# scheme: HTTPS
# #httpHeaders:
# # - name: Custom-Header
# # value: Awesome
# only uncomment if you can't use http check, must comment/delete http check otherwise will get this error:
# invalid: spec.containers[0].livenessProbe.tcpSocket: Forbidden: may not specify more than 1 handler type
tcpSocket:
port: 5432
#grpc: # Kubernetes 1.27+
# port: 2379 # must be number, not port name or fqdn
# #service: readiness # to use same grpc port to distinguish between checks
initialDelaySeconds: 0 # default
successThreshold: 1 # default
failureThreshold: 3 # default
periodSeconds: 10 # default (interval)
timeoutSeconds: 1 # default
livenessProbe:
#httpGet:
# path: /healthz
# port: 8080 # or 'http' to use name from ports section
# #scheme: HTTPS
# #httpHeaders:
# # - name: Custom-Header
# # value: Awesome
tcpSocket:
port: 5432
#grpc: # Kubernetes 1.27+
# port: 2379 # must be number, not port name or fqdn
# #service: readiness # to use same grpc port to distinguish between checks
initialDelaySeconds: 0 # default
successThreshold: 1 # default
failureThreshold: 3 # default
periodSeconds: 10 # default (interval)
timeoutSeconds: 1 # default
volumeMounts:
- name: pgdata
mountPath: /var/lib/pgsql
- name: env
mountPath: /app/.env
subPath: .env
#lifecycle:
# postStart:
# exec:
# command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]
# preStop:
# exec:
# # recent nginx-ingress already has this, but still a decent example to gracefully finish connections on your own nginx via SIGQUIT not SIGTERM
# # also remember to increase terminationGracePeriodSeconds
# command: ["/bin/sh","-c","nginx -s quit; while killall -0 nginx; do sleep 1; done"]
#
# GCP Cloud SQL Proxy sidecar - app can then connect to DB at localhost
#
# https://cloud.google.com/sql/docs/postgres/connect-kubernetes-engine
#
# Set up Workload Identity integration between k8s SA + GCP SA
#
# https://cloud.google.com/sql/docs/postgres/connect-kubernetes-engine#workload-identity
#
- name: cloud-sql-proxy
image: gcr.io/cloudsql-docker/gce-proxy:1.17
command:
- /cloud_sql_proxy
- "-ip_address_types=PRIVATE"
- "-instances=$(CLOUD_SQL_INSTANCE)=tcp:5432" # populate CLOUD_SQL_INSTANCE env var via configmap, must also specify a port (eg. 5432 for PostgreSQL or 3306 for MySQL, or else a -dir option if using socket dir)
securityContext:
# The default Cloud SQL proxy image runs as the
# "nonroot" user and group (uid: 65532) by default.
runAsNonRoot: true
privileged: false
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
resources:
requests:
cpu: 100m
memory: 128mi
limits:
cpu: "1"
memory: 2gi
readinessProbe:
tcpSocket:
#port: 3306 # mysql
port: 5432 # postgres
periodSeconds: 5
livenessProbe:
tcpSocket:
#port: 3306 # mysql
port: 5432 # postgres
failureThreshold: 10
volumes:
- name: env
configMap:
name: myconfigmap
defaultMode: 0400
items:
- key: .env
path: .env
volumeClaimTemplates:
- metadata:
name: pgdata
spec:
# XXX: use a storageclass-*.yaml to:
# - set a "reclaimPolicy: Retain" for safety to avoid data loss if deleting/recreating the namespace with the pvc
# (it still requires a manual recovery for this easy mistake to make but at least the data isn't deleted)
# - choose disk type (SSD vs spinning disk)
# - make disk dynamically resizeable (eg. on GCP)
#storageClassName: ssd
#storageClassName: ssd-resizeable
#storageClassName: standard-resizeable
accessModes:
- ReadWriteOnce
resources:
requests:
# resize here doesn't affect the PVC, must edit the PVC directly to trigger the provider resize, didn't even need a pod cycle in GCP testing
storage: 10Gi