Jenkins 部署
转自 http://www.cnblogs.com/kevingrace/p/5651427.html
Jenkins通过脚本任务触发,实现代码的自动化分发,是CI持续化集成环境中不可缺少的一个环节。
下面对Jenkins环境的部署做一记录。
--------------------------------------------------------------------------------------
1)安装java(yum安装java1.7)
java环境安装参考:Centos中yum方式安装java
[root@115 ~]# java -version
java version "1.7.0_111"
OpenJDK Runtime Environment (rhel-2.6.7.2.el6_8-x86_64 u111-b01)
OpenJDK 64-Bit Server VM (build 24.111-b01, mixed mode)
[root@115 ~]# which java
/usr/bin/java
[root@115 ~]# ll /usr/bin/java
lrwxrwxrwx. 1 root root 22 Oct 30 01:29 /usr/bin/java -> /etc/alternatives/java
[root@115 ~]# ll /etc/alternatives/java
lrwxrwxrwx. 1 root root 46 Oct 30 01:29 /etc/alternatives/java -> /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
[root@115 jvm]# pwd
/usr/lib/jvm
[root@115 jvm]# ll
total 4
lrwxrwxrwx. 1 root root 26 Oct 30 01:29 java -> /etc/alternatives/java_sdk
lrwxrwxrwx. 1 root root 32 Oct 30 01:29 java-1.7.0 -> /etc/alternatives/java_sdk_1.7.0
drwxr-xr-x. 9 root root 4096 Oct 30 01:29 java-1.7.0-openjdk-1.7.0.111.x86_64
lrwxrwxrwx. 1 root root 35 Oct 30 01:29 java-1.7.0-openjdk.x86_64 -> java-1.7.0-openjdk-1.7.0.111.x86_64
lrwxrwxrwx. 1 root root 34 Oct 30 01:29 java-openjdk -> /etc/alternatives/java_sdk_openjdk
lrwxrwxrwx. 1 root root 21 Oct 30 01:29 jre -> /etc/alternatives/jre
lrwxrwxrwx. 1 root root 27 Oct 30 01:29 jre-1.7.0 -> /etc/alternatives/jre_1.7.0
lrwxrwxrwx. 1 root root 39 Oct 30 01:29 jre-1.7.0-openjdk.x86_64 -> java-1.7.0-openjdk-1.7.0.111.x86_64/jre
lrwxrwxrwx. 1 root root 29 Oct 30 01:29 jre-openjdk -> /etc/alternatives/jre_openjdk
由此可知,java的home目录是/usr/lib/jvm/java-1.7.0-openjdk.x86_64
设置java的环境变量
[root@115 ~]# vim /etc/profile
.......
export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk.x86_64
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
使之生效
[root@115 ~]# source /etc/profile
[root@115 ~]# echo $JAVA_HOME
/usr/lib/jvm/java-1.7.0-openjdk.x86_64
[root@115 ~]# echo $CLASSPATH
.:/usr/lib/jvm/java-1.7.0-openjdk.x86_64/jre/lib/rt.jar:/usr/lib/jvm/java-1.7.0-openjdk.x86_64/lib/dt.jar:/usr/lib/jvm/java-1.7.0-openjdk.x86_64/lib/tools.jar
------------------------------------------------------------------------------------------------------------------------------
上面java的环境变量一定要设置!不然tomcat启动会失败,会有以下报错:
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
------------------------------------------------------------------------------------------------------------------------------
2)安装tomcat环境
安装包下载地址:https://pan.baidu.com/s/1bDHT5G
提取密码:btir
将下载的apache-tomcat-7.0.42.tar.gz包解压放到/usr/local目录下,改名为tomcat7。
即tomcat路径为/usr/local/tomcat7
3)安装jenkins
jenkins安装包下载地址:http://pan.baidu.com/s/1o7T26Rg
提取密码:aeka
直接将 jenkins.war拷贝到/usr/local/tomcat7/webapps目录下,webapps目录下原来的内容全部删除
[root@115 webapps]# pwd
/usr/local/tomcat7/webapps
[root@115 webapps]# ll
-rw-r--r--. 1 root root 63664946 2月 3 22:26 jenkins.war
修改tomcat的配置文件,确保使用UTF-8编码(如下添加URIEncoding="UTF-8"),以防jenkins出现中文乱码:
[root@115 conf]# pwd
/usr/local/tomcat7/conf
[root@jenkins-server conf]# vim server.xml
........
<Connector port="8080" URIEncoding="UTF-8" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
设置系统环境变量,在/etc/profile底部添加下面内容
[root@115 conf]# vim /etc/profile
.......
JENKINS_HOME="/usr/local/tomcat7/webapps/jenkins"
export JENKINS_HOME
使之生效
[root@115 conf]# source /etc/profile
[root@115 conf]# echo $JENKINS_HOME
/usr/local/tomcat7/webapps/jenkins
然后重启tomcat服务
[root@115 webapps]# /usr/local/tomcat7/bin/startup.sh
[root@115 webapps]# lsof -i:8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 45515 root 46u IPv6 47030248 0t0 TCP *:radan-http (LISTEN)
[root@115 webapps]# pwd
/usr/local/tomcat7/webapps
[root@115 webapps]# ll
total 62180
drwxr-xr-x. 9 root root 4096 Oct 30 20:39 jenkins
-rw-r--r--. 1 root root 63664946 Oct 30 20:38 jenkins.war
然后访问http://ip/8088/jenkins即可!
登陆jenkins后,可以在“系统管理”的Configure Global Security创建用户,创建后还需要注册后才能登陆。
=======================温馨提示====================
1
|
注意tomcat版本问题,本案例中的jenkins.war包适用于tomcat7版本。tomcat版本如果出现不兼容问题,会导致webapps下的应用问题(比如数据目录被莫名情况等问题) |
另:
上面的jenkins环境部署服务器只有内网ip(192.168.1.25),没有外网ip。
那么访问是通过具有外网ip(比如是113.110.186.5)服务器NAT端口转发进行的。
操作如下:
1)在113.110.186.5/192.168.1.5机器上进行NAT端口转发设置,由8080端口转发到192.168.1.25的8080端口:
[root@bastion-IDC ~]# iptables -t nat -A PREROUTING -p tcp -m tcp --dport 8080 -j DNAT --to-destination 192.168.1.25:8080
[root@bastion-IDC ~]# iptables -t nat -A POSTROUTING -d 192.168.1.25/32 -p tcp -m tcp --sport 8080 -j SNAT --to-source 192.168.1.5
[root@bastion-IDC ~]# iptables -t filter -A INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT
[root@bastion-IDC ~]# service iptables save
[root@bastion-IDC ~]# service iptables restart
确保nat端口转发设置成功后,/etc/sysconfig/iptables文件里要注释掉下面两行!不然nat转发会有问题!
一般如上面在nat转发规则设置好并save和restart防火墙之后就会自动在/etc/sysconfig/iptables文件里删除掉下面两行内容了。
[root@bastion-IDC ~]# vim /etc/sysconfig/iptables
..........
#-A INPUT -j REJECT --reject-with icmp-host-prohibited
#-A FORWARD -j REJECT --reject-with icmp-host-prohibited
[root@bastion-IDC ~]# service iptables restart
开启ip路由转发功能
[root@linux-node1 ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
或者
[root@linux-node1 ~]# cat /etc/sysctl.conf
..........
net.ipv4.ip_forward = 1
[root@linux-node1 ~]# sysctl -p
2)在192.168.1.25上关闭防火墙
首先关闭防火墙
[root@115 ~]# service iptables stop
设置网关为192.168.1.5
[root@115 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
......
GATEWAY=192.168.1.5
......
[root@115 ~]# /etc/init.d/network restart
或者
[root@115 ~]# route add default gw 192.168.1.5
[root@dev-new-test1 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 192.168.1.5 0.0.0.0 UG 0 0 0 eth0
最后测试访问jenkins:
http://113.110.186.5:8080/jenkins就可以正常访问到192.168.1.25的8080端口了