之前在公司机房部署了一套jenkins环境,现需要迁移至IDC机房服务器上,迁移过程中记录了一些细节:
1)jenkins默认的主目录放在当前用户家目录路径下的.jenkins目录中。如jenkins使用root用户启动,则主目录为/root/.jenkins
[[email protected] ~]# ll -d /root/.jenkins/ drwxr-x--- 14 root root 4096 Dec 18 15:22 /root/.jenkins/
2)可以在系统环境变量里手动设置jenkins的主目录,这样启动jenkins后,jenkins数据就会写入到手动设置的主目录里。
root用户可以在/etc/profile文件里设置 [[email protected] ~]# vim /etc/profile ...... JENKINS_HOME="/data/tomcat8.5/webapps/jenkins" export JENKINS_HOME [[email protected] ~]# source /etc/profile =========================================================== 如果是非root用户,就在用户家目录的.bashrc文件里设置 [[email protected] ~]$ vim .bashrc ...... JENKINS_HOME="/data/tomcat8.5/webapps/jenkins" export JENKINS_HOME [[email protected] ~]$ source .bashrc [[email protected] ~]$ echo $JENKINS_HOME /data/tomcat8.5/webapps/jenkins [[email protected] ~]$ env ....... JENKINS_HOME=/data/tomcat8.5/webapps/jenkins
3)jenkins迁移
一、如果迁移前没有指定jenkins主目录,则迁移步骤为: 0)先关闭老服务器的tomcat程序。jenkins程序关闭最好是直接kill掉jenkins的tomcat程序pid。 1)将老服务器的jenkins的tomcat程序下的webapps里的jenkins和jenkins.war拷贝到新服务器的jenkins的tomcat的webapps目录下(最好是直接拷贝tomcat整个程序目录) 2)将.jenkins默认主目录下的users、workspace、plugins目录下的数据文件拷贝到新服务器的jenkins的tomcat的webapps/jenkins的对应目录下。 3)最后重启新服务器jenkins的tomcat程序 [[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 [[email protected] ~]# rsync -e "ssh -p22" -avpgolr /data/tomcat8.5 [email protected]:/data/ [[email protected] ~]# rsync -e "ssh -p22" -avpgolr /root/.jenkins/users [email protected]:/data/tomcat8/webapps/users [[email protected] ~]# rsync -e "ssh -p22" -avpgolr /root/.jenkins/plugins [email protected]:/data/tomcat8/webapps/plugins [[email protected] ~]# rsync -e "ssh -p22" -avpgolr /root/.jenkins/workspace [email protected]:/data/tomcat8/webapps/users 然后对比下上面拷贝的三个目录users、workspace、plugins下的文件是否完整的拷贝过来,最后重启新服务器jenkins的tomcat程序 [[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) 二、如果迁移前已经手动指定了jenkins主目录,比如指定到tomcat安装目录的webapps/jenkins,则只需要将tomcat整个安装目录拷贝到新服务器上,然后启动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 [[email protected] ~]# rsync -e "ssh -p22" -avpgolr /data/tomcat8.5 [email protected]:/data/ [[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) 迁移完成后,登陆新服务器的jenkins界面,对比下迁移前后jenkins界面里创建的用户、工程、插件是否完整。
4)通过ssh方式下载gitlab代码到jenkins本机
一般来说,会在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]
5)非root用户启动jenkins的tomcat程序
需要记住的一个细节: 在linux系统下,只有root用户才可以使用1024以下的端口号,非root用户只能启动1024以上的端口。 所有如果使用非root用户启动jenkins,则端口必须配置成大于1024的,比如采用默认的8080端口,如果配置成80端口,则启动失败!
6)当ssh是非标准的22端口时,进行git clone下载gitlab代码
如上,将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分支代码
比如将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]$ 查看分支详细情况 (推荐这种方式) [[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备机环境
部署jenkins备机时,只需要定期将master机器上jenkins主目录数据拷贝到本机上即可。 比如: jenkins master:10.0.8.60 jenkins01 jenkins slave:10.0.8.61 jenkins02 两台机器的jenkins主目录都是:/data/tomcat8.5/webapps/jenkins [[email protected] ~]$ vim /etc/profile JENKINS_HOME="/data/tomcat8.5/webapps/jenkins" export JENKINS_HOME [[email protected] ~]$ source /etc/profile 那么只需要定期将master机器10.0.8.60上的/data/tomcat8.5/webapps/jenkins目录下的文件拷贝到10.0.8.61机器/data/tomcat8.5/webapps/jenkins下即可!