Based on the dynamic jenkins slave kubernetes

                          **基于 Jenkins 的 CI/CD**

If it wants to create a deployment, then we would let him create a namespace

kubectl create namespace kube-ops

Then we create a deployment

Based on the dynamic jenkins slave kubernetes
Based on the dynamic jenkins slave kubernetes

Here we use a program called jenkins / jenkins: lts of the mirror, which is jenkins official Docker mirror, and then there are some environment variables, of course, we can also customize a mirror according to their needs, such as we move some plug-ins packaged in self defined image which can refer to the document: https://github.com/jenkinsci/docker, here we use the default official mirror on the line, the other is also important to note that our container / var / jenkins_home directory is mounted to a PVC opspvc object named above, so we also have to create a PVC objects corresponding advance

Then we create pvc (pvc.yaml) for him
Based on the dynamic jenkins slave kubernetes

For testing we used to do persistent storage nfs

Then we create the pvc objects.

kubectl apply -f pvc.yaml

Here we also need to serviceAccount a have the relevant permissions: jenkins2, we are here just to jenkins given the necessary permissions, of course, if you permission to serviceAccount not very familiar with it, we give sa bind a cluster-admin of cluster role privilege is also possible, of course, this has a certain security risks: (rbac.yaml)

Based on the dynamic jenkins slave kubernetesBased on the dynamic jenkins slave kubernetes

kubectl apply -f rbac.yaml

In order to facilitate our testing, we here by NodePort (later will use traefik, nginx-ingress) in the form of web services to expose Jenkins, fixed at 30005 port, also need to expose a port agent. This port is mainly used for Jenkins' between master and slave communications.

一切准备的资源准备好过后,我们直接创建 Jenkins 服务:

kubectl apply -f jenkins2.yaml

我们首次创建的时候

Based on the dynamic jenkins slave kubernetes

可以看到该 Pod 处于 Running 状态,但是 READY 值确为0,然后我们用 describe 命令去查看下该 Pod 的详细信息:

Based on the dynamic jenkins slave kubernetes

可以看到上面的 Warning 信息,健康检查没有通过,具体原因是什么引起的呢?可以通过查看日志进一步了解:

Based on the dynamic jenkins slave kubernetes

很明显可以看到上面的错误信息,意思就是我们没有权限在 jenkins 的 home 目录下面创建文件,这是因为默认的镜像使用的是 jenkins 这个用户,而我们通过 PVC 挂载到 nfs 服务器的共享数据目录下面却是 root 用户的,所以没有权限访问该目录,要解决该问题,也很简单,我只需要在 nfs 共享数据目录下面把我们的目录权限重新分配下即可:

chown -R 1000 /data/k8s/jenkins2

然后我们重建

kubectl delete jenkins2.yaml
kubectl apply -f jenkins2.yaml

Based on the dynamic jenkins slave kubernetes

你就可以看到这个pods的状态已经是正常的了。

我们就可以通过任意节点的 IP:30005 端口就可以访问 jenkins 服务了,可以根据提示信息进行安装配置即可:

Based on the dynamic jenkins slave kubernetes

到了这个页面,我们就可以去pod里面获取密码了,或者在日志里面查看密码

kubectl exec -it jenkins2-6bbb7d9f4c-v9cfd -n kube-ops bash

然后根据上面的提示获取密码。
Based on the dynamic jenkins slave kubernetes

然后就看到了jenkins的主页面了

先不要着集使用

我们知道持续构建与发布是我们日常工作中必不可少的一个步骤,目前大多公司都采用 Jenkins 集群来搭建符合需求的 CI/CD 流程,然而传统的 Jenkins Slave 一主多从方式会存在一些痛点,比如:

主 Master 发生单点故障时,整个流程都不可用了
每个 Slave 的配置环境不一样,来完成不同语言的编译打包等操作,但是这些差异化的配置导致管理起来非常不方便,维护起来也是比较费劲
资源分配不均衡,有的 Slave 要运行的 job 出现排队等待,而有的 Slave 处于空闲状态
资源有浪费,每台 Slave 可能是物理机或者虚拟机,当 Slave 处于空闲状态时,也不会完全释放掉资源。

正因为上面的这些种种痛点,我们渴望一种更高效更可靠的方式来完成这个 CI/CD 流程,而 Docker 虚拟化容器技术能很好的解决这个痛点,又特别是在 Kubernetes 集群环境下面能够更好来解决上面的问题,下图是基于 Kubernetes 搭建 Jenkins 集群的简单示意图:

Based on the dynamic jenkins slave kubernetes

那么我们使用这种方式带来了哪些好处呢?

服务高可用,当 Jenkins Master 出现故障时,Kubernetes 会自动创建一个新的 Jenkins Master 容器,并且将 Volume 分配给新创建的容器,保证数据不丢失,从而达到集群服务高可用。
动态伸缩,合理使用资源,每次运行 Job 时,会自动创建一个 Jenkins Slave,Job 完成后,Slave 自动注销并删除容器,资源自动释放,而且 Kubernetes 会根据每个资源的使用情况,动态分配 Slave 到空闲的节点上创建,降低出现因某节点资源利用率高,还排队等待在该节点的情况。
扩展性好,当 Kubernetes 集群的资源严重不足而导致 Job 排队等待时,可以很容易的添加一个 Kubernetes Node 到集群中,从而实现扩展。

配置

接下来我们就需要来配置 Jenkins,让他能够动态的生成 Slave 的 Pod。

我们先进去到插件管理

Based on the dynamic jenkins slave kubernetes

因为我已经安装了,你们只需要在可选插件里面选择kubernetes安装就好了。

安装好了之后,我们到系统管理的系统配置,拖到最下方,选择kubernetes

Based on the dynamic jenkins slave kubernetes

注意 namespace,我们这里填 kube-ops,然后点击Test Connection,如果出现 Connection test successful 的提示信息证明 Jenkins 已经可以和 Kubernetes 系统正常通信了,然后下方的 Jenkins URL 地址:http://jenkins2.kube-ops.svc.cluster.local:8080,这里的格式为:服务名.namespace.svc.cluster.local:8080,根据上面创建的jenkins 的服务名填写,我这里是之前创建的名为jenkins,如果是用上面我们创建的就应该是jenkins2

另外需要注意,如果这里 Test Connection 失败的话,很有可能是权限问题,这里就需要把我们创建的 jenkins 的 serviceAccount 对应的 secret 添加到这里的 Credentials 里面。

然后我们配置 Pod Template,其实就是配置 Jenkins Slave 运行的 Pod 模板,命名空间我们同样是用 kube-ops,Labels 这里也非常重要,对于后面执行 Job 的时候需要用到该值,然后我们这里使用的是 cnych/jenkins:jnlp 这个镜像,这个镜像是在官方的 jnlp 镜像基础上定制的,加入了 kubectl 等一些实用的工具。

Based on the dynamic jenkins slave kubernetes

后面运行的命令和命令参数我们给他删掉。

Also important to note here that we need to mount the two host directory below, one is /var/run/docker.sock, the document is a container used to be able to share in the Pod Docker host, which is? We say docker in /root/.kube directory docker way, Docker binaries we've packed into top mirror, the other a directory /root/.kube directory, this directory will be mounted to the container which is to enable us to Pod in containers that can be used kubectl tools to access our Kubernetes cluster, easy to deploy behind us Kubernetes application in Slave Pod.

Based on the dynamic jenkins slave kubernetes

When configured to run Slave Pod after the emergence of the rights issue, if the lack of permission problems, where the Slave Pod configuration click on the following high-level, you can add the corresponding ServiceAccount:

Based on the dynamic jenkins slave kubernetes

Here we are Kubernetes Plugin? Plug-in even if the configuration is complete.

Based on the dynamic jenkins slave kubernetes

Then we create a free project.

Based on the dynamic jenkins slave kubernetes

Based on the dynamic jenkins slave kubernetes

Then we save.

We tested the building.

When built, we went to see jenkins state, will create a jnlp of the pod.

Based on the dynamic jenkins slave kubernetes

After building the task is completed, we then see a list of pod, the pod has no jnlp's.

Here we have completed the method Kubernetes of dynamically generated Jenkins Slave.

Guess you like

Origin blog.51cto.com/14181888/2467844