Skip to content

Instantly share code, notes, and snippets.

@h0hmj
Last active March 26, 2024 06:37
Show Gist options
  • Save h0hmj/8e6ce4594962b9b7bb14ce6936d74567 to your computer and use it in GitHub Desktop.
Save h0hmj/8e6ce4594962b9b7bb14ce6936d74567 to your computer and use it in GitHub Desktop.
root@k8s:~# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: admin
spec:
containers:
- name: shell
image: ubuntu:22.04
command: ["sleep", "infinity"]
securityContext:
privileged: true
hostNetwork: true
hostPID: true
hostIPC: true
root@k8s:~# cat test.sh
#!/bin/bash
while true; do
echo "alive"
sleep 1
done
root@k8s:~# k exec -it admin -- bash
root@k8s:/# nsenter -t 1 -m -p -n -i -u systemd-run --unit test -- /root/test.sh
Running as unit: test.service
root@k8s:/# exit
exit
root@k8s:~# systemctl status test
● test.service - /root/test.sh
Loaded: loaded (/run/systemd/transient/test.service; transient)
Transient: yes
Active: active (running) since Tue 2024-03-26 06:32:20 UTC; 9s ago
Main PID: 45729 (test.sh)
Tasks: 2 (limit: 19043)
Memory: 532.0K
CPU: 14ms
CGroup: /system.slice/test.service
├─45729 /bin/bash /root/test.sh
└─45818 sleep 1
Mar 26 06:32:20 k8s test.sh[45729]: alive
Mar 26 06:32:21 k8s test.sh[45729]: alive
Mar 26 06:32:22 k8s test.sh[45729]: alive
Mar 26 06:32:23 k8s test.sh[45729]: alive
Mar 26 06:32:24 k8s test.sh[45729]: alive
Mar 26 06:32:25 k8s test.sh[45729]: alive
Mar 26 06:32:26 k8s test.sh[45729]: alive
Mar 26 06:32:27 k8s test.sh[45729]: alive
Mar 26 06:32:28 k8s test.sh[45729]: alive
Mar 26 06:32:29 k8s test.sh[45729]: alive
root@k8s:~# ps -ef | grep test.sh
root 45729 1 0 06:32 ? 00:00:00 /bin/bash /root/test.sh
root 47075 1758 0 06:34 pts/0 00:00:00 grep --color=auto test.sh
root@k8s:~# k delete -f pod.yaml
pod "admin" deleted
root@k8s:~# systemctl status test
● test.service - /root/test.sh
Loaded: loaded (/run/systemd/transient/test.service; transient)
Transient: yes
Active: active (running) since Tue 2024-03-26 06:32:20 UTC; 4min 3s ago
Main PID: 45729 (test.sh)
Tasks: 2 (limit: 19043)
Memory: 580.0K
CPU: 308ms
CGroup: /system.slice/test.service
├─45729 /bin/bash /root/test.sh
└─48153 sleep 1
Mar 26 06:36:14 k8s test.sh[45729]: alive
Mar 26 06:36:15 k8s test.sh[45729]: alive
Mar 26 06:36:16 k8s test.sh[45729]: alive
Mar 26 06:36:17 k8s test.sh[45729]: alive
Mar 26 06:36:18 k8s test.sh[45729]: alive
Mar 26 06:36:19 k8s test.sh[45729]: alive
Mar 26 06:36:20 k8s test.sh[45729]: alive
Mar 26 06:36:21 k8s test.sh[45729]: alive
Mar 26 06:36:22 k8s test.sh[45729]: alive
Mar 26 06:36:23 k8s test.sh[45729]: alive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment