3

I have my jenkins running on EC2 and installed kubectl. I've also configured kubectl to communicate with my EKS cluster in AWS.

Now, I'm trying to automate the deployment of our application to EKS using Jenkins pipeline. But when jenkins reaches this code below

sh "kubectl apply -f platform_api_deployment.yml"

It's showing me this error

/var/lib/jenkins/workspace/platform-api-pipeline/kubernetes@tmp    
/durable-2696d974/script.sh: line 1: kubectl: command not found

I've tried deploying it manually and it works fine.

prix
  • 123
  • 2
  • 13
  • It looks like `kubectl` command cannot be found on the system you run your script in. "I've tried deploying it manually and it works fine." - do you mean that it works when you run the script manually, not via jenkins ? Did you try to provide full path to `kubectl` ? Doesn't it help ? – mario Aug 30 '19 at 10:24
  • Check [this](https://stackoverflow.com/questions/57643659/npm-command-is-not-found-when-ssh-with-bitbucket-pipelines-on-shared-hosting/57648458#57648458) link. It should help. – Technext Aug 30 '19 at 11:19

4 Answers4

3

Basically you need a kubeconfig file to communicate with the API Server. Therefore, You just need login to the server where jenkins is installed, then switch to the user "may be root" from where you can execute kubectl command and execute below commands manually on the shell. These command will copy the kubeconfig to the jenkins user root directory. After that re-run jenkins pipeline.

sudo cp ~/.kube/config ~jenkins/.kube/
sudo chown -R jenkins: ~jenkins/.kube/
Nitin G
  • 714
  • 7
  • 31
1

You need to install kubectl on EC2 instance (follow this link: https://kubernetes.io/docs/tasks/tools/install-kubectl/).

Once it is installed you have to export the path: https://opensource.com/article/17/6/set-path-linux

or you use full path like : sh "~/place/where/kubectl/installed/kubectl apply -f platform_api_deployment.yml"

Rohith
  • 327
  • 4
  • 12
  • 1
    I've already installed the kubectl under /usr/local/bin. what I did was, installed it under /usr/bin to make it globally available. – prix Sep 08 '19 at 23:28
0

Are you running jenkins slave on the different Machine / EC2 ? I think you need to install the kubectl on the slave if it's different with your master node

Fauzan
  • 654
  • 1
  • 7
  • 15
0

You need to add the kubectl binary in dockerfile while building image in order to execute during jenkins job.

COPY kubectl /bin/kubectl 

Add this value in your docker file, once you get the kubectl binaries.

Aimery
  • 1,559
  • 1
  • 19
  • 24
  • 1
    You are assuming kubectl is in the current directory, but the location will depend on how you got it installed. – pablochacin Sep 08 '20 at 21:29