Jenkins迁移及日常操作的一点总结

之前在公司机房部署了一套jenkins环境,现需要迁移至IDC机房服务器上,迁移过程中记录了一些细节:
1)jenkins默认的主目录放在当前用户家目录路径下的.jenkins目录中。如jenkins使用root用户启动,则主目录为/root/.jenkins

1
2
[[email protected] ~]# ll -d /root/.jenkins/
drwxr-x--- 14 root root 4096 Dec 18 15:22 /root/.jenkins/

2)可以在系统环境变量里手动设置jenkins的主目录,这样启动jenkins后,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
root用户可以在/etc/profile文件里设置
[[email protected] ~]# vim /etc/profile
......
JENKINS_HOME="/data/jenkins"
export JENKINS_HOME
  
[[email protected] ~]# source /etc/profile
  
===========================================================
如果是非root用户,就在用户家目录的.bashrc文件里设置
[[email protected] ~]$ vim .bashrc
......
JENKINS_HOME="/data/jenkins"
export JENKINS_HOME
  
[[email protected] ~]$ source .bashrc
[[email protected] ~]$ echo $JENKINS_HOME
/data/tomcat8.5/webapps/jenkins
[[email protected] ~]$ env
.......
JENKINS_HOME=/data/jenkins
==========================================================
 
温馨提示:
最好别将jenkins主目录指定到tomcat/webapps/jenkins,因为如果有其他同名的tomcat项目启动,容易造成数据覆盖!
所以最好将jenkins主目录指定到其他地方,比如指定到/data/jenkins

jenkins主目录设置之后,可以登录jenkins界面查看它的主目录路径,依次点击"Jenkins"->"系统管理"->"系统设置"

Jenkins迁移及日常操作的一点总结

3)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
迁移步骤为:
1)先关闭新老服务器的tomcat程序,确保迁移时新老机器的jenkins都处于关闭状态。jenkins程序关闭最好是直接kill掉jenkins的tomcat程序pid。
2)将老服务器jenkins主目录下的config.xml文件以及jobs、users、workspace、plugins四个目录拷贝到新机器的jenkins主目录下。
3)重启新服务器jenkins的tomcat程序。
 
迁移的时候可以直接将jenkins主目录数据整个拷贝过去,也可以单独拷贝jenkins主目录下的config.xml文件以及jobs、users、workspace、plugins四个目录(这是主要的迁移数据)。一般来说,手动设置好jenkins主目录路径,启动jenkins后就会自动生成(但要确保jenkins用户有权限创建这个主目录,最好是提前手动创建并赋予jenkins启动用户的权限)
 
关闭老机器的jenkins程序
[[email protected] ~]# lsof -i:8080
COMMAND    PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
bundle   13481  git   15u  IPv4 2839661      0t0  TCP localhost:webcache (LISTEN)
[[email protected] ~]# kill -9 13481
 
新机器的jenkins程序也要同样关闭
 
拷贝老服务器的jenkins主目录或者上面说的那几个重要数据到新机器的jenkins主目录下
[[email protected] ~]# rsync -e "ssh -p22" -avpgolr --delete /data/jenkins/ [email protected]:/data/jenkins/
 
或者
[[email protected] ~]# rsync -e "ssh -p22" -avpgolr /data/jenkins/config.xml [email protected]:/data/jenkins/
[[email protected] ~]# rsync -e "ssh -p22" -avpgolr --delete /data/jenkins/users/ [email protected]:/data/jenkins/users/
[[email protected] ~]# rsync -e "ssh -p22" -avpgolr --delete /data/jenkins/plugins/ [email protected]:/data/jenkins/plugins/
[[email protected] ~]# rsync -e "ssh -p22" -avpgolr --delete /data/jenkins/jobs/ [email protected]:/data/jenkins/jobs/
[[email protected] ~]# rsync -e "ssh -p22" -avpgolr --delete /data/jenkins/workspace/ [email protected]:/data/jenkins/workspace/
 
尤其是plugins目录,最好保证新机器下的这个目录和老机器下的这个目录数据保持一致。否则容易造成新机器的jenkins访问报错
 
最后启动新机器的jenkins服务
[[email protected] ~]$ /data/tomcat8.5/bin/startup.sh
[[email protected] ~]$ lsof -i:8080
COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
java    101037  app   46u  IPv6 498942      0t0  TCP *:webcache (LISTEN)

4)通过ssh方式下载gitlab代码到jenkins本机

1
2
3
4
5
6
7
8
9
10
11
一般来说,会在jenkins界面里创建工程,在工程里配置gitlab的地址,将gitlab代码下载到jenkins本机,然后通过脚本自动发版。
安全考虑,通过ssh方式下载gitlab代码。这就需要将jenkins本机的id_rsa.pub公钥上传到gitlab里。
1)如果jenkins程序通过root用户启动,则需要将root用户下的id_rsa.pub公钥上传到gitlab的SSH Keys里。
2)如果jenkins程序通过非root用户启动,则需要将非root用户的id_rsa.pub公钥上传到gitlab的SSH Keys里。
 
比如jenkins程序是通过app用户启动的
[[email protected] ~]$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAw/I9657ZRmducrkbagPfptLwRaCmJIIQIcQ3VljBLBlwyNFBYg6KfiktMB4KHlvu4WCrGpDjVtWf7gQy
Ey+iJNsL7TyiIZdg0RRGpssu93w6IhgoHtRZni/775MrdjLQpi7hhB6wiX+eCfU7duqXT+arnEUonAF+27HegVbXuqz+oeDS/1QBzKsOoMg0K4nA7Btl
GNIn1ljvvQzyHxIJevWM0UUhFl8lv9+RCcT0cyHmjSrw/9+gr4AYZmYaqlVmCWHmwuAixx7bt3Mh3ri+BK385qAUxaVVsw3kg/vHbJEg+JYn/Xm4pdnw
j+CLn6OpQAMZm+bEx12Iwd3gazBy+Q== [email protected]

Jenkins迁移及日常操作的一点总结

Jenkins迁移及日常操作的一点总结

5)非root用户启动jenkins的tomcat程序

1
2
3
需要记住的一个细节:
在linux系统下,只有root用户才可以使用1024以下的端口号,非root用户只能启动1024以上的端口。
所有如果使用非root用户启动jenkins,则端口必须配置成大于1024的,比如采用默认的8080端口,如果配置成80端口,则启动失败!

6)当ssh是非标准的22端口时,进行git clone下载gitlab代码

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
如上,将jenkins本机的id_rsa.pub公钥拷贝到gitlab的SSH Keys里。
 
1)如果jenkins机器和gitlab机器ssh都采用默认的22端口,则就可以直接git clone下载git代码了。
[[email protected] ~]$ mkdir /data/git_data/
[[email protected] ~]$ cd /data/git_data/
[[email protected] git_data]$ git init .
Reinitialized existing Git repository in /data/git_data/.git/
 
[[email protected] git_data]$ git clone [email protected]:fanglianchao/dbops.git
Initialized empty Git repository in /data/git_data/dbops/.git/
Warning: Permanently added '172.16.50.25' (RSA) to the list of known hosts.
remote: Counting objects: 1224, done.
remote: Compressing objects: 100% (812/812), done.
remote: Total 1224 (delta 379), reused 1220 (delta 377)
Receiving objects: 100% (1224/1224), 9.50 MiB, done.
Resolving deltas: 100% (379/379), done.
 
[[email protected] git_data]$ ls
dbops
 
====================================================================
2)如果jenkins机器和gitlab机器ssh采用的端口不一致,这就需要在jenkins本机的.ssh目录下手动创建config文件,在config文件中指定
连接gitlab时的信息。
 
例如:jenkins本机的ssh端口是6666,jenkins本机(172.16.50.25)的ssh端口是22,则在jenkins本机的操作如下:
[[email protected] ~]$ mkdir /data/git_data/
[[email protected] ~]$ cd /data/git_data/
[[email protected] git_data]$ git init .
Reinitialized existing Git repository in /data/git_data/.git/
 
[[email protected] ~]$ cat ~/.ssh/config
Host "172.16.50.25"
Port 22
 
注意:config文件必须是600权限
[[email protected] git_data]$ sudo chmod 600 ~/.ssh/config
[[email protected] git_data]$ ll ~/.ssh/config
-rw-------. 1 app app 28 Dec 20 23:26 /home/app/.ssh/config
 
然后就可以正常git clone下载代码了
[[email protected] git_data]$ git clone [email protected]:qwfss/qwfss.git
Initialized empty Git repository in /data/git_data/qwfss/.git/
remote: Counting objects: 110, done.
remote: Compressing objects: 100% (59/59), done.
remote: Total 110 (delta 23), reused 0 (delta 0)
Receiving objects: 100% (110/110), 19.99 KiB, done.
Resolving deltas: 100% (23/23), done.
[[email protected] git_data]$ ls
qwfss

7)下载gitlab上非master分支代码

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
比如将gitlab上的[email protected]:qwfss/qwfss.git下develop分支代码下载到jenkins本机,操作如下:
 
[[email protected] git_data]$ git clone [email protected]:qwfss/qwfss.git
Initialized empty Git repository in /data/git_data/qwfss/.git/
remote: Counting objects: 110, done.
remote: Compressing objects: 100% (59/59), done.
remote: Total 110 (delta 23), reused 0 (delta 0)
Receiving objects: 100% (110/110), 19.99 KiB, done.
Resolving deltas: 100% (23/23), done.
 
[[email protected] git_data]$ ls
qwfss
[[email protected] git_data]$ cd qwfss/
 
查看分支详细情况 (推荐这种方式)
[[email protected] qwfss]$ git branch
* develop
[[email protected] qwfss]$ git branch -av
* develop                29e5e1f fix(fss): 测试环境配置文件同步
  remotes/origin/HEAD    -> origin/develop
  remotes/origin/develop 29e5e1f fix(fss): 测试环境配置文件同步
 
切换到develop分支下
[[email protected] qwfss]$ git checkout -b develop origin/develop
fatal: git checkout: branch develop already exists
====================================================================
或者
[[email protected] qwfss]$ git checkout -b testapp remotes/origin/develop
====================================================================
 
[[email protected] qwfss]$ git branch
* develop
[[email protected] qwfss]$ git branch -av
* develop                29e5e1f fix(fss): 测试环境配置文件同步
  remotes/origin/HEAD    -> origin/develop
  remotes/origin/develop 29e5e1f fix(fss): 测试环境配置文件同步
 
=====================================================================
git分支的日常操作可以参考:http://www.cnblogs.com/kevingrace/p/5690820.html

8)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
部署jenkins备机时,只需要定期将master机器上jenkins主目录数据拷贝到本机上即可。
    
比如:
jenkins master:10.0.8.60   jenkins01
jenkins slave:10.0.8.61    jenkins02
    
两台机器的jenkins主目录都是:/data/jenkins
[[email protected] ~]$ vim /etc/profile
JENKINS_HOME="/data/jenkins"
export JENKINS_HOME
[[email protected] ~]$ source /etc/profile
 
手动指定jenkins主目录后,待jenkins启动后,该主目录会自动生成。
但是要注意的是:jenkins启动用户要有权限生成主目录(可以提前手动创建该目录,并将权限设置成jenkins启动用户的权限)
    
那么只需要定期将master机器10.0.8.60上的/data/jenkins目录下的文件拷贝到10.0.8.61机器/data/jenkins下即可!
   
10.0.8.61上写备份脚本(两台机器提前做app账号下的ssh无密码登陆的信任关系):
[[email protected] ~]$ cat /data/script/rsync_jenkins02.sh          (如下脚本,如果不添加--delete参数,则只会同步增加的数据,删除的数据不会同步)
#!/bin/bash
/usr/bin/rsync -e "ssh -p6666" -avpgolr --delete /data/jenkins/ [email protected]:/data/jenkins/
 
/usr/bin/ssh -p6666 [email protected] "/bin/bash -x /home/app/kill_tomcat.sh"
/usr/bin/ssh -p6666 [email protected] "/data/tomcat8.5/bin/startup.sh"
   
10.0.8.61上的脚本:
[[email protected] ~]$ cat /home/app/kill_tomcat.sh
#!/bin/bash
ps -ef|grep java|grep -v grep|awk '{print $2}'|xargs kill -9
   
如上脚本准备好后,只需要每天定时去执行10.0.8.60机器上的同步脚本/data/script/rsync_jenkins02.sh,即可完成jenkins的备机操作了。
***************当你发现自己的才华撑不起野心时,就请安静下来学习吧***************

本文转自散尽浮华博客园博客,原文链接:http://www.cnblogs.com/kevingrace/p/8076488.html,如需转载请自行联系原作者