Can I run mayactl from my nodes (OpenEBS)? - openebs

I don't have access to the namespace openebs and maya-apiserver. Can I run mayactl on my nodes to get the same information? If yes, how does mayactl know which PVCs/PVs I have access to? How does it protect other volumes from accidental deletion via mayactl volume delete?

You can do it from maya-apiserver pod. You can do it with the below command in the master node.
kubectl exec -it <pod name> -n openebs bash
Once you are inside the pod, you can run required mayactl command
Else you can run the command directly as per below format.
kubectl exec -it <pod name> -n openebs <required mayactl command>

Related

shell script to run the docker image in bash, take db dump and copy file to the host

completely new to the shell script. I want to run the sql image (image is just there to take a db dump) and take a dump of the db and copy file to the host using shell script.
how i do manually is
1) docker run -it <image_name> bash (this takes in image bash)
2) mysqldump -h <ip> -u <user> -p db > filename.sql
3) docker cp <containerId>:/file/path/within/container /host/path/target (running this in host machine)
doing this i get the dump from container to host manually.
but while making shell script, i am having problem with the point 1) docker run -it <image_name> bash (this takes in image bash) since this takes me to the bash and i have to manually type the command.
how can i do it in the shell script.
any help will be greatly appreciated!
If I understand this correctly, you don't want to type those command manually and instead shell script should execute your command as and when you container is up and running. Now if you can modify sql related Dockerfile and can re-create image then use ENTRYPOINT [and if needed CMD] to execute shell script at startup. Check this link for details on ENTRYPOINT shell script.
Else, if you cannot recreate image then check this post i.e. how to run bash script from run command.
NOTE in both these cases you will have to mount your directory/volume and your sqldump command should copy dump this map volume/directory
You can pass the command to Bash as a parameter:
docker run -it <image_name> --name sqldump bash -c "mysqldump -h <ip> -u <user> -p db > /tmp/filename.sql"
docker cp sqldump:/tmp/filename.sql /path/on/host/filename.sql
Ignore the Docker steps, and just run mysqldump on your host. The -h option is the IP address or DNS name of the host running the database (can be 127.0.0.1 if the container is running on the same host, but not localhost because MySQL misinterprets that); if you mapped the database external port to a non-default port, you also need a -P (capital P) option to specify that port.
For example, if you started the container with
docker run -p 5433:5432 ... mysql:8
then you can take the dump from the host with
mysqldump -h 127.0.0.1 -P 5433 -p db > dump.sql
and not worry about the Docker details at all.

Bash - Exiting script file not child bash command | Exit command [duplicate]

my question is simple.
How to execute a bash command in the pod? I want to do everything with one bash command?
[root#master ~]# kubectl exec -it --namespace="tools" mongo-pod --bash -c "mongo"
Error: unknown flag: --bash
So, the command is simply ignored.
[root#master ~]# kubectl exec -it --namespace="tools" mongo-pod bash -c "mongo"
root#mongo-deployment-78c87cb84-jkgxx:/#
Or so.
[root#master ~]# kubectl exec -it --namespace="tools" mongo-pod bash mongo
Defaulting container name to mongo.
Use 'kubectl describe pod/mongo-deployment-78c87cb84-jkgxx -n tools' to see all of the containers in this pod.
/usr/bin/mongo: /usr/bin/mongo: cannot execute binary file
command terminated with exit code 126
If it's just a bash, it certainly works. But I want to jump into the mongo shell immediatelly.
I found a solution, but it does not work. Tell me if this is possible now?
Executing multiple commands( or from a shell script) in a kubernetes pod
Thanks.
The double dash symbol "--" is used to separate the command you want to run inside the container from the kubectl arguments.
So the correct way is:
kubectl exec -it --namespace=tools mongo-pod -- bash -c "mongo"
You forgot a space between "--" and "bash".
To execute multiple commands you may want:
to create a script and mount it as a volume in your pod and execute it
to launch a side container with the script and run it
I use something like this to get into the pod's shell:
kubectl exec -it --namespace develop pod-name bash
then you can execute the command you want within the pod (e.g. ping)
ping www.google.com
then you can see your ping log and voila ... enjoy it :D

How to enter a pod as root?

Currently I enter the pod as a mysql user using the command:
kubectl exec -it PODNAME -n NAMESPACE bash
I want to enter a container as root.
I've tried the following command:
kubectl exec -it PODNAME -n NAMESPACE -u root ID /bin/bash
kubectl exec -it PODNAME -n NAMESPACE -u root ID bash
There must be a way.
:-)
I found the answer.
You cannot log into the pod directly as root via kubectl.
You can do via the following steps.
1) find out what node it is running on kubectl get po -n [NAMESPACE] -o wide
2) ssh node
3) find the docker container sudo docker ps | grep [namespace]
4) log into container as root sudo docker exec -it -u root [DOCKER ID] /bin/bash
Actually there is already a possibility to connect via kubectl addon kubectl-plugins. Found a solution replying onto related question.
git clone https://github.com/jordanwilson230/kubectl-plugins.git
cd kubectl-plugins
./install-plugins.sh
source ~/.bash_profile
kubectl ssh -u root suse
Connecting...
Pod: suse
Namespace: NONE
User: root
Container: NONE
Command: /bin/sh
If you don't see a command prompt, try pressing enter.
sh-5.0#
SSH as root to kubernates pod.
For those on Windows Platform using minikube.
First you to ssh inside minikube
minikube ssh --user root
Then you need to find desired docker container
docker ps | grep NAME_POD
Copy fully qualified docker container name then use docker exec:
sudo docker exec -it -u root FQDN_CONTAINER bash
In my case it was :
sudo docker exec -it -u root k8s_jupyter_my-jupyter-0_default
_f05e2913-f1fd-4084-a8e8-e783519d4a71_0 bash
Once then i had full root access in bash inside POD.

Dynamically get a running container id/name created by docker run command

So I'm trying to run the following shell script which requires the container id/name of the container (in which the script would be run) dynamically.
One way could be to do docker ps and then getting the Container Id, but that won't be dynamic.
So is there a way to do this dynamically?
#!/bin/bash
docker exec <container id/name> /bin/bash -c "useradd -m <username> -p <password>"
You can give your container a specific name when running it using --name option.
docker run --name mycontainer ...
Then your exec command can use the specified name:
docker exec -it mycontainer ...
You can start your container and store the container id inside a variable like so:
container_id=$(docker run -it --rm --detach busybox)
Then you can use the container id in your docker exec command like so:
docker exec $container_id ls -la
or
docker stop $container_id
Note: Not using a (unique) name for the container but using an ID instead is inspired by this article on how to treat your servers/containers as cattle and not pets
I just figured out a way to do this that works for this. I'm constantly going into my container in bash, but each time I do it I have to look up the id of the running container - which is a pain. I use the --filter command like so:
docker ps -q --filter="NAME={name of container}"
Then the only thing that's output is the id of the container, which allows me to run:
docker exec -it $(docker ps -q --filter="NAME={name of container}") bash
...which is what I really want to do in this case.
You can filter by
id, name, label, exited, status, ancestor,
beforesince, volume, network, publishexpose,
health,isolation, or is-task
The documentation for filter is here.

Create new container with interactive shell

I wanted to create a new container with Node.js and start a bash-shell in it where I can interactively verify something.
Therefore I did docker run node /bin/bash but it exited instantly.
What did I do wrong?
You missed the -it: docker run -it <image-name> /bin/bash
--interactive, -i: Keep STDIN open even if not attached
--tty, -t: Allocate a pseudo-TTY
docker run reference

Resources