参考:kubernetes 部署 Jenkins jenkins kubernetes pipeline_mob64ca14116c53的技术博客_51CTO博客
第七篇:kubernetes部署jenkins-CSDN博客
1、当前kubernetes集群已部署nfs服务
showmount -e

创建jenkins目录

2、添加jenkins的pvc
kubectl create namespace jenkins-k8s
cd /opt/dockerfile/jenkins/
touch jenkins-pv.yaml
- apiVersion: v1
- kind: PersistentVolume
- metadata:
- name: jenkins-k8s-pv
- spec:
- capacity:
- storage: 10Gi
- accessModes:
- - ReadWriteMany
- nfs:
- server: 192.168.1.247
- path: /opt/nfsdata/jenkins
touch jeckins-pvc.yaml
- kind: PersistentVolumeClaim
- apiVersion: v1
- metadata:
- name: jenkins-k8s-pvc
- namespace: jenkins-k8s
- spec:
- resources:
- requests:
- storage: 10Gi
- accessModes:
- - ReadWriteMany
kubectl get pv -n jenkins-k8s

创建一个sa账号,做rbac授权
kubectl create sa jenkins-k8s-sa -n jenkins-k8s
![]()
kubectl create clusterrolebinding jenkins-k8s-sa-cluster -n jenkins-k8s --clusterrole=cluster-admin --serviceaccount=jenkins-k8s:jenkins-k8s-sa

2、部署 jenkins
kubectl apply -f jeckins-deploy.yaml
- kind: Deployment
- apiVersion: apps/v1
- metadata:
- name: jenkins
- namespace: jenkins-k8s
- spec:
- replicas: 1
- selector:
- matchLabels:
- app: jenkins
- template:
- metadata:
- labels:
- app: jenkins
- spec:
- serviceAccount: jenkins-k8s-sa
- containers:
- - name: jenkins
- image: jenkins/jenkins:lts
- imagePullPolicy: IfNotPresent
- ports:
- - containerPort: 8080
- name: web
- protocol: TCP
- - containerPort: 50000
- name: agent
- protocol: TCP
- resources:
- limits:
- cpu: 1000m
- memory: 1Gi
- requests:
- cpu: 500m
- memory: 512Mi
- livenessProbe:
- httpGet:
- path: /login
- port: 8080
- initialDelaySeconds: 60
- timeoutSeconds: 5
- failureThreshold: 12
- readinessProbe:
- httpGet:
- path: /login
- port: 8080
- initialDelaySeconds: 60
- timeoutSeconds: 5
- failureThreshold: 12
- volumeMounts:
- - name: jenkins-volume
- subPath: jenkins-home
- mountPath: /var/jenkins_home
- volumes:
- - name: jenkins-volume
- persistentVolumeClaim:
- claimName: jenkins-k8s-pvc
kubectl apply -f jeckins-deploy.yaml
kubectl describe pod jenkins-74bb6797d9-qd92c -n jenkins-k8s
kubectl logs pod jenkins-74bb6797d9-qd92c -c jenkins -n jenkins-k8s
没权限

chown -R 1000:1000 /opt/nfsdata/jenkins
kubectl delete -f jeckins-deploy.yaml
重装
kubectl apply -f jeckins-deploy.yaml
kubectl get pod -n jenkins-k8s -owide

3、验证
kubectl get pod -n jenkins-k8s -owide
curl 10.100.27.19:8080

4、kubectl apply -f jenkins-service.yaml
- apiVersion: v1
- kind: Service
- metadata:
- name: jenkins-service
- namespace: jenkins-k8s
- labels:
- app: jenkins
- spec:
- selector:
- app: jenkins
- type: NodePort
- ports:
- - name: web
- port: 8080
- targetPort: web
- nodePort: 30002
- - name: agent
- port: 50000
- targetPort: agent
![]()
kubectl get svc -n jenkins-k8s


cd /opt/nfsdata/jenkins/jenkins-home/secrets
![]()

若无法正常配置,可尝试http://192.168.1.12:30002/restart
安装推荐的插件:

创建用户名密码,搭建完成。
