https://blog.csdn.net/zhou920786312/article/details/124919461
# 创建目录
mkdir /nfs/devops -p
# 给与权限
chmod 777 /nfs/devops
# vim /etc/exports
/nfs/devops 192.168.187.0/24(rw,no_root_squash)
# 宣告共享目录
exportfs -arv
# 重启NFS服务程序
systemctl restart nfs
kubectl create namespace jenkins-k8s
vi /root/k8s/devops/pv.yaml
内容
apiVersion: v1
kind: PersistentVolume
metadata:
# pv名称
name: jenkins-k8s-pv
spec:
# 设置资源大小
capacity:
storage: 1Gi
# 设置访问模式
accessModes:
# 多节点读写
- ReadWriteMany
# nfs配置
nfs:
server: 192.168.187.154
path: /nfs/devops
kubectl apply -f /root/k8s/devops/pv.yaml
# 查看
kubectl get pv

vi /root/k8s/devops/pvc.yaml
内容
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
# 定义pvc的名称和空间
name: jenkins-k8s-pvc
namespace: jenkins-k8s
spec:
# 定义资源
resources:
requests:
storage: 1Gi
# 访问模式
accessModes:
# 多节点读写
- ReadWriteMany
kubectl apply -f /root/k8s/devops/pvc.yaml
# 查看
kubectl get pvc -n jenkins-k8s
kubectl get pv -n jenkins-k8s

# 在jenkins-k8s名称空间下,创建sa账号 jenkins-k8s-sa
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
vi /root/k8s/devops/jenkins-deployment.yaml
内容
kind: Deployment
apiVersion: apps/v1
metadata:
# Deployment 名称
name: jenkins
# Deployment 使用的名称空间
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
# 修改nfs 共享目录的权限
chown -R 1000 /nfs/devops/jenkins-home
kubectl apply -f /root/k8s/devops/jenkins-deployment.yaml
# 查看
kubectl get Deployment -n jenkins-k8s
kubectl get pods -n jenkins-k8s

vi /root/k8s/devops/jenkins-service.yaml
内容
apiVersion: v1
kind: Service
metadata:
# 服务名称和服务所在的名称空间
name: jenkins-service
namespace: jenkins-k8s
# 标签
labels:
app: jenkins
spec:
# 通过标签和对应的POD关联在一起
selector:
app: jenkins
# 可以让浏览器访问
type: NodePort
# 定义端口
ports:
- name: web
port: 8080
# 目标pod的端口,可以是名称,也可以是数字,如果写了名称,那么对应的pod,必须定义对应名称的端口
targetPort: web
nodePort: 30002
- name: agent
port: 50000
targetPort: agent
kubectl apply -f /root/k8s/devops/jenkins-service.yaml
# 查看
kubectl get svc -n jenkins-k8s

更多的资料,看我的Jenkins学习
http://192.168.187.154:30002

在nfs服务端,也就是我们的master1节点获取密码
cd /nfs/devops/jenkins-home/secrets
cat initialAdminPassword



我这都是admin

