8.3 k8s部署jenkins,通过pv/pvc结合NFS服务器持久化

1.制作jenkins docker镜像

1.1 下载jenkins

wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/war-stable/2.303.2/jenkins.war

1.2 准备JDK(版本>=1.8)

jdk-8u192-linux-x64.tar.gz

1.3 编写Dockerfile

基础镜像使用的是centos7

FROM 192.168.1.110/base/centos:7
LABEL maintainer="yanql<yanqianling@faxuan.net>"

ADD jdk-8u192-linux-x64.tar.gz /usr/local/
ADD jenkins.war /apps/jenkins/
ADD run_jenkins.sh /usr/bin/

ENV JAVA_HOME /usr/local/jdk1.8.0_192
ENV PATH ${PATH}:${JAVA_HOME}/bin

EXPOSE 8080

CMD ["/usr/bin/run_jenkins.sh"]

1.4 run_jenkins.sh脚本

#!/bin/sh
cd /apps/jenkins && java -server -Xms1024m -Xmx1024m -Xss512k -jar jenkins-2.303.2.war --webroot=/apps/jenkins/jenkis-data --httpPort=8080
tail -f /etc/hosts

1.5 生成镜像

docker build -t 192.168.1.110/web/jenkins-2.303.2:v20211023-1211 .

1.6 测试镜像

docker run -d -p 8080:8080 192.168.1.110/web/jenkins-2.303.2:v20211023-1211

浏览器访问测试
image

1.7 将镜像上传至本地harbor

docker push 192.168.1.110/web/jenkins-2.303.2:v20211023-1211

2.为jenkins数据存储持久化部署创建PV/PVC

2.1 nfs 创建相关目录

jenkins会使用一下2个目录存储数据 '/apps/jenkins/jenkis-data','/root/.jenkins'

mkdir -p /data/k8s-data/jenkins/datadir-pv
mkdir -p /data/k8s-data/jenkins/root-datadir-pv

2.2 编写pv/pvc yaml

kind: PersistentVolume
metadata:
  name: jenkins-datadir-pv
spec:
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  nfs:
    server: 192.168.2.10
    path: /data/k8s-data/jenkins/datadir-pv
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: jenkins-root-datadir-pv
spec:
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  nfs:
    server: 192.168.2.10
    path: /data/k8s-data/jenkins/root-datadir-pv

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: jenkins-datadir-pvc
  namespace: yan-test
spec:
  accessModes:
    - ReadWriteOnce
  volumeName: jenkins-datadir-pv
  resources:
    requests:
      storage: 10Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: jenkins-root-datadir-pvc
  namespace: yan-test
spec:
  accessModes:
    - ReadWriteOnce
  volumeName: jenkins-root-datadir-pv
  resources:
    requests:
      storage: 10Gi

2.3 创建pv/pvc并查看

# 创建
kubectl apply -f pv/jenkin-pv.yaml

# 查看
root@k8-deploy:~/k8s-yaml/jenkins/yaml/pv# kubectl get pv
NAME                      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                               STORAGECLASS   REASON   AGE
jenkins-datadir-pv        20Gi       RWO            Retain           Bound    yan-test/jenkins-datadir-pvc                                3d1h
jenkins-root-datadir-pv   20Gi       RWO            Retain           Bound    yan-test/jenkins-root-datadir-pvc                           3d1h

root@k8-deploy:~/k8s-yaml/jenkins/yaml/pv# kubectl get pvc -n yan-test
NAME                       STATUS   VOLUME                    CAPACITY   ACCESS MODES   STORAGECLASS   AGE
jenkins-datadir-pvc        Bound    jenkins-datadir-pv        20Gi       RWO                           3d1h
jenkins-root-datadir-pvc   Bound    jenkins-root-datadir-pv   20Gi       RWO                           3d1h

3.部署jenkins服务器

3.1 编写deployment,svc yaml

apiVersion: v1
kind: Service
metadata:
  name: jenkins
  namespace: yan-test
spec:
  type: NodePort
  ports:
    - name: http
      port: 8080
      protocol: TCP
      targetPort: 8080
      nodePort: 38080
  selector:
    app: jenkins
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
  namespace: yan-test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      containers:
        - name: server
          image: 192.168.1.110/web/jenkins-2.303.2:v20211023-1211
          imagePullPolicy: Always
          ports:
            - containerPort: 8080
              protocol: TCP
              name: http
          volumeMounts:
          - mountPath: "/apps/jenkins/jenkis-data"
            name: jenkins-datadir-vl
          - mountPath: "/root/.jenkins"
            name: jenkins-root-datadir-vl
      volumes:
        - name: jenkins-datadir-vl
          persistentVolumeClaim:
            claimName: jenkins-datadir-pvc
        - name: jenkins-root-datadir-vl
          persistentVolumeClaim:
            claimName: jenkins-root-datadir-pvc

3.2 创建

root@k8-deploy:~/k8s-yaml/jenkins/yaml# kubectl apply -f jenkins.yaml
service/jenkins created
deployment.apps/jenkins created

3.2 查看验证

# kubectl describe pod jenkins-7f4c78cdd5-48hb8 -n yan-test
...
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  45s   default-scheduler  Successfully assigned yan-test/jenkins-7f4c78cdd5-48hb8 to 192.168.2.18
  Normal  Pulling    44s   kubelet            Pulling image "192.168.1.110/web/jenkins-2.303.2:v20211023-1211"
  Normal  Pulled     16s   kubelet            Successfully pulled image "192.168.1.110/web/jenkins-2.303.2:v20211023-1211" in 28.065811801s
  Normal  Created    13s   kubelet            Created container server
  Normal  Started    12s   kubelet            Started container server

root@k8-deploy:~/k8s-yaml/jenkins/yaml# kubectl get pod -n yan-test
NAME                       READY   STATUS    RESTARTS   AGE
jenkins-7f4c78cdd5-48hb8   1/1     Running   0          56s

root@k8-deploy:~/k8s-yaml/jenkins/yaml# kubectl exec jenkins-7f4c78cdd5-48hb8 -n yan-test -it -- bash   
[root@jenkins-7f4c78cdd5-48hb8 /]# df -h
Filesystem                                           Size  Used Avail Use% Mounted on
...
192.168.2.10:/data/k8s-data/jenkins/root-datadir-pv   98G   22G   72G  24% /root/.jenkins
192.168.2.10:/data/k8s-data/jenkins/datadir-pv        98G   22G   72G  24% /apps/jenkins/jenkis-data
...

4.验证测试jenkins服务

4.1 查看登录密码

image

root@k8-deploy:~/k8s-yaml/jenkins/yaml# kubectl exec jenkins-7f4c78cdd5-48hb8 -n yan-test -it -- bash 

[root@jenkins-7f4c78cdd5-48hb8 /]# cat /root/.jenkins/secrets/initialAdminPassword
3cef3596ebdb4af592f331383023897

4.2 重启jenkins是初始化安装的插件生效

因为数据都放在了nfs存储上,所以重建jenkins不会导致数据丢失。

kubectl delete -f jenkins.yaml 
kubectl apply -f jenkins.yaml 

4.3如果密码已更改,但有忘记admin密码了可以参考。

https://www.cnblogs.com/aerospace191/p/14063088.html

posted @ 2021-11-07 11:05  yanql  阅读(637)  评论(0编辑  收藏  举报