為了從 ingress 的設定下手,我們將更動從 ALB 改成 ingress,方法就是使用變數的設定來做切換。首先,我們先定義不同的 rule 對應到的 target group
inMaintenanceMode: false
rules:
- name: "abc"
hostPrefix: "abc"
paths:
- path: "/*"
serviceName: "abc-api-service"
servicePort: 80
###############
# Maintenance #
###############
maintenanceRules:
- name: "abc-maintenance-1"
hostPrefix: "abc"
paths:
- path: "/"
serviceName: "abc-maintenance-rule-1"
servicePort: 80
- name: "abc-maintenance-2"
hostPrefix: "abc"
paths:
- path: "/"
serviceName: "abc-maintenance-rule-2"
servicePort: 80
- name: "abc-maintenance-3"
hostPrefix: "abc"
paths:
- path: "/"
serviceName: "abc-maintenance-rule-3"
servicePort: 80
- name: "maintenanceCode"
hostPrefix: "abc"
paths:
- path: "/"
serviceName: "maintenance-503"
servicePort: 80
為什麼類似的 Name 要建立多個,是因為我們有特定的白名單要加入,這些白名單是為了讓 QA Team 可以在 maintenance mode 的時候如預期進入服務內做測試。
完成之後,再來要到 template 裡面進行設定
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ .Release.Namespace }}-ingress
namespace: {{ .Release.Namespace }}
annotations:
##############################
# maintenance mode - abc #
##############################
alb.ingress.kubernetes.io/conditions.abc-maintenance-rule-1 : {{ .Values.maintenanceIpAddress1 | toJson | quote }}
alb.ingress.kubernetes.io/actions.abc-maintenance-rule-1 : {{ .Values.maintenanceRules | toJson | quote }}
alb.ingress.kubernetes.io/actions.maintenance-503: >
{"type":"fixed-response","fixedResponseConfig":{"contentType":"text/plain","statusCode":"503","messageBody":"maintenance mode"}}
spec:
rules:
# maintenance rules
{{- $inMaintenanceMode := .Values.inMaintenanceMode -}}
{{- if $inMaintenanceMode }}
{{- range .Values.maintenanceRules }}
{{- else }}
- http:
pathType: Prefix
backend:
service:
name: {{ .serviceName }}
port:
name: use-annotation
{{- end }}
{{- end }}
# if not inMaintenanceMode
{{- else }}
# default rules
{{- range .Values.rules }}
- http:
pathType: Prefix
backend:
service:
name: {{ .serviceName }}
port:
number: {{ .servicePort }}
{{- end }}
{{- end }}
{{- end }}
如此一來,我們就可以藉由切換變數的 inMaintenanceMode 來切換模式了!