简体   繁体   English

无法在 dockerized jenkins 中运行 kubectl

[英]Unable to run kubectl inside dockerized jenkins

I have installed jenkins using docker like -我已经使用 docker 安装了 jenkins,例如 -

docker network create jenkins

docker volume create jenkins-docker-certs
docker volume create jenkins-data

docker image pull docker:dind

docker image pull jenkinsci/blueocean

docker container run --name jenkins-docker \
  --restart unless-stopped \
  --detach \
  --privileged --network jenkins \
  --network-alias docker \
  --env DOCKER_TLS_CERTDIR=/certs \
  --volume jenkins-docker-certs:/certs/client \
  --volume jenkins-data:/var/jenkins_home \
  --publish 2376:2376\
  docker:dind

docker container run --name jenkins-blueocean \
  --restart unless-stopped \
  --detach \
  --network jenkins \
  --env DOCKER_HOST=tcp://docker:2376 \
  --env DOCKER_CERT_PATH=/certs/client \
  --env DOCKER_TLS_VERIFY=1 \
  --volume jenkins-data:/var/jenkins_home \
  --volume jenkins-docker-certs:/certs/client:ro \
  --publish 8080:8080 \
  --publish 50000:50000 \
jenkinsci/blueocean

Now I want to use kubectl inside jenkins, so I added kubernetes-cli plugin and installed kubectl as mentioned here .现在我想在 jenkins 中使用 kubectl,所以我添加了kubernetes-cli插件并安装了 kubectl,如这里所述 I have my jenkins file as (kubecreds in kube config file)我有我的 jenkins 文件(kubecreds in kube config file)

pipeline {
    agent any
    stages {
        stage('Cloning Repo') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'githubcreds', url: '<repo url>']]])
            }
        }
        stage('List pods') {
            steps {
                withKubeConfig([credentialsId: 'kubecreds']) {
                    sh 'curl -LO "https://storage.googleapis.com/kubernetes-release/release/v1.20.5/bin/linux/amd64/kubectl"'
                    sh 'chmod u+x ./kubectl'
                    sh './kubectl get pods -n stage'
                }
            }
        }
    }
}

But running this jenkins file throws error -但是运行这个 jenkins 文件会引发错误 -

+ ./kubectl get pods -n stage
Unable to connect to the server: getting credentials: exec: executable aws not found

It looks like you are trying to use a client-go credential plugin that is not installed.

To learn more about this feature, consult the documentation available at:
      https://kubernetes.io/docs/reference/access-authn-authz/authentication/#client-go-credential-plugins
[Pipeline] }
[kubernetes-cli] kubectl configuration cleaned up
[Pipeline] // withKubeConfig
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE

I added aws sdk plugin but still same error.我添加了aws sdk 插件,但仍然是同样的错误。 So I thought of installing awscli manually and tried -所以我想手动安装 awscli 并尝试 -

pipeline {
    agent any
    stages {
        stage('Cloning Repo') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'githubcreds', url: '<repo url>']]])
            }
        }
        stage('List pods') {
            steps {
                withKubeConfig([credentialsId: 'kubecreds']) {
                    sh 'curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"'
                    sh 'unzip awscliv2.zip'
                    sh './aws/install --update -i . -b .'
                    sh './aws --version'
                    sh 'curl -LO "https://storage.googleapis.com/kubernetes-release/release/v1.20.5/bin/linux/amd64/kubectl"'
                    sh 'chmod u+x ./kubectl'
                    sh './kubectl get pods -n stage'
                }
            }
        }
    }
}

but got error -但有错误 -

+ ./aws/install --update -i . -b .
Found same AWS CLI version: ./v2/2.3.2. Skipping install.
[Pipeline] sh
+ ./aws --version
/var/jenkins_home/workspace/callbreak-deploy-job@tmp/durable-b3361486/script.sh: line 1: ./aws: Permission denied
[Pipeline] }
[kubernetes-cli] kubectl configuration cleaned up
[Pipeline] // withKubeConfig
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 126
Finished: FAILURE

Any ideas on how I can make it work, ie.关于如何使其工作的任何想法,即。 run kubectl successfully inside jenkins job?在 jenkins 工作中成功运行kubectl

Thanks谢谢

------ EDIT 1 ------编辑 1

As recommended in the answer below, I tried making aws executable -正如下面的答案中所建议的,我尝试使aws可执行 -

pipeline {
    agent any
    stages {
        stage('Cloning Repo') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'githubcreds', url: '<repo url>']]])
            }
        }
        stage('List pods') {
            steps {
                withKubeConfig([credentialsId: 'kubecreds']) {
                    sh 'rm awscliv2.zip'
                    sh 'rm -rf aws'
                    sh 'curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"'
                    sh 'unzip awscliv2.zip'
                    sh './aws/install --update -i . -b .'
                    sh 'chmod u+x ./aws'
                    sh './aws --version'
                    sh 'curl -LO "https://storage.googleapis.com/kubernetes-release/release/v1.20.5/bin/linux/amd64/kubectl"'
                    sh 'chmod u+x ./kubectl'
                    sh './kubectl get pods -n stage'
                }
            }
        }
    }
}

but still same error -但仍然是同样的错误 -

+ ./aws/install --update -i . -b .
Found same AWS CLI version: ./v2/2.3.2. Skipping install.
[Pipeline] sh
+ chmod u+x ./aws
[Pipeline] sh
+ ./aws --version
/var/jenkins_home/workspace/callbreak-deploy-job@tmp/durable-cf2a75a8/script.sh: line 1: ./aws: Permission denied
[Pipeline] }
[kubernetes-cli] kubectl configuration cleaned up
[Pipeline] // withKubeConfig
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 126
Finished: FAILURE

Error is from AWS cli it's have permission错误来自 AWS cli 它有权限

you should run你应该跑

chmod +x /usr/bin/aws*

or或者

sh 'chmod u+x ./aws'

or binary give permission of AWS cli to execute wherever it's installed.或二进制文件允许AWS cli在它安装的任何地方执行。

as you are doing it for正如你所做的那样

sh 'chmod u+x ./kubectl'

I was finally able to run kubectl in jenkins after creating a custom jenkins docker image (with kubectl and aws cli already installed) and using aws plugins.在创建自定义 jenkins docker 映像(已安装kubectlaws cli)并使用 aws 插件后,我终于能够在jenkins中运行kubectl

My dockerfile :我的dockerfile

FROM jenkins/jenkins:2.303.2-jdk11
USER root
RUN apt-get update && apt-get install -y apt-transport-https \
       ca-certificates curl gnupg2 \
       software-properties-common
RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
RUN apt-key fingerprint 0EBFCD88
RUN add-apt-repository \
       "deb [arch=amd64] https://download.docker.com/linux/debian \
       $(lsb_release -cs) stable"
RUN apt-get update && apt-get install -y docker-ce-cli
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
RUN unzip awscliv2.zip
RUN ./aws/install
RUN curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
RUN chmod +x kubectl
RUN mv ./kubectl /usr/local/bin/kubectl
USER jenkins
RUN aws --version
RUN kubectl version --client
RUN jenkins-plugin-cli --plugins "blueocean:1.25.0 docker-workflow:1.26"

and my new jenkinsfile :和我的新jenkinsfile

pipeline {
    agent any
    stages {
        stage('Cloning Repo') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'githubcreds', url: '<repo url>']]])
            }
        }
        stage('List pods') {
            steps {
                withAWS([credentials: 'awscreds']) {
                    sh 'aws eks --region ap-south-1 update-kubeconfig --name <name>'
                    sh 'kubectl apply -f deploy/stage/$service.yaml'
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS 上的 Dockerized Jenkins 服务器 - Dockerized Jenkins server on AWS 无法在单个AWS实例上运行SonarQube和Jenkins - Unable to run SonarQube and Jenkins on single instance of AWS 无法使用JenkinsCI插件在Jenkins中运行Amazon Device Farm构建 - Unable to run Amazon Device Farm build in Jenkins using JenkinsCI Plugin AWS ECS上的dockerized Jenkins的DNS问题和入站规则 - DNS problem and inbound rules for dockerized Jenkins on AWS ECS Kubectl 在 EKS 上创建后无法连接到服务器 - Kubectl unable to connect to server after creation on EKS 我想每天在 AWS 上运行一次 dockerized 脚本 - I want to run a dockerized script once every day on AWS kubectl get 节点无法连接到 AWS EC2 实例上的服务器 - kubectl get nodes unable to connect to the server on AWS EC2 Instance 在 AWS VPC 中运行 Jenkins 主从架构的最佳方式? - Best Way to Run Jenkins Master-Worker Architecture inside the AWS VPC? Kubectl 命令抛出错误:无法连接到服务器:获取凭据:执行:退出状态 2 - Kubectl command throwing error: Unable to connect to the server: getting credentials: exec: exit status 2 无法使用Jenkins和SES发送电子邮件 - Unable to send email using Jenkins and SES
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM