簡體   English   中英

無法在 dockerized jenkins 中運行 kubectl

[英]Unable to run kubectl inside dockerized jenkins

我已經使用 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插件並安裝了 kubectl,如這里所述 我有我的 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'
                }
            }
        }
    }
}

但是運行這個 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 插件,但仍然是同樣的錯誤。 所以我想手動安裝 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

錯誤來自 AWS cli 它有權限

你應該跑

chmod +x /usr/bin/aws*

或者

sh 'chmod u+x ./aws'

或二進制文件允許AWS cli在它安裝的任何地方執行。

正如你所做的那樣

sh 'chmod u+x ./kubectl'

在創建自定義 jenkins docker 映像(已安裝kubectlaws cli)並使用 aws 插件后,我終於能夠在jenkins中運行kubectl

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM