Add liveness and readiness probes (and use initContainer)

This commit is contained in:
Santiago Lo Coco 2023-11-17 22:15:17 -03:00
parent 492e06f2af
commit 5632470e74
5 changed files with 127 additions and 67 deletions

View File

@ -18,12 +18,11 @@ services:
---------- ----------
En `ingress` se repite el `host` por dos razones: En `ingress` se repite el `host` debido a facilita que la api pueda estar en otro dominio, si así se desea:
1) Si quisieran que la api esté en otro dominio se pueda hacer fácilmente mediante:
```yaml ```yaml
ingress: ingress:
ssl: true
annotations: annotations:
nginx.ingress.kubernetes.io/ssl-redirect: "true" nginx.ingress.kubernetes.io/ssl-redirect: "true"
hosts: hosts:
@ -44,6 +43,4 @@ ingress:
- kube.slc.ar - kube.slc.ar
- api.kube.slc.ar - api.kube.slc.ar
``` ```
2) No se pueden reutilizar variables en YAML. Existe la posibilidad de usar YAML anchors pero en la [documentación](https://helm.sh/docs/chart_template_guide/yaml_techniques/#yaml-anchors) no lo recomiendan: "Because Helm and Kubernetes often read, modify, and then rewrite YAML files, the anchors will be lost."
TODO: probar igual los anchors

View File

@ -65,7 +65,7 @@ Create the name of the service account to use
Return true if a TLS secret should be created Return true if a TLS secret should be created
*/}} */}}
{{- define "exam.createTlsSecret" -}} {{- define "exam.createTlsSecret" -}}
{{- if and .Values.tls.enabled (not .Values.tls.certificatesSecret) -}} {{- if .Values.ingress.ssl -}}
{{- true -}} {{- true -}}
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}
@ -97,5 +97,19 @@ Search already generated TLS secret
Get host Get host
*/}} */}}
{{- define "exam.host" -}} {{- define "exam.host" -}}
{{- default "kube-exam.local" .Values.tls.host -}} {{- default "kube-exam.local" .Values.shared.host -}}
{{- end -}}
{{/*
Get secrets name
*/}}
{{- define "exam.secrets" -}}
{{ include "exam.fullname" . }}-secrets
{{- end -}}
{{/*
Get pv-claim name
*/}}
{{- define "exam.pvclaim" -}}
{{ include "exam.fullname" . }}-pv-claim
{{- end -}} {{- end -}}

View File

@ -21,6 +21,16 @@ spec:
tier: {{ .tier }} tier: {{ .tier }}
{{- include "exam.selectorLabels" $ | nindent 8 }} {{- include "exam.selectorLabels" $ | nindent 8 }}
spec: spec:
{{- if .initContainer }}
initContainers:
- name: {{ .name }}-init
{{- with .image }}
image: {{ .repository }}:{{ .tag | default $.Chart.AppVersion }}
{{- end }}
{{- with .initContainer }}
{{- toYaml . | nindent 10 }}
{{- end }}
{{- end }}
containers: containers:
- name: {{ .name }} - name: {{ .name }}
{{- with .image }} {{- with .image }}
@ -43,8 +53,7 @@ spec:
- name: {{ $envKey }} - name: {{ $envKey }}
valueFrom: valueFrom:
secretKeyRef: secretKeyRef:
# TODO name: {{ include "exam.secrets" $ }}
name: {{ include "exam.fullname" $ }}-secrets
key: {{ $val }} key: {{ $val }}
{{- end }} {{- end }}
{{- end }} {{- end }}
@ -52,6 +61,29 @@ spec:
{{- end }} {{- end }}
{{- end }} {{- end }}
resources: {} resources: {}
{{- if not .probe }}
livenessProbe:
httpGet:
path: /ping
port: {{ .port }}
scheme: HTTP
initialDelaySeconds: 10
readinessProbe:
httpGet:
path: /ping
port: {{ .port }}
scheme: HTTP
initialDelaySeconds: 10
{{- else }}
livenessProbe:
exec:
{{- toYaml .probe | nindent 14 }}
initialDelaySeconds: 5
readinessProbe:
exec:
{{- toYaml .probe | nindent 14 }}
initialDelaySeconds: 5
{{- end }}
{{- if .mountPath }} {{- if .mountPath }}
volumeMounts: volumeMounts:
- mountPath: {{ .mountPath }} - mountPath: {{ .mountPath }}
@ -61,7 +93,6 @@ spec:
volumes: volumes:
- name: {{ .storage }} - name: {{ .storage }}
persistentVolumeClaim: persistentVolumeClaim:
# TODO claimName: {{ include "exam.pvclaim" $ }}
claimName: {{ include "exam.fullname" $ }}-postgres-pv-claim
{{- end }} {{- end }}
{{- end }} {{- end }}

View File

@ -1,7 +1,7 @@
apiVersion: v1 apiVersion: v1
kind: PersistentVolume kind: PersistentVolume
metadata: metadata:
name: {{ include "exam.fullname" . }}-postgres-pv name: {{ include "exam.fullname" . }}-pv
labels: labels:
type: local type: local
{{- include "exam.labels" . | nindent 4 }} {{- include "exam.labels" . | nindent 4 }}
@ -9,11 +9,11 @@ spec:
accessModes: accessModes:
- ReadWriteOnce - ReadWriteOnce
capacity: capacity:
storage: {{ .Values.pvc.request | quote }} storage: {{ .Values.pv.request | quote }}
local: local:
path: /var/lib/minikube path: {{ .Values.pv.path | quote }}
persistentVolumeReclaimPolicy: Retain persistentVolumeReclaimPolicy: Retain
storageClassName: local-storage storageClassName: {{ .Values.pv.class }}
volumeMode: Filesystem volumeMode: Filesystem
nodeAffinity: nodeAffinity:
required: required:
@ -27,7 +27,7 @@ spec:
apiVersion: v1 apiVersion: v1
kind: PersistentVolumeClaim kind: PersistentVolumeClaim
metadata: metadata:
name: {{ include "exam.fullname" . }}-postgres-pv-claim name: {{ include "exam.pvclaim" $ }}
labels: labels:
app: db app: db
{{- include "exam.labels" . | nindent 4 }} {{- include "exam.labels" . | nindent 4 }}
@ -36,5 +36,5 @@ spec:
- ReadWriteOnce - ReadWriteOnce
resources: resources:
requests: requests:
storage: {{ .Values.pvc.request | quote }} storage: {{ .Values.pv.request | quote }}
storageClassName: local-storage storageClassName: {{ .Values.pv.class }}

View File

@ -1,40 +1,52 @@
tls: shared:
enabled: true host: &host kube.slc.ar
host: kube.slc.ar api:
name: &apiName "api"
port: &apiPort 5000
tier: &apiTier "backend"
db:
name: &dbName "db"
port: &dbPort 5432
tier: &dbTier "backend"
client:
name: &clientName "client"
port: &clientPort 8080
tier: &clientTier "frontend"
ingress: ingress:
ssl: true
annotations: annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2 nginx.ingress.kubernetes.io/rewrite-target: /$2
nginx.ingress.kubernetes.io/ssl-redirect: "true" nginx.ingress.kubernetes.io/ssl-redirect: "true"
nginx.ingress.kubernetes.io/use-regex: "true" nginx.ingress.kubernetes.io/use-regex: "true"
hosts: hosts:
- host: kube.slc.ar - host: *host
paths: paths:
- path: /()(.*) - path: /()(.*)
name: "client" name: "client"
port: 8080 port: *clientPort
- path: /api(/|$)(.*) - path: /api(/|$)(.*)
name: "api" name: "api"
port: 5000 port: *apiPort
tls: tls:
- secretName: exam-crt - secretName: exam-crt
hosts: hosts:
- kube.slc.ar - *host
services: services:
- api: - api:
name: "api" name: *apiName
tier: "backend" tier: *apiTier
port: 5000 port: *apiPort
- client: - client:
name: "client" name: *clientName
tier: "frontend" tier: *clientTier
port: 8080 port: *clientPort
target: 80 target: 80
- postgres: - db:
name: "db" name: *dbName
tier: "backend" tier: *dbTier
port: 5432 port: *dbPort
# secrets.yaml # secrets.yaml
secrets: secrets:
@ -42,34 +54,38 @@ secrets:
password: "password1234" password: "password1234"
database-url: "postgresql://username:password1234@exam-db/api_prod" database-url: "postgresql://username:password1234@exam-db/api_prod"
pvc: pv:
class: local-storage class: local-storage
request: 1Gi request: 1Gi
path: /var/lib/minikube
deployments: deployments:
- api: - api:
name: "api" name: *apiName
tier: "backend" tier: *apiTier
env: env:
app-settings: src.config.ProductionConfig app-settings: src.config.ProductionConfig
port: "5000" port: *apiPort
secrets: secrets:
database-url: database-url database-url: database-url
image: image:
repository: api repository: api
tag: prod tag: prod
port: 5000 port: *apiPort
replicas: 3
initContainer:
command: ['/bin/sh', '-c', 'until nc -z exam-db 5432; do sleep 1; done;']
# cmd: ['sh', '-c', 'until pg_isready -U username -h exam-db -p 5432; do sleep 1; done']
- client: - client:
tier: "frontend" name: *clientName
name: "client" tier: *clientTier
image: image:
repository: client repository: client
tag: prod tag: prod
port: 8080 port: 80
replicas: 3
- db: - db:
tier: "backend" name: *dbName
name: "db" tier: *dbTier
env: env:
pgdata: /var/lib/postgresql/data/pgdata pgdata: /var/lib/postgresql/data/pgdata
secrets: secrets:
@ -78,6 +94,8 @@ deployments:
image: image:
repository: db repository: db
tag: 13.3 tag: 13.3
port: 5432 port: *dbPort
mountPath: /var/lib/postgresql/data mountPath: /var/lib/postgresql/data
storage: postgres-pv-storage storage: pv-storage
probe:
command: ['/bin/sh', '-c', 'exec pg_isready -U "$(POSTGRES_USER)" -h localhost -p 5432;']