1. 程式人生 > 實用技巧 >Jenkins的部署安裝

Jenkins的部署安裝

一、安裝JDK

(1) 下載JDK1.8

下載地址:https://www.oracle.com/java/technologies/javase-downloads.html

[root@jenkins ~]# ll jdk-8u251-linux-x64.tar.gz 
-rw-r--r-- 1 root root 195132576 Jun  4 14:10 jdk-8u251-linux-x64.tar.gz

(2) 解壓安裝包並配置環境變數

[root@jenkins ~]# mkdir /usr/java
[root@jenkins ~]# tar -xf jdk-8u251-linux-x64.tar
.gz -C /usr/java [root@jenkins ~]# vim /etc/profile.d/java.sh export JAVA_HOME=/usr/java/jdk1.8.0_251 export PATH=$JAVA_HOME/bin/:$PATH export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$CLASSPATH [root@jenkins ~]# source /etc/profile.d/java.sh

(3) 驗證是否安裝成功

[root@jenkins ~]# java -version
java version 
"1.8.0_251" Java(TM) SE Runtime Environment (build 1.8.0_251-b08) Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)

二、安裝git

[root@jenkins ~]# yum install -y git

三、安裝docker

3.1、線上安裝

(1) 設定倉庫,安裝所需的軟體包。

yum-utils 提供了 yum-config-manager ,並且 device mapper 儲存驅動程式需要 device-mapper-persistent-data 和 lvm2。

[root@jenkins ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
[root@jenkins ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

(2) 安裝依賴包 container-selinux

[root@jenkins ~]# wget http://mirror.centos.org/centos/7/extras/x86_64/Packages/container-selinux-2.119.1-1.c57a6f9.el7.noarch.rpm
[root@jenkins ~]# yum install -y ./container-selinux-2.119.1-1.c57a6f9.el7.noarch.rpm

(3) 安裝Docker Engine-Community和containerd

[root@jenkins ~]# yum install -y docker-ce docker-ce-cli containerd.io

(4) 檢查docker是否安裝成功

[root@jenkins ~]# systemctl start docker
[root@jenkins ~]# docker version
Client: Docker Engine - Community
 Version:           19.03.12
 API version:       1.40
 Go version:        go1.13.10
 Git commit:        48a66213fe
 Built:             Mon Jun 22 15:46:54 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.12
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.10
  Git commit:       48a66213fe
  Built:            Mon Jun 22 15:45:28 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

3.2、離線安裝

使用離線yum安裝的方式,需提前下載好相應的rpm包,這裡以版本dockers-19.0.3.4為例。

(1) 下載docker-ce、docker-ce-cli、containerd.io安裝包

下載地址:https://download.docker.com/linux/centos/7/x86_64/stable/Packages/

相關包名:
docker-ce-19.03.4-3.el7.x86_64.rpm
docker-ce-cli-19.03.4-3.el7.x86_64.rpm
containerd.io-1.2.6-3.3.el7.x86_64.rpm

(2) 下載container-selinux安裝包

下載地址:https://pkgs.org/download/container-selinux

相關包名:container-selinux-2.107-3.el7.noarch.rpm

(3) 下載相關依賴包

相關包名:
audit-2.8.5-4.el7.x86_64.rpm
audit-libs-2.8.5-4.el7.x86_64.rpm
audit-libs-python-2.8.5-4.el7.x86_64.rpm
checkpolicy-2.5-8.el7.x86_64.rpm
libcgroup-0.41-21.el7.x86_64.rpm
libseccomp-2.3.1-4.el7.x86_64.rpm
libsemanage-python-2.5-14.el7.x86_64.rpm
libtool-ltdl-2.4.2-22.el7_3.x86_64.rpm
policycoreutils-2.5-34.el7.x86_64.rpm
policycoreutils-python-2.5-34.el7.x86_64.rpm
python-IPy-0.75-6.el7.noarch.rpm
setools-libs-3.3.8-4.el7.x86_64.rpm

下載地址:http://mirror.centos.org/centos/7/extras/x86_64/Packages

相關包名:pigz-2.3.3-1.el7.centos.x86_64.rpm

(4) 安裝

將以上包放在一個資料夾裡,如install_docker,然後進行安裝即可。

yum install install_docker/*.rpm

(5) 啟動

systemctl start docker
systemctl stop docker

(6) 安裝docker-compose (非必需)

 首先訪問 docker-compose 的 GitHub 版本釋出頁面:https://github.com/docker/compose/releases ,下載 Linux 版本的 docker-compose。

[root@localhost ~]# ll docker-compose-Linux-x86_64 
-rw-r--r-- 1 root root 12255808 Aug 13 14:14 docker-compose-Linux-x86_64

再將檔案移至/usr/local/bin/目錄下,並加執行許可權即可。

[root@localhost ~]# mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose
[root@localhost ~]# chmod +x /usr/local/bin/docker-compose 
[root@localhost ~]# docker-compose -version
docker-compose version 1.26.1, build f216ddbf

四、安裝Jenkins

(1) 安裝倉庫並匯入驗證金鑰

[root@jenkins ~]# wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
[root@jenkins ~]# rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
[root@jenkins ~]# yum install -y jenkins

如果出現 Delta RPMs disabled because /usr/bin/applydeltarpm not installed. 則安裝以下包:

[root@jenkins ~]# yum -y  install deltarpm

如果網路較慢,可以提前下載rpm包,使用離線安裝方式。

[root@jenkins ~]# wget http://pkg.jenkins.io/redhat-stable/jenkins-2.235.3-1.1.noarch.rpm

(2) 修改jenkins的啟動指令碼 /etc/init.d/jenkins 中的java路徑

[root@jenkins ~]# vim /etc/init.d/jenkins
candidates="
/etc/alternatives/java
/usr/lib/jvm/java-1.8.0/bin/java
/usr/lib/jvm/jre-1.8.0/bin/java
/usr/lib/jvm/java-1.7.0/bin/java
/usr/lib/jvm/jre-1.7.0/bin/java
/usr/lib/jvm/java-11.0/bin/java
/usr/lib/jvm/jre-11.0/bin/java
/usr/lib/jvm/java-11-openjdk-amd64
/usr/bin/java
/usr/java/jdk1.8.0_251/bin/java            #新增此行
"
##如果不想改檔案,可以對java路徑做一個軟連結##
[root@jenkins ~]# ln -sv /usr/java/jdk1.8.0_251/bin/java /usr/bin/java

(3) 啟動

[root@jenkins ~]# systemctl daemon-reload
[root@jenkins ~]# systemctl start jenkins
[root@jenkins ~]# ss -nlt|grep 8080
LISTEN     0      50          :::8080                    :::*

(4) 修改預設映象源

[root@jenkins ~]# vim /var/lib/jenkins/hudson.model.UpdateCenter.xml
#將<url>中的源為國內源
<?xml version='1.1' encoding='UTF-8'?>
<sites>
  <site>
    <id>default</id>
    <url>https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json</url>
  </site>
</sites>

(5) 加快jenkins初始化安裝外掛過程

[root@jenkins ~]# find / -name 'default.json'
/var/lib/jenkins/updates/default.json
[root@jenkins ~]# cp /var/lib/jenkins/updates/default.json /var/lib/jenkins/updates/default.json_bak
[root@jenkins ~]# sed -i 's/http:\/\/updates.jenkins-ci.org\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' /var/lib/jenkins/updates/default.json && sed -i 's/http:\/\/www.google.com/https:\/\/www.baidu.com/g' /var/lib/jenkins/updates/default.json
[root@jenkins ~]# systemctl restart jenkins

(6) 在瀏覽器輸入 http://IP:8080 進行下一步操作,頁面提示初始密碼在以下檔案中/var/lib/jenkins/secrets/initialAdminPassword,在伺服器上檢視密碼,並在瀏覽器登入即可

[root@jenkins ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
f79dacc653c4484bbab603bab1694b49