K8s部署Jenkins主从节点并与Gitblit集成
⽬录
前提条件
已安装K8s集
熟悉docker命令和构建docker镜像
⼀、制作Jenkins-master镜像
1. 拉取base镜像
docker pull jenkinsci/jenkins:lts
2. 运⾏base镜像,为便于构建,这⾥打算⼿动运⾏和安装好镜像内容后进⾏docker commit出⼀个新镜像。
docker run -tid -p 8080:8080 –name jenkins-master jenkinsci/jenkins:lts
3.
4. 根据提⽰在容器内获取admin的初始秘钥。
5. 安装推荐的插件(Suggested plugins),如果没有安装Kubernetes插件,则在
系统管理–>管理插件–>可选插件–>输⼊ Kubernetes
选择 Kubernetes plugin–>直接安装
6. 复制kubectl命令到容器中
docker cp kubectl jenkins-master:/usr/bin
7. 安装 libltdl7
docker exec -ti -u root jenkins-master /bin/sh
apt-get update
apt-get install libltdl7
apt-get clean
8. 把Jenkins Home的⽂件拷贝出来,作为在k8s上持久化存放(Gluster或者NFS⽂件系统)。注意:主机上 .kube/config ⽂件也要放
到这个⽬录下以供jenkins的k8s插件与kubernetes交互。
docker cp jenkins-master:/var/jenkins_home ./config/jenkins_home
mkdir -p ./config/jenkins_home/.kube && cp /root/.kube/config ./config/jenkins_home.kube/
9. 如果后续需要在master上构建⼯程,安装所需要的组件(如nodejs、maven、JDK等)。然后提交镜像。
docker commit jenkins-master jenkins-k8s-master:v1
⼆、部署Jenkins-master
环境中master节点⽤Rancher部署,这⾥截取部分yaml内容以供参考:jenkins-deploy.yaml
apiVersion: apps/v1beta2
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "3"
labels:
workload.user.cattle.io/workloadselector: deployment-ci-cd-jenkins-master
name: jenkins-master
namespace: ci-cd
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
workload.user.cattle.io/workloadselector: deployment-ci-cd-jenkins-master
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
annotations:
labels:
workload.user.cattle.io/workloadselector: deployment-ci-cd-jenkins-master
spec:
affinity: {}
containers:
- image: jenkins-k8s-master:v1
imagePullPolicy: IfNotPresent
name: jenkins-master
ports:
- containerPort: 8080
hostPort: 8080
protocol: TCP
- containerPort: 50000
hostPort: 50000
protocol: TCP
resources: {}
volumeMounts:
- mountPath: /var/run/docker.sock
name: dockersock
- mountPath: /var/jenkins_home
name: jenkinshome
- mountPath: /root/.kube
name: kube-config
- mountPath: /usr/bin/docker
name: docker-binary
dnsPolicy: ClusterFirst
nodeName: izwz9ghgtf18pi19ytou0vz
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
volumes:
- hostPath:
path: /var/run/docker.sock
type: ""
name: dockersock
- hostPath:
path: /mnt/jenkins/jenkins_home
type: ""
name: jenkinshome
- hostPath:
path: /root/.kube
type: ""
name: kube-config
-
hostPath:
path: /usr/bin/docker
type: ""
name: docker-binary
jenkins-svc.yaml
apiVersion: v1
kind: Service
metadata:
annotations:
field.cattle.io/targetWorkloadIds: '["deployment:ci-cd:jenkins-master"]'
name: jenkins-master
namespace: ci-cd
spec:
ports:
- name: 8080tcp01-jenkins-master
port: 8080
protocol: TCP
targetPort: 8080
- name: 50000tcp01-jenkins-master
port: 50000
protocol: TCP
targetPort: 50000
selector:
workload.user.cattle.io/workloadselector: deployment-ci-cd-jenkins-master
sessionAffinity: None
type: ClusterIP
三、制作Jenkins-slave镜像
在Dockerfile所在⽬录准备好以下内容
maven仓库的位置po
kubectl可执⾏⽂件
jenkins-slave可执⾏⽂件
需要特殊配置的hosts⽂件
其它构建⼯具如node等
下载base镜像centos
docker pull centos
vi Dockerfile
nodeselectorFROM centos
ARG AGENT_WORKDIR=/root/agent
RUN curl --create-dirs-sSLo /usr/share/jenkins/slave.jar /public/org/jenkins-ci/main/remoting/3.16/remoting-3.16.jar \ && chmod 755 /usr/share/jenkins
po /pos.po
RUN yum -y install git libtool-ltdl java-1.8.0-openjdk apache-maven
RUN rm -rf /var/cache/yum/x86_64/7/*
ENV AGENT_WORKDIR=${AGENT_WORKDIR}
COPY node /usr/local/
COPY kubectl /usr/bin/kubectl
COPY jenkins-slave /usr/local/bin/jenkins-slave
COPY .kube /root/.kube
COPY hosts /tmp/hosts
COPY ojdbc6.jar /root/ojdbc6.jar
RUN mkdir -p -- /lib-override && cp /usr/lib64/libnss_files.so.2 /lib-override
RUN sed -i 's:/etc/hosts:/tmp/hosts:g' /lib-override/libnss_files.so.2
ENV LD_LIBRARY_PATH /lib-override
RUN mkdir /root/.jenkins && mkdir -p ${AGENT_WORKDIR}
ENV JAVA_HOME=/usr/lib/jvm/java-1.8.0
WORKDIR /root
ENTRYPOINT ["jenkins-slave"]
构建slave镜像
docker build -t jenkins-slave:v1 .
四、配置Jenkins与K8s协作
在Jenkins-master的UI界⾯上进⼊
系统管理 > 系统设置 > (最下⽅)新增⼀个云 > 选择Kubernetes
参考下图的配置
添加Pod template
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论