【发布时间】: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