这是我已经做好的nfs存储目录
cd /data/nfsData/
创建jenkins存储目录
mkdir jenkins
修改nfs配置
vim /etc/exports
加上如下内容保存
/data/nfsData/jenkins *(rw,no_root_squash,no_all_squash,sync)
重启nfs服务
systemctl restart nfs rpcbind
需要修改下目录权限
因为当映射本地数据卷时,/data/nfsData/jenkins目录的拥有者为root用户,而容器中jenkins user的uid为1000
chmod 777 jenkins/
chown -R 1000:1000 jenkins
mkdir /root/jenkins
vim 01-jenkins-ns.yaml
apiVersion: v1
kind: Namespace
metadata:
name: jenkins-k8s
vim 02-jenkins-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: jenkins-k8s-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteMany
nfs:
server: nfs.myit.icu
path: /data/nfsData/jenkins
vim 03-jenkins-pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: jenkins-k8s-pvc
namespace: jenkins-k8s
spec:
resources:
requests:
storage: 10Gi
accessModes:
- ReadWriteMany
vim 04-jenkins-account.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: jenkins-k8s-sa
namespace: jenkins-k8s
vim 05-jenkins-cluster-role-binding.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: jenkins-k8s-sa-cluster
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: jenkins-k8s-sa
namespace: jenkins-k8s
vim 06-jenkins-deployment.yaml ### 如果出现问题,可以把探测关掉---待测试
apiVersion: apps/v1
kind: Deployment
metadata:
name: jenkins
namespace: jenkins-k8s
spec:
replicas:
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:
name: web
protocol: TCP
- containerPort:
name: agent
protocol: TCP
resources:
limits:
cpu: 1000m
memory: 1Gi
requests:
cpu: 500m
memory: 512Mi
livenessProbe:
httpGet:
path: /login
port:
initialDelaySeconds:
timeoutSeconds:
failureThreshold:
readinessProbe:
httpGet:
path: /login
port:
initialDelaySeconds:
timeoutSeconds:
failureThreshold:
volumeMounts:
- name: jenkins-volume
subPath: jenkins-home
mountPath: /var/jenkins_home
volumes:
- name: jenkins-volume
persistentVolumeClaim:
claimName: jenkins-k8s-pvc
vim 07-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:
targetPort: web
nodePort:
- name: agent
port:
targetPort: agent
kubectl create -f 01-jenkins-ns.yaml
kubectl create -f 02-jenkins-pv.yaml
kubectl create -f 03-jenkins-pvc.yaml
kubectl create -f 04-jenkins-account.yaml
kubectl create -f 05-jenkins-cluster-role-binding.yaml
kubectl create -f 06-jenkins-deployment.yaml
kubectl create -f 07-jenkins-service.yaml
kubectl get pods,svc -n jenkins-k8s
kubectl logs $(kubectl get pods -n jenkins-k8s | awk '{print $1}' | grep jenkins) -n jenkins-k8s
会出现以下内容:
*************************************************************
*************************************************************
*************************************************************
Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:
e989cb4f27a942948fb6163674ef512d
This may also be found at: /var/jenkins_home/secrets/initialAdminPassword
*************************************************************
*************************************************************
*************************************************************
kubectl exec -n jenkins-k8s $(kubectl get pods -n jenkins-k8s | awk '{print $1}' | grep jenkins) -it -- cat /var/jenkins_home/secrets/initialAdminPassword
密码在nfs自己映射的路径下
cat /data/nfsData/jenkins/jenkins-home/secrets/initialAdminPassword
安装kubernetes相关插件: Kubernetes CLI Plugin Kubernetes Client API Plugin Kubernetes Credentials Plugin Kubernetes plugin