How to access a container in Rancher from the command line without SSH
If you have deployed an application pod in rancher, sometimes you have to access the running container for various tasks. Maybe, to check some files, monitor logs, run some commands, copy files, etc.
Assume you have an application pod running in Rancher as the following image.
Using the Web UI
Using Rancher CLI
And navigate inside the extracted directory.
cd rancher-v2.3.2
You can find a binary file of the Rancher CLI there.
Before using the Rancher CLI we have to have the kubectl package installed.
You can refer to this page to install kubectl.
https://kubernetes.io/docs/tasks/tools/
Now, we are almost ready with the rancher CLI.
In order to use the rancher CLI commands, we have to have an access token. The CLI binary is using APIs to communicate with the Rancher server.
To create a token, go to the Rancher Web UI and select the API & Keys from the user context menu.
Then, you'll see the list of existing keys.
Click Add Key to create a new key.
It will be a good idea to select an expiration as a best practice.
Note: Make sure you don't set a scope here since you need to use it for the CLI. Due to a bug, this doesn't work in this Rancher version. Hopefully, this is fixed in the later versions (I didn't test yet though).
Then hit Create and it will show you the newly created tokens.
(I'll be deleting this token just after writing this 😋 )
Make sure you copy and keep all the information in a safe place as the message tells.
Login to the Rancher server from the CLI.
$ ./rancher login https://<SERVER_URL> --token <BEARER_TOKEN>
If login is a success, it will ask you to select a project.
Type the project number and hit enter. It will create some configs and save in your home folder.
Now we are ready to connect to our containers.
Hereafter it is always plain old kubectl commands.
If you want to execute bash in the container locate the pod and run the bash command for the container.
./rancher kubectl get pods -n mytestproject1
./rancher kubectl -n mytestproject1 exec -it myapp1-5c75bff649-hrxkn -- bash
How to copy a file to and from a container in Rancher
Again this is using the Rancher CLI as above.
Let's say I need to copy test.txt file to my container.
In the following command, the pod should be given as <namespace>/<pod name>:<destination dir>
./rancher kubectl cp /tmp/test.txt mytestproject1/myapp1-5c75bff649-hrxkn:/tmp
If you need to copy a file from a container, just switch the source and the destination.
eg:
./rancher kubectl cp mytestproject1/myapp1-5c75bff649-hrxkn:/tmp/test.txt test.txt
All the rancher CLI commands can be found here https://rancher.com/docs/rancher/v2.0-v2.4/en/cli/
Make sure you select the correct Rancher version in the documentation. I'm running 2.3 a bit old version. So this article is based on that.
P.S. Of course if you have the access to the Kubernetes cluster nodes, you can SSH into one of the nodes and run any kubectl commands from there. However, some organizations may limit the physical SSH access to the nodes. In that case, this is a better way to do it.
Comments
Post a Comment