1 环境要求:
- windows系统
- VMware Workstation Pro 12版本及以上
- Centos7 系统镜像iso安装文件
Everything (ISO):全部安装包
Minimal (ISO):最小安装包(推荐,本文实验使用CentOS-7-x86_64-Minimal-1810.iso)
NetInstall (ISO):在线安装包
U盘系统安装盘制作
2 搭建过程
2.0 centos7系统安装
服务器一般都装minimal版因为定制性好,其中安装过程注意一下三点
- 如果是初次安装,请选用默认分区配置
- Ethernet网络配置,不同的机器网卡不一样,可能出现网络名称不一样,记下此网络名称,方便后期找到相应的网络配置文件进行修改。
- Host name主机名称,根据自己喜好去一个自己喜欢的名字
2.1 基本环境配置
更新yum软件
$yum update
本地时间设置:centos7查看修改时区
服务器上的时间同步的方法
$yum -y install ntp ntpdate #安装ntpdate工具
$ntpdate cn.pool.ntp.org #设置系统时间与网络时间同步
$hwclock --systohc #将系统时间写入硬件时间
网络配置
$vi /etc/sysconfig/network-scripts/ifcfg-ens32
修改如下
例如:
BOOTPROTO="static" #dhcp改为static 这个是改静态用的.实际不改. 自己记录用
ONBOOT="yes" #开机启用本配置 改这里就OK
IPADDR=192.168.7.106 #静态IP 根据路由器实际分配的IP进行设置
NETMASK=255.255.255.0 #子网掩码
GATEWAY=192.168.7.1 #默认网关 根据路由器网关设置
DNS1= 8.8.8.8
DNS2=8.8.8.4
#[CentOS7配置网卡为静态IP](https://segmentfault.com/a/1190000011954814)
重启网络
$systemctl restart network
$ping www.baidu.com #测试网络通信是否正常
2.2 在线安装
2.2.1 基本软件安装
$shutdown -c #重启
#mini版本不能使用wget命令,安装软件的时候很不方便,这里需要安装wget命令,直接yum安装即可,如果需要设置http代理,安装wget命令
$yum -y install wget
$yum install rsync #安装rsync参考:https://www.jianshu.com/p/0df0633613d8
$yum install dos2unix #安装dos2unix,解决bash脚本运行时出现的bash:$'\r': command not found 的问题
$sudo yum install policycoreutils #安装软件
$sudo yum install openssh-server #安装ssh服务
$sudo yum install openssh-clients #安装ssh客户端
$sudo systemctl enable sshd #开机使能ssh服务
$sudo systemctl start sshd #打开ssh服务
$sudo yum install postfix #安装软件
$sudo systemctl enable postfix #开机使能postfix服务
$sudo systemctl start postfix
$sudo firewall-cmd --permanent --add-service=http
$sudo systemctl reload firewalld #重启firewalld,注意不是firewall
#查看当前开了哪些端口,一个服务对应一个端口,每个服务对应/usr/lib/firewalld/services下面一个xml文件
$firewall-cmd --list-services
#查看还有哪些服务可以打开
$firewall-cmd --get-services
#添加一个服务到firewalld
$firewall-cmd --add-service=http //http换成想要开放的service
#这样添加的service当前立刻生效,但系统下次启动就失效,可以测试使用。要永久开发一个service,加上 --permanent
$firewall-cmd --permanent --add-service=http
这里关闭和禁止防火墙是关闭全部,测试较方便,实际慎用,可以只开放某些端口号
$firewall-cmd --state # 查看默认防火墙状态,关闭后显示notrunning,开启后显示running(如果防火墙开启则需要使用下面两条命令)
$systemctl stop firewalld.service # 停止防火墙(仅暂时有效,虚拟机重启后会自动开启)
$systemctl disable firewalld.service # 禁止防火墙开机启动
为gitlab单独开启一个端口,例如8083端口
$firewall-cmd --query-port=8083/tcp #查看端口号是否开启
$firewall-cmd --zone=public --add-port=8083/tcp --permanent #永久开启8083端口
$firewall-cmd --reload #重启防火墙
$firewall-cmd --query-port=8083/tcp #查看端口号是否开启
2.2.2 安装gitlab工具
在线安装
$curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
$sudo yum install gitlab-ce
本地安装
$sudo yum install gitlab-ce-11.7.5-ce.0.el7.x86_64.rpm #*本文实际安装版本*
$cd /etc/gitlab
$vi gitlab.rb #配置external_url 通常是本机局域网ip的形式,例如:external_url 'http://192.168.1.106:8083'
$sudo gitlab-ctl reconfigure #执行配置
$sudo gitlab-ctl restart #重启gitlab,提示信息OK即表示成功
注意设置访问端口不能是8080端口,因为8080gitlab有其他用处,否则会出现502的错误
查看本机ip地址command:ip addr
2.3 离线安装(待测试)
2.3.1 ntfs格式U盘挂载问题
CentOS7上无法识别NTFS格式U盘的解决方法
ntfs安装包下载
2.3.2 下载基本软件(rpm安装包)
Centos7官方安装包地址
在Centos7官方网站下载基本软件:policycoreutils,openssh-server,openssh-clients,postfix
2.3.3 下载gitlab安装包
2.3.4 安装rpm包
将下载好的rpm进行安装,安装教程参考How to Install .rpm File on CentOS
建议使用yum进行离线安装rpm包
#yum安装rpm方式
$yum localinstall package-1.2.3.rpm ## CentOS, RHEL systems
$yum remove package-1.2.3.rpm ##Removing Package
#rpm安装rpm方式
$rpm -i package-1.2.3.rpm ## CentOS, RHEL systems
$rpm -e package-1.2.3.rpm ##Removing Package
3 Git服务器测试
打开浏览器,输入192.168.1.106:8083
设置root密码
如果root密码忘记,需要重置密码请参考gitlab重置root密码
具体重置密码步骤
$sudo gitlab-rails console production
$User.all #查看所有用户
$user=User.where(name: "root").first
$user.password=12345678 #只能是数字???
$user.save!
$quit
$exit
save后提示true,则表示修改成功
4 gitlab简单维护
4.1 设置下次开机自启动
$systemctl enable gitlab-runsvdir.service
4.2 设置git-data存储位置
gitlab数据存储位置设置:Storing Git data in an alternative directory
总结步骤如下
$vi /etc/gitlab/gitlab.rb
#git_data_dirs({ "default" => { "path" => "/home/gitlab/git-data" } }) #添加指定存储位置
$sudo gitlab-ctl reconfigure
If you already have existing Git repositories in /var/opt/gitlab/git-data you can move them to the new location as follows:
$sudo gitlab-ctl stop
#$rsync -av /var/opt/gitlab/git-data/repositories /home/gitlab/git-data/ #官方提供此命令有误,请用如下命令
$rsync -av /var/opt/gitlab/git-data/ /home/gitlab/git-data/ #同步文件内容
$sudo gitlab-ctl upgrade
$ls /mnt/nas/git-data/
$gitlab-ctl start
4.3 手动备份
$gitlab-rake gitlab:backup:create
默然的备份目录为: /var/opt/gitlab/backups
备份文件名类似: xxxxxx_gitlab_backup.tar
备份目录的修改关键字 backup_path
$vi /etc/gitlab/gitlab.rb
#gitlab_rails['backup_path'] = '/mnt/gitlab_backups'
注意:/mnt/gitlab_backups硬盘(例如新挂载的硬盘)文件系统格式必须与/var/opt/gitlab/backups相同
4.4 自动定时备份
备份如果不能及时清理硬盘的空间很快会不够用,同时也要能够自动清理不需要人工花时间在这一块。设置备份自动清理,只保存最近7天的备份
$vi /etc/gitlab/gitlab.rb
#gitlab_rails['backup_keep_time'] = 604800 # 删除注释 #, 7天*3600秒*24时=604800s
$gitlab-ctl reconfigure
配置定时任务 需重启cron服务
$sudo crontab -e
0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create #每天凌晨2点备份
#* * * * * 格式表示:分,时,日,月,周
$crontab -l 查看定时任务
$systemctl enable crond.service #设置cron服务开机使能
$systemctl restart crond #修改后重启cron服务
#$systemctl start crond #启动cron服务
#$systemctl stop crond #停止cron服务
#$systemctl restart crond #重启cron服务
4.5 手动恢复
停服务
$sudo gitlab-ctl stop unicorn
$sudo gitlab-ctl stop sidekiq
$sudo gitlab-ctl status
恢复数据
$gitlab-rake gitlab:backup:restore BACKUP=141111111
$sudo gitlab-rake gitlab:check SANITIZE=true #check恢复数据是否OK
BACKUP后面跟的是备份文件的时间戳,比如恢复备份文件141111111_gitlab_backup.tar
恢复后启动gitlab服务
$gitlab-ctl start
4.6 远程登录
Centos7 服务器远程连接
建议使用putty,因为免费
4.7 安装gitlab-runner
#/usr/local/bin/gitlab-runner #将下载的gitlab-runner-linux-amd64重命名放入此路径
$chmod +x /usr/local/bin/gitlab-runner #修改权限,Give it permissions to execute
$useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash #Create a GitLab CI user
$gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
$Install and run as service
$gitlab-runner start
gitlab-runner register详细步骤
gitlab-runner register官方步骤