-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathkeepalived-template.yaml
328 lines (314 loc) · 10.4 KB
/
keepalived-template.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
# expected merge structure
# .KeepAlivedGroup
# .Services
- apiVersion: apps/v1
kind: DaemonSet
metadata:
name: {{ .KeepalivedGroup.ObjectMeta.Name }}
namespace: {{ .KeepalivedGroup.ObjectMeta.Namespace }}
spec:
selector:
matchLabels:
keepalivedGroup: {{ .KeepalivedGroup.ObjectMeta.Name }}
template:
metadata:
{{- with .KeepalivedGroup.Spec.DaemonsetPodAnnotations }}
annotations:
{{ range $index, $element := . }}
{{ $index }}: {{ $element }}
{{ end }}
{{- end }}
labels:
keepalivedGroup: {{ .KeepalivedGroup.ObjectMeta.Name }}
spec:
{{- if .KeepalivedGroup.Spec.DaemonsetPodPriorityClassName }}
priorityClassName: {{ .KeepalivedGroup.Spec.DaemonsetPodPriorityClassName }}
{{- end }}
tolerations:
- operator: Exists
nodeSelector:
{{ range $index, $element := .KeepalivedGroup.Spec.NodeSelector }}
{{ $index }}: {{ $element }}
{{ end }}
hostNetwork: true
automountServiceAccountToken: false
enableServiceLinks: false
shareProcessNamespace: true
initContainers:
- name: config-setup
image: {{ .Misc.image }}
imagePullPolicy: Always
command:
- bash
- -c
- /usr/local/bin/notify.sh
env:
- name: file
value: /etc/keepalived.d/src/keepalived.conf
- name: dst_file
value: /etc/keepalived.d/dst/keepalived.conf
- name: reachip
{{- if .KeepalivedGroup.Spec.InterfaceFromIP }}
value: {{ .KeepalivedGroup.Spec.InterfaceFromIP }}
{{- else }}
value: ""
{{- end }}
- name: create_config_only
value: "true"
volumeMounts:
- mountPath: /etc/keepalived.d/src
name: config
readOnly: true
- mountPath: /etc/keepalived.d/dst
name: config-dst
securityContext:
runAsUser: 0
containers:
- name: keepalived
image: {{ .KeepalivedGroup.Spec.Image }}
command:
- /bin/bash
args:
- -c
- >
exec /usr/sbin/keepalived
--log-console
--log-detail
--dont-fork
--config-id=${POD_NAME}
--use-file=/etc/keepalived.d/keepalived.conf
--pid=/etc/keepalived.pid/keepalived.pid
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
volumeMounts:
- mountPath: /lib/modules
name: lib-modules
readOnly: true
- mountPath: /etc/keepalived.d
name: config-dst
readOnly: true
- mountPath: /etc/keepalived.pid
name: pid
- mountPath: /tmp
name: stats
securityContext:
privileged: true
- name: config-reloader
image: {{ .Misc.image }}
imagePullPolicy: Always
command:
- bash
- -c
- /usr/local/bin/notify.sh
env:
- name: pid
value: /etc/keepalived.pid/keepalived.pid
- name: file
value: /etc/keepalived.d/src/keepalived.conf
- name: dst_file
value: /etc/keepalived.d/dst/keepalived.conf
- name: reachip
{{- if .KeepalivedGroup.Spec.InterfaceFromIP }}
value: {{ .KeepalivedGroup.Spec.InterfaceFromIP }}
{{- else }}
value: ""
{{- end }}
- name: create_config_only
value: "false"
volumeMounts:
- mountPath: /etc/keepalived.d/src
name: config
readOnly: true
- mountPath: /etc/keepalived.d/dst
name: config-dst
- mountPath: /etc/keepalived.pid
name: pid
securityContext:
runAsUser: 0
- name: prometheus-exporter
image: {{ .Misc.image }}
imagePullPolicy: Always
command:
- /usr/local/bin/keepalived_exporter
args:
- '-web.listen-address'
- ':9650'
- '-web.telemetry-path'
- '/metrics'
securityContext:
privileged: true
ports:
- name: metrics
containerPort: 9650
protocol: TCP
volumeMounts:
- mountPath: /lib/modules
name: lib-modules
readOnly: true
- mountPath: /tmp
name: stats
volumes:
- hostPath:
path: /lib/modules
name: lib-modules
- name: config
configMap:
name: {{ .KeepalivedGroup.ObjectMeta.Name }}
- name: config-dst
emptyDir: {}
- name: pid
emptyDir:
medium: Memory
- name: stats
emptyDir: {}
- apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .KeepalivedGroup.ObjectMeta.Name }}
namespace: {{ .KeepalivedGroup.ObjectMeta.Namespace }}
labels:
keepalivedGroup: {{ .KeepalivedGroup.ObjectMeta.Name }}
data:
keepalived.conf: |
global_defs {
router_id {{ .KeepalivedGroup.ObjectMeta.Name }}
{{ range $key,$value := .KeepalivedGroup.Spec.VerbatimConfig }}
{{ $key }} {{ $value }}
{{ end }}
}
{{- range $service := .Services }}
{{- if eq $service.Spec.ExternalTrafficPolicy "Local" }}
{{- $namespacedName := printf "%s/%s" $service.ObjectMeta.Namespace $service.ObjectMeta.Name }}
vrrp_script {{ $namespacedName }} {
script "/usr/bin/curl --fail --max-time 1 http://127.0.0.1:{{ $service.Spec.HealthCheckNodePort }}/health"
timeout 10
rise 3
fall 3
}
{{- end }}
{{- end }}
{{ $root:=. }}
{{ $verbatim_key:="keepalived-operator.redhat-cop.io/verbatimconfig"}}
{{ $spread_key:="keepalived-operator.redhat-cop.io/spreadvips" }}
{{ range $service := .Services }}
{{ $namespacedName:=printf "%s/%s" $service.ObjectMeta.Namespace $service.ObjectMeta.Name }}
{{- if and (eq (index $service.GetAnnotations $spread_key) "true") (gt (len $root.KeepalivedPods) 0) }}
{{- range $i, $ip := (mergeStringSlices $service.Status.LoadBalancer.Ingress $service.Spec.ExternalIPs) }}
{{- $namespacedNameForIP := printf "%s/%s" $namespacedName $ip }}
{{- $owner := index $root.KeepalivedPods (modulus $i (len $root.KeepalivedPods)) }}
vrrp_instance {{ $namespacedNameForIP }} {
@{{ $owner.ObjectMeta.Name }} state MASTER
@^{{ $owner.ObjectMeta.Name }} state BACKUP
@{{ $owner.ObjectMeta.Name }} priority 200
@^{{ $owner.ObjectMeta.Name }} priority 100
interface {{ $root.KeepalivedGroup.Spec.Interface }}
virtual_router_id {{ index $root.KeepalivedGroup.Status.RouterIDs $namespacedNameForIP }}
virtual_ipaddress {
{{ $ip }}
}
{{- if eq $root.KeepalivedGroup.Spec.UnicastEnabled true }}
unicast_peer {
{{ range $pod := $root.KeepalivedPods }}
{{- if $pod.Status.HostIP }}
{{ $pod.Status.HostIP }}
{{- end -}}
{{ end }}
}
{{- end -}}
{{- if ne $root.Misc.authPass "" }}
authentication {
auth_type PASS
auth_pass {{ $root.Misc.authPass }}
}
{{- end }}
{{- if eq $service.Spec.ExternalTrafficPolicy "Local" }}
track_script {
{{ $namespacedName }}
}
{{- end }}
{{ range $key , $value := (parseJson (index $service.GetAnnotations $verbatim_key)) }}
{{ $key }} {{ $value }}
{{ end }}
}
{{- end }}
{{- else }}
vrrp_instance {{ $namespacedName }} {
interface {{ $root.KeepalivedGroup.Spec.Interface }}
virtual_router_id {{ index $root.KeepalivedGroup.Status.RouterIDs $namespacedName }}
virtual_ipaddress {
{{ range mergeStringSlices $service.Status.LoadBalancer.Ingress $service.Spec.ExternalIPs }}
{{ . }}
{{ end }}
}
{{- if eq $root.KeepalivedGroup.Spec.UnicastEnabled true }}
unicast_peer {
{{ range $pod := $root.KeepalivedPods }}
{{- if $pod.Status.HostIP }}
{{ $pod.Status.HostIP }}
{{- end -}}
{{ end }}
}
{{- end -}}
{{- if ne $root.Misc.authPass "" }}
authentication {
auth_type PASS
auth_pass {{ $root.Misc.authPass }}
}
{{- end }}
{{- if eq $service.Spec.ExternalTrafficPolicy "Local" }}
track_script {
{{ $namespacedName }}
}
{{- end }}
{{ range $key , $value := (parseJson (index $service.GetAnnotations $verbatim_key)) }}
{{ $key }} {{ $value }}
{{ end }}
}
{{- end }}
{{ end }}
{{ if eq .Misc.supportsPodMonitor "true" }}
- apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
name: {{ .KeepalivedGroup.ObjectMeta.Name }}
namespace: {{ .KeepalivedGroup.ObjectMeta.Namespace }}
labels:
keepalivedGroup: {{ .KeepalivedGroup.ObjectMeta.Name }}
metrics: keepalived
spec:
selector:
matchLabels:
keepalivedGroup: {{ .KeepalivedGroup.ObjectMeta.Name }}
podMetricsEndpoints:
- port: metrics
- apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: {{ .KeepalivedGroup.ObjectMeta.Name }}-prometheus-k8s
rules:
- apiGroups:
- ""
resources:
- endpoints
- pods
- services
verbs:
- get
- list
- watch
- apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: {{ .KeepalivedGroup.ObjectMeta.Name }}-prometheus-k8s
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: {{ .KeepalivedGroup.ObjectMeta.Name }}-prometheus-k8s
subjects:
- kind: ServiceAccount
name: prometheus-k8s
namespace: openshift-monitoring
{{ end}}