[英]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 映像(已安装kubectl和aws 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.