目录
编辑
一、环境准备
1.1 环境说明
二、安装nfs
2.1 安装NFS
2.2 创建NFS共享文件夹
2.3 配置共享文件夹
2.4 使配置生效
2.5 查看所有共享目录
2.6 启动nfs
2.7 其他节点安装nfs-utils
三、创建PVC卷
3.1 创建namespace
3.2 创建nfs 客户端sa授权
3.3 创建nfs 客户端
3.4 创建StorageClass
3.5 创建PVC卷
四、创建Service
五、创建deployment
六、登录验证
6.1 登录
一、环境准备
1.1 环境说明
本文搭建MongoDB,基于WMware虚拟机,操作系统CentOS 8,且已经基于Kubeadm搭好了k8s集群,k8s节点信息如下:
服务器 | IP地址 |
master | 192.168.31.80 |
node1 | 192.168.31.8 |
node2 | 192.168.31.9 |
如需知道k8s集群搭建,可跳转我的文章《kubeadm部署k8s集群》查看。
二、安装nfs
2.1 安装NFS
我选择在 master 节点创建 NFS 存储,首先执行如下命令安装 NFS:
yum -y install nfs-utils rpcbind
2.2 创建NFS共享文件夹
mkdir -p /var/nfs/jenkins/pv1
2.3 配置共享文件夹
vim /etc/exports/var/nfs/jenkins/pv1 *(rw,sync,no_root_squash)
2.4 使配置生效
exportfs -r
2.5 查看所有共享目录
exportfs -v
2.6 启动nfs
systemctl start nfs-server
systemctl enabled nfs-server
systemctl start rpcbind
systemctl enabled rpcbind
2.7 其他节点安装nfs-utils
yum -y install nfs-utils
三、创建PVC卷
PVC是资源的申请,用来声明对存储空间、访问模式、存储类别需求信息。在创建PVC卷之前,需要创建NFS客户端、NFS 客户端sa授权和StoreClass存储类。
3.1 创建namespace
kubectl create ns jenkins
3.2 创建nfs 客户端sa授权
cat > jenkins-nfs-client-sa.yamlapiVersion: v1
kind: ServiceAccount
metadata:name: jenkins-nfs-clientnamespace: jenkins
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:name: jenkins-nfs-client-runnernamespace: jenkins
rules:- apiGroups: [""]resources: ["persistentvolumes"]verbs: ["get","list","watch","create","delete"]- apiGroups: [""]resources: ["persistentvolumeclaims"]verbs: ["get","list","watch","create","delete"]- apiGroups: ["storage.k8s.io"]resources: ["storageclasses"]verbs: ["get","list","watch"]- apiGroups: [""]resources: ["events"]verbs: ["get","list","watch","create","update","patch"]- apiGroups: [""]resources: ["endpoints"]verbs: ["create","delete","get","list","watch","patch","update"]---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:name: jenkins-run-nfs-provisionernamespace: jenkins
subjects:- kind: ServiceAccountname: jenkins-nfs-clientnamespace: jenkins
roleRef:kind: ClusterRolename: jenkins-nfs-client-runnerapiGroup: rbac.authorization.k8s.io
3.3 创建nfs 客户端
cat > jenkins-nfs-client.yamlapiVersion: apps/v1
kind: Deployment
metadata:name: jenkins-nfs-clientlabels:app: jenkins-nfs-client# replace with namespace where provisioner is deployednamespace: jenkins
spec:replicas: 1strategy:type: Recreateselector:matchLabels:app: jenkins-nfs-clienttemplate:metadata:labels:app: jenkins-nfs-clientspec:serviceAccountName: jenkins-nfs-clientcontainers:- name: jenkins-nfs-clientimage: quay.io/external_storage/nfs-client-provisioner:latestvolumeMounts:- name: jenkins-nfs-client-rootmountPath: /persistentvolumesenv:- name: PROVISIONER_NAME ## 这个名字必须与storegeclass里面的名字一致value: my-jenkins-nfs- name: ENABLE_LEADER_ELECTION ## 设置高可用允许选举,如果replicas参数等于1,可不用value: "True"- name: NFS_SERVERvalue: 192.168.31.80 #修改为自己的ip(部署nfs的机器ip)- name: NFS_PATHvalue: /var/nfs/jenkins #修改为自己的nfs安装目录volumes:- name: jenkins-nfs-client-rootnfs:server: 192.168.31.80 #修改为自己的ip(部署nfs的机器ip)path: /var/nfs/jenkins #修改为自己的nfs安装目录
3.4 创建StorageClass
cat > jenkins-storeclass.yamlapiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:name: jenkins-nfs-storagenamespace: jenkins
provisioner: my-jenkins-nfs
3.5 创建PVC卷
cat > jenkins-pvc.yamlapiVersion: v1
kind: PersistentVolumeClaim
metadata:name: jenkins-pvcnamespace: jenkinslabels:pvc: jenkins-pvc # 自定义
spec:storageClassName: jenkins-nfs-storageaccessModes:- ReadWriteOnceresources:requests:storage: 20G
四、创建Service
cat > jenkins-service.yamlapiVersion: v1
kind: Service
metadata:name: jenkins-servicenamespace: jenkinsannotations:prometheus.io/scrape: 'true' #这一段是官方文档带的,是用来让prometheus(普罗米修斯)来识别pod的,可暂时不用管他prometheus.io/path: /prometheus.io/port: '8080'
spec:selector:app: jenkins-servertype: NodePortports:- name: httpport: 8080targetPort: 8080nodePort: 32000- name: agentport: 50000targetPort: 50000nodePort: 50000
提示:
如果50000端口没挂载上,需要修改kube-apiserver.yaml
vim /etc/kubernetes/manifests/kube-apiserver.yaml- --service-node-port-range=3000-55000 #这行默认没有,自己给他加上,那端口范围我就不用解释了吧
五、创建deployment
cat > jenkins-deployment.yamlapiVersion: apps/v1
kind: Deployment
metadata:name: jenkinsnamespace: jenkins
spec:replicas: 1selector:matchLabels:app: jenkins-servertemplate:metadata:labels:app: jenkins-serverspec:securityContext:fsGroup: 1000runAsUser: 1000serviceAccountName: jenkins-nfs-clientcontainers:- name: jenkinsimage: jenkins/jenkins:2.369 #镜像这里用这个就行,版本是2.369,官方文档的镜像版本太低,好像是2.2版本的,不支持一些插件imagePullPolicy: IfNotPresentports:- name: httpportcontainerPort: 8080- name: jnlpportcontainerPort: 50000volumeMounts:- name: jenkins-datamountPath: /var/jenkins_home #这里为jenkins工作目录,挂载出来就好了,这样重启jenkins数据也不会丢失volumes:- name: jenkins-datapersistentVolumeClaim:claimName: jenkins-pvcreadOnly: false
六、登录验证
6.1 登录
进入k8s页面,进入到jenkins的deploment服务工作页面,找到服务的ip
然后浏览器输入访问地址:
Sign in [Jenkins]
输出默认的admin登录,密码初始密码在安装jenkins的 /var/jenkins_home/secrets/initialAdminPassword
可以cat /var/jenkins_home/secrets/initialAdminPassword 打印出来,复制粘贴出来登录
登录之后,可以创建自己的账号和密码
好了,今天的在k8s中部署jenkins就分享到这里!欢约大家点赞+收藏,有疑问也欢迎大家评论留言!