CI/CD工具
sa就是service account
[root@master ~]# mkdir jenkins
[root@master ~]# cd jenkins
[root@master jenkins]# kubectl create ns devops
[root@master jenkins]# vim jenkins-sa.yaml
namespace/devops created
metadata:
name: jenkins-admin
rules:
- apiGroups: [""]
resources: ["*"]
verbs: ["*"]
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: jenkins-admin
namespace: devops
---
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: devops
一定要先创建devops,如果你想部署在默认的命名空间下也是可以的,那就可以不创建的vops命名空间了
[root@master jenkins]# kubectl apply -f jenkins-sa.yaml
clusterrole.rbac.authorization.k8s.io/jenkins-admin created
serviceaccount/jenkins-admin created
clusterrolebinding.rbac.authorization.k8s.io/jenkins-admin created
直接创建pvc而没有创建pv是因为我采用的动态制备
[root@master jenkins]# vim jenkins-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: jenkins
namespace: devops
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
[root@master jenkins]# kubectl apply -f jenkins-pvc.yaml
查看一下pvc是否创建好了
[root@master jenkins]# kubectl get pvc -n devops
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
jenkins Bound pvc-131cc674-c33a-4d48-9a6a-e562374d4e0c 10Gi RWO csi-rbd-sc 7s
如果配置了动态制备而这里还是pending的话看一下sc是否设置为默认
文件内容比较多
vim jenkins-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: jenkins-server
name: jenkins-server
namespace: devops
spec:
replicas: 1
selector:
matchLabels:
app: jenkins-server
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: jenkins-server
spec:
volumes:
- name: jenkins-data
persistentVolumeClaim:
claimName: jenkins
serviceAccountName: jenkins-admin
containers:
- image: jenkins/jenkins
imagePullPolicy: IfNotPresent
name: jenkins
securityContext:
runAsUser: 0
volumeMounts:
- name: jenkins-data
mountPath: /var/jenkins_home
livenessProbe:
httpGet:
path: "/login"
port: 8080
initialDelaySeconds: 90
periodSeconds: 10
timeoutSeconds: 5
failureThreshold: 3
readinessProbe:
httpGet:
path: "/login"
port: 8080
initialDelaySeconds: 90
periodSeconds: 10
timeoutSeconds: 5
failureThreshold: 3
ports:
- name: http
containerPort: 8080
- name: tcp
containerPort: 50000
resources:
limits:
memory: "2Gi"
cpu: "1"
requests:
memory: "500Mi"
cpu: "0.5"
status: {}
创建deployment
[root@master jenkins]# kubectl apply -f jenkins-deoloy.yaml
deployment.apps/jenkins-server created
[root@master jenkins]# kubectl get pods -n devops
NAME READY STATUS RESTARTS AGE
jenkins-server-5db58f78dc-jqcqt 0/1 Running 0 34s
这里等待这个pod就绪就好了,因为是java进程,所以启动的有点慢
[root@master jenkins]# vim jenkins-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: jenkins-svc
namespace: devops
spec:
selector:
app: jenkins-server
type: NodePort
ports:
- name: http
port: 8080
targetPort: 8080
nodePort: 32000
- name: agent
port: 50000
targetPort: 50000
[root@master jenkins]# kubectl apply -f jenkins-svc.yaml
service/jenkins-svc created
等待jenkins就绪之后就可以通过nodePort映射出来的32000端口来进行访问了
然后进入容器查看初始密码
[root@master jenkins]# kubectl exec -it -n devops jenkins-server-5db58f78dc-jqcqt -- bash
root@jenkins-server-5db58f78dc-jqcqt:/# cat /var/jenkins_home/secrets/initialAdminPassword
15953b138edd4f68a434c31603455a4e
输出的这一串就是密码了,拿着这个密码去登录
我们登录之后会让我们安装插件,这个时候不要点击安装推荐的插件,因为他推荐的插件非常多,下载起来很慢的
我们现在安装2个插件就好了
找不到可以直接在搜索栏里面去搜
插件装完之后会让你创建第一个管理员用户
默认插件是从国外下载,速度会有些许慢,我们可以讲下载地址改为国内
如果你的插件管理点进去就报错的话往后看,有解决方法
然后往下滑,最下面这里,如果你是英文就是UpdateSite
在页面上改完之后还需要到容器内修改一个配置文件
[root@master jenkins]# kubectl exec -it -n devops jenkins-server-55cfc6dbd8-dmpn8 -- bash
# 进入jenkins的家目录
root@jenkins-server-55cfc6dbd8-dmpn8:/# cd /var/jenkins_home/updates/
root@jenkins-server-55cfc6dbd8-dmpn8:/var/jenkins_home/updates# ls
default.json hudson.tasks.Maven.MavenInstaller
修改这个default.json
root@jenkins-server-55cfc6dbd8-dmpn8:/var/jenkins_home/updates# sed -i 's/https:\/\/updates.jenkins.io\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' default.json && sed -i 's/http:\/\/www.google.com/https:\/\/www.baidu.com/g' default.json
重启的话直接在url输入restart
等他重启完之后再进去插件管理的页面就可以正常的下载插件了
按照自己的场景选择插件,我这里选择
安装完插件之后是需要重启jenkins的,所以建议一次性安装完
Git 拉取代码
SSH SSH 远程执行
Publish Over SSH
Gitlab gitlab插件
Maven 对 Maven 项目构建
NodeJs 对 NodeJs 项目构建
DingTalk 钉钉通知
Pipeline 流水线
Kubernetes 动态创建代理
simple theme 主题
Git Parameter 动态从 git 中获取所有分支
Managed Scripts 管理脚本插件
Deploy to container 热部署插件
Extended Choice Parameter 扩展参数构建
Role-based Authorization Strategy 权限管理
Localization: Chinese (Simplified) 简体中文
ansiColor 设置颜色
Conditional BuildStep 在pipeline中使用when
HTTP Request Plugin:执行http请求并返回响应体,详细用法: https://www.jenkins.io/doc/pipeline/steps/http_request/
Pipeline Utility Steps:pipeline中常用的一些工具集,如压缩与解压缩、创建文件、查找文件等;详细用法: https://www.jenkins.io/doc/pipeline/steps/pipeline-utility-steps/