k8s部署jenkins
創建nfs共享目錄,
mkdir -p /data/v2
echo ''/data/v2 *(rw,no_root_squash)' > /etc/exports
exportfs -arv
創建pv、pvc
vi pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:name: jenkins-k8s-pv
spec:capacity:storage: 1GiaccessModes:- ReadWriteManynfs:server: 172.16.80.131path: /data/v2vi pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:name: jenkins-k8s-pvcnamespace: jenkins-k8s
spec:resources:requests:storage: 1GiaccessModes:- ReadWriteMany
創建一個sa賬號,并做rbac授權
kubectl create sa jenkins-k8s-sa -n jenkins-k8s
kubectl create clusterrolebinding jenkins-k8s-sa-cluster --clusterrole=cluster-admin --serviceaccount=jenkins-k8s:jenkins-k8s-sa
創建四個名稱空間,分別為jenkins-k8s、開發、測試、生產
kubectl create namespace jenkins-k8s
kubectl create namespace development
kubectl create namespace qatest
kubectl create namespace production
harbor創建jenkins項目,上傳jenkins2.426.3.tar.gz、jenkins.agent:v2到harbor/jenkins項目
部署jenkins,包括deployment和svc
vi jenkins-deployment.yaml
kind: Deployment
apiVersion: apps/v1
metadata:name: jenkinsnamespace: jenkins-k8s
spec:replicas: 1selector:matchLabels:app: jenkinstemplate:metadata:labels:app: jenkinsspec:serviceAccount: jenkins-k8s-sacontainers:- name: jenkinsimage: 172.16.80.140/jenkins/jenkins:2.426.3imagePullPolicy: IfNotPresentports:- containerPort: 8080name: webprotocol: TCP- containerPort: 50000name: agentprotocol: TCPlivenessProbe:httpGet:path: /loginport: 8080initialDelaySeconds: 60timeoutSeconds: 5failureThreshold: 12readinessProbe:httpGet:path: /loginport: 8080initialDelaySeconds: 60timeoutSeconds: 5failureThreshold: 12volumeMounts:- name: jenkins-volumesubPath: jenkins-homemountPath: /var/jenkins_homevolumes:- name: jenkins-volumepersistentVolumeClaim:claimName: jenkins-k8s-pvcvi jenkins-service.yaml
apiVersion: v1
kind: Service
metadata:name: jenkins-servicenamespace: jenkins-k8slabels:app: jenkins
spec:selector:app: jenkinstype: NodePortports:- name: webport: 8080targetPort: webnodePort: 30002- name: agentport: 50000targetPort: agent
通過svc,用瀏覽器登錄jenkins