-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcluster-template-auto-image.yaml
436 lines (423 loc) · 17.7 KB
/
cluster-template-auto-image.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
---
apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
name: "${CLUSTER_NAME}"
labels:
cluster.x-k8s.io/cluster-name: "${CLUSTER_NAME}"
spec:
clusterNetwork:
pods:
cidrBlocks: ["192.168.0.0/16"]
infrastructureRef:
apiVersion: infrastructure.cluster.x-k8s.io/v1alpha1
kind: IonosCloudCluster
name: "${CLUSTER_NAME}"
controlPlaneRef:
kind: KubeadmControlPlane
apiVersion: controlplane.cluster.x-k8s.io/v1beta1
name: "${CLUSTER_NAME}-control-plane"
---
apiVersion: v1
kind: Secret
metadata:
name: "${CLUSTER_NAME}-credentials"
type: Opaque
stringData:
token: "${IONOS_TOKEN}"
---
apiVersion: infrastructure.cluster.x-k8s.io/v1alpha1
kind: IonosCloudCluster
metadata:
name: "${CLUSTER_NAME}"
spec:
controlPlaneEndpoint:
host: ${CONTROL_PLANE_ENDPOINT_HOST:-${CONTROL_PLANE_ENDPOINT_IP}}
port: ${CONTROL_PLANE_ENDPOINT_PORT:-6443}
location: ${CONTROL_PLANE_ENDPOINT_LOCATION}
credentialsRef:
name: "${CLUSTER_NAME}-credentials"
---
kind: KubeadmControlPlane
apiVersion: controlplane.cluster.x-k8s.io/v1beta1
metadata:
name: "${CLUSTER_NAME}-control-plane"
spec:
replicas: ${CONTROL_PLANE_MACHINE_COUNT}
machineTemplate:
infrastructureRef:
kind: IonosCloudMachineTemplate
apiVersion: infrastructure.cluster.x-k8s.io/v1alpha1
name: "${CLUSTER_NAME}-control-plane"
kubeadmConfigSpec:
users:
- name: root
sshAuthorizedKeys: [${IONOSCLOUD_MACHINE_SSH_KEYS}]
ntp:
enabled: true
servers:
- 0.de.pool.ntp.org
- 1.de.pool.ntp.org
- 2.de.pool.ntp.org
- 3.de.pool.ntp.org
files:
- path: /etc/ssh/sshd_config.d/ssh-audit_hardening.conf
owner: root:root
permissions: '0644'
content: |
# Restrict key exchange, cipher, and MAC algorithms, as per sshaudit.com
# hardening guide.
KexAlgorithms [email protected],curve25519-sha256,[email protected],gss-curve25519-sha256-,diffie-hellman-group16-sha512,gss-group16-sha512-,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha256
HostKeyAlgorithms [email protected],[email protected],[email protected],[email protected],[email protected],ssh-ed25519,rsa-sha2-512,rsa-sha2-256
CASignatureAlgorithms [email protected],ssh-ed25519,rsa-sha2-512,rsa-sha2-256
GSSAPIKexAlgorithms gss-curve25519-sha256-,gss-group16-sha512-
HostbasedAcceptedAlgorithms [email protected],[email protected],[email protected],ssh-ed25519,[email protected],rsa-sha2-512,[email protected],rsa-sha2-256
PubkeyAcceptedAlgorithms [email protected],[email protected],[email protected],ssh-ed25519,[email protected],rsa-sha2-512,[email protected],rsa-sha2-256
- path: /etc/sysctl.d/k8s.conf
content: |
fs.inotify.max_user_watches = 65536
net.netfilter.nf_conntrack_max = 1000000
- path: /etc/modules-load.d/k8s.conf
content: |
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
ip_vs_sed
# Crictl config
- path: /etc/crictl.yaml
content: |
runtime-endpoint: unix:///run/containerd/containerd.sock
timeout: 10
- path: /etc/kubernetes/manifests/kube-vip.yaml
owner: root:root
content: |
apiVersion: v1
kind: Pod
metadata:
name: kube-vip
namespace: kube-system
spec:
containers:
- args:
- manager
env:
- name: cp_enable
value: "true"
- name: vip_interface
value: ${VIP_NETWORK_INTERFACE=""}
- name: address
value: ${CONTROL_PLANE_ENDPOINT_IP}
- name: port
value: "${CONTROL_PLANE_ENDPOINT_PORT:-6443}"
- name: vip_arp
value: "true"
- name: vip_leaderelection
value: "true"
- name: vip_leaseduration
value: "15"
- name: vip_renewdeadline
value: "10"
- name: vip_retryperiod
value: "2"
image: ghcr.io/kube-vip/kube-vip:v0.7.1
imagePullPolicy: IfNotPresent
name: kube-vip
resources: {}
securityContext:
capabilities:
add:
- NET_ADMIN
- NET_RAW
volumeMounts:
- mountPath: /etc/kubernetes/admin.conf
name: kubeconfig
hostAliases:
- hostnames:
- kubernetes
- localhost
ip: 127.0.0.1
hostNetwork: true
volumes:
- hostPath:
path: /etc/kubernetes/admin.conf
type: FileOrCreate
name: kubeconfig
status: {}
- path: /etc/kube-vip-prepare.sh
content: |
#!/bin/bash
# Copyright 2020 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e
# Configure the workaround required for kubeadm init with kube-vip:
# xref: https://github.com/kube-vip/kube-vip/issues/684
# Nothing to do for kubernetes < v1.29
KUBEADM_MINOR="$(kubeadm version -o short | cut -d '.' -f 2)"
if [[ "$KUBEADM_MINOR" -lt "29" ]]; then
exit 0
fi
IS_KUBEADM_INIT="false"
# cloud-init kubeadm init
if [[ -f /run/kubeadm/kubeadm.yaml ]]; then
IS_KUBEADM_INIT="true"
fi
# ignition kubeadm init
if [[ -f /etc/kubeadm.sh ]] && grep -q -e "kubeadm init" /etc/kubeadm.sh; then
IS_KUBEADM_INIT="true"
fi
if [[ "$IS_KUBEADM_INIT" == "true" ]]; then
sed -i 's#path: /etc/kubernetes/admin.conf#path: /etc/kubernetes/super-admin.conf#' \
/etc/kubernetes/manifests/kube-vip.yaml
fi
owner: root:root
permissions: "0700"
# CSI Metadata config
- content: |
{
"datacenter-id": "${IONOSCLOUD_DATACENTER_ID}"
}
owner: root:root
path: /etc/ie-csi/cfg.json
permissions: '0644'
- content: |
#!/bin/bash
set -e
# Nothing to do for kubernetes < v1.29
KUBEADM_MINOR="$(kubeadm version -o short | cut -d '.' -f 2)"
if [[ "$KUBEADM_MINOR" -lt "29" ]]; then
exit 0
fi
NODE_IPv4_ADDRESS=$(ip -j addr show dev ens6 | jq -r '.[].addr_info[] | select(.family == "inet") | select(.scope=="global") | select(.dynamic) | .local')
if [[ $NODE_IPv4_ADDRESS ]]; then
sed -i '$ s/$/ --node-ip '"$NODE_IPv4_ADDRESS"'/' /etc/default/kubelet
fi
# IPv6 currently not set, the ip is not set then this runs. Needs to be waited for.
NODE_IPv6_ADDRESS=$(ip -j addr show dev ens6 | jq -r '.[].addr_info[] | select(.family == "inet6") | select(.scope=="global") | .local')
if [[ $NODE_IPv6_ADDRESS ]]; then
sed -i '$ s/$/ --node-ip '"$NODE_IPv6_ADDRESS"'/' /etc/default/kubelet
fi
owner: root:root
path: /etc/set-node-ip.sh
permissions: '0700'
preKubeadmCommands:
- systemctl restart systemd-networkd.service systemd-modules-load.service systemd-journald containerd
# disable swap
- swapoff -a
- sed -i '/ swap / s/^/#/' /etc/fstab
- sysctl --system
- /etc/kube-vip-prepare.sh
# workaround 1.29 IP issue
- /etc/set-node-ip.sh
postKubeadmCommands:
- >
sed -i 's#path: /etc/kubernetes/super-admin.conf#path: /etc/kubernetes/admin.conf#' \
/etc/kubernetes/manifests/kube-vip.yaml
- >
systemctl disable --now udisks2 multipathd motd-news.timer fwupd-refresh.timer
packagekit ModemManager snapd snapd.socket snapd.apparmor snapd.seeded
# INFO(schegi-ionos): We decided to not remove this for now, since removing this would require the ccm to be installed for cluster-api
# to continue after the first node.
- export system_uuid=$(kubectl --kubeconfig /etc/kubernetes/kubelet.conf get node $(hostname) -ojsonpath='{..systemUUID }')
- >
kubectl --kubeconfig /etc/kubernetes/kubelet.conf
patch node $(hostname)
--type strategic -p '{"spec": {"providerID": "ionos://'$${system_uuid}'"}}'
- rm /etc/ssh/ssh_host_*
- ssh-keygen -t rsa -b 4096 -f /etc/ssh/ssh_host_rsa_key -N ""
- ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""
- sed -i 's/^\#HostKey \/etc\/ssh\/ssh_host_\(rsa\|ed25519\)_key$/HostKey \/etc\/ssh\/ssh_host_\1_key/g' /etc/ssh/sshd_config
- awk '$5 >= 3071' /etc/ssh/moduli > /etc/ssh/moduli.safe
- mv /etc/ssh/moduli.safe /etc/ssh/moduli
- iptables -I INPUT -p tcp --dport 22 -m state --state NEW -m recent --set
- iptables -I INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 10 --hitcount 10 -j DROP
- ip6tables -I INPUT -p tcp --dport 22 -m state --state NEW -m recent --set
- ip6tables -I INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 10 --hitcount 10 -j DROP
- apt-get update
- DEBIAN_FRONTEND=noninteractive apt-get install -q -y netfilter-persistent iptables-persistent
- service netfilter-persistent save
- systemctl restart sshd
initConfiguration:
localAPIEndpoint:
bindPort: ${CONTROL_PLANE_ENDPOINT_PORT:-6443}
nodeRegistration:
kubeletExtraArgs:
# use cloud-provider: external when using a CCM
cloud-provider: ""
joinConfiguration:
nodeRegistration:
criSocket: unix:///run/containerd/containerd.sock
kubeletExtraArgs:
# use cloud-provider: external when using a CCM
cloud-provider: ""
version: "${KUBERNETES_VERSION}"
---
kind: IonosCloudMachineTemplate
apiVersion: infrastructure.cluster.x-k8s.io/v1alpha1
metadata:
name: "${CLUSTER_NAME}-control-plane"
spec:
template:
spec:
datacenterID: ${IONOSCLOUD_DATACENTER_ID}
numCores: ${IONOSCLOUD_MACHINE_NUM_CORES:-4}
memoryMB: ${IONOSCLOUD_MACHINE_MEMORY_MB:-8192}
disk:
image:
selector:
resolutionPolicy: ${IONOSCLOUD_IMAGE_RESOLUTION_POLICY:-Newest}
matchLabels:
${IONOSCLOUD_IMAGE_LABEL_KEY}: ${IONOSCLOUD_IMAGE_LABEL_VALUE}
---
apiVersion: cluster.x-k8s.io/v1beta1
kind: MachineDeployment
metadata:
name: "${CLUSTER_NAME}-workers"
labels:
cluster.x-k8s.io/cluster-name: "${CLUSTER_NAME}"
spec:
clusterName: "${CLUSTER_NAME}"
replicas: ${WORKER_MACHINE_COUNT}
selector:
matchLabels:
template:
metadata:
labels:
cluster.x-k8s.io/cluster-name: "${CLUSTER_NAME}"
node-role.kubernetes.io/node: ""
spec:
clusterName: "${CLUSTER_NAME}"
version: "${KUBERNETES_VERSION}"
bootstrap:
configRef:
name: "${CLUSTER_NAME}-worker"
apiVersion: bootstrap.cluster.x-k8s.io/v1beta1
kind: KubeadmConfigTemplate
infrastructureRef:
name: "${CLUSTER_NAME}-worker"
apiVersion: infrastructure.cluster.x-k8s.io/v1alpha1
kind: IonosCloudMachineTemplate
---
apiVersion: infrastructure.cluster.x-k8s.io/v1alpha1
kind: IonosCloudMachineTemplate
metadata:
name: "${CLUSTER_NAME}-worker"
spec:
template:
spec:
datacenterID: ${IONOSCLOUD_DATACENTER_ID}
numCores: ${IONOSCLOUD_MACHINE_NUM_CORES:-2}
memoryMB: ${IONOSCLOUD_MACHINE_MEMORY_MB:-4096}
disk:
image:
selector:
resolutionPolicy: ${IONOSCLOUD_IMAGE_RESOLUTION_POLICY:-Newest}
matchLabels:
${IONOSCLOUD_IMAGE_LABEL_KEY}: ${IONOSCLOUD_IMAGE_LABEL_VALUE}
---
apiVersion: bootstrap.cluster.x-k8s.io/v1beta1
kind: KubeadmConfigTemplate
metadata:
name: "${CLUSTER_NAME}-worker"
spec:
template:
spec:
users:
- name: root
sshAuthorizedKeys: [${IONOSCLOUD_MACHINE_SSH_KEYS}]
ntp:
enabled: true
servers:
- 0.de.pool.ntp.org
- 1.de.pool.ntp.org
- 2.de.pool.ntp.org
- 3.de.pool.ntp.org
files:
- path: /etc/ssh/sshd_config.d/ssh-audit_hardening.conf
owner: root:root
permissions: '0644'
content: |
# Restrict key exchange, cipher, and MAC algorithms, as per sshaudit.com
# hardening guide.
KexAlgorithms [email protected],curve25519-sha256,[email protected],gss-curve25519-sha256-,diffie-hellman-group16-sha512,gss-group16-sha512-,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha256
HostKeyAlgorithms [email protected],[email protected],[email protected],[email protected],[email protected],ssh-ed25519,rsa-sha2-512,rsa-sha2-256
CASignatureAlgorithms [email protected],ssh-ed25519,rsa-sha2-512,rsa-sha2-256
GSSAPIKexAlgorithms gss-curve25519-sha256-,gss-group16-sha512-
HostbasedAcceptedAlgorithms [email protected],[email protected],[email protected],ssh-ed25519,[email protected],rsa-sha2-512,[email protected],rsa-sha2-256
PubkeyAcceptedAlgorithms [email protected],[email protected],[email protected],ssh-ed25519,[email protected],rsa-sha2-512,[email protected],rsa-sha2-256
- path: /etc/sysctl.d/k8s.conf
content: |
fs.inotify.max_user_watches = 65536
net.netfilter.nf_conntrack_max = 1000000
- path: /etc/modules-load.d/k8s.conf
content: |
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
ip_vs_sed
# Crictl config
- path: /etc/crictl.yaml
content: |
runtime-endpoint: unix:///run/containerd/containerd.sock
timeout: 10
# CSI Metadata config
- content: |
{
"datacenter-id": "${IONOSCLOUD_DATACENTER_ID}"
}
owner: root:root
path: /etc/ie-csi/cfg.json
permissions: '0644'
preKubeadmCommands:
- systemctl restart systemd-networkd.service systemd-modules-load.service systemd-journald containerd
# disable swap
- swapoff -a
- sed -i '/ swap / s/^/#/' /etc/fstab
- sysctl --system
postKubeadmCommands:
- >
systemctl disable --now udisks2 multipathd motd-news.timer fwupd-refresh.timer
packagekit ModemManager snapd snapd.socket snapd.apparmor snapd.seeded
# INFO(schegi-ionos): We decided to not remove this for now, since removing this would require the ccm to be
# installed for cluster-api to continue after the first node.
- export system_uuid=$(kubectl --kubeconfig /etc/kubernetes/kubelet.conf get node $(hostname) -ojsonpath='{..systemUUID }')
- >
kubectl --kubeconfig /etc/kubernetes/kubelet.conf
patch node $(hostname)
--type strategic -p '{"spec": {"providerID": "ionos://'$${system_uuid}'"}}'
- rm /etc/ssh/ssh_host_*
- ssh-keygen -t rsa -b 4096 -f /etc/ssh/ssh_host_rsa_key -N ""
- ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""
- sed -i 's/^\#HostKey \/etc\/ssh\/ssh_host_\(rsa\|ed25519\)_key$/HostKey \/etc\/ssh\/ssh_host_\1_key/g' /etc/ssh/sshd_config
- awk '$5 >= 3071' /etc/ssh/moduli > /etc/ssh/moduli.safe
- mv /etc/ssh/moduli.safe /etc/ssh/moduli
- iptables -I INPUT -p tcp --dport 22 -m state --state NEW -m recent --set
- iptables -I INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 10 --hitcount 10 -j DROP
- ip6tables -I INPUT -p tcp --dport 22 -m state --state NEW -m recent --set
- ip6tables -I INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 10 --hitcount 10 -j DROP
- apt-get update
- DEBIAN_FRONTEND=noninteractive apt-get install -q -y netfilter-persistent iptables-persistent
- service netfilter-persistent save
- systemctl restart sshd
joinConfiguration:
nodeRegistration:
kubeletExtraArgs:
# use cloud-provider: external when using a CCM
cloud-provider: ""
criSocket: unix:///run/containerd/containerd.sock