四、基于java包方式运行jenkins应用
基于java命令,运行java war包或jar包,本次以jenkins.war 包部署方式为例,且要求jenkins的数据保存至外部存 储(NFS或者PVC),其他java应用看实际需求是否需要将数据保存至外部存储。 [root@localhost7C jenkins]# ll -rwxr-xr-x 1 root root 226 4月 3 15:26 build-command.sh -rw-r--r-- 1 root root 266 4月 3 15:26 Dockerfile -rw-r--r-- 1 root root 77335220 4月 7 2020 jenkins-2.164.3.war -rw-r--r-- 1 root root 78245883 4月 7 2020 jenkins-2.190.1.war -rw-r--r-- 1 root root 66288019 3月 25 2020 jenkins-2.222.1.war -rwxr-xr-x 1 root root 152 4月 9 2020 run_jenkins.sh [root@localhost7C jenkins]# cat run_jenkins.sh #!/bin/bash cd /apps/jenkins && java -server -Xms1024m -Xmx1024m -Xss512k -jar jenkins-2.222.1.war --webroot=/apps/jenkins/jenkins-data --httpPort=8080 #镜像参考https://www.cnblogs.com/Yuanbangchen/p/17264215.html [root@localhost7C jenkins]# cat Dockerfile #Jenkins Version 2.190.1 FROM harbor.zzhz.com/pub-images/jdk-base:v8.212 MAINTAINER zhangshijie zhangshijie@magedu.net RUN mkdir /apps/jenkins/ -p ADD jenkins-2.222.1.war /apps/jenkins/ ADD run_jenkins.sh /usr/bin/ EXPOSE 8080 CMD ["/usr/bin/run_jenkins.sh"] [root@localhost7C jenkins]# cat build-command.sh #!/bin/bash docker build -t harbor.zzhz.com/linux39/jenkins:v2.222.1 . echo "镜像制作完成,即将上传至Harbor服务器" sleep 1 docker push harbor.zzhz.com/linux39/jenkins:v2.222.1 echo "镜像上传完成" sleep 1 [root@localhost7C jenkins]# chmod +x *.sh [root@localhost7C jenkins]# ./build-command.sh 2.安装nfs服务器,基于PV和PVC作为后端存储 #需要两个PVC,一个保存jenkins的数据,一个保存.jenkins的数据。 [root@localhost7B ]# cat /etc/exports /data/k8sdata *(rw,no_root_squash) [root@localhost7B ~]# mkdir /data/k8sdata/magedu/jenkins-data [root@localhost7B ~]# mkdir /data/k8sdata/magedu/jenkins-root-data [root@localhost7B ]# systemctl restart nfs-server.service #写数据测试 mount -t nfs 192.168.80.110:/data/k8sdata/magedu/redis-datadir-1 /mnt [root@localhost7C jenkins]# tree . ├── jenkins.yaml └── pv ├── jenkins-persistentvolumeclaim.yaml └── jenkins-persistentvolume.yaml [root@localhost7C jenkins]# cat pv/jenkins-persistentvolume.yaml --- apiVersion: v1 kind: PersistentVolume metadata: name: jenkins-datadir-pv namespace: magedu spec: capacity: storage: 120Gi accessModes: - ReadWriteOnce nfs: server: 192.168.80.110 path: /data/k8sdata/magedu/jenkins-data --- apiVersion: v1 kind: PersistentVolume metadata: name: jenkins-root-datadir-pv namespace: magedu spec: capacity: storage: 120Gi accessModes: - ReadWriteOnce nfs: server: 192.168.80.110 path: /data/k8sdata/magedu/jenkins-root-data [root@localhost7C jenkins]# [root@localhost7C jenkins]# [root@localhost7C jenkins]# cat pv/jenkins-persistentvolumeclaim.yaml --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: jenkins-datadir-pvc namespace: magedu spec: volumeName: jenkins-datadir-pv accessModes: - ReadWriteOnce resources: requests: storage: 100Gi --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: jenkins-root-data-pvc namespace: magedu spec: volumeName: jenkins-root-datadir-pv accessModes: - ReadWriteOnce resources: requests: storage: 100Gi [root@localhost7C jenkins]# cat pv/jenkins-persistentvolumeclaim.yaml --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: jenkins-datadir-pvc namespace: magedu spec: volumeName: jenkins-datadir-pv accessModes: - ReadWriteOnce resources: requests: storage: 100Gi --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: jenkins-root-data-pvc namespace: magedu spec: volumeName: jenkins-root-datadir-pv accessModes: - ReadWriteOnce resources: requests: storage: 100Gi [root@localhost7C jenkins]# 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.zzhz.com/linux39/jenkins:v2.222.1 #imagePullPolicy: IfNotPresent imagePullPolicy: Always ports: - containerPort: 8080 protocol: TCP name: http volumeMounts: - mountPath: "/apps/jenkins/jenkins-data/" name: jenkins-datadir-magedu - mountPath: "/root/.jenkins" name: jenkins-root-datadir volumes: - name: jenkins-datadir-magedu persistentVolumeClaim: claimName: jenkins-datadir-pvc - name: jenkins-root-datadir 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 #验证web访问jenkins: