Bootstrap

k8s部署jenkins

1.jenkins-rbac.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: jenkins-admin       #ServiceAccount
  namespace: jenkins    
  labels:
    name: jenkins
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: jenkins-admin
  labels:
    name: jenkins
subjects:
  - kind: ServiceAccount
    name: jenkins-admin
    namespace: jenkins
roleRef:
  kind: ClusterRole
  name: cluster-admin
  apiGroup: rbac.authorization.k8s.io

2.jenkins-storage.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: jenkins
  labels:
    app: jenkins
spec:
  capacity:          
    storage: 10Gi
  accessModes:       
    - ReadWriteOnce
  volumeMode: Filesystem  
  persistentVolumeReclaimPolicy: Retain  
  hostPath:  
    path: /mnt/jenkins
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: jenkins
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi     #存储空间大小
  selector:
    matchLabels:
      app: jenkins	

3.jenkins-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
  labels:
    app: jenkins
spec: 
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: jenkins
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      containers:
      - name: jenkins
        image: jenkins/jenkins:latest
        imagePullPolicy: IfNotPresent
        ports:
            - name: http
              containerPort: 8080
            - name: jnlp
              containerPort: 50000
        env:
        # - name: "JAVA_OPTS"                 #设置变量,指定时区和 jenkins slave 执行者设置
        #   value: " 
        #             -XshowSettings:vm 
        #             -Dhudson.slaves.NodeProvisioner.initialDelay=0
        #             -Dhudson.slaves.NodeProvisioner.MARGIN=50
        #             -Dhudson.slaves.NodeProvisioner.MARGIN0=0.85
        #             -Duser.timezone=Asia/Shanghai
        #             -Djava.awt.headless=true 
        #             -XX:MaxRAMPercentage=80.0 
        #             -XX:InitialRAMPercentage=80.0
        #           "    
        # - name: "JENKINS_OPTS"
        #   value: "--prefix=/jenkins"         #设置路径前缀加上 Jenkins
        volumeMounts:                        #设置要挂在的目录
        - name: data
          mountPath: /var/jenkins_home
        resources: {}
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: jenkins   
---
apiVersion: v1
kind: Service
metadata:
  name: jenkins
  labels:
    app: jenkins
spec:
  type: NodePort
  ports:
  - name: http
    port: 8080                    #服务端口
    targetPort: 8080
    nodePort: 32001               #NodePort方式暴露 Jenkins 端口
  - name: jnlp
    port: 50000                   #代理端口
    targetPort: 50000
    nodePort: 32002
  selector:
    app: jenkins
;