jenkins yum部署
jenkins源
1 2 3 4 5 6 7 8 9
| cp -a /etc/yum.repos.d /etc/yum.repos.d.backup rm -f /etc/yum.repos.d/* curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
|
jdk17环境获取
方法一:二进制安装
1 2 3 4 5 6 7 8 9 10 11 12 13
| wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.tar.gz
tar -xf jdk-17_linux-x64_bin.tar.gz mv jdk-17.0.12 /usr/local/jdk-17
echo "export JAVA_HOME=/usr/local/jdk-17" >> /etc/profile echo 'export PATH=$JAVA_HOME/bin:$PATH' >> /etc/profile source /etc/profile
java --version java 17.0.12 2024-07-16 LTS Java(TM) SE Runtime Environment (build 17.0.12+8-LTS-286) Java HotSpot(TM) 64-Bit Server VM (build 17.0.12+8-LTS-286, mixed mode, sharing)
|
方法二:rpm安装
1 2 3 4 5 6 7 8 9 10 11
| wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.rpm rpm -ivh jdk-17_linux-x64_bin.rpm echo "export JAVA_HOME=/usr/lib/jvm/jdk-17-oracle-x64/" >> /etc/profile echo 'export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin' >> /etc/profile echo 'export CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar' >> /etc/profile source /etc/profile
java --version java 17.0.12 2024-07-16 LTS Java(TM) SE Runtime Environment (build 17.0.12+8-LTS-286) Java HotSpot(TM) 64-Bit Server VM (build 17.0.12+8-LTS-286, mixed mode, sharing)
|
安装jenkins
1 2 3 4
| yum -y install fontconfig yum -y install jenkins
systemctl enable jenkins.service --now
|
jenkins k8s容器化部署
环境说明
1 2 3 4 5 6 7 8
| k8s版本v1.28.1 Jenkins版本 2.452 192.168.10.121 ws-k8s-master1 192.168.10.122 ws-k8s-master2 192.168.10.123 ws-k8s-master3 192.168.10.131 ws-k8s-node1 192.168.10.132 ws-k8s-node2 192.168.10.133 ws-k8s-node3
|
环境准备
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| cat > /etc/hosts << EOF 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.10.100 1panel 192.168.10.120 pve 192.168.10.121 ws-k8s-master1 192.168.10.122 ws-k8s-master2 192.168.10.123 ws-k8s-master3 192.168.10.130 harbor 192.168.10.131 ws-k8s-node1 192.168.10.132 ws-k8s-node2 192.168.10.133 ws-k8s-node3 192.168.10.140 docker-host 192.168.10.141 ceph-node1 192.168.10.142 ceph-node2 192.168.10.143 ceph-node3 EOF
cat > /etc/docker/daemon.json << EOF { "registry-mirrors": [ "https://hub-mirror.c.163.com", "https://docker.m.daocloud.io", "https://ghcr.io", "https://mirror.baidubce.com", "https://docker.nju.edu.cn"], "exec-opts": ["native.cgroupdriver=systemd"], "insecure-registries": ["192.168.10.130","harbor"] } EOF systemctl daemon-reload systemctl restart docker
docker pull jenkins:latest docker save jenkins:latest -o jenkins.latest.tar.gz ctr -n=k8s.io image import jenkins.latest.tar.gz
|
目录准备
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| mkdir jenkins cd jenkins
yum -y install nfs-utils && systemctl enable nfs-server.service --now
mkdir -p /data/v1 mkdir -p /data/v2 cat > /etc/exports << EOF /data/v1 *(rw,no_root_squash) /data/v2 *(rw,no_root_squash) EOF exportfs -arv
kubectl create namespace jenkins-k8s
cat > pv.yaml << EOF apiVersion: v1 kind: PersistentVolume metadata: name: jenkins-k8s-pv spec: capacity: storage: 10Gi accessModes: - ReadWriteMany nfs: server: 192.168.10.121 path: /data/v2 --- kind: PersistentVolumeClaim apiVersion: v1 metadata: name: jenkins-k8s-pvc namespace: jenkins-k8s spec: resources: requests: storage: 10Gi accessModes: - ReadWriteMany EOF kubectl get pv -n jenkins-k8s NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE jenkins-k8s-pv 10Gi RWX Retain Bound jenkins-k8s/jenkins-k8s-pvc 42s
kubectl create sa jenkins-k8s-sa -n jenkins-k8s kubectl create clusterrolebinding jenkins-k8s-sa-cluster -n jenkins-k8s \ --clusterrole=cluster-admin --serviceaccount=jenkins-k8s:jenkins-k8s-sa
ctr -n=k8s.io image import jenkins-slave-latest.tar.gz
|
deployment文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| kind: Deployment apiVersion: apps/v1 metadata: name: jenkins namespace: jenkins-k8s spec: replicas: 1 selector: matchLabels: app: jenkins template: metadata: labels: app: jenkins spec: serviceAccount: jenkins-k8s-sa containers: - name: jenkins image: jenkins/jenkins:2.328 imagePullPolicy: IfNotPresent ports: - containerPort: 8080 name: web protocol: TCP - containerPort: 50000 name: agent protocol: TCP resources: limits: cpu: 2000m memory: 2Gi requests: cpu: 500m memory: 512Mi livenessProbe: httpGet: path: /login port: 8080 initialDelaySeconds: 60 timeoutSeconds: 5 failureThreshold: 12 readinessProbe: httpGet: path: /login port: 8080 initialDelaySeconds: 60 timeoutSeconds: 5 failureThreshold: 12 volumeMounts: - name: jenkins-volume subPath: jenkins-home mountPath: /var/jenkins_home volumes: - name: jenkins-volume persistentVolumeClaim: claimName: jenkins-k8s-pvc
|
部署Jenkins
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| chown -R 1000.1000 /data/v2 kubectl apply -f jenkins-deployment.yaml
cat > jenkins-service.yaml << EOF apiVersion: v1 kind: Service metadata: name: jenkins-service namespace: jenkins-k8s labels: app: jenkins spec: selector: app: jenkins type: NodePort ports: - name: web port: 8080 targetPort: web nodePort: 30002 - name: agent port: 50000 targetPort: agent EOF kubectl apply -f jenkins-service.yaml
web访问http://192.168.10.200:30002/ 使用管理员密码登录 cat /var/jenkins_home/secrets/initialAdminPassword
按推荐插件安装 创建管理员用户admin →保存并完成
选择管理Jenkins——插件管理——可选插件——搜索k8s、与blueOcean然后安装——重启生效
|