Deployment vs StatefulSet » Historie » Zyklus 2
Peter Pfläging, 18.03.2022 16:29
| 1 | 1 | Peter Pfläging | # Deployment-vs-StatefulSet |
|---|---|---|---|
| 2 | |||
| 3 | 2 | Peter Pfläging | Taking PostgreSQL as example, ... |
| 4 | |||
| 5 | What is the main difference: |
||
| 6 | |||
| 7 | - in a StatefulSet you declare the PVC's inside your `spec` as `volumeClaimTemplate` |
||
| 8 | - you don't declare `strategy`. This is a construct for Deployments |
||
| 9 | - the `template` part is equal in both implementations |
||
| 10 | |||
| 11 | Though it's quite easy to convert a Deployment to a StatefulSet in your definition or vice versa. |
||
| 12 | |||
| 13 | But keep in mind: |
||
| 14 | |||
| 15 | - like in the example down below it is not a good idea to scale up a PostgreSQL Deployment. This will cause some confusion, because you have to pods running on the same persistent after this. |
||
| 16 | - on the other hand: if you scale up the StatefulSet, the StatefulSet will create an additional PVC and will mount this on the second pod. This may be better, but not what you want. |
||
| 17 | You now have to independent databases and maybe a SVC pointing on both databases ;-). |
||
| 18 | |||
| 19 | 1 | Peter Pfläging | |
| 20 | ## StatefulSet |
||
| 21 | |||
| 22 | ```yaml |
||
| 23 | kind: StatefulSet |
||
| 24 | apiVersion: apps/v1 |
||
| 25 | metadata: |
||
| 26 | name: postgresql |
||
| 27 | spec: |
||
| 28 | replicas: 1 |
||
| 29 | serviceName: postgresql |
||
| 30 | selector: |
||
| 31 | matchLabels: |
||
| 32 | app: postgresql |
||
| 33 | template: |
||
| 34 | metadata: |
||
| 35 | creationTimestamp: null |
||
| 36 | labels: |
||
| 37 | app: postgresql |
||
| 38 | spec: |
||
| 39 | containers: |
||
| 40 | - name: postgresql |
||
| 41 | image: docker.io/centos/postgresql-12-centos8:latest |
||
| 42 | envFrom: |
||
| 43 | - secretRef: |
||
| 44 | name: database |
||
| 45 | ports: |
||
| 46 | - name: postgresql |
||
| 47 | containerPort: 5432 |
||
| 48 | protocol: TCP |
||
| 49 | resources: |
||
| 50 | requests: |
||
| 51 | cpu: "200m" |
||
| 52 | memory: "128Mi" |
||
| 53 | limits: |
||
| 54 | cpu: "400m" |
||
| 55 | memory: "256Mi" |
||
| 56 | readinessProbe: |
||
| 57 | tcpSocket: |
||
| 58 | port: 5432 |
||
| 59 | timeoutSeconds: 1 |
||
| 60 | periodSeconds: 10 |
||
| 61 | successThreshold: 1 |
||
| 62 | failureThreshold: 3 |
||
| 63 | livenessProbe: |
||
| 64 | tcpSocket: |
||
| 65 | port: 5432 |
||
| 66 | timeoutSeconds: 1 |
||
| 67 | periodSeconds: 10 |
||
| 68 | successThreshold: 1 |
||
| 69 | failureThreshold: 3 |
||
| 70 | volumeMounts: |
||
| 71 | - name: postgresql |
||
| 72 | mountPath: /var/lib/pgsql/data |
||
| 73 | terminationMessagePath: /dev/termination-log |
||
| 74 | terminationMessagePolicy: File |
||
| 75 | imagePullPolicy: Always |
||
| 76 | restartPolicy: Always |
||
| 77 | terminationGracePeriodSeconds: 10 |
||
| 78 | dnsPolicy: ClusterFirst |
||
| 79 | securityContext: {} |
||
| 80 | schedulerName: default-scheduler |
||
| 81 | volumeClaimTemplates: |
||
| 82 | - metadata: |
||
| 83 | name: postgresql |
||
| 84 | labels: |
||
| 85 | app: postgresql |
||
| 86 | spec: |
||
| 87 | accessModes: |
||
| 88 | - ReadWriteOnce |
||
| 89 | storageClassName: changed-by-upper-kustomize |
||
| 90 | volumeMode: Filesystem |
||
| 91 | resources: |
||
| 92 | requests: |
||
| 93 | storage: 500Mi |
||
| 94 | ``` |
||
| 95 | |||
| 96 | ## Deployment |
||
| 97 | |||
| 98 | Here we have a split. We have to explicit declare the PVC: |
||
| 99 | |||
| 100 | ```yaml |
||
| 101 | --- |
||
| 102 | kind: Deployment |
||
| 103 | apiVersion: apps/v1 |
||
| 104 | metadata: |
||
| 105 | name: postgresql |
||
| 106 | spec: |
||
| 107 | replicas: 1 |
||
| 108 | selector: |
||
| 109 | matchLabels: |
||
| 110 | app: postgresql |
||
| 111 | template: |
||
| 112 | metadata: |
||
| 113 | labels: |
||
| 114 | app: postgresql |
||
| 115 | spec: |
||
| 116 | containers: |
||
| 117 | - name: postgresql |
||
| 118 | image: docker.io/centos/postgresql-12-centos8:latest |
||
| 119 | envFrom: |
||
| 120 | - secretRef: |
||
| 121 | name: database |
||
| 122 | resources: |
||
| 123 | requests: |
||
| 124 | cpu: "200m" |
||
| 125 | memory: "128Mi" |
||
| 126 | limits: |
||
| 127 | cpu: "400m" |
||
| 128 | memory: "256Mi" |
||
| 129 | readinessProbe: |
||
| 130 | tcpSocket: |
||
| 131 | port: 5432 |
||
| 132 | timeoutSeconds: 1 |
||
| 133 | periodSeconds: 10 |
||
| 134 | successThreshold: 1 |
||
| 135 | failureThreshold: 3 |
||
| 136 | livenessProbe: |
||
| 137 | tcpSocket: |
||
| 138 | port: 5432 |
||
| 139 | timeoutSeconds: 1 |
||
| 140 | periodSeconds: 10 |
||
| 141 | successThreshold: 1 |
||
| 142 | failureThreshold: 3 |
||
| 143 | volumeMounts: |
||
| 144 | - name: postgresql |
||
| 145 | mountPath: /var/lib/pgsql/data |
||
| 146 | terminationMessagePath: /dev/termination-log |
||
| 147 | terminationMessagePolicy: File |
||
| 148 | imagePullPolicy: Always |
||
| 149 | restartPolicy: Always |
||
| 150 | terminationGracePeriodSeconds: 10 |
||
| 151 | dnsPolicy: ClusterFirst |
||
| 152 | securityContext: {} |
||
| 153 | schedulerName: default-scheduler |
||
| 154 | volumes: |
||
| 155 | - name: postgresql |
||
| 156 | persistentVolumeClaim: |
||
| 157 | claimName: postgresql |
||
| 158 | strategy: |
||
| 159 | type: RollingUpdate |
||
| 160 | rollingUpdate: |
||
| 161 | maxUnavailable: 25% |
||
| 162 | maxSurge: 25% |
||
| 163 | revisionHistoryLimit: 10 |
||
| 164 | progressDeadlineSeconds: 600 |
||
| 165 | --- |
||
| 166 | kind: PersistentVolumeClaim |
||
| 167 | apiVersion: v1 |
||
| 168 | metadata: |
||
| 169 | name: postgresql |
||
| 170 | spec: |
||
| 171 | accessModes: |
||
| 172 | - ReadWriteOnce |
||
| 173 | resources: |
||
| 174 | requests: |
||
| 175 | storage: 2Gi |
||
| 176 | storageClassName: changed-by-upper-kustomize |
||
| 177 | volumeMode: Filesystem |
||
| 178 | ``` |