首页 > 其他分享 >云原生之旅 - 10)手把手教你安装 Jenkins on Kubernetes

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

时间:2022-11-12 10:55:08浏览次数:77  
标签:10 kind name Kubernetes kubectl yaml Jenkins jenkins

前言

谈到持续集成工具就离不开众所周知的Jenkins,本文带你了解如何在 Kubernetes 上安装 Jenkins,后续文章会带你深入了解如何使用k8s pod 作为 Jenkins的build agents。

 

准备

需要一个running的 Kubernetes Cluster, 可以参考我前面的文章 云原生之旅 - 4)基础设施即代码 使用 Terraform 创建 Kubernetes

 

安装

Step 1: 创建Namespace

apiVersion: v1
kind: Namespace
metadata:
  name: jenkins
namespace.yaml
kubectl apply -f namespace.yaml

  

Step 2: 创建 k8s service account and RBAC 权限

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: jenkins-admin
rules:
  - apiGroups: [""]
    resources: ["*"]
    verbs: ["*"]

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: jenkins-admin
  namespace: jenkins

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: jenkins-admin
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: jenkins-admin
subjects:
- kind: ServiceAccount
  name: jenkins-admin
  namespace: jenkins
serviceAccount.yaml
kubectl apply -f serviceAccount.yaml

 

Step 3: 创建 StorageClass 和 PersistentVolumeClaim(我的例子是在GCP上面,其它云提供商类似)

---
## if not create StorageClass, default to use standard StorageClass
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: jenkins-sc
  namespace: jenkins
provisioner: kubernetes.io/gce-pd
volumeBindingMode: Immediate
allowVolumeExpansion: true
reclaimPolicy: Delete
parameters:
  type: pd-standard
  fstype: ext4
  replication-type: none

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: jenkins-storage
  namespace: jenkins
spec:
  storageClassName: jenkins-sc
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
volume.yaml
  • Create a storage class
  • Provision a Persistent volume using the storage class.
kubectl apply -f volume.yaml

 检查绑定结果

kubectl get pvc -n jenkins

NAME              STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
jenkins-storage   Bound    pvc-27efe7b9-c963-4366-b100-a3b01bb25666   20Gi       RWO            jenkins-sc     23s

 

Step 4: 创建 Deployment

Jenkins home 目录需要mount,不然 Jenkins pod 一旦重启的话,数据会丢失。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
  namespace: jenkins
spec:
  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
deployment.yaml
kubectl apply -f deployment.yaml

检查部署结果

kubectl get deploy -n jenkins

NAME      READY   UP-TO-DATE   AVAILABLE   AGE
jenkins   1/1     1            1           89s

 

Step 5: Create Service

---
apiVersion: v1
kind: Service
metadata:
  name: jenkins-service
  namespace: jenkins
spec:
  selector: 
    app: jenkins-server
  type: NodePort  
  ports:
    - port: 8080
      targetPort: 8080
      nodePort: 32000

---
kind: Service
apiVersion: v1
metadata:
  name: jenkins-agent
  namespace: jenkins
spec:
  selector:
    app: jenkins-server
  ports:
    - protocol: TCP
      port: 50000
      targetPort: 50000
service.yaml
kubectl apply -f service.yaml

### 本文首发于 https://www.cnblogs.com/wade-xu/p/16863933.html

 

访问 Jenkins Dashboard

Option 1: 用  Kube Proxy

kubectl -n jenkins port-forward service/jenkins-service 8010:8080
然后打开本地浏览器访问 Jenkins dashboard ==》 http://127.0.0.1:8010

 

Option 2:  推荐使用Gateway 

1. Ingress-Nginx 可以参考 云原生之旅 - 8)云原生时代的网关 Ingress Nginx

2. Emissary Ingress 可以参考云原生之旅 - 9)云原生时代网关的后起之秀Envoy Proxy 和基于Envoy 的 Emissary Ingress

 

第一次访问Jenkins Dashboard 会提示需要初始密码,通过如下访问获取初始密码。
kubectl get pods -n jenkins

kubectl logs jenkins-998474795-7n6ls -n jenkins

 日志结果

*************************************************************

Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:

xxxxxxxxxxxxxx

This may also be found at: /var/jenkins_home/secrets/initialAdminPassword

输入 password 然后会提示 install the suggested plugin 和创建一个 admin user.

### 本文首发于 https://www.cnblogs.com/wade-xu/p/16863933.html

 

High Availability 高可用

  • Jenkins active/passive setup --- 只有企业版Jenkins才有此功能。
  • 本文介绍的 Jenkins running on Kubernetes, 一旦 Jenkins master pod 挂了,另一个新的 Jenkins master pod 会自动起来,并将存储卷挂载至新创建的容器,保证数据不会丢失,从而实现集群高可用。

 

参考

https://github.com/scriptcamp/kubernetes-jenkins/blob/main/deployment.yaml https://www.jenkins.io/doc/book/installing/kubernetes/   感谢阅读,如果您觉得本文的内容对您的学习有所帮助,您可以打赏和推荐,您的鼓励是我创作的动力    

标签:10,kind,name,Kubernetes,kubectl,yaml,Jenkins,jenkins
From: https://www.cnblogs.com/wade-xu/p/16863933.html

相关文章

  • 10.http客户端feign
    http客户端feignRestTemplate方式调用存在的问题先来看我们以前利用RestTemplate发起远程调用的代码:Stringurl="http://userservice/user/"+order.getUserld();Us......
  • #10077. 「一本通 3.2 练习 3」最短路计数
    问1~n的最短路有几个 #include<iostream>#include<cstring>#include<queue>usingnamespacestd;constintN=1e6+2,M=2e6+2;constintinf=0x3f3f3f3f,m......
  • 10. 对象深拷贝问题
    首先,要知道基本数据保存在栈内存,对象数据保存在堆内存,对象地址(就是变量名)保存在栈内存,浅拷贝只会复制栈内存的内容,而深拷贝会复制栈和堆内存中的内容;所以深拷贝的对象有......
  • #10075. 「一本通 3.2 练习 1」农场派对
    图上每个点有一头牛,现在牛群聚集到点X上聚会,然后又回到各自的点,而且牛只走最短路径问所有最短路中最长的一条(路径包含来回) 正反跑一次 spfa(X), spfa(i), an......
  • #10074. 「一本通 3.2 例 3」架设电话线
    在加权无向图上求出一条从1号结点到N号结点的路径,使路径上第K+1大的边权尽量小 二分答案md,判断1~n是否存在一条路径,花费不超过md把w<=md的边看作0,否则看作1......
  • 2022-11-11 这10天,纳斯达克V形反转的一点记录
    1.从11月2号开始,首先已经跌破上升趋势线2.联席会议开始,多头突然发力,2段上涨3.结果空头发力,多空争夺激烈。空头回落到多头的1/2甚至2/3以下,一定要出了!开盘,收盘,事件,会......
  • Linux学习笔记(10)——Linux账号管理与ACL权限设置
    Linux账号管理与ACL权限设置Linux账号管理与ACL权限设置一、Linux的账号与用户组1.1用户标识符:UID与GID1.2用户账户1.3关于用户组:有效与初始用户组,groups,newgr......
  • GL-Suggesting a book 20221104
    TopicSuggestingabookWhichbookisbeingdescribed?Canyouthinkofanymoregenres?IsShakespeareyourfavoriteauthororisAgathaChristiemoreyour......
  • python10
    一、创建并打开记录蚂蚁庄园动态的文件print("\n","="*10,"蚂蚁庄园动态","="*10)film=open('message.txt','w')print("\n即将显示……\n")  二、向蚂蚁庄园的动态文......
  • GL-Planning a trip 20221103 same
    Planningatrip20221103Needtogetawayfromitall?Planyourdreamvacationwithyourclassmates,Whowouldyouliketogoonholidaywith?这节课有人吗?I......