其他分享
首页 > 其他分享> > 20190422 Gitlab Jenkins 的搭建及准备web页面

20190422 Gitlab Jenkins 的搭建及准备web页面

作者:互联网

20190422  Gitlab  Jenkins 的搭建及准备web页面
实验准备:共用6台主机:172.18.9.200:Gitlab,172.18.9.201:Jenkins,172.18.9.202:Haproxy+Keepalived(master),172.18.9.203:Haproxy+Keepalived(slave),172.18.9.204:Tomcat 1,172.18.9.205:Tomcat 2,

一、思路;安装tomcat之前先安装Java环境

[root@205/204 ~]#cd /apps
[root@205/204 apps]#ls
apache-tomcat-8.5.32 jdk-8u191-linux-x64.rpm apache-tomcat-8.5.32.tar.gz tomcat
[root@205/204 apps]#ln -sv /apps/apache-tomcat-8.5.37 /apps/tomcat
' /apps/tomcat' -> ' /apps/apache-tomcat-8.5.37'
[root@204/205 apps]#yum -y install jdk-8u191-linux-x64.rpm
[root@204/205 apps]#/apps/tomcat/bin/catalina.sh start
Using CATALINA_BASE: /apps/tomcat
Using CATALINA_HOME: /apps/tomcat
Using CATALINA_TMPDIR: /apps/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /apps/tomcat/bin/bootstrap.jar:/apps/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@204/205 apps]#ss -ntl #先将端口开启!
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 :::8080 :::*

二、编写tomcat启动脚本:

[root@204 apps]#vim /etc/init.d/tomcat
#!/bin/bash
JDK_HOME=/apps/jdk
CATALINA_HOME=/apps/tomcat
export JDK_HOME CATALINA_HOME
source /etc/profile
#PID=ps -ef | grep -v grep | grep java | awk '{print $2}'
#NUM=ps -ef | grep -v grep | grep java | awk '{print $2}' | wc -l

#case $1 in
start() {
echo "正在判断服务状态,请稍等!"
echo "请稍等3秒钟"
echo "3";sleep 1;echo "2";sleep 1;echo "1";sleep 1
if netstat -an | grep 8080 | grep LISTEN >/dev/null
then
echo "Tomcat已经正在运行了!"
else
echo "Tomcat没有运行,1秒后启动!"
echo 1;sleep 1
$CATALINA_HOME/bin/catalina.sh start
echo "Tomcat 已经成功启动完成,5秒后判断是否启动成功"
echo "5";sleep 1;echo "4";sleep 1
echo "3";sleep 1;echo "2";sleep 1;echo "1";sleep 1
if netstat -an | grep 8080 | grep LISTEN >/dev/null
then
PID=ps -ef | grep tomcat | grep jdk | awk '{print $2}'
NUM=ps -ef | grep tomcat | grep jdk | awk '{print $2}' | wc -l
echo "Tomcat 已经成功启动${NUM} 个Tomcat进程!,PID为${PID}"
else
echo "Tomcat启动失败,请重新启动!"
echo 1
fi
fi
}
stop() {
PID=ps -ef | grep -v grep | grep java | awk '{print $2}'
NUM=ps -ef | grep -v "color" | grep tomcat | awk '{print $2}' | wc -l
echo "正在判断服务状态,请稍等3秒钟!"
echo "3";sleep 1;echo "2";sleep 1;echo "1";sleep 1
if netstat -an | grep 8080 | grep LISTEN >/dev/null
then
echo "Tomcat运行中,1秒后关闭!"
echo 1;sleep 1
echo "即将关闭Tomcat服务,请稍等!"
$CATALINA_HOME/bin/catalina.sh stop ;echo "已经执行关闭命令,正在检查关闭了多少Tomcat进程,请稍等30秒钟!"
sleep 3
echo "3";sleep 1;echo "2";sleep 1;echo "1";sleep 1
pkill java && pkill tomcat
if netstat -an | grep 8080 | grep LISTEN >/dev/null;then
PID=ps -ef | grep -v grep | grep java | awk '{print $2}'
NUM=ps -ef | grep -v "color" | grep tomcat | awk '{print $2}' | wc -l
kill -9 $PID ;echo "已成功关闭${NUM} 个tomcat进程"
else
echo "Tomcat 已经关闭完成!"
echo "3";sleep 1;echo "2";sleep 1;echo "1";sleep 1
fi
else
echo "Tomcat 没有运行"
echo 1
fi
if netstat -an | grep 8080 | grep LISTEN >/dev/null;then
PID=ps -ef | grep -v grep | grep java | awk '{print $2}'
#NUM=ps -ef | grep -v "color" | grep tomcat | awk '{print $2}' | wc -l
echo "关闭失败,即将强制删除tomcat进程!"
sleep 2
pkill tomcat ;sleep 2
if netstat -an | grep 8080 | grep LISTEN >/dev/null;then
echo "强制关闭失败,即将再次强制删除tomcat进程!"
pkill java; sleep 2
fi
fi
}
restart() {
stop
start
}

case "$1" in
start)
start
;;

stop)
stop
;;

restart)
restart
;;

*)
echo $"Usage: $0 {start|stop|restart|status}"
esac

三、加权限/看状态:[root@204 apps]#chmod a+x /etc/init.d/tomcat

[root@204 apps]# /etc/init.d/tomcat status
Usage: /etc/init.d/tomcat {start|stop|restart|status}
[root@204 apps]# /etc/init.d/tomcat start
正在判断服务状态,请稍等!
请稍等3秒钟
3
2
1
Tomcat已经正在运行了!
[root@204 apps]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 :::8080 :::*

四、访问页面:

20190422  Gitlab  Jenkins 的搭建及准备web页面

五、测试将其关停,可开启可关闭,脚本方可使用,便scp到其他tomcat主机即可:

[root@204 apps]# /etc/init.d/tomcat stop
正在判断服务状态,请稍等3秒钟!
3
2
1
Tomcat 没有运行
1
[root@204 apps]#ps -ef |grep tomcat #此时已经没有tomcat服务在运行了!!
root 18598 17020 0 11:41 pts/0 00:00:00 grep --color=auto tomcat
[root@204 apps]#scp /etc/init.d/tomcat 172.18.9.205:/etc/init.d/tomcat
root@172.18.9.205's password:
tomcat 100% 3339 973.5KB/s 00:00

六、从204scp到205:[root@205 apps]#/etc/init.d/tomcat start

正在判断服务状态,请稍等!
请稍等3秒钟
3
2
1
Tomcat已经正在运行了!

七、[root@204/205 apps]#useradd www -u 2000 #该账号用于管理tomcat

[root@centos7 apps]#chown www.www /apps/tomcat/ -R
[root@204/205 apps]#passwd www
Changing password for user www.
New password:123gxy
BAD PASSWORD: The password is shorter than 7 characters
Retype new password:123gxy
passwd: all authentication tokens updated successfully.

八、这时,我们先把tomcat关停,用普通账号登陆;

[root@204/205 apps]#/etc/init.d/tomcat stop
正在判断服务状态,请稍等3秒钟!
3
2
1
Tomcat 没有运行
1

九、普户登陆:[root@205 apps]#su - www

[www@205 ~]$/etc/init.d/tomcat start
正在判断服务状态,请稍等!
请稍等3秒钟
3
2
1
Tomcat 已经成功启动完成,5秒后判断是否启动成功
5
4
3
2
1
Tomcat 已经成功启动1 个Tomcat进程!,PID为23324

十、创建新的路径,放tomcat:

[root@204/205 apps]#mkdir -p /data/tomcat/webapps
[root@204/205 apps]#chown www.www /data/tomcat/webapps/ -R

十一、修改tomcat的存放路径:

[www@205 ~]$vim /apps/tomcat/conf/server.xml
<Host name="localhost" appBase="/data/tomcat/webapps"
unpackWARs="false" autoDeploy="false"> #在该文件的第148行

十二、用普户创建文件测试是否生效:

[www@205 ~]$cd /data/tomcat/webapps/
[www@205 webapps]$mkdir testapp
[www@205 webapps]$vim testapp/index.jsp
testjsp
[www@205 webapps]$/etc/init.d/tomcat restart
Tomcat 已经成功启动完成,5秒后判断是否启动成功
5
4
3
2
1
Tomcat 已经成功启动1 个Tomcat进程!,PID为24404
[www@205 webapps]$ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 :::8080 :::*
[www@205 webapps]$ll
total 0
drwxrwxr-x. 2 www www 23 Apr 25 13:58 testapp
20190422  Gitlab  Jenkins 的搭建及准备web页面
在204主机上:
[root@204/205 apps]#useradd www -u 2000 #该账号用于管理tomcat
[root@204/205 apps]#chown www.www /apps/tomcat/ -R
[root@204/205 apps]#passwd www
Changing password for user www.
New password:123gxy
BAD PASSWORD: The password is shorter than 7 characters
Retype new password:123gxy
passwd: all authentication tokens updated successfully.

[root@204 apps]#mkdir -p /data/tomcat/webapps
[root@204 apps]#chown www.www /data/tomcat/webapps/ -R

[root@204 apps]#su - www
[www@204 ~]$vim /apps/tomcat/conf/server.xml
<Host name="localhost" appBase="/data/tomcat/webapps"
unpackWARs="false" autoDeploy="false">
[www@204 ~]$cd /data/tomcat/webapps/
[www@204 webapps]$mkdir testapp
[www@204 webapps]$vim testapp/index.jsp
testapp 204
[www@204 webapps]$/etc/init.d/tomcat restart
Tomcat 已经成功启动完成,5秒后判断是否启动成功
5
4
3
2
1
Tomcat 已经成功启动1 个Tomcat进程!,PID为20650
20190422  Gitlab  Jenkins 的搭建及准备web页面

十三、部署负载均衡器:在172.20.102.202/203上安装haproxy和keepalived:

slave:[root@202 ~]#vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {br/>acassen@firewall.loc
failover@firewall.loc
br/>sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_iptables #该参数务必加入脚本中,否则在其他主机上ping不通VIP
vrrp_garp_interval 0
vrrp_gna_interval 0
}

vrrp_instance VI_1 {
state MASTER
interface ens37
virtual_router_id 120
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.18.9.248 dev eth0 label ens37:0
}
} #该脚本下面的都删除!!
[root@202 ~]#systemctl restart keepalived
[root@202 ~]#ifconfig #网卡子接口绑定成功
ens37:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.18.9.248 netmask 255.255.255.255 broadcast 0.0.0.0
ether fa:16:3e:75:57:73 txqueuelen 1000 (Ethernet)
[root@204 ~]#ping 172.18.9.248 #加上参数后就ping通了!
PING 172.18.9.248 (172.18.9.248) 56(84) bytes of data.
64 bytes from 172.18.9.248: icmp_seq=48 ttl=64 time=0.029 ms
64 bytes from 172.18.9.248: icmp_seq=49 ttl=64 time=0.046 ms

现在在203 slave上同款:[root@203 ~]#vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state BACKUP
interface ens37
virtual_router_id 120
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.18.9.248 dev ens37 label ens37:0
}
}
[root@202/203 ~]#systemctl restart keepalived
[root@202/203 ~]#systemctl enable keepalived
Created symlink from /etc/systemd/system/multiuser.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.

十四、配置haproxy:[root@202 ~]#vim /etc/haproxy/haproxy.cfg

listen webserver
bind 172.18.9.248:80
mode http
log global
option httplog
server 1 172.18.9.204:8080 check inter 3000 fall 2 rise 5
server 2 172.18.9.205:8080 check inter 3000 fall 2 rise 5
[root@202 ~]#systemctl start haproxy
[root@202 ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 172.18.9.248:80 :

十五、[root@202 ~]#vim /etc/sysctl.conf

net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1 #将2者写入配置文件,并改为1,打开状态!
[root@202 ~]#sysctl -p #验证查看

十六、此时去访问VIP,查看tomcat显示的web界面:

20190422  Gitlab  Jenkins 的搭建及准备web页面
Jenkins的下载路径:https://pkg.jenkins.io/redhat-stable/jenkins-2.138.4-1.1.noarch.rpm

十七、在172.18.9.201主机: #没装Java,Jenkins就启动失败!!

[root@201 ~]#wget https://pkg.jenkins.io/redhat-stable/jenkins-2.138.4-1.1.noarch.rpm
[root@201 ~]#yum -y install jenkins-2.138.4-1.1.noarch.rpm
[root@201 ~]#yum install -y jdk-8u191-linux-x64.rpm

十八、修改Jenkins配置文件并启动:

[root@201 ~]#vim /etc/sysconfig/jenkins
JENKINS_USER="root/www" 两者都行,默认为Jenkins,为了方便登陆才修改的
[root@201 ~]#systemctl start jenkins
[root@201 ~]#systemctl enable jenkins
jenkins.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig jenkins on
[root@201 ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 :::8080 :::* #Jenkins的端口也是8080,端口开启即可访问页面!!

十九、访问测试:

20190422  Gitlab  Jenkins 的搭建及准备web页面
查看密码:[root@201 ~]#cat /var/lib/jenkins/secrets/initialAdminPassword
dab1711156e2417180f029f56100984c
查看日志:[root@201 ~]#tail -f /var/log/jenkins/jenkins.log
Apr 26, 2019 10:16:10 PM hudson.model.DownloadService$Downloadable load
INFO: Obtained the updated data file for hudson.tasks.Maven.MavenInstaller
Apr 26, 2019 10:16:10 PM hudson.model.AsyncPeriodicWork$1 run
INFO: Finished Download metadata. 19,854 ms
Apr 26, 2019 10:16:21 PM hudson.model.UpdateSite updateData
INFO: Obtained the latest update center data file for UpdateSource default
Apr 26, 2019 10:16:21 PM jenkins.InitReactorRunner$1 onAttained
INFO: Completed initialization

二十、点击推荐安装的插件,

20190422  Gitlab  Jenkins 的搭建及准备web页面

标签:web,www,grep,204,tomcat,Gitlab,apps,20190422,root
来源: https://blog.51cto.com/14128387/2385480