使用现成的docker容器能够快速搭建gitlab服务,且管理起来很方便,具有容灾、可移植等优点
为了避免不必要的错误,建议数据存放路径与我这里保持一致:
[root@JackRoy ~]# mkdir -p /opt/dockers/docker_tool_gitlab/online
[root@JackRoy ~]# mkdir -p /opt/dockers/docker_tool_gitlab/gitlab_data
[root@JackRoy ~]# mkdir -p /opt/dockers/docker_tool_gitlab/gitlab_data/config
[root@JackRoy ~]# mkdir -p /opt/dockers/docker_tool_gitlab/gitlab_data/logs
[root@JackRoy ~]# mkdir -p /opt/dockers/docker_tool_gitlab/gitlab_data/data
这里借用一下同事刘某源(感谢老源~)编写的docker启动命令,直接编辑输入:
[root@JackRoy online]# vim /usr/bin/dockerutils
#!/bin/bash
# Settle issue (e.g.: SELINUX) of when mount host directory, it has no privilege to access.
chcon -Rt svirt_sandbox_file_t $dir > /dev/null 2>&1
container_name=
TOP_PID=$$
trap 'exit 127' TERM
gate_way=`route | grep 'default' | awk '{print $2}'`
code=0
find_name(){
_container_name=`cat $dir/docker-compose.yml | grep container_name | awk -F ':' '{print $2}' | sed 's/^[ ]*//g' | sed 's/[ ]*$//g'`
_container_name=${
_container_name//\"/}
container_name=${_container_name//\'/}
if [ x$container_name == x ]
then
echo -e "\033[31mERROR : please set container_name in your docker-compose.yml file \033[0m"
kill -s TERM $TOP_PID
fi
}
check(){
_host_name=`cat $dir/docker-compose.yml | grep hostname | awk -F ':' '{print $2}' | sed 's/^[ ]*//g' | sed 's/[ ]*$//g'`
_host_name=${_host_name//\"/}
host_name=${_host_name//\'/}
echo "-------------------------------------------------"$host_name
if [ x$host_name == x ]
then
echo -e "\033[31mERROR : hostname not set ! \033[0m"
code=1
fi
_ip=`cat $dir/docker-compose.yml | grep ipv4_address | awk -F ':' '{print $2}' | sed 's/^[ ]*//g' | sed 's/[ ]*$//g'`
_ip=${_ip//\"/}
ip=${_ip//\'/}
if [ $1 = y ] ; then
fail=`ping $ip -c 3 -s 1 -W 1 | grep "100% packet loss" | wc -l`
if [ $fail -eq 0 ]
then
echo -e "\033[31mERROR : address is already in use \033[0m"
code=1
fi
fi
if [ $code -ne 0 ] ; then kill -s TERM $TOP_PID ;fi
}
up(){
check n
docker-compose -f $dir/docker-compose.yml up -d
start
}
reup(){
docker rm -f $container_name
up
}
start(){
docker start $container_name
}
stop(){
docker stop $container_name
}
restart(){
docker stop $container_name
sleep 3
docker start $container_name
}
usage(){
echo -e " Usage : \n \t $0 [ usage ] | < up | reup | start | stop | restart > "
echo -e "\t ============= nat docker ==============="
echo -e "\t up : create container and start app"
echo -e "\t reup : re-create container and start app"
echo -e "\t start : start container and app"
echo -e "\t stop : stop container and app"
echo -e "\t restart : restart container and app"
kill -s TERM $TOP_PID
}
if [ "$1"x = ""x ] || [ "$1"x = "usage" ] ; then
usage
fi
find_name
$@
exit $?
进入/opt/dockers/docker_tool_gitlab/online目录下:
[root@JackRoy online]# cd /opt/dockers/docker_tool_gitlab/online
编辑deploy_comm.sh并输入:
#!/bin/bash
dir=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
. dockerutils $@
编辑docker-compose.yml并输入:
version: '2'
services:
gitlab:
image: 'gitlab/gitlab-ce' # 使用镜像
container_name: "gitlab" # 容器名
restart: unless-stopped # 容器退出时总是重新启动
privileged: true # 开通权限
hostname: 'gitlab' # 主机名
environment:
TZ: 'Asia/Shanghai' # 时区
ports:
- '9090:9090' # 宿主机端口:docker端口 有端口占用的情况请换宿主机端口
- '2222:22'
volumes:
- ../gitlab_data/config:/etc/gitlab # 内外路径映射关系
- ../gitlab_data/data:/var/opt/gitlab
- ../gitlab_data/logs:/var/log/gitlab
networks:
aidata_network: # 使用网络
ipv4_address: 10.111.113.3 # 分配ip
networks:
aidata_network:
external: true
在/opt/dockers/docker_tool_gitlab/online路径下,执行启动命令
# deploy_comm.sh 支持 reup up stop多种命令
[root@JackRoy online]# sh deploy_comm.sh reup
-------------------------------------------------gitlab
Pulling gitlab (gitlab/gitlab-ce:)...
Trying to pull repository docker.io/gitlab/gitlab-ce ...
latest: Pulling from docker.io/gitlab/gitlab-ce
3386e6af03b0: Pull complete
49ac0bbe6c8e: Pull complete
d1983a67e104: Pull complete
1a0f3a523f04: Pull complete
0a1871e024e9: Pull complete
22db415dc935: Pull complete
7a635a8736d3: Pull complete
cc0ef2dfa7c2: Pull complete
816bca05932d: Pull complete
9aff335224a3: Pull complete
Digest: sha256:ad3988896b5c7ccf8272bc6987ce5cff132164e9a6bfc252976b6a2a98bd4896
Status: Downloaded newer image for docker.io/gitlab/gitlab-ce:latest
Creating gitlab ... done
gitlab
执行启动命令以后我们进入docker容器,如果能成功进入那便是执行成功了:
[root@JackRoy online]# docker exec -it gitlab bash
root@gitlab:/#
root@gitlab:/# exit
exit
访问url:
http://10.20*.4.5*:9090/users/password/edit?reset_password_token=1G_Ns5rhU_gDrppdfVJR
效果如下:
这个页面需要设置密码,设置完了用root登录:
成功登录以后,先创建一个空项目:
项目创建完成以后,我们将我们存放代码的本机公钥配置到用户的sshkey中:
上传代码前需要说明,这里凡是spring boot项目都需要自带两个文件,分别是:logback.xml和start-app.sh,其中logback.xml是用来规范打印日志格式,start-app.sh用来在docker中启动项目jar包。
请把logback.xml文件放于resources目录下与application.yml同级:
logback.xml内容(无需更改):
<configuration>
<property name="fileBasePath" value="./logs" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%npattern>
encoder>
appender>
<appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${fileBasePath}/sales-error-%d{yyyy-MM-dd}.logfileNamePattern>
<maxHistory>60maxHistory>
rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%npattern>
encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERRORlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<appender name="ACCESS" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${fileBasePath}/sales-access-%d{yyyy-MM-dd}.logfileNamePattern>
<maxHistory>60maxHistory>
rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%npattern>
encoder>
appender>
<appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${fileBasePath}/sales-info-%d{yyyy-MM-dd}.logfileNamePattern>
<maxHistory>60maxHistory>
rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%npattern>
encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFOlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<logger name="com.lvmama.dstools" additivity="false">
<level value="DEBUG" />
<appender-ref ref="STDOUT"/>
<appender-ref ref="ERROR"/>
<appender-ref ref="ACCESS"/>
<appender-ref ref="INFO" />
logger>
<root level="info">
<appender-ref ref="STDOUT" />
root>
configuration>
同时,在项目根(注意一定是项目根目录,与src同级)目录下创建config目录,并新建一个名为start-app.sh的脚本:
start-app.sh明细(需要修改APP_NAME和APP_JAR):
#!/usr/bin/sh
JAVA_HOME=/opt/jdk1.8
PATH=$PATH:$JAVA_HOME/bin:.
export JAVA_HOME PATH
dir=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
conf=${dir}/config
APP_NAME='euraka-server' # 修改点1
APP_JAR=${dir}/euraka-server-1.0.0-SNAPSHOT.jar # 修改点2
LOG_PATH=${dir}/logs
command="nohup java -Xms2g -Xmx2g -Dlog.path=$LOG_PATH -Dlogback.configurationFile=$conf/logback.xml -jar $APP_JAR"
log_file_url="logs/log.`hostname`.log"
pid_file="ds-platformawareness.$(hostname).pid"
log_path="logs"
cd $dir
start(){
if [ "$log_file_url" != "" ]; then
echo "STARTING $APP_NAME ....."
exec $command > "$log_file_url" 2>&1 &
echo $! > "$pid_file"
else
echo "STARTING $APP_NAME ....."
exec $command &
echo $! > "$pid_file" &
fi
}
stop(){
echo "STOPING $APP_NAME ....."
PID=$(cat "$pid_file")
kill -9 $PID
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
printf 'Usage: %s {start|stop|restart}\n'
exit 1
;;
esac
在windows本地使用gitbash上传代码至gitlab仓库,如果没有装gitlab先装一下(网上教程很多,我这里就不赘述了),确保安装了gitlab以后,我们打开交互窗口进到项目目录,依次执行(注意:ssh://[email protected]*.4.5*:2222/root/ucash-servers.git从gitlab项目主页面处获取,不要直接copy这里的):
$ cd d:
$ cd project/ucash-servers/
$ rm -rf .git
$ git init
# ssh://[email protected]*.4.5*:2222/root/ucash-servers.git从gitlab项目主页面处获取,不要直接copy这里的
$ git remote add origin ssh://[email protected]*.4.5*:2222/root/ucash-servers.git
$ git add .
$ git commit -m "JackRoy test"
$ git push -u origin master
写博客的时候我重新找了台干净的机器进行部署,整个流程没有出现任何问题(以前踩过坑,但都被规避了,这里的流程就是最精简的),大家如果在部署过程遇到什么问题,可以留言交流
第一节 基础环境介绍与准备
第二节 docker版gitlab安装和配置
第三节 docker版jenkins安装和配置
第四节 docker版mysql安装和配置
第五节 docker信息明细表设计与实现(含收集脚本)
第六节 docker容器自动化生成脚本
第七节 自动生成docker容器并启动项目