VBox虚拟机CentOS7系统搭建gitlab

VBox上centos网络配置

CentOS在VBox上安装好后会存在以下问题:

  • 不能上网
  • 主机不能查看到CentOS虚拟机的存在,比如没法获取CentOS虚拟机ip。

不能上网是因为DNS没有配置。主机不能查看CentOS虚拟机ip是因为没有配置静态ip。

gitlab是通过网页访问的,主机不能访问centos虚拟机ip就没法在主机上连接gitlab。怎么配置呢?

Vbox上centos配置静态ip及DNS

配置Vbox网卡

  1. 打开VirtualBox
  2. 选择CentOS系统
  3. 右键选择“设置”
  4. 选择“网络”
  5. 网卡1选项卡内,连接方式设置为:桥接网卡

查看主机ip,子网掩码,网关,dns信息

根据自己的系统,自行查询。我的主机信息为:

  • IP:192.168.1.2
  • 子网掩码:255.255.255.0
  • 网关:192.168.1.1
  • DNS1: 114.114.114.114
  • DNS2: 4.2.2.2

配置CentOS虚拟机ip DNS信息

VBox中启动CentOS系统

cd /etc/sysconfig/network-scripts
ls ifcfg-e*
sudo vi ifcfg-enp0s3
#enp0s3是网卡名也可能是eth0等

在ifcfg-enp0s3文件中:修改ONBOOT值为yes,并且增加ip、网关、子网掩码、DNS信息。

  • 修改BOOTPROTO值为static
  • 修改ONBOOT值为yes
  • 并且增加ip、网关、子网掩码、DNS信息。

ip地址需要和主机在一个网段内,网关、子网掩码和主机一样即可。我的修改如下:

#其他文件内容不动
BOOTPROTO=static #设置静态IP
ONBOOT=yes  #这里如果为no的话就改为yes,表示网卡设备自动启动
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=114.114.114.114
DNS2=4.2.2.2

保存ifcfg-enp0s3文件。

重启网络

#centos7
sudo systemctl restart network
# centos6使用service network restart

查看ip

ip addr #centos7 默认无ifconfig命令,使用ip addr命令查看ip

ip addr命令就可以看到两个ip信息。分别包含:

...
    inet 172.0.0.1/8 scope host lo
...
...
    inet 192.168.1.100/24 brd 192.168.1.255 cope global noprefixroute enp0s3
...

静态ip地址192.168.1.100配置成功。

检查网络连通性

分别检查内部网络连通性、主机是否连通、外网连通

ping localhost
ping 主机ip:192.168.1.2
ping baidu.com

ok网络配置成功

vbox网络有4中接入方式,有兴趣可以了解一下,没兴趣可以忽略:

  • NAT模式:虚拟机访问网络的所有数据都是由主机提供的,虚拟机可以上网,但是主机与网络中的任何机器都不能查看和访问到虚拟机的存在。
  • Bridged Adapter模式是通过主机网卡,架设了一条桥,直接连入到网络中了。因此,它使得虚拟机能被分配到一个网络中独立的IP,所有网络功能完全和在网络中的真实机器一样。
  • Internal模式顾名思义就是内部网络模式,虚拟机与外网完全断开,只实现虚拟机于虚拟机之间的内部网络模式。主机也不能和虚拟机连接。
  • Host-only Adapter模式:主机模式,这是一种比较复杂的模式,需要有比较扎实的网络基础知识才能玩转。可以说前面几种模式所实现的功能,在这种模式下,通过虚拟机及网卡的设置都可以被实现。

虚拟机访问互联网,默认使用10.0.2.x段ip;虚拟机和主机互相通信,默认使用192.168.56.x段

安装和配置sshd、firwall及postfix

安装和配置sshd、firwall

安装gitlab必须的依赖和配置

sudo yum install -y curl policycoreutils-python openssh-server
sudo systemctl enable sshd
sudo systemctl start sshd
sudo firewall-cmd --permanent --add-service=http
sudo systemctl reload firewalld

安装postfix

用于gitlab的notification邮件。

sudo yum install postfix
sudo systemctl enable postfix
sudo systemctl start postfix

重centos虚拟机后,需要重新sudo systemctl start postfix一次

centos7中查看已经启动的服务列表用:systemctl list-unit-files | grep enable

systemctl list-unit-files | grep postfix查看postfix服务状态

安装gitlab

gitlab-ce和gitlab-ee

gitlab-ce是社区版,gitlab-ee是企业版。

gitlab-ee在不缴费的时候可以当做gitlab-ce使用,功能相同。所以官网推荐大家都安装gitlab-ee。

参考[ce-or-ee]

网络安装

1 添加Gitlab包repository

curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.rpm.sh | sudo bash

2 在线安装Gitlab

sudo EXTERNAL_URL="192.168.1.100" yum install -y gitlab-ee

192.168.1.100为centos系统ip地址

手动安装

下载gitlab离线版:

下载地址:https://packages.gitlab.com/gitlab/gitlab-ee

下载版本: gitlab-ee-11.7.4-ee.0.el7.x86_64.rpm

安装:

cd gitlab下载目录
sudo EXTERNAL_URL="192.168.1.100" rpm -i gitlab-ee-9.5.2-ee.0.el7.x86_64.rpm

安装成功

安装成功后会显示gitlab的图标,并输出如下文字:

Thank you for installing Gitlab!
GitLab should be availe at http://192.168.1.100

For a comprehensive list of configuration options please see the Omnibus GitLab readme https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.

在虚拟机以及主机都可以通过http://192.168.1.100打开gitlab了

登录

  • 打开gitlab:在浏览器中输入http://192.168.1.100打开gitlab
  • 设置root用户密码:第一打开gitlab时会显示设置root密码的页面,输入密码
  • 登录:用户名用root,密码用上一步设置的密码

当然也可以注册新用户。

如果centos重启了需要重新启动gitlab服务命令为:

gitlab-ctl start启动gitlab

gitlab-ctl restart重启gitlab

gitlab-ctl status查看gitlab状态

安装gitlab-Runner

GitLab-Runner是实际执行GitLab-CI自动化工作任务的模块。当某个工程的仓库代码发生变动时,比如有人push了代码,GitLab就会将这个变动通知GitLab-CI。这时GitLab-CI会找出与这个工程相关联的Runner,并通知这些Runner把代码更新到本地并执行预定义好的执行脚本。所以如果要使用GitLab-CI功能,就必须要安装Runner。

GitLab-Runner可以分类两种类型:Shared Runner(共享型)和Specific Runner(指定型)。
Shared Runner是所有工程都能够用的。只有系统管理员能够创建Shared Runner。
Specific Runner只能为指定的工程服务。拥有该工程访问权限的人都能够为该工程创建Specific Runner。

gitlab-runner只有几十M,比较小,所以就在线安装了。步骤如下:

添加gitlab repository源

curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo sh

安装gitlab-runner

sudo yum install gitlab-runner

注册runner

在centos中使用gitlab-runner register命令注册runner

获取url和token

注册runner必需要两个参数:GitLab-CI的url和注册token。这两个参数把runner和gitlab关联起来。

如果要注册Shared Runner,需要到管理界面的Runners页面里面去找注册token。步骤如下;

  1. 管理员账户登录gitlab
  2. 顶部导航栏点击扳手图标(Admin area)进入管理页面
  3. 选择OverView->Runners
  4. 可以看到url和token信息,类似如下:
Set up a Shared Runner manually
1. Install GitLab Runner
2. Specify the following URL during the Runner setup: http://192.168.1.100/
3. Use the following registration token during setup: xxxxxxxxxxxxxxx
4. Start the Runner! 

如果要注册Specific Runner,你需要到某个项目的设置里找到Runner页面。很容易就可以找到了。

注册runner

  1. 运行如下命令:
sudo gitlab-runner register
  1. 输入安装的GitLab的URL:
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
http://192.168.1.100

在这里我们输入centos虚拟机的ip

  1. 输入token:
Please enter the gitlab-ci token for this runner
xxxxxxxxxxxxxxx

在这里输入我们上一步获得的token

  1. 添加runner描述
Please enter the gitlab-ci description for this runner
[hostame] my gitlab runner

可以输入任意字符串,这个描述可以在gitlab网页里修改

  1. 添加描述标签,若添加多个需用逗号隔开
Please enter the gitlab-ci tags for this runner (comma separated):
 my-tag1,another-tag1

可以输入任意文字作为tag,同样可以在gitlab网页里修改

  1. 提醒注册完成,选择runner要运行的平台和方式
Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
shell

我们选择最简单的shell方式,注意使用shell方式,需要手动安装依赖(比如runner里需要编译程序,则要手动在runner所在机器里安装编译环境和工具)

几个不同executor的具体差别可以参考https://docs.gitlab.com/runner/executors/README.html

如果选择了docker,还会提醒输入默认的Docker Image,需要电脑已经安装了docker

查看runner

登录gitlab,在runner页面(和上一步查看url和token同一个位置)可以看到多了一个runner项。并且可以在runner页面对runner进行edit、pause、remove操作。

建议点击edit按钮进入runner编辑页面后,勾选下面的选项:

Run untagged jobs Indicates whether this runner can pick jobs without tags

这个选项是指,runner是否在没有标记tag的job上运行。勾选了之后在没有tag的项目上也会运行这个runner。否则在没有tag的项目上会一直停留在pending,出现This job is stuck错误。

** gitlab-runner命令**

安装好gitlab-runner后默认runner是启动的,并且每次启动gitlab后都会自动启动。
当然有时候也需要手动操作,比如修改配置后。

gitlab-runner stop  #停止Runner
gitlab-runner start #启动Runner
gitlab-runner restart #重启Runner
gitlab-runner status #查看Runner运行状态

找不到命令问题

权限不够问题

gitlab runner默认运行时user mode,想提升权限,只需要运行gitlab runner 的时候使用sudo即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hustlei

您的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值