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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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环境

[[email protected]/204 ~]#cd /apps
[[email protected]/204 apps]#ls
apache-tomcat-8.5.32 jdk-8u191-linux-x64.rpm apache-tomcat-8.5.32.tar.gz tomcat
[[email protected]/204 apps]#ln -sv /apps/apache-tomcat-8.5.37 /apps/tomcat
‘ /apps/tomcat‘ -> ‘ /apps/apache-tomcat-8.5.37‘
[[email protected]/205 apps]#yum -y install jdk-8u191-linux-x64.rpm
[[email protected]/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.
[[email protected]/205 apps]#ss -ntl #先将端口开启!
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 :::8080 :::*

二、编写tomcat启动脚本:

[[email protected] 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

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

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

四、访问页面:

技术图片

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

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

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

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

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

[[email protected] apps]#chown www.www /apps/tomcat/ -R
[[email protected]/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关停,用普通账号登陆;

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

九、普户登陆:[[email protected] apps]#su - www

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

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

[[email protected]/205 apps]#mkdir -p /data/tomcat/webapps
[[email protected]/205 apps]#chown www.www /data/tomcat/webapps/ -R

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

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

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

[[email protected] ~]$cd /data/tomcat/webapps/
[[email protected] webapps]$mkdir testapp
[[email protected] webapps]$vim testapp/index.jsp
testjsp
[[email protected] webapps]$/etc/init.d/tomcat restart
Tomcat 已经成功启动完成,5秒后判断是否启动成功
5
4
3
2
1
Tomcat 已经成功启动1 个Tomcat进程!,PID为24404
[[email protected] webapps]$ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 :::8080 :::*
[[email protected] webapps]$ll
total 0
drwxrwxr-x. 2 www www 23 Apr 25 13:58 testapp
技术图片
在204主机上:
[[email protected]/205 apps]#useradd www -u 2000 #该账号用于管理tomcat
[[email protected]/205 apps]#chown www.www /apps/tomcat/ -R
[[email protected]/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.

[[email protected] apps]#mkdir -p /data/tomcat/webapps
[[email protected] apps]#chown www.www /data/tomcat/webapps/ -R

[[email protected] apps]#su - www
[[email protected] ~]$vim /apps/tomcat/conf/server.xml
<Host name="localhost" appBase="/data/tomcat/webapps"
unpackWARs="false" autoDeploy="false">
[[email protected] ~]$cd /data/tomcat/webapps/
[[email protected] webapps]$mkdir testapp
[[email protected] webapps]$vim testapp/index.jsp
testapp 204
[[email protected] webapps]$/etc/init.d/tomcat restart
Tomcat 已经成功启动完成,5秒后判断是否启动成功
5
4
3
2
1
Tomcat 已经成功启动1 个Tomcat进程!,PID为20650
技术图片

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

slave:[[email protected] ~]#vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {br/>[email protected]
[email protected]
br/>[email protected]
}
notification_email_from [email protected]
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
}
} #该脚本下面的都删除!!
[[email protected] ~]#systemctl restart keepalived
[[email protected] ~]#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)
[[email protected] ~]#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上同款:[[email protected] ~]#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
}
}
[[email protected]/203 ~]#systemctl restart keepalived
[[email protected]/203 ~]#systemctl enable keepalived
Created symlink from /etc/systemd/system/multiuser.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.

十四、配置haproxy:[[email protected] ~]#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
[[email protected] ~]#systemctl start haproxy
[[email protected] ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 172.18.9.248:80 :

十五、[[email protected] ~]#vim /etc/sysctl.conf

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

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

技术图片
Jenkins的下载路径:
https://pkg.jenkins.io/redhat-stable/jenkins-2.138.4-1.1.noarch.rpm

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

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

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

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

十九、访问测试:

技术图片
查看密码:[[email protected] ~]#cat /var/lib/jenkins/secrets/initialAdminPassword
dab1711156e2417180f029f56100984c
查看日志:[[email protected] ~]#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页面的主要内容,如果未能解决你的问题,请参考以下文章

jenkins + gitlab配置webhook

Gitlab+Jenkins持续部署配置

gitlab jenkins配置WebHook控制代码提交自动构建

gitlab+jenkins

GitLab CI 与 Jenkins [关闭]

Jenkins——Jenkins+harbor+gitlab(git) 部署maven项目