【问题标题】:Unable to run kubectl inside dockerized jenkins无法在 dockerized jenkins 中运行 kubectl
【发布时间】:2021-12-15 00:25:54
【问题描述】:

我已经使用 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

现在我想在 jenkins 中使用 kubectl,所以我添加了 kubernetes-cli 插件并按照 here 安装了 kubectl。我的 jenkins 文件为(kube 配置文件中的 kubecreds)

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'
                }
            }
        }
    }
}

但是运行这个 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

我添加了aws sdk plugin,但仍然是同样的错误。所以我想到了手动安装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'
                }
            }
        }
    }
}

但出现错误 -

+ ./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

关于如何使它发挥作用的任何想法,即。在 jenkins 作业中成功运行 kubectl

谢谢

------ 编辑1

按照以下答案中的建议,我尝试使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'
                }
            }
        }
    }
}

但还是同样的错误 -

+ ./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

【问题讨论】:

    标签: amazon-web-services docker jenkins kubernetes


    【解决方案1】:

    错误来自 AWS cli 它有权限

    你应该跑

    chmod +x /usr/bin/aws*
    

    sh 'chmod u+x ./aws'
    

    或二进制授予AWS cli 的权限,以便在安装的任何地方执行。

    因为你正在这样做

    sh 'chmod u+x ./kubectl'
    

    【讨论】:

    • 对我不起作用 :( 得到同样的错误。
    • 将该命令放在安装上方,然后解压缩您正在运行的命令。先更改权限,然后运行 ​​./aws install
    【解决方案2】:

    在创建自定义 jenkins docker 映像(已安装 kubectlaws cli)后,我终于能够在 jenkins 中运行 kubectl 并且使用 aws 插件。

    我的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"
    

    还有我的新 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'
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-24
      • 2021-12-13
      • 2018-05-12
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多