Kubernetes部署Jenkins

小辉助手 Jenkins评论1.4K字数 4446阅读14分49秒阅读模式

一、系统环境

IP

角色

主机名

Pod

192.168.2.198

Master

k8s-master1

192.168.2.199

Master

k8s-master2

192.168.2.200

Master

k8s-master3

192.168.2.195

Node

k8s-node1

Jenkins

192.168.2.196

Node

k8s-node2

Jenkins

192.168.2.197

Node

k8s-node3

Jenkins

192.168.2.201

NFS

k8s-nfs

注:本教程使用kubernetes v1.27.6版本,其他版本请自行测试。

二、部署NFS

1、安装NFS

[root@k8s-nfs ~]# yum -y install rpcbind nfs-utils

2、创建共享目录

[root@k8s-nfs ~]# mkdir -p /data/jenkins

3、配置exports

[root@k8s-nfs ~]# cat >>/etc/exports <<EOF

/data/jenkins 192.168.2.0/24(rw,no_root_squash,no_all_squash,sync)

EOF

4、启动rpcbind、nfs服务

[root@k8s-nfs ~]# systemctl start rpcbind

[root@k8s-nfs ~]# systemctl start nfs

[root@k8s-nfs ~]# systemctl enable rpcbind

[root@k8s-nfs ~]# systemctl enable nfs

5、查看共享目录

[root@k8s-nfs ~]# showmount -e 192.168.2.201

Export list for 192.168.2.201:

/data/jenkins 192.168.2.0/24

三、创建StorageClass、Namespace

1、创建StorageClass

[root@k8s-master1 ~]# mkdir kube-devops && cd kube-devops

[root@k8s-master1 kube-devops]# vim kube-devops-storageclass.yaml

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: devops-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer

[root@k8s-master1 kube-devops]# kubectl apply -f kube-devops-storageclass.yaml

storageclass.storage.k8s.io/devops-storage created

2、创建Namespace

[root@k8s-master1 kube-devops]# vim kube-devops-namespace.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: kube-devops
  labels:
    app: devops

[root@k8s-master1 kube-devops]# kubectl apply -f kube-devops-namespace.yaml

namespace/kube-devops created

3、Node节点安装nfs-utils

[root@k8s-node1 ~]# yum -y install nfs-utils

[root@k8s-node2 ~]# yum -y install nfs-utils

[root@k8s-node3 ~]# yum -y install nfs-utils

四、部署Jenkins

1、创建PV、PVC

[root@k8s-master1 kube-devops]# vim kube-devops-nfs.yaml

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: jenkins-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
  - ReadWriteMany
  persistentVolumeReclaimPolicy: Delete
  nfs:
    server: 192.168.2.201
    path: /data/jenkins
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: jenkins-pvc
  namespace: kube-devops
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 5Gi

[root@k8s-master1 kube-devops]# kubectl apply -f kube-devops-nfs.yaml

persistentvolume/jenkins-pv unchanged

persistentvolumeclaim/jenkins-pvc created

2、创建角色授权

[root@k8s-master1 kube-devops]# vim kube-devops-rbac.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: jenkins-admin  
  namespace: kube-devops
  labels:
    name: jenkins
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: jenkins-admin
  labels:
    name: jenkins
subjects:
  - kind: ServiceAccount
    name: jenkins-admin
    namespace: kube-devops
roleRef:
  kind: ClusterRole
  name: cluster-admin
  apiGroup: rbac.authorization.k8s.io

[root@k8s-master1 kube-devops]# kubectl apply -f kube-devops-rbac.yaml

serviceaccount/jenkins-admin created

clusterrolebinding.rbac.authorization.k8s.io/jenkins-admin created

3、创建Deployment

[root@k8s-master1 kube-devops]# vim kube-devops-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
  namespace: kube-devops
  labels:
    app: jenkins
spec:
  selector:
    matchLabels:
      app: jenkins
  replicas: 1
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      serviceAccountName: jenkins-admin
      containers:
      - name: jenkins
        image: jenkins/jenkins:lts
        securityContext:                     
          runAsUser: 0 
          privileged: true
        ports:
        - name: http
          containerPort: 8080
        - name: jnlp
          containerPort: 50000
        resources:
          limits:
            memory: 2Gi
            cpu: "1000m"
          requests:
            memory: 1Gi
            cpu: "500m"
        env:
        - name: LIMITS_MEMORY
          valueFrom:
            resourceFieldRef:
              resource: limits.memory
              divisor: 1Mi
        - name: "JAVA_OPTS"
          value: "
                   -Xmx$(LIMITS_MEMORY)m
                   -XshowSettings:vm
                   -Dhudson.slaves.NodeProvisioner.initialDelay=0
                   -Dhudson.slaves.NodeProvisioner.MARGIN=50
                   -Dhudson.slaves.NodeProvisioner.MARGIN0=0.85
                   -Duser.timezone=Asia/Shanghai
                 "    
        volumeMounts:               
        - name: data
          mountPath: /var/jenkins_home
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: jenkins-pvc

[root@k8s-master1 kube-devops]# kubectl apply -f kube-devops-deployment.yaml

deployment.apps/jenkins created

4、创建Service

[root@k8s-master1 kube-devops]# vim kube-devops-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: jenkins
  namespace: kube-devops
  labels:
    app: jenkins
spec:
  type: NodePort
  ports:
  - name: http
    port: 8080
    targetPort: 8080
    nodePort: 32001
  - name: jnlp
    port: 50000         
    targetPort: 50000
    nodePort: 32002
  selector:
    app: jenkins

[root@k8s-master1 kube-devops]# kubectl apply -f kube-devops-service.yaml

service/jenkins created

5、查看Pod和Service

[root@k8s-master1 kube-devops]# kubectl get pod,svc -n kube-devops

Kubernetes部署Jenkins-图片1

6、访问Jenkins

1)浏览器输入http://192.168.2.198:32001,如下图所示

Kubernetes部署Jenkins-图片2

2)获取Jenkins初始化生成密码

[root@k8s-master1 kube-devops]# kubectl logs -n kube-devops jenkins-6dd7dddb8-kkgr9

Kubernetes部署Jenkins-图片3

3)登录Jenkins

Kubernetes部署Jenkins-图片4

4)安装推荐插件

Kubernetes部署Jenkins-图片5

Kubernetes部署Jenkins-图片6

5)使用admin登录

Kubernetes部署Jenkins-图片7

6)实例配置

Kubernetes部署Jenkins-图片8

Kubernetes部署Jenkins-图片9

7)进入Jenkins界面

Kubernetes部署Jenkins-图片10

8)修改密码

Kubernetes部署Jenkins-图片11

Kubernetes部署Jenkins-图片12

9)重启Jenkins加载中文插件

# 浏览器输入http://192.168.2.198:32001,如下图所示

Kubernetes部署Jenkins-图片13

Kubernetes部署Jenkins-图片14

10)使用新密码重新登录Jenkins

Kubernetes部署Jenkins-图片15

Kubernetes部署Jenkins-图片16

若文章图片、下载链接等信息出错,请在评论区留言反馈,博主将第一时间更新!如本文“对您有用”,欢迎随意打赏,谢谢!

继续阅读
微信号
微信扫一扫,加我!
weinxin
微信公众号
微信扫一扫,关注我!
weinxin
小辉助手
  • 本文由 小辉助手 发表于 2023年11月7日15:14:24
  • 声明:本站所有文章,如无特殊说明或标注,本站文章均为原创。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。转载请务必保留本文链接:https://www.yangxinghui.cn/1635.html
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

拖动滑块以完成验证