云原生之旅 - 10)手把手教你安装 Jenkins on Kubernetes

前言谈到持续集成工具就离不开众所周知的Jenkins,本文带你了解如何在 Kubernetes 上安装 Jenkins , 后续文章会带你深入了解如何使用k8s pod 作为 Jenkins的build agents 。
准备需要一个running的 Kubernetes Cluster,可以参考我前面的文章 云原生之旅 - 4)基础设施即代码 使用 Terraform 创建 Kubernetes
安装
Step 1: 创建Namespace
apiVersion: v1kind: Namespacemetadata:  name: jenkinsnamespace.yaml
kubectl apply -f namespace.yaml
Step 2: 创建 k8s service account and RBAC 权限
---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata:  name: jenkins-adminrules:  - apiGroups: [""]    resources: ["*"]    verbs: ["*"]---apiVersion: v1kind: ServiceAccountmetadata:  name: jenkins-admin  namespace: jenkins---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata:  name: jenkins-adminroleRef:  apiGroup: rbac.authorization.k8s.io  kind: ClusterRole  name: jenkins-adminsubjects:- kind: ServiceAccount  name: jenkins-admin  namespace: jenkinsserviceAccount.yaml
kubectl apply -f serviceAccount.yamlStep 3: 创建 StorageClass 和 PersistentVolumeClaim(我的例子是在GCP上面 , 其它云提供商类似)
---## if not create StorageClass, default to use standard StorageClassapiVersion: storage.k8s.io/v1kind: StorageClassmetadata:  name: jenkins-sc  namespace: jenkinsprovisioner: kubernetes.io/gce-pdvolumeBindingMode: ImmediateallowVolumeExpansion: truereclaimPolicy: Deleteparameters:  type: pd-standard  fstype: ext4  replication-type: none---apiVersion: v1kind: PersistentVolumeClaimmetadata:  name: jenkins-storage  namespace: jenkinsspec:  storageClassName: jenkins-sc  accessModes:    - ReadWriteOnce  resources:    requests:      storage: 20Givolume.yaml

  • Create a storage class
  • Provision a Persistent volume using the storage class.
kubectl apply -f volume.yaml检查绑定结果
kubectl get pvc -n jenkinsNAME              STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGEjenkins-storage   Bound    pvc-27efe7b9-c963-4366-b100-a3b01bb25666   20Gi       RWO            jenkins-sc     23sStep 4: 创建 Deployment
Jenkins home 目录需要mount,不然 Jenkins pod 一旦重启的话,数据会丢失 。
apiVersion: apps/v1kind: Deploymentmetadata:  name: jenkins  namespace: jenkinsspec:  replicas: 1  selector:    matchLabels:      app: jenkins-server  template:    metadata:      labels:        app: jenkins-server    spec:      securityContext:            fsGroup: 1000            runAsUser: 1000      serviceAccountName: jenkins-admin      containers:        - name: jenkins          image: jenkins/jenkins:lts          resources:            limits:              memory: "2Gi"              cpu: "1000m"            requests:              memory: "500Mi"              cpu: "500m"          ports:            - name: httpport              containerPort: 8080            - name: jnlpport              containerPort: 50000          livenessProbe:            httpGet:              path: "/login"              port: 8080            initialDelaySeconds: 90            periodSeconds: 10            timeoutSeconds: 5            failureThreshold: 5          readinessProbe:            httpGet:              path: "/login"              port: 8080            initialDelaySeconds: 60            periodSeconds: 10            timeoutSeconds: 5            failureThreshold: 3          volumeMounts:            - name: jenkins-data              mountPath: /var/jenkins_home      volumes:        - name: jenkins-data          persistentVolumeClaim:              claimName: jenkins-storage

推荐阅读