Script to copy data from cluster local to pod is neither working nor giving any error

5/25/2021

The bash script I'm trying to run on the K8S cluster node from a proxy server is as below:

#!/usr/bin/bash
cd /home/ec2-user/PVs/clear-nginx-deployment
for file in $(ls)
do
    kubectl -n migration cp $file clear-nginx-deployment-d6f5bc55c-sc92s:/var/www/html
done

This script is not copying data which is therein path /home/ec2-user/PVs/clear-nginx-deployment of the master node.

But it works fine when I try the same script manually on the destination cluster.

I am using python's paramiko.SSHClient() for executing the script remotely:

def ssh_connect(ip, user, password, command, port):
    try:
        client = paramiko.SSHClient()

        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(ip, username=user, password=password, port=port)

        stdin, stdout, stderr = client.exec_command(command)

        lines = stdout.readlines()

        for line in lines:
            print(line)

    except Exception as error:
        filename = os.path.basename(__file__)
        error_handler.print_exception_message(error, filename)

    return

To make sure the above function is working fine, I tried another script:

#!/usr/bin/bash
cd /home/ec2-user/PVs/clear-nginx-deployment
mkdir kk

This one runs fine with the same python function, and creates the directory 'kk' in desired path. If you could please suggest the reason behind, or suggest an alternative to carry out this. Thank you in advance.

-- kkpareek
kubernetes
remote-access
scripting
shell

1 Answer

5/28/2021

The issue is now solved.

Actually, the issue was related to permissions which I got to know later. So what I did to resolve is, first scp the script to remote machine with:

scp script.sh user@ip:/path/on/remote

And then run the following command from the local machine to run the script remotely:

sshpass -p "passowrd" ssh user@ip "cd /path/on/remote ; sudo su -c './script.sh'"

And as I mentioned in question, I am using python for this.

I used the system function in os module of python to run the above commands on my local to both:

  1. scp the script to remote:
import os

command = "scp script.sh user@ip:/path/on/remote"
os.system(command)
  1. scp the script to remote:
import os

command = "sshpass -p \"passowrd\" ssh user@ip \"cd /path/on/remote ; sudo su -c './script.sh'\""
os.system(command) 
-- kkpareek
Source: StackOverflow