Run Command in Application Pod via TrueNAS Shell

HarryMuscle

Contributor
Joined
Nov 15, 2021
Messages
161
Is it possible with the Kubernetes that TrueNAS Scale uses to run a command inside an application pod from the TrueNAS shell without going into the application's pod's shell? Sort of the equivalent to the docker exec command?

Thanks,
Harry
 

Kris Moore

SVP of Engineering
Administrator
Moderator
iXsystems
Joined
Nov 12, 2015
Messages
1,471
Right now the container backend used for Kubernetes is still docker, so you can run "docker ps" to find your specific pod, and then use "docker exec -it <pod> <command>" as usual.
 

FrostyCat

Explorer
Joined
Jan 4, 2022
Messages
79
Yes you can. Also, my advice would be to skip docker altogether and use only kubectl.

Code:
k3s kubectl -n <NAMESPACE> exec -ti <POD> -- <COMMAND>
# the next command would get you a shell inside the container
# k3s kubectl -n <NAMESPACE> exec -ti <POD> -- bash


Useful links:
 

FrostyCat

Explorer
Joined
Jan 4, 2022
Messages
79
Quick example:
Code:
root@nas[~]# k3s kubectl -n ix-jellyfin get pods
NAME                        READY   STATUS    RESTARTS   AGE
jellyfin-585c6ff5d8-kwcmt   1/1     Running   0          14h

root@nas[~]# k3s kubectl -n ix-jellyfin exec -ti jellyfin-585c6ff5d8-kwcmt -- bash
Defaulted container "jellyfin" out of: jellyfin, autopermissions (init)
I have no name!@jellyfin-585c6ff5d8-kwcmt:/$
 

peterge

Explorer
Joined
Sep 22, 2021
Messages
57
Quick example:
Code:
root@nas[~]# k3s kubectl -n ix-jellyfin get pods
NAME                        READY   STATUS    RESTARTS   AGE
jellyfin-585c6ff5d8-kwcmt   1/1     Running   0          14h

root@nas[~]# k3s kubectl -n ix-jellyfin exec -ti jellyfin-585c6ff5d8-kwcmt -- bash
Defaulted container "jellyfin" out of: jellyfin, autopermissions (init)
I have no name!@jellyfin-585c6ff5d8-kwcmt:/$
Thanks for the command, i want to use this command, to export my documents in paperless-ng with a ?weekly? cron job into my home share, which backups to backblaze. However, i would like to get the cron job as automatic as possible, do you know a solution to get always the correct pod name behind exec -ti <pod_name> ?
Do you know a better way than running this crappy command:
k3s kubectl -n ix-paperless-ng exec -ti $(k3s kubectl -n ix-paperless-ng get pods | tail -n 1 | cut -d " " -f 1) -- document_exporter
It works, but it may be not the smartest thing to do...

root@truenas[~]# k3s kubectl -n ix-paperless-ng exec -ti $(k3s kubectl -n ix-paperless-ng get pods | tail -n 1 | cut -d " " -f 1) -- document_exporter Defaulted container "paperless-ng" out of: paperless-ng, autopermissions (init), hostpatch (init), postgresql-init (init) usage: manage.py document_exporter [-h] [-c] [-f] [-d] [--no-progress-bar] [--version] [-v {0,1,2,3}] [--settings SETTINGS] [--pythonpath PYTHONPATH] [--traceback] [--no-color] [--force-color] [--skip-checks] target manage.py document_exporter: error: the following arguments are required: target command terminated with exit code 2
 

truecharts

Guru
Joined
Aug 19, 2021
Messages
788
Thanks for the command, i want to use this command, to export my documents in paperless-ng with a ?weekly? cron job into my home share, which backups to backblaze. However, i would like to get the cron job as automatic as possible, do you know a solution to get always the correct pod name behind exec -ti <pod_name> ?
Do you know a better way than running this crappy command:
k3s kubectl -n ix-paperless-ng exec -ti $(k3s kubectl -n ix-paperless-ng get pods | tail -n 1 | cut -d " " -f 1) -- document_exporter
It works, but it may be not the smartest thing to do...

root@truenas[~]# k3s kubectl -n ix-paperless-ng exec -ti $(k3s kubectl -n ix-paperless-ng get pods | tail -n 1 | cut -d " " -f 1) -- document_exporter Defaulted container "paperless-ng" out of: paperless-ng, autopermissions (init), hostpatch (init), postgresql-init (init) usage: manage.py document_exporter [-h] [-c] [-f] [-d] [--no-progress-bar] [--version] [-v {0,1,2,3}] [--settings SETTINGS] [--pythonpath PYTHONPATH] [--traceback] [--no-color] [--force-color] [--skip-checks] target manage.py document_exporter: error: the following arguments are required: target command terminated with exit code 2

Pods are not designed to be reached from a Cronjob and are ephemeral, both in name and design.
So not likely to have an easy solution, though there are solutions out there :)
 

googlehupf

Cadet
Joined
Oct 2, 2022
Messages
1
Thanks for the command, i want to use this command, to export my documents in paperless-ng with a ?weekly? cron job into my home share, which backups to backblaze. However, i would like to get the cron job as automatic as possible, do you know a solution to get always the correct pod name behind exec -ti <pod_name> ?
Do you know a better way than running this crappy command:
k3s kubectl -n ix-paperless-ng exec -ti $(k3s kubectl -n ix-paperless-ng get pods | tail -n 1 | cut -d " " -f 1) -- document_exporter
It works, but it may be not the smartest thing to do...

root@truenas[~]# k3s kubectl -n ix-paperless-ng exec -ti $(k3s kubectl -n ix-paperless-ng get pods | tail -n 1 | cut -d " " -f 1) -- document_exporter Defaulted container "paperless-ng" out of: paperless-ng, autopermissions (init), hostpatch (init), postgresql-init (init) usage: manage.py document_exporter [-h] [-c] [-f] [-d] [--no-progress-bar] [--version] [-v {0,1,2,3}] [--settings SETTINGS] [--pythonpath PYTHONPATH] [--traceback] [--no-color] [--force-color] [--skip-checks] target manage.py document_exporter: error: the following arguments are required: target command terminated with exit code 2

I was having the same problem. I'm doing my backup like this:
1. Mount an export volume to the paperless-ng deployment
2. a) have pre script running to a backup job where I run document_exporter --> to the mounted volume
b) sync the export volume to a AWS S3 Bucket with a cloud backup routine in Truenas

To get the pod from the deployment I use this command:
Code:
POD=$(k3s kubectl -n ix-paperless-ng get pods --no-headers | grep -E -v "(redis|postgresql|svclb)" | awk '{print $1}')
k3s kubectl -n ix-paperless-ng exec -it ${POD} -- python ./manage.py document_exporter ../export
 
Top