-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathk3s-killer.sh
executable file
·294 lines (241 loc) · 8.54 KB
/
k3s-killer.sh
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
#!/bin/bash
K3S_KILL="/usr/local/bin/k3s-kill.sh"
K3S_KILL_PATCH=$(cat <<'EOF'
31c31
< killtree $({ set +x; } 2>/dev/null; getshims; set -x)
---
> # killtree $({ set +x; } 2>/dev/null; getshims; set -x)
41c41
< do_unmount_and_remove '/run/k3s'
---
> # do_unmount_and_remove '/run/k3s'
EOF
)
K3S_SERVICE="/lib/systemd/system/k3s.service"
K3S_SERVICE_PATCH=$(cat <<'EOF'
21c21
< Restart=always
---
> Restart=never
EOF
)
APP_POOL="/mnt/$(cli -c 'app kubernetes config' | grep -oP '(?<=dataset\s\|\s).*(?=\s\|)')"
##################################
# Utility functions #
##################################
check_root () {
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi
}
continue_prompt () {
read -p "$1 (Y/N): " confirm && [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]] || exit 1
}
patch_file () {
mkdir -p "backup"
cp $2 backup/$(basename "$2")
echo
echo "Backed up $2 to backup/$(basename ${2})"
echo
echo "Patching $2..."
echo
echo "Testing Patch:"
echo "$1"
patch --read-only=fail --forward --dry-run "$2" <<< "$1"
echo
if [ $? -eq 0 ]; then
echo "Test successful, the patch can be applied!"
continue_prompt "Continue?"
patch --read-only=fail --no-backup-if-mismatch --forward "$2" <<< "$1"
echo "Patch applied!"
else
echo "Patch can not be applied! Maybe you already patched the file once? If so try `k3s-killer.sh uninstall` else contact the developer!" && exit 0
fi
}
check_patches () {
if ! patch --read-only=ignore -s -R -f --dry-run "$K3S_KILL" <<< "$K3S_KILL_PATCH"; then
echo "Patches not applied run the 'install' subcommand first!" && exit 0
fi
if ! patch --read-only=ignore -s -R -f --dry-run "$K3S_SERVICE" <<< "$K3S_SERVICE_PATCH"; then
echo "Patches not applied run the 'install' subcommand first!" && exit 0
fi
}
start_stop_containerd () {
if [[ "$1" == "stop" ]]; then
systemctl is-failed --quiet containerd_inplace && systemctl reset-failed --quiet containerd_inplace
systemctl stop containerd_inplace
elif [[ "$1" == "start" ]]; then
systemctl is-active --quiet k3s && echo "K3S is already running, will not start an extra containerd instance!" && return
CD_BIN="$APP_POOL/k3s/data/current/bin/containerd"
CD_STATE="/run/k3s/containerd"
CD_ROOT="$APP_POOL/k3s/agent/containerd"
CD_CONFIG="$APP_POOL/k3s/agent/etc/containerd/config.toml"
if [[ $(systemctl is-active containerd_inplace) == "active" ]]; then
echo "The `containerd_inplace` service is already loaded, restarting it..."
systemctl reload-or-restart containerd_inplace
else
echo "The 'containerd_inplace' service is now being loaded and started."
systemd-run -u containerd_inplace \
-p Delegate=yes -p KillMode=process --collect \
$CD_BIN \
-c $CD_CONFIG --state $CD_STATE \
-a $CD_STATE/containerd.sock --root $CD_ROOT
fi
# Check if containerd is up already
CTR_BIN="$APP_POOL/k3s/data/current/bin/ctr"
ADDRESS="/run/k3s/containerd/containerd.sock"
NS="k8s.io"
for i in {1..20}; do
if $CTR_BIN -a "$ADDRESS" -n "$NS" containers ls > /dev/null; then
break
else
sleep 1
fi
done
fi
}
##################################
# Command functions #
##################################
kill_k3s () {
check_root
check_patches
k3s_server_pid=$(systemctl status k3s | grep -oP '\d+(?= "\/usr\/local\/bin\/k3s server")')
echo "Killing k3s server with PID $k3s_server_pid..."
kill -9 "$k3s_server_pid"
}
run_command () {
check_root
check_patches
start_stop_containerd "start"
CTR_BIN="$APP_POOL/k3s/data/current/bin/ctr"
ADDRESS="/run/k3s/containerd/containerd.sock"
NS="k8s.io"
CONTAINER_REGEX="$1"
EXEC_ID="$2"
EXEC_USER="$3"
COMMAND="${@:4}"
CONTAINER=""
for c in $($CTR_BIN -a "$ADDRESS" -n "$NS" containers ls | grep "$CONTAINER_REGEX" | awk '{ print $1 }'); do
$CTR_BIN -a "$ADDRESS" -n "$NS" task ls | grep -q "$c" && CONTAINER="$c" && break
done
if [ -n "$3" ]; then
$CTR_BIN -a "$ADDRESS" -n "$NS" task exec --exec-id $EXEC_ID --user $EXEC_USER $CONTAINER $COMMAND
else
$CTR_BIN -a "$ADDRESS" -n "$NS" task exec --exec-id $EXEC_ID $CONTAINER $COMMAND
fi
start_stop_containerd "stop"
}
run_ctr () {
check_root
check_patches
start_stop_containerd "start"
$APP_POOL/k3s/data/current/bin/ctr $@
start_stop_containerd "stop"
}
restart_k3s () {
check_root
check_patches
if [[ $(systemctl status containerd_inplace | grep -q 'Active') ]]; then
start_stop_containerd "stop"
fi
echo "Starting k3s..."
systemctl start k3s
}
install_stuff () {
check_root
echo "Patching your k3s service so apps can run without it!"
echo "It is highly recommended to backup before doing this!"
continue_prompt "Continue?"
patch_file "$K3S_KILL_PATCH" "$K3S_KILL"
patch_file "$K3S_SERVICE_PATCH" "$K3S_SERVICE"
systemctl daemon-reload
echo "Successfully installed k3s-killer."
continue_prompt "You will now have to restart the k3s service, this may or may not re-deploy your apps, do you want to do that? Alternatively you can also restart your system."
systemctl restart k3s
}
uninstall_stuff() {
check_root
echo "Reverting patches..."
patch -R "$K3S_KILL" <<< "$K3S_KILL_PATCH"
patch -R "$K3S_SERVICE" <<< "$K3S_SERVICE_PATCH"
systemctl daemon-reload
}
cron_help () {
script=$(cat <<EOF
#!/bin/bash
# Check if the k3s server is running, if yes exit.
# Comment this out if you do not want this (for example if you
# are using this as a general cron job script)
systemctl is-active --quiet k3s && exit 1
# Find the correct container to talk to, to find it you can use 'k3s ctr' while k3s is running
# use 'k3s ctr containers ls' to list all running containers, then grep your way
# to your own container, replace the CONTAINER_REGEX variable with it, normally the name
# of the application should suffice!
CONTAINER_REGEX="nextcloud"
# Now run your cron tasks, the first argument here is the
# container regex, the second is the exec id (just use any arbitrary number,
# just keep in mind that if running two command ideally you should use two diffrent exec ids)
# the fourth is the user to run the command as and the last one is the command to run.
#
# In this case we run the nextcloud cron job and the preview generator with exec id 3 and 4
# respectivly and as user with id 33 (www-data), the user may be specified with the name
# or the id, it should not matter (normally).
/path/to/k3s-killer.sh run "\$CONTAINER_REGEX" 3 33 "php -f /var/www/html/cron.php"
/path/to/k3s-killer.sh run "\$CONTAINER_REGEX" 4 33 "php -f /var/www/html/occ preview:pre-generate"
# Add your own commands
EOF
)
echo "This is a guide on how to use your system cron to replace the kubernetes cron service thingy. This is especially helpful for Nextcloud.
The steps are as follows:
- write your script
- add a cron job in the truenas ui
- enjoy - I guess...
The following is a template/example for a cron shell script:
$script
" | less
continue_prompt "Do you want to write the template Script to a file?"
echo "$script" > cron_example.sh
chmod +x cron_example.sh
}
print_help () {
echo "
TRUENAS SCALE ONLY! NO GUARANTEES!
k3s-killer is a tool that makes it possible to run k3s (kubernetes) without
k3s using so much CPU. This is experimental and may not work in all usecases!
First install the patches with the 'install' subcommand!
For applications that need the kubernetes cronjob stuff (e.g. nextcloud) refer
to the instructions output by the 'cron' subcommand.
It is highly recommended to look into this script first by yourself to get a hold of
what exactly it is doing.
Usage: k3s-killer.sh [kill|restart|cron|install|uninstall]
kill: Kill the k3s server. This will make apps disappear from the GUI
restart: Start k3s server after killing it.
run: Run a command in a container while k3s is down (or active). Use the 'cron' subcommand for more info on it.
cron: Help for running cronjobs while k3s is down (or active).
ctr: Run the ctr programm while the k3s server is down (or active).
install: Install patches for the k3s systemd service. This is needed for this to work. It will also backup the unpatched files.
uninstall: Revert the patches for the k3s systemd service.
"
}
case "$1" in
"kill") kill_k3s ${@:2}
;;
"restart") restart_k3s ${@:2}
;;
"install") install_stuff ${@:2}
;;
"uninstall") uninstall_stuff ${@:2}
;;
"run") run_command ${@:2}
;;
"ctr") run_ctr ${@:2}
;;
"cron") cron_help ${@:2}
;;
"help"|"-h"|"--help") print_help ${@:2}
;;
*) print_help ${@:2}
;;
esac