文件数量

一共有三个jenkins的yaml文件,分别是jenkins.yaml,pvc.yaml,rbac.yaml。

文件说明

rbac.yaml:用于在K8S中创建jenkins的集群角色,并赋予角色权限,用于jenkins调用K8S集群拉取一个slave pod。

pvc.yaml:用于创建NFS文件系统的存储卷和存储卷声明,用于存放数据,可改为其他存储类。

Jenkins.yaml:用于创建jenkins副本和暴露端口。

文件内容

rbac.yaml

注:目前为默认的命名空间,根据实际情况更改

---

apiVersion: v1
kind: ServiceAccount
metadata:
  name: jenkins
  namespace: default

---

kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: jenkins
rules:
  - apiGroups: ["extensions", "apps"]
    resources: ["deployments"]
    verbs: ["create", "delete", "get", "list", "watch", "patch", "update"]
  - apiGroups: [""]
    resources: ["services"]
    verbs: ["create", "delete", "get", "list", "watch", "patch", "update"]
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["create","delete","get","list","patch","update","watch"]
  - apiGroups: [""]
    resources: ["pods/exec"]
    verbs: ["create","delete","get","list","patch","update","watch"]
  - apiGroups: [""]
    resources: ["pods/log"]
    verbs: ["get","list","watch"]
  - apiGroups: [""]
    resources: ["secrets"]
    verbs: ["get"]

---

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: jenkins
  namespace: default
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: jenkins
subjects:
  - kind: ServiceAccount
    name: jenkins
    namespace: default

pvc.yaml

注:目前存储类型为NFS,存放目录为/data/jenkins,存储大小为21Gi,nfs服务器地址,目录等可根据实际情况更改

---

apiVersion: v1
kind: PersistentVolume
metadata:
  name: jenkins-pv
  namespace: default
spec:
  capacity:
    storage: 21Gi
  accessModes:
  - ReadWriteMany
  persistentVolumeReclaimPolicy: Delete
  nfs:
    server: 192.168.0.12
    path: /data/jenkins
    
---

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: jenkins-pvc
  namespace: default
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 20Gi

jenkins.yaml

注:此yaml文件已经进行了优化,建议只更改部分关键配置,目前持久化存储位置为/data/jenkins_pxdy,可根据实际情况匹配pv,端口为30002,slave通信端口为30003。

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
  namespace: default
spec:
  selector:
    matchLabels:
      app: jenkins
  template:
    metadata: 
      labels: 
        app : jenkins
    spec:
      terminationGracePeriodSeconds: 10
      serviceAccount: jenkins
      containers:
      - name: jenkins
        image: jenkins:2.269     #根据需求修改
        imagePullPolicy: IfNotPresent
        securityContext:
          runAsUser: 0                    
          privileged: true
        ports:
        - containerPort: 8080
          name: web
          protocol: TCP
        - containerPort: 50000
          name: agent
          protocol: TCP
        resources:
          limits:
            cpu: 3000m
            memory: 3Gi
          requests:
            cpu: 1000m
            memory: 1Gi
        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: jenkinshome
          # subPath: jenkins
          mountPath: /var/jenkins_home
        env:
        - name: LIMITS_MEMORY
          valueFrom:
            resourceFieldRef:
              resource: limits.memory
              divisor: 1Mi
        - name: JAVA_OPTS
          value: -Xmx$(LIMITS_MEMORY)m -XshowSettings:vm -Dhudson.slaves.NodeProvisioner.initialDelay=0 -Dhudson.slaves.NodeProvisioner.MARGIN=50 -Dhudson.slaves.NodeProvisioner.MARGIN0=0.85 -Duser.timezone=Asia/Shanghai
      securityContext:
        fsGroup: 1000
      volumes:
      - name: jenkinshome
        hostPath: 
          path: /data/jenkins_pxdy
          type: Directory
      #volumes:               
      #- name: jenkinshome
      #  persistentVolumeClaim:
      #    claimName: jenkins-pvc
              
---

apiVersion: v1
kind: Service
metadata:
  name: jenkins
  namespace: default
  labels:
    app: jenkins
spec:
  selector:
    app: jenkins
  type: NodePort
  ports:
  - name: web
    port: 8080
    targetPort: 8080
    nodePort: 30002
  - name: agent
    port: 50000
    targetPort: 50000
    nodePort: 30003
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐