hugo-teek is loading...

实战-Terraform部署GitLab-成功测试

最后更新于:

实战:Terraform 部署GitLab(泽阳-博客分享-成功测试)-2022.5.7

image-20220507101128416

目录

[TOC]

实验环境

1win10笔记本
22台centos7.6虚机(1台用作部署gitlab server(需要具有docker/terraform环境),一台用作跑gitlab runner)
3Terraform版本: v1.1.8
4docker版本: v20.10.11
5gitlab版本:gitlab/gitlab-ce:14.9.3-ce.0
6gitlab-ruuner版本:gitlab-runner-14.9.1-1.x86_64.rpm

实验软件

链接:https://pan.baidu.com/s/1vGpLqcTR-6Db0OaUr9LH8Q?pwd=puoj 提取码:puoj 2022.5.7-Terraform 部署GitLab-code

image-20220507122933741

前置条件

  • centos7机器需要提前安装好terraform环境、docker环境;

1、部署gitlab server

1.创建本地持久化目录

  • 本次测试目录:
1[root@devops remote-vscode]#pwd
2/root/remote-vscode
3[root@devops remote-vscode]#ls
4gitlab.tf
  • 创建本地目录:
1[root@devops remote-vscode]#mkdir -p /data/devops4/gitlab/logs
2[root@devops remote-vscode]#mkdir -p /data/devops4/gitlab/data
3[root@devops remote-vscode]#mkdir -p /data/devops4/gitlab/config
image-20220505075932960
  • 先确认本地主机端口是否被占用
1[root@devops remote-vscode]#netstat -antlp|grep 80
2[root@devops remote-vscode]#netstat -antlp|grep 443
3[root@devops remote-vscode]#netstat -antlp|grep 2222

image-20220505080500941

2.使用terraform部署gitlab

  • 准备好terraform需要用到的配置文件

cat main.tf :

1# 定义provider(main.tf)
2terraform {
3  required_providers {
4    docker = {
5      source = "kreuzwerker/docker"
6      version = "~> 2.13.0"
7      }
8  }
9}

cat gitlab.tf:

 1resource "docker_image" "gitlab" {
 2  name         = "gitlab/gitlab-ce:14.9.3-ce.0"
 3  keep_locally = true  //销毁时不删除本地镜像
 4}
 5
 6resource "docker_container" "gitlab" {
 7  image = docker_image.gitlab.name
 8  name  = "devops_tutorial_gitlab"
 9  ports { #本次课程目前只用到80端口
10    internal = 80
11    external = 80
12  }
13  ports {
14    internal = 443
15    external = 443
16  }
17  ports {
18      internal = 22
19      external = 2222
20  }
21  volumes{
22      container_path = "/etc/gitlab" #gitlab配置文件
23      host_path = "/data/devops4/gitlab/config"
24  }
25  volumes{
26      container_path = "/var/log/gitlab" #gitlab日志文件
27      host_path = "/data/devops4/gitlab/logs"
28  }
29  volumes{
30      container_path = "/var/opt/gitlab" #gitlab数据
31      host_path = "/data/devops4/gitlab/data"
32  }
33}
  • terraform init
1[root@devops remote-vscode]#terraform init

image-20220505080143458

  • terraform plan
1[root@devops remote-vscode]#terraform plan

image-20220505080233500

  • terraform apply
1[root@devops remote-vscode]#terraform apply

image-20220505082423560

⚠️ 注意:gitlab的镜像很大的,需要2.45GB,因此这里需要等好久,耐心等待即可!(如果实在不行,可先用docker把近线拉取下下来,再执行terraroem命令)

image-20220505100138221

image-20220505100125352

3.验证

  • 查看容器状态
1[root@devops remote-vscode]#docker ps

image-20220505095632295

  • 查看gitlab容器日志
1[root@devops remote-vscode]#docker logs -f  790a4c992c45

image-20220505095607680

  • 网页验证

登录http://宿主机ip/

http://172.29.9.101/

image-20220505082927535

  • 输入密码

默认密码: 进入容器:/etc/gitlab/initial_root_password #14版本以后发生了变化!

root 密码

新的密码:Devops123456

 1[root@devops remote-vscode]#docker ps
 2CONTAINER ID   IMAGE                          COMMAND             CREATED          STATUS                    PORTS
 3                               NAMES
 4790a4c992c45   gitlab/gitlab-ce:14.9.3-ce.0   "/assets/wrapper"   12 minutes ago   Up 12 minutes (healthy)   0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:2222->22/tcp   devops_tutorial_gitlab
 5[root@devops remote-vscode]#docker exec devops_tutorial_gitlab cat /etc/gitlab/initial_root_password
 6# WARNING: This value is valid only in the following conditions
 7#          1. If provided manually (either via `GITLAB_ROOT_PASSWORD` environment variable or via `gitlab_rails['initial_root_password']` setting in `gitlab.rb`, it was provided before database was seeded for the first time (usually, the first reconfigure run).
 8#          2. Password hasn't been changed manually, either via UI or via command line.
 9#
10#          If the password shown here doesn't work, you must reset the admin password following https://docs.gitlab.com/ee/security/reset_user_password.html#reset-your-root-password.
11
12Password: cO/jgYrZ1JBxjHxVwCjXbh/a2HxTRMGpuLUfo9XYLJM=
13
14# NOTE: This file will be automatically deleted in the first reconfigure run after 24 hours.
15[root@devops remote-vscode]#
  • 修改root密码:

点击头像-Preferences-Passsword,修改密码:

image-20220505100831950

使用修改后的额密码登录gitlab:

image-20220505100959731

image-20220505103358968

结束。😘

注意事项

1.登录gitlab报502错误问题

image-20220507102603453

2.解决容器名解析问题

image-20220505144856493

这边打开后,会出现问题:

image-20220505144826455

有几种解决办法:

1.修改容器里的配置

2.在自己笔记本的hosts里添加域名解析

方法1:修改容器里的配置

1[root@devops remote-vscode]#docker ps
2CONTAINER ID   IMAGE                          COMMAND             CREATED       STATUS                 PORTS
3                         NAMES
4790a4c992c45   gitlab/gitlab-ce:14.9.3-ce.0   "/assets/wrapper"   5 hours ago   Up 5 hours (healthy)   0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:2222->22/tcp   devops_tutorial_gitlab
5[root@devops remote-vscode]#docker exec -it devops_tutorial_gitlab bash
6root@790a4c992c45:/# vim /etc/gitlab/gitlab.rb 
7取消注释这行,并将这里的`GENERATED_EXTERNAL_URL`替换为gitlab所在虚拟机的ip即可:

image-20220505145847977

image-20220505150227098

执行生效命令:

1root@790a4c992c45:/# gitlab-ctl reconfigure

这里需要稍等一会儿:

image-20220505150537563

重新配置后,我们再次来验证下效果:

image-20220505150657554

image-20220505150714352

这边可以看到,以上那个问题被解决了。😘

方法2:在自己笔记本的hosts里添加域名解析

1#C:\WINDOWS\System32\drivers\etc
2172.29.9.101  790a4c992c45

3.一台虚机是可以同时充当几个runner的

 1gitlab-runner register \
 2  --non-interactive \
 3  --url "http://172.29.9.101/" \
 4  --registration-token "9Yni-g-svEdGJqZrs2Vv" \
 5  --executor "shell" \
 6  --description "buildrunner" \
 7  --tag-list "build,k8s,go" \
 8  --run-untagged="true" \
 9  --locked="false" \
10  --access-level="not_protected"

image-20220505164550813

image-20220507104150260

image-20220507104225875

4.gitlab不能跨大版本升级

例如,你是12版本,你只能先升级到12最新版本,然后再升级到1版本,再升级到13最新版本,再升级到14版本! —2022.5.5

5.gitlab runnergit版本问题

后续gitlab-ci跑流水线实验时会出问题,因此要升级到2版本以上的!

image-20220507122815955

注意:使用yum remove git后,gitlab-runner会自动被删除的!

image-20220506133220004

image-20220506133201824

因此,升级git版本后,再使用rm -ivh命令安装gitlab-runner时,需要依赖,因此要用yum来安装,但yum安装后是会默认安装老版本git的,因此后面需要先删除/usr/bin/git再执行如下命令ln -s /usr/local/git/bin/git /usr/bin/git即可!

1yum install -y gitlab-runner-14.9.1-1.x86_64.rpm

关于我

我的博客主旨:

  • 排版美观,语言精炼;
  • 文档即手册,步骤明细,拒绝埋坑,提供源码;
  • 本人实战文档都是亲测成功的,各位小伙伴在实际操作过程中如有什么疑问,可随时联系本人帮您解决问题,让我们一起进步!
  1. 个人微信二维码:x2675263825 (舍得), qq:2675263825。

    image-20211002091450217

  2. 个人微信公众号:《云原生架构师实战》

    image-20211002141739664

  3. 个人csdn

    https://blog.csdn.net/weixin_39246554?spm=1010.2135.3001.5421

    image-20211002092344616

  4. 个人已开源干货😘

    不服来怼:宇宙中最好用的云笔记 & 其他开源干货:https://www.yuque.com/go/doc/73723298?#

    image-20220423100718009

  5. 个人博客:(www.onlyyou520.com)

    image-20220507104750327

  6. 知乎:https://www.zhihu.com/people/foryouone/posts

    image-20220507123311568

最后

好了,关于xxx实验就到这里了,感谢大家阅读,最后贴上我女神的photo,祝大家生活快乐,每天都过的有意义哦,我们下期见!

image-20211108223350304

推荐使用微信支付
微信支付二维码
推荐使用支付宝
支付宝二维码
最新文章

文档导航