如何使用 Jenkins 部署到(本地)Kubernetes 集群

How to deploy to a (local) Kubernetes cluster using Jenkins

这个问题与 中的一个有点相关,因为它让我更清楚地了解我想要实现的目标。这个问题是关于我 运行 在尝试时遇到的问题完成上一个问题中的任务...

我正在尝试测试我的 kubectl 是否在 Jenkins 容器中工作。当我启动我的 Jenkins 容器时,我使用以下命令:

docker run \ 
    -v /home/student/Desktop/jenkins_home:/var/jenkins_home \
    -v $(which kubectl):/usr/local/bin/kubectl \ #bind docker host binary to docker container binary
    -v ~/.kube:/home/jenkins/.kube \ #docker host kube config file stored in /.kube directory. Binding this to $HOME/.kube in the docker container
    -v /var/run/docker.sock:/var/run/docker.sock \ 
    -v $(which docker):/usr/bin/docker -v ~/.kube:/home/root/.kube \ 
    --group-add 998 
    -p 8080:8080 -p 50000:50000 
    -d --name jenkins jenkins/jenkins:lts

容器启动,我可以 login/create jobs/run 管道脚本都没有问题。

我创建了一个管道脚本来检查我是否可以像这样访问我的集群:

pipeline {
    agent any
    stages {
        stage('Kubernetes test') {
            steps {
                sh "kubectl cluster-info"
            }
        }
    }
}

当 运行 此作业失败并出现以下错误:

+ kubectl cluster-info // this is the step

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
error: the server doesn't have a resource type "services"

谢谢!

我不明白为什么有:

-v $(which kubectl):/usr/local/bin/kubectl -v ~/.kube:/home/jenkins/.kube

/usr/local/bin/kubectl 是一个 kubectl 二进制文件,~/.kube:/home/jenkins/.kube 应该是 kubectl 二进制文件查找集群上下文文件的位置,即 kubeconfig。首先,您应该确保 kubeconfig 已安装到位于 /home/jenkins/.kube 的容器中,并且 kubectl 二进制文件可以访问。在适当的卷挂载后,您可以通过在 jenkins 容器中使用 docker container exec -it jenkins /bin/bash 创建会话并使用 kubectl get svc 进行测试来进行验证。确保在会话中设置了 KUBECONFIG 环境变量:

export KUBECONFIG=/home/jenkins/.kube/kubeconfig

在你运行验证测试和

之前
withEnv(["KUBECONFIG=$HOME/.kube/kubeconfig"]) {
// Your stuff here
}

在您的管道代码中。如果它适用于会话,它也应该适用于管道。

我个人建议为 Jenkins 创建自定义 Docker 映像,其中将包含 kubectl 二进制文件和其他必要的实用程序(例如 aws-iam-authenticator 用于 AWS EKS IAM-based身份验证)用于与 Kubernetes 集群一起工作。这会在您的主机系统二进制文件和您的 Jenkins 二进制文件之间创建隔离。

下面是我正在使用的 Dockerfile,其中包含 helmkubectlaws-iam-authenticator

# This Dockerfile contains Helm, Docker client-only, aws-iam-authenticator, kubectl with Jenkins LTS.

FROM jenkins/jenkins:lts
USER root

ENV VERSION v2.9.1
ENV FILENAME helm-${VERSION}-linux-amd64.tar.gz
ENV HELM_URL https://storage.googleapis.com/kubernetes-helm/${FILENAME}
ENV KUBE_LATEST_VERSION="v1.11.0"

# Install the latest Docker CE binaries
RUN apt-get update && \
    apt-get -y install apt-transport-https \
      ca-certificates \
      curl \
      gnupg2 \
      software-properties-common && \
    curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; echo "$ID")/gpg > /tmp/dkey; apt-key add /tmp/dkey && \
    add-apt-repository \
      "deb [arch=amd64] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") \
      $(lsb_release -cs) \
      stable" && \
   apt-get update && \
   apt-get -y install docker-ce \
   && curl -o /tmp/$FILENAME ${HELM_URL} \
   && tar -zxvf /tmp/${FILENAME} -C /tmp \
   && mv /tmp/linux-amd64/helm /bin/helm \
   && rm -rf /tmp/linux-amd64/helm \
   && curl -L https://storage.googleapis.com/kubernetes-release/release/${KUBE_LATEST_VERSION}/bin/linux/amd64/kubectl -o /usr/local/bin/kubectl \
   && chmod +x /usr/local/bin/kubectl \
   && curl -L https://amazon-eks.s3-us-west-2.amazonaws.com/1.11.5/2018-12-06/bin/linux/amd64/aws-iam-authenticator -o /usr/local/bin/aws-iam-authenticator \
   && chmod +x /usr/local/bin/aws-iam-authenticator

这是我在 windows 机器上本地安装的 jenkins 的解决方案。