目录
一、deb包单机部署jenkins
1、安装依赖包
1.1、jenkins依赖jdk,安装jdk
apt install openjdk-11-jdk
1.2、安装依赖包 daemon
apt install daemon
2、下载jenkins包
2.1、进入清华大学镜像源,搜索jenkins ,然后点击jenkins
2.2、点击stable稳定版
2.3、任意选择一个版本下载
2.4、下载jenkins包
root@haproxy02:~# wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/debian-stable/jenkins_2.361.4_all.deb
3、部署jenkins
3.1、解决报错,根据保存提示安装依赖包net-tools ,再部署就OK了
root@haproxy02:~# apt install net-tools
root@haproxy02:~# dpkg -i jenkins_2.361.4_all.deb
3.2、修改配置文件
vim /etc/default/jenkins
把启动用户改成root,因为jenkins用户权限有限
3.3、重启服务
root@haproxy02:~# systemctl status jenkins.service
4、访问jenkins
二、单机war包部署jenkins
1、下载
1.1、进入官网搜索jenkins,然后进入页面点击war-stable
1.2、选择合适的版本下载
1.3、点击一个版本进入下一个页面,下载jenkins.war包,或者右击复制包的下载连接
1.4、下载war包
wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/war-stable/2.361.4/jenkins.war
2、启动war包
java -jar jenkins.war
查看jenkins进程
访问jenkins
三、 jenkins容器化部署
1、首先需要把jenkins的war包放到镜像里
jenkins的父镜像jdk-base:v8.212构建可参见博客
https://blog.csdn.net/weixin_46476452/article/details/128164447
root@master1~# ls build-command.sh Dockerfile jenkins-2.319.2.war run_jenkins.sh root@master1~# cat Dockerfile #使用jdk-base镜像作为父镜像 FROM harbor.magedu.net/pub-images/jdk-base:v8.212 MAINTAINER zhaoyang 2569220198@qq.com #把jenkins.war包传到容器中 ADD jenkins-2.319.2.war /apps/jenkins/jenkins.war #把启动脚本放到容器中 ADD run_jenkins.sh /usr/bin/ EXPOSE 8080 #执行启动脚本 CMD ["/usr/bin/run_jenkins.sh"]
查看启动jenkins的启动脚本
root@master1~# vim run_jenkins.sh #!/bin/bash cd /apps/jenkins && java -server -Xms1024m -Xmx1024m -Xss512k -jar jenkins.war --webroot=/apps/jenkins/jenkins-data --httpPort=8080 #注释:进入/apps/jenkins执行java -jar指定些参数执行war包,并指定工作目录/apps/jenkins/jenkins-data ;这个目录需要挂载到存储
2、构建镜像
root@master1~# nerdctl build -t harbor.magedu.net/magedu/jenkins:v2.319.2 .
3、上传镜像
root@master1:~# nerdctl push harbor.magedu.net/magedu/jenkins:v2.319.2
4、创建PV
注:jenkins服务容器化后有两个目录是需要做持久化的
单体jenkins.war包部署的单体jenkins服务会有两个数据目录分别是/var/lib/jenkins/ 和 /root/.jenkins/,因此容器化过程需要创建2个PV用来存放数据
5、 编写PV的yaml文件
root@master1:/opt/k8s-data/yaml/magedu/jenkins/pv# cat jenkins-persistentvolume.yaml
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: jenkins-datadir-pv
namespace: magedu
spec:
capacity:
storage: 100Gi
accessModes:
- ReadWriteOnce
nfs:
server: 172.31.7.109
path: /data/k8sdata/magedu/jenkins-data
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: jenkins-root-datadir-pv
namespace: magedu
spec:
capacity:
storage: 100Gi
accessModes:
- ReadWriteOnce
nfs:
server: 172.31.7.109
path: /data/k8sdata/magedu/jenkins-root-data
6、手动创建nfs的共享目录
root@haproxy02:~# mkdir -p /data/k8sdata/magedu/jenkins-data
root@haproxy02:~# mkdir -p /data/k8sdata/magedu/jenkins-root-data
赋权
root@haproxy02:~# tail -1 /etc/exports
/data/k8sdata *(rw,no_root_squash)
root@haproxy02:~# exportfs -arv
7、创建PV资源
root@haproxy02:~# kubectl apply -f jenkins-persistentvolume.yaml
8、PV创建好了,再创建PVC
root@master1:~# vim jenkins-persistentvolumeclaim.yaml
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: jenkins-datadir-pvc
namespace: magedu
spec:
volumeName: jenkins-datadir-pv # jenkins-datadir-pvc建立在这个PV上
accessModes: #访问模式
- ReadWriteOnce #访问模式是被单个节点以读写权限挂载
resources:
requests:
storage: 80Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: jenkins-root-data-pvc
namespace: magedu
spec:
volumeName: jenkins-root-datadir-pv #jenkins-root-data-pvc建立在这个PV上
accessModes: #访问模式
- ReadWriteOnce #访问模式是被单个节点以读写权限挂载
resources:
requests:
storage: 80Gi
root@master1:~# kubectl apply -f jenkins-persistentvolumeclaim.yaml
9、 显示bound绑定成功即可
10、存储都创建好了,下面开始创建jenkins容器了
root@master1~# cat jenkins.yaml
kind: Deployment
#apiVersion: extensions/v1beta1
apiVersion: apps/v1
metadata:
labels:
app: magedu-jenkins
name: magedu-jenkins-deployment
namespace: magedu
spec:
replicas: 1
selector:
matchLabels:
app: magedu-jenkins
template:
metadata:
labels:
app: magedu-jenkins
spec:
containers:
- name: magedu-jenkins-container
image: harbor.magedu.net/magedu/jenkins:v2.319.2
#imagePullPolicy: IfNotPresent
imagePullPolicy: Always
ports:
- containerPort: 8080
protocol: TCP
name: http
volumeMounts: #把jenkins产生数据的两个目录挂载到了存储上,以后即使容器删除了,数据也不会丢失
- mountPath: "/apps/jenkins/jenkins-data/" #把jenkins-datadir-magedu挂载到容器的这个目录下,就相当于单体jenkins的/var/lib/jenkins目录挂载到这里
name: jenkins-datadir-magedu
- mountPath: "/root/.jenkins" #把jenkins-root-datadir目录挂载到容器的/root/.jenkins目录下,相当于单体jenkins的/root/.jenkins目录挂载到这里
name: jenkins-root-datadir
volumes:
- name: jenkins-datadir-magedu #jenkins-datadir-magedu这个目录就相当于单体jenkins的/var/lib/jenkins目录
persistentVolumeClaim:
claimName: jenkins-datadir-pvc
- name: jenkins-root-datadir #jenkins-root-datadir这个目录就相当于单体jenkins的/root/.jenkins目录
persistentVolumeClaim:
claimName: jenkins-root-data-pvc
---
kind: Service
apiVersion: v1
metadata:
labels:
app: magedu-jenkins
name: magedu-jenkins-service
namespace: magedu
spec:
type: NodePort
ports:
- name: http
port: 80
protocol: TCP
targetPort: 8080
nodePort: 38080
selector:
app: magedu-jenkins
root@master1~# kubectl apply -f jenkins.yaml
查看pod状态running,运行在7.111节点上
四、访问jenkins
进入jenkins容器中看下密码
root@master1:~# kubectl exec -it magedu-jenkins-deployment-595c995d9d-bd4dw bash -n magedu
[root@magedu-jenkins-deployment-595c995d9d-bd4dw /]# cat /root/.jenkins/secrets/initialAdminPassword
df187932c6ab494dbb7bc3f16f4957dd
现在jenkins存放插件的目录中已经有一些数据了,安装完插件的话数据会更多
五、jenkins新建任务
1、进入jenkins后,点击new ltem新建个任务
2、输入新建任务名称,再选择自由风格的项目,最后点击OK
3、选择build构建-->Execute shell执行shell,调试下jenkins是否可用
4、写个命令然后保存用来调试
5、保存后点击build now构建一下
6、点击完构建后,查看下任务
7、点击console output控制台输出,查看结果是否OK,这里是OK的