Need to run more than one commands - kubernetes

I have to run 2 commands at a time:
bash
service nginx start
How can I pass those by using the following command?
kubectl run nginx --image=nginx --command -- <cmd> <arg1> ... <argN>
kubectl run -it testnew --image=imagename --command -- "/bin/bash","-c","service nginx start && while true; do echo bye; sleep 10;done" --requests=cpu=200m

Not sure how the --command flag works or is supposed to work.
This works for me, in that I get a running nginx with bash looping forever and printing 'bye'.
kubectl run -it testnew --image=nginx -- /bin/bash -c "service nginx start && while true; do echo bye; sleep 10;done"
Instead of this special command, you probably want to create a tweaked image that runs a script on start. Easier to manage what is running and harder to lose the customizations.

Related

How to create a pod and attach with a terminal later on

I want to run a multi-container pod, and interactuate with it via a terminal.
With kubectl run -ti I can run a monocontainer pod and attach easily to it.
I think I have to kubectl apply to create a complex pod, and later attach to it.
I've tried this and it doesn't work:
 kubectl run alpine --image alpine pod/alpine created
 kubectl attach -i alpine If you don't see a command prompt, try pressing enter.
error: unable to upgrade connection: container alpine not found in pod alpine
I'm using latest k3s v1.25.4+k3s1 under a Linux host (Ubuntu 22.04).
This does work:
 kubectl run alpine --image alpine --command sleep -- 999d
pod/alpine created
 kubectl exec -ti alpine -- ash
/ #
I need an auxiliary sleep.
You need attach interactively first:
kubectl run -it alpine --image alpine
Then detach by using CTRL-P, then CTRL-Q
To reattach, you then use:
kubectl attach alpine -c alpine -i -t
Note that if you close the shell at any point, you terminate the pod, and it will restart

kubectl run - How to pass all the ENV variables of the hosting shell to the pod?

It is possible to pass some ENV variables to the pod via the --env option of kubectl run, for example:
kubectl run -ti --rm test --image=busybox --env location=city --env time=morning --namespace default -- sh
I would like to send all the present ENV variables to the pod without specifying all of them the via --env, especially for those that I might not know the variable name. Is there an way to do so? Thanks
You could try something like this:
kubectl run -ti --rm test --image=busybox $(printenv | xargs -I % echo '--env' "%" | tr '\n' ' ') --namespace default -- sh

Understanding kubectl run command

I am tryng to create a pod using kubectl run by creating an yaml file, where first command is creating container but showing state as error and second one is creating with out any issue. what is the difference between these commands?
master $ kubectl run --restart=Never --image=busybox static-busybox --command -- sleep 1000 --dry-run -o yaml //Error container
master $ kubectl run --restart=Never --image=busybox static-busybox --dry-run -o yaml --command -- sleep 1000 //working command
In the first one parameters --dry-run -o yaml are applied to the command you run in the container (sleep), in the second one, they are applied to your kubectl execution
As per the syntax of the kubectl run command needs to be at the end. That's precisely the reason why first command is not working but second one is working.
Usage:
kubectl run NAME --image=image [--env="key=value"] [--port=port] [--dry-run=server|client] [--overrides=inline-json]
[--command] -- [COMMAND] [args...] [options]

Do we have command to execute multiple arguments in kubernetes

I have a pod running in kubernetes and i need to run two commands in one line.
Say,
kubectl exec -it <pod name> -n <namespace > -- bash -c redis-cli
above command will open redis-cli
i want to run one more command after exec in one line ie info, i am trying below which is not working:
kubectl exec -it <pod name> -n <namespace > -- bash -c redis-cli -- info
You have to put your command and all the parameters between apostrophes.
in your example it would be:
kubectl exec -it <pod_name> -n <namespace> -- bash -c 'redis-cli info'
From Bash manual: bash -c: If the -c option is present, then commands are read from the first non-option argument commaqnd_string.
Other option (which in my opinion is a better approach) is to get the output from the command with an instant pod, which creates, runs and deletes the pod right after that, like this:
kubectl run --namespace <YOUR_NAMESPACE> <TEMP_RANDOM_POD_NAME> --rm --tty -i --restart='Never' --env REDIS_PASSWORD=$REDIS_PASSWORD --image docker.io/bitnami/redis:5.0.7-debian-10-r0 -- bash -c 'redis-cli -h redis-master -a $REDIS_PASSWORD info'
in my case the password was stored in a envvar called $REDIS_PASSWORD and I'm connecting to a server in a pod called redis-master.
I let it as I runned it to show that you can use as much parameters as needed.
POC:
user#minikube:~$ kubectl run --namespace default redis-1580466120-client --rm --tty -i --restart='Never' --env REDIS_PASSWORD=$REDIS_PASSWORD --image docker.io/bitnami/redis:5.0.7-debian-10-r0 -- bash -c 'redis-cli -h redis-master -a $REDIS_PASSWORD info'
10:41:10.65
10:41:10.66 Welcome to the Bitnami redis container
10:41:10.66 Subscribe to project updates by watching https://github.com/bitnami/bitnami-docker-redis
10:41:10.66 Submit issues and feature requests at https://github.com/bitnami/bitnami-docker-redis/issues
10:41:10.67 Send us your feedback at containers#bitnami.com
10:41:10.67
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Server
redis_version:5.0.7
redis_git_sha1:00000000
redis_git_dirty:0
...
{{{suppressed output}}}
...
# CPU
used_cpu_sys:1.622434
used_cpu_user:1.313600
used_cpu_sys_children:0.013942
used_cpu_user_children:0.008014
# Cluster
cluster_enabled:0
# Keyspace
pod "redis-1580466120-client" deleted
Not get your question, do you want to get the information from redis-cli?
kubernetes exec -it <pod name> -n <namespace > -- bash -c 'redis-cli info'
Did you try to link your commands using && ?
kubernetes exec -it <pod name> -n <namespace > -- bash -c redis-cli && info

How to Deploy our Customize Thingsboard to Kubenetes Engine?

After make docker image of cassandra, cassandra-setup, application and zookeeper from my custom thingsboard.
I tried to deploy that to Kubernetes Engine, there's no error, but not running well.
Here is my command for yaml from my github:
curl -L https://raw.githubusercontent.com/Firdauzfan/ThingsboardGSPE/master/docker/k8s/common.yaml > common.yaml
curl -L https://raw.githubusercontent.com/Firdauzfan/ThingsboardGSPE/master/docker/k8s/cassandra.yaml > cassandra.yaml
curl -L https://raw.githubusercontent.com/Firdauzfan/ThingsboardGSPE/master/docker/k8s/zookeeper.yaml > zookeeper.yaml
curl -L https://raw.githubusercontent.com/Firdauzfan/ThingsboardGSPE/master/docker/k8s/tb.yaml > tb.yaml
curl -L https://raw.githubusercontent.com/Firdauzfan/ThingsboardGSPE/master/docker/k8s/cassandra-setup.yaml > cassandra-setup.yaml
and here is my docker image:
https://hub.docker.com/u/firdauzfanani/
Example: when i run command kubectl create -f cassandra.yaml, cassandra engine just show running but not ready.
Status screenshot here
If it is shown as not ready even if it is running with no issue (es: you can ssh into it and all the services are running), could be an misconfiguration of your redinessprobe that I see defined in the YAML file as follow, but I have no clue regarding its behaviour. Consider that accordingly to documentation it should return 0.
readinessProbe:
exec:
command:
- /bin/bash
- -c
- /ready-probe.sh
On the other hand, if when you try to access the pod you face some kind of errors, I would suggest you if you didn't do it already to retrieve further information to carry on the troubleshooting running the following commands:
$ kubectl describe deployments
$ kubectl describe pods
$ kubectl describe services
This series of commands could help you in order to understand better what is going on.
Please run them and edit your initial post with the output and I can take a look to them.
To ssh into the pod run:
$ kubectl get pods (to retrieve pod name)
$ kubectl exec -ti PODNAME /bn/bash
UPDATE
I deployed your YAML files, the pods is running correctly (I believe) what is failing is the probe whose content is the following:
cat /ready-probe.sh
if [[ $(nodetool status | grep $POD_IP) == *"UN"* ]]; then
if [[ $DEBUG ]]; then
echo "UN";
fi
exit 0;
else
if [[ $DEBUG ]]; then
echo "Not Up";
fi
exit 1;