Search code examples
shellkubernetessshcron

How to run Shell script having ssh commands using cron job in kubernetes


I want to run shell script which are having ssh commands in the file using cron job in the kubernetes

test.sh

#!/bin/bash

echo copying files from edge node to batch pods;
ssh -q userid@<hostname>"kubectl config use-context testnamespace";
ssh -q userid@<hostname>"kubectl scale statefulsets testpod --replicas=1";

when I executing it manually by going pod, it is throwing me error "ssh command not found" and by triggering job throwing me permission denied message.

can anyone help me how to resolve this issue.


Solution

  • This message (ssh command not found) indicates a lack of ssh client in your pod. In the comments, you are mentioning docker so I assume you are using your docker image.

    To install SSH in your docker container you have to run apt-get or yum or any other package manager according to our Linux Distribution and this needs to be expressed in your Dockerfile.

    Here is an example of how to achieve this:

    # A basic apache server. To use either add or bind mount content under /var/www
    
    FROM ubuntu:12.04
    
    MAINTAINER Kimbro Staken version: 0.1
    
    RUN apt-get update && apt-get install -y apache2 && apt-get clean && rm -rf /var/lib/apt/lists/*
    
    ENV APACHE_RUN_USER www-data
    
    ENV APACHE_RUN_GROUP www-data
    
    ENV APACHE_LOG_DIR /var/log/apache2
    
    EXPOSE 80
    
    CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]
    

    In this example, we are installing apache on a ubuntu image. In your scenario you need to run something similar to this:

    RUN apt-get update && apt-get install -y openssh-client && apt-get clean && rm -rf /var/lib/apt/lists/*