做摘抄的网站/广州今日刚刚发生的新闻
k8s–基础–27.2–企业级devops平台–安装jenkins
1、安装nfs服务
1.1、安装
- 我选择master1节点(192.168.187.154)
- 因为我们已经安装过了,这里只给安装资料
- 参考资料
https://blog.csdn.net/zhou920786312/article/details/124919461
1.2、设置共享目录
1.2.1、创建共享目录
# 创建目录
mkdir /nfs/devops -p# 给与权限
chmod 777 /nfs/devops
1.2.2、编辑配置文件
# vim /etc/exports
/nfs/devops 192.168.187.0/24(rw,no_root_squash)
说明
- /nfs/devops 允许192.168.187.0/24的客户端访问。
- rw:表示允许读写,ro表示为只读;
- no_root_squash:表示当客户机以root身份访问时赋予本地root权限
1.3、重启NFS服务程序
# 宣告共享目录
exportfs -arv
# 重启NFS服务程序
systemctl restart nfs
2、部署jenkins
2.1、创建名称空间
kubectl create namespace jenkins-k8s
2.2、创建pv
2.2.1、脚本
vi /root/k8s/devops/pv.yaml
内容
apiVersion: v1
kind: PersistentVolume
metadata:# pv名称name: jenkins-k8s-pv
spec:# 设置资源大小capacity:storage: 1Gi# 设置访问模式accessModes:# 多节点读写- ReadWriteMany# nfs配置nfs:server: 192.168.187.154path: /nfs/devops
2.2.2、执行
kubectl apply -f /root/k8s/devops/pv.yaml# 查看
kubectl get pv
2.3、创建pvc
2.3.1、脚本
vi /root/k8s/devops/pvc.yaml
内容
kind: PersistentVolumeClaim
apiVersion: v1
metadata:# 定义pvc的名称和空间name: jenkins-k8s-pvcnamespace: jenkins-k8s
spec:# 定义资源resources:requests:storage: 1Gi# 访问模式accessModes:# 多节点读写- ReadWriteMany
2.3.2、执行
kubectl apply -f /root/k8s/devops/pvc.yaml# 查看
kubectl get pvc -n jenkins-k8s
kubectl get pv -n jenkins-k8s
2.4、创建一个sa账号并rbac授权
2.4.1、创建一个sa账号
# 在jenkins-k8s名称空间下,创建sa账号 jenkins-k8s-sa
kubectl create sa jenkins-k8s-sa -n jenkins-k8s
2.4.2、rbac授权
kubectl create clusterrolebinding jenkins-k8s-sa-cluster -n jenkins-k8s --clusterrole=cluster-admin --serviceaccount=jenkins-k8s:jenkins-k8s-sa
- 在jenkins-k8s名称空间下创建一个clusterrolebinding,名字叫做jenkins-k8s-sa-cluster
- 将jenkins-k8s名称空间下的jenkins-k8s-sa账号通过clusterrole绑定集群角色cluster-admin,这样jenkins-k8s-sa账号就有了cluster-admin角色的权限。
2.5、通过deployment部署jenkins
2.5.1、脚本
vi /root/k8s/devops/jenkins-deployment.yaml
内容
kind: Deployment
apiVersion: apps/v1
metadata:# Deployment 名称name: jenkins# Deployment 使用的名称空间namespace: jenkins-k8s
spec:# 副本数量replicas: 1# 通过标签,定义选择哪个模板selector:matchLabels:app: jenkins# 定义模板的标签template:metadata:labels:app: jenkinsspec:# 使用的账号serviceAccount: jenkins-k8s-sa# 定义容器containers:- name: jenkins# 镜像地址image: jenkins/jenkins:lts# 镜像拉取策略imagePullPolicy: IfNotPresent# 定义端口ports:- containerPort: 8080name: webprotocol: TCP- containerPort: 50000name: agentprotocol: TCP# 资源限制resources:limits:cpu: 1000mmemory: 1Girequests:cpu: 500mmemory: 512Mi# 存活检查livenessProbe:httpGet:path: /loginport: 8080initialDelaySeconds: 60timeoutSeconds: 5failureThreshold: 12# 监控检查readinessProbe:httpGet:path: /loginport: 8080initialDelaySeconds: 60timeoutSeconds: 5failureThreshold: 12# 存储卷volumeMounts:- name: jenkins-volumesubPath: jenkins-homemountPath: /var/jenkins_home# 存储卷定义volumes:- name: jenkins-volumepersistentVolumeClaim:claimName: jenkins-k8s-pvc
2.5.2、执行
# 修改nfs 共享目录的权限
chown -R 1000 /nfs/devops/jenkins-homekubectl apply -f /root/k8s/devops/jenkins-deployment.yaml# 查看
kubectl get Deployment -n jenkins-k8s
kubectl get pods -n jenkins-k8s
2.6、部署jenkins对应的service
- 可以让浏览器访问
2.6.1、脚本
vi /root/k8s/devops/jenkins-service.yaml
内容
apiVersion: v1
kind: Service
metadata:# 服务名称和服务所在的名称空间name: jenkins-servicenamespace: jenkins-k8s# 标签labels:app: jenkins
spec:# 通过标签和对应的POD关联在一起selector:app: jenkins# 可以让浏览器访问type: NodePort# 定义端口ports:- name: webport: 8080# 目标pod的端口,可以是名称,也可以是数字,如果写了名称,那么对应的pod,必须定义对应名称的端口targetPort: webnodePort: 30002- name: agentport: 50000targetPort: agent
2.6.2、执行
kubectl apply -f /root/k8s/devops/jenkins-service.yaml# 查看
kubectl get svc -n jenkins-k8s
3、配置Jenkins
更多的资料,看我的Jenkins学习
3.1、登陆
http://192.168.187.154:30002
3.1.1、获取管理员密码
在nfs服务端,也就是我们的master1节点获取密码
cd /nfs/devops/jenkins-home/secrets
cat initialAdminPassword
3.2、操作
安装推荐的插件
插件安装好,会让你创建用户
我这都是admin