Skip to main content

How to access a container in Rancher from the command line

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.

An application pod in rancher

Using the Web UI


One of the ways to access the container would be using the Rancher UI. If you click on the pod name and then on the next page, you can select Execute Shell from the Pod's context menu.

Rancher Pod context menu


Then it is supposed to bring the command window in the web UI itself.

Rancher execute shell



However, this method has its limitations. You cannot copy files to or from the Pod using the web UI itself. 

Using Rancher CLI

The link to the CLI package can be found in the bottom right corner of the Rancher web UI.


Select the CLI client according to your OS and download.

I'm using Linux, so I've downloaded the Linux client. 


Extract it.

tar -xf rancher-linux-amd64-v2.3.2.tar.gz

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>
eg:
./rancher login https://myrancherhost:8443 --token token-9ls7g:4xzprvtn2ghgxkmsh6d5jz4txb6m5mpplm7h62kl5ptnfx6b6bt5wp

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

Popular posts from this blog

VLC skins

It's been a while from my first blog post, so today I thought to make a blog post about skins in VLC player. I think you all are familiar with VLC player if not, here's the link for the website www.videolan.org/vlc/index.htm l In brief, VLC player is a free and opensource player that can play almost all the types of media formats. Even though it is a such a nice player, it looks kind of old and rusty, like the windows classic look. But you can make it prettier by just adding some new skins to it.  You can download skins for the player from here http://www.videolan.org/vlc/skins.php Then place the downloaded skin file (.vlt file) in the " skin " folder in the installation directory.           eg:- in windows:    C:\Program Files\VideoLAN\VLC\skins           or in linux systems:  ~/.local/share/vlc/skins Then, open the VLC player and go to the preferences. ...

Java, how to create a list with a single element

 I wanted to create a Java List with a single element. Yet, I wanted to add more elements later. So, I was looking for a couple of ways to do this. So far there are multiple elegant ways to create a list with a single element with a one-liner. Not so much for a modifiable list though. Here's what I gathered so far. Followings are a few ways of creating a list with strictly a single entry. Can't add more elements. 1. Collections.singletonList() This returns an immutable list that cannot be modified or add more elements. // An immutable list containing only the specified object. List<String> oneEntryList = Collections. singletonList ( "one" ) ; oneEntryList.add( "two" ) ; // throws UnsupportedOperationException 2.  Arrays.asList() This returns a fixed-size list consisting of the number of elements provided as arguments. The number of elements provided would be an array hence the size is fixed to the length of the array. // Returns a fixed-size list List...

Find command: How to find and delete 0 bytes files in Linux

In Linux, "find" command is a powerful command that we can use not only to find files. I've used it for different requirements, so thought of posting some of the convenient usages of find commands in a series of posts. For some application issue, I've got one of my directories in Ubuntu system swarmed with empty files with 0 bytes. Some of the files got data in them but some of them were empty. I just wanted to delete the empty files and it wasn't possible to identify them by file names or dates. So find command comes to rescue. find . -size 0 -type f  If I needed to isolate the files by file extension, we can use something like this. find . -size 0 -type f -name "*.tar" You can get the file count by piping it to wc command. find . -size 0 -type f -name "*.tar" | wc -l The awesome thing about the find command is you can use it for deleting files just by adding the -delete flag to the command. find . -size 0 -type f -name "*.tar" -dele...