Kubernetes部署Jenkins

小柒博客 Jenkins评论173字数 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

6、访问Jenkins

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

Kubernetes部署Jenkins

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

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

Kubernetes部署Jenkins

3)登录Jenkins

Kubernetes部署Jenkins

4)安装推荐插件

Kubernetes部署Jenkins

Kubernetes部署Jenkins

5)使用admin登录

Kubernetes部署Jenkins

6)实例配置

Kubernetes部署Jenkins

Kubernetes部署Jenkins

7)进入Jenkins界面

Kubernetes部署Jenkins

8)修改密码

Kubernetes部署Jenkins

Kubernetes部署Jenkins

9)重启Jenkins加载中文插件

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

Kubernetes部署Jenkins

Kubernetes部署Jenkins

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

Kubernetes部署Jenkins

Kubernetes部署Jenkins

继续阅读
若文章图片、下载链接等信息出错,请在评论区留言反馈,博主将第一时间更新!如本文“对您有用”,欢迎随意打赏,谢谢!
  • 微信号
  • 微信扫一扫加我!
  • weinxin
  • 微信公众号
  • 微信扫一扫关注我!
  • weinxin
小柒博客
  • 本文由 发表于 2023年10月14日 15:46:29
  • 声明:本站所有文章,如无特殊说明或标注,本站文章均为原创。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。转载请务必保留本文链接:https://www.yangxingzhen.com/9747.html
评论  0  访客  0
匿名

发表评论

匿名网友 填写信息

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