gitlab-ce遷移到docker與版本升級


服務器是aws的,之前的gitlab是源碼安裝,升級很不方便,現在遷移到docker,版本從11.7.5升級到12.2.5

一、源數據備份

  1. 登錄線上gitlab
  2. 執行命令
$ cd /home/git/gitlab
$ sudo -u git -H bundle exec rake gitlab:backup:create RAILS_ENV=production

備份後數據在/home/git/gitlab/tmp/backups/下,名字爲 日期_版本_gitlab_backup.tar
注:/home/git爲掛載的硬盤

  1. 將需要的文件拷貝到/home/git/gitlab下
$ sudo mkdir /home/git/gitlab/gitlab_file
$ sudo cp /etc/nginx/conf.d/default.conf  /home/git/gitlab/gitlab_file         nginx配置文件
$ sudo cp /etc/nginx/conf.d/gitlab-pages.conf  /home/git/gitlab/gitlab_file    pages配置文件
$ sudo cp /etc/nginx/fullchain.pem  /home/git/gitlab/gitlab_file     	       nginx密鑰文件
$ sudo cp /etc/nginx/privkey.pem   /home/git/gitlab/gitlab_file		           nginx密鑰文件

注:根據需求,gitlab要開啓pages,所以拷貝pages的配置文件;nginx要啓用ssl

  1. 複製gitlab數據盤(/home/git) 卷—>創建快照 快照—>創建卷

二、恢復前準備

  1. 新啓動的gitlab服務器系統爲ubuntu18.04,將新建的卷掛載到服務器上
  2. 2.創建存放相應文件及存放數據的的目錄、掛載數據盤
$ mkdir /home/ubuntu/gitlab_file
$ sudo mkdir /data
$ 將數據盤掛載到/data下,可將其他文件刪除,只保留備份數據文件,/data作爲存放數據的盤。
  1. 將需要的文件拷貝到gitlab_file中
$ sudo cp /opt/gitlab/gitlab_file/default.conf  /home/ubuntu/gitlab_file         nginx配置文件
$ sudo cp /opt/gitlab/gitlab_file/gitlab-pages.conf /home/ubuntu/gitlab_file     pages配置文件
$ sudo cp /opt/gitlab/gitlab_file/fullchain.pem  /home/ubuntu/gitlab_file        nginx密鑰文件
$ sudo cp /opt/gitlab/gitlab_file/privkey.pem   /home/ubuntu/gitlab_file	     nginx密鑰文件
  1. Docker安裝
    各系統安裝docker 鏈接 https://zhuanlan.zhihu.com/p/54147784
    將ubuntu用戶加入docker組
$ sudo gpasswd -a ubuntu docker
  1. Docker-compose安裝

(1) 運行以下命令下載最新版本的 docker-compose:

$ sudo curl -L https://github.com/docker/compose/releases/download/1.18.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

(2) 更改二進制文件的權限,使其能夠運行:

$ sudo  chmod +x /usr/local/bin/docker-compose

(3) 測試安裝

$ docker-compose --version
  1. 更改本機sshd服務端口爲23
    注:需要將gitlab容器22端口映射到本機22端口

三、數據恢復

  1. 啓動Postgresql

(1) 拉取數據庫鏡像

$ docker pull sameersbn/postgresql:10-2

(2) 創建數據目錄

$ mkdir -p  /data/postgresql/data

(3) 啓動數據庫

$ bash pg.sh
$ cat pg.sh
docker run --rm --name postgresql -d \
        -e  'DB_NAME=gitlabhq_production' \
        -e  'DB_USER=gitlab' \
        -e  'DB_PASS=123456' \
        -e  'DB_EXTENSION=pg_trgm' \
        -v /data/postgresql/data:/var/lib/postgresql \
        sameersbn/postgresql:10-2
  1. 啓動redis

(1) 拉取redis鏡像

$ docker pull sameersbn/redis:4.0.9-3

(2) 創建redis數據目錄

$ mkdir -p /data/redis/data

(3) 啓動redis

$ bash redis.sh
$ cat redis.sh
docker run --rm --name redis -d \
        -v /data/redis/data:/var/lib/redis  \
        sameersbn/redis:4.0.9-3
  1. gitlab

(1) 拉取gitlab鏡像

$ docker pull sameersbn/gitlab:11.7.5  舊版本
$ docker pull sameersbn/gitlab:11.11.0 中間版本
$ docker pull sameersbn/gitlab:12.2.5  新版本

注:從11.7.5升級到12.2.5版本需要先升級到11.11.0版本
(2) 創建數據目錄

$ mkdir -p /data/gitlab/data

(3) 初始化 gitlab

$ bash init.sh
$ cat init.sh
docker run --name gitlab -it --rm \
        --link postgresql:postgresql \
        --link redis:redisio \
        -e "DB_USER=gitlab" \
        -e "DB_PASS=123456" \
        -e "DB_NAME=gitlabhq_production"  \
        -e 'GITLAB_SECRETS_DB_KEY_BASE=long-and-random-alpha-numeric-string' \
        -e 'GITLAB_SECRETS_SECRET_KEY_BASE=long-and-random-alpha-numeric-string' \
        -e 'GITLAB_SECRETS_OTP_KEY_BASE=long-and-random-alpha-numeric-string' \
        -v /data/gitlab/data:/home/git/data \
		sameersbn/gitlab:11.7.5 \
        app:rake gitlab:setup

注:會遇到報錯:

Failed to connect to Gitaly...  Error: 14:Connect Failed

解決:先啓動gitlab 拷貝/home/git/gitlab/lib/tasks/gitlab/setup.rake到本地 註釋第四行

注:GITLAB_SECRETS_DB_KEY_BASE GITLAB_SECRETS_SECRET_KEY_BASE GITLAB_SECRETS_OTP_KEY_BASE這三個參數的值要和老版本的一樣(/home/git/gitlab/config/secrets.yml),否則會出現“項目–>設置–>集成”頁面500報錯。

啓動:

$ docker run --name gitlab -itd --rm \
        --link postgresql:postgresql \
        --link redis:redisio \
        -e "DB_USER=gitlab" \
        -e "DB_PASS=123456" \
        -e "DB_NAME=gitlabhq_production"  \
        -e 'GITLAB_SECRETS_DB_KEY_BASE=long-and-random-alpha-numeric-string' \
        -e 'GITLAB_SECRETS_SECRET_KEY_BASE=long-and-random-alpha-numeric-string' \
        -e 'GITLAB_SECRETS_OTP_KEY_BASE=long-and-random-alpha-numeric-string' \
        -v /data/gitlab/data:/home/git/data \
        sameersbn/gitlab:11.7.5

掛載、修改配置文件:

$ docker cp gitlab:/home/git/gitlab/lib/tasks/gitlab/setup.rake  /home/ubuntu/gitlab_file/setup.rake
$ vim /home/ubuntu/gitlab_file/setup.rake 把第四行註釋掉 #  check_gitaly_connection

初始化時把修改後的文件掛載進容器:

$ docker stop gitlab
$ docker run --name gitlab -it --rm \
        --link postgresql:postgresql \
        --link redis:redisio \
        -e "DB_USER=gitlab" \
        -e "DB_PASS=123456" \
        -e "DB_NAME=gitlabhq_production"  \
        -e 'GITLAB_SECRETS_DB_KEY_BASE=long-and-random-alpha-numeric-string' \
        -e 'GITLAB_SECRETS_SECRET_KEY_BASE=long-and-random-alpha-numeric-string' \
        -e 'GITLAB_SECRETS_OTP_KEY_BASE=long-and-random-alpha-numeric-string' \
        -v /data/gitlab/data:/home/git/data \
        -v /home/ubuntu/gitlab_file/setup.rake:/home/git/gitlab/lib/tasks/gitlab/setup.rake \
        sameersbn/gitlab:11.7.5 \
        app:rake gitlab:setup

輸入一次yes
注:最後會出現報錯:

Database 'gitlabhq_production' already exists
rake aborted!
ActiveRecord::ProtectedEnvironmentError: You are attempting to run a destructive action against your 'production' database.

因爲運行數據庫時已經創建gitlabhq_production數據庫,所以報錯正常,初始化結束後自動退出容器。
(4) 恢復數據
把備份數據放在/data/gitlab/data/backups 下面,執行:

$ sudo chmod 777 備份文件

注:加可寫權限,否者恢復時會報錯:

Unpacking backup ... tar: 1568898165_2019_09_19_11.7.5_gitlab_backup.tar: Cannot open: Permission denied
tar: Error is not recoverable: exiting now
unpacking backup failed

啓動gitlab

$ bash up.sh
$ cat up.sh
docker run --name gitlab -itd --rm \
        --link postgresql:postgresql \
        --link redis:redisio \
        -e "DB_USER=gitlab" \
        -e "DB_PASS=123456" \
        -e "DB_NAME=gitlabhq_production"  \
        -e 'GITLAB_SECRETS_DB_KEY_BASE=long-and-random-alpha-numeric-string' \
        -e 'GITLAB_SECRETS_SECRET_KEY_BASE=long-and-random-alpha-numeric-string' \
        -e 'GITLAB_SECRETS_OTP_KEY_BASE=long-and-random-alpha-numeric-string' \
        -v /data/gitlab/data:/home/git/data \
        -v /home/ubuntu/gitlab_file/setup.rake:/home/git/gitlab/lib/tasks/gitlab/setup.rake \
        sameersbn/gitlab:11.7.5

進入容器

$ docker exec -it gitlab /bin/bash

執行命令

$ /sbin/entrypoint.sh app:rake gitlab:backup:restore to restore a backup

輸入備份數據文件名 回車 進入恢復狀態
期間會輸入兩次yes,恢復時間較長

注:最後會報錯退出:

rake aborted!
Don't know how to build task 'to' (See the list of available tasks with `rake --tasks`)
/home/git/gitlab/vendor/bundle/ruby/2.5.0/gems/rake-12.3.2/exe/rake:27:in `<top (required)>'
(See full trace by running task with --trace)

恢復結束後 關閉之前的postgresql redis gitlab容器 通過docker-compose重啓新的容器

$ docker stop postgresql 
$ docker stop redis
$ docker stop gitlab 

啓動容器:

$ docker-compose up -d

docker-compose.yml文件在文末,docker-compose.yml文件中的gitlab版本11.7.5
啓動成功後,登錄gitlab 測試各部分功能

注:測試頁面admin/runner時 可能會出現500報錯
解決:進入gitlab容器
執行:

$ cd /home/git/gitlab
$ sudo -u git -H bundle exec rails console production

在交互界面執行:

ApplicationSetting.current.reset_runners_registration_token! 

成功後exit退出,刷新runner頁面。
此步驟可以不用操作,等升級到12.2.5版本後統一解決。

四、gitlab升級

  1. 升級gitlab到11.11.0版本

將docker-compose.yml文件中gitlab版本改爲11.11.0
關閉gitlab容器:

$ docker stop gitlab&&docker rm gitlab

用中間版本鏡像啓動gitlab:

$ docker-compose up -d --no-recreate

啓動成功後,登錄gitlab 測試各部分功能

注:可能會出現admin/runner及項目ci/cd頁面 500報錯
解決:進入postgresql容器 登錄數據庫
執行:

$ sudo su postgres
$ psql
postgres=# \c gitlabhq_production
gitlabhq_production=# UPDATE projects SET runners_token = null, runners_token_encrypted = null;
gitlabhq_production=# UPDATE namespaces SET runners_token = null, runners_token_encrypted = null;
gitlabhq_production=# UPDATE application_settings SET runners_registration_token_encrypted = null;
gitlabhq_production=# UPDATE ci_runners SET token = null, token_encrypted = null;

成功後\q退出,刷新runner頁面。
此步驟可以不用操作,等升級到12.2.5版本後統一解決。

  1. 升級gitlab到12.2.5版本

將docker-compose.yml文件中gitlab版本改爲12.2.5
關閉gitlab容器

$ docker stop gitlab&&docker rm gitlab

用新版本鏡像啓動gitlab

$ docker-compose up -d --no-recreate

啓動成功後,登錄gitlab 測試各部分功能
注:可能會出現admin/runner及項目ci/cd頁面 500報錯
解決:
(1) 進入postgresql容器 登錄數據庫
執行:

$ sudo su postgres
$ psql
postgres=# \c gitlabhq_production
gitlabhq_production=# UPDATE projects SET runners_token = null, runners_token_encrypted = null;
gitlabhq_production=# UPDATE namespaces SET runners_token = null, runners_token_encrypted = null;
gitlabhq_production=# UPDATE application_settings SET runners_registration_token_encrypted = null;
gitlabhq_production=# UPDATE ci_runners SET token = null, token_encrypted = null;

成功後\q退出。
(2) 進入gitlab容器
執行:

$ cd /home/git/gitlab 
$ sudo -u git -H bundle exec rails console production

在交互界面執行:

ApplicationSetting.current.reset_runners_registration_token!

成功後exit退出,刷新runner頁面。

Docker-compose文件

$ cat docker-compose.yml

redis:
  restart: always
  container_name: redis
  image: sameersbn/redis:4.0.9-3
  volumes:
    - /data/redis/data:/var/lib/redis
postgresql:
  restart: always
  container_name: postgresql
  image: sameersbn/postgresql:10-2
  volumes:
    - /data/postgresql/data:/var/lib/postgresql
gitlab:
  restart: always
  container_name: gitlab
  image: sameersbn/gitlab:11.7.5
  ports:
    - "80:80"
    - "22:22"
    - "443:443"
  links:
    - redis:redisio
    - postgresql:postgresql
  volumes:
    - /data/gitlab/data:/home/git/data
    - /home/ubuntu/gitlab_file/default.conf:/etc/nginx/conf.d/default.conf
    - /home/ubuntu/gitlab_file/gitlab-pages.conf:/etc/nginx/conf.d/gitlab-pages.conf
    - /home/ubuntu/gitlab_file/fullchain.pem:/etc/nginx/fullchain.pem
    - /home/ubuntu/gitlab_file/privkey.pem:/etc/nginx/privkey.pem
    - /home/ubuntu/gitlab_file/setup.rake:/home/git/gitlab/lib/tasks/gitlab/setup.rake
  environment:
    - DEBUG=false

    - DB_ADAPTER=postgresql
    - DB_HOST=postgresql
    - DB_PORT=5432
    - DB_USER=gitlab
    - DB_PASS=123456
    - DB_NAME=gitlabhq_production

    - REDIS_HOST=redisio
    - REDIS_PORT=6379

    - GITLAB_HOST=gitlab-test.com
    - [email protected]
    - GITLAB_EMAIL_ENABLED=true

    - SMTP_ENABLED=true
    - SMTP_DOMAIN=qq.com
    - SMTP_HOST=smtp.exmail.qq.com
    - SMTP_PORT=465
    - SMTP_STARTTLS=true
    - SMTP_TLS=true
    - [email protected]
    - SMTP_PASS=xxxxxxxxxx
    - SMTP_AUTHENTICATION=login

    - RACK_ATTACK_WHITELIST=127.0.0.1,xxxxxx

    - GITLAB_PAGES_ENABLED=true
    - GITLAB_PAGES_ACCESS_CONTROL=false
    - GITLAB_PAGES_DOMAIN=pages-test.com
    - GITLAB_PAGES_PORT=80
    - GITLAB_PAGES_ARTIFACTS_SERVER=true
    - GITLAB_PAGES_HTTPS=false

    - GITLAB_MATTERMOST_ENABLED=true

    - GITLAB_TIMEZONE=Beijing

    - GITLAB_SECRETS_DB_KEY_BASE=long-and-random-alpha-numeric-string
    - GITLAB_SECRETS_SECRET_KEY_BASE=long-and-random-alpha-numeric-string
	- GITLAB_SECRETS_OTP_KEY_BASE=long-and-random-alpha-numeric-string 

FAQ

  1. link後redis名爲redisio。

  2. 啓動gitlab時連接不上數據庫和redis 解決:docker-compose.yml 中變量DB_HOST=postgresql REDIS_HOST=redisio 爲link後的名字。

  3. 啓動gitlab時連接不上數據庫 解決:通過docker-compose.yml啓動postgresql時不需要指定DB_NAME DB_USER DB_PASS(第一次啓動時已經指定)。

  4. gitlab啓動成功後“項目的設置”界面500 解決:變量GITLAB_HOST=gitlab-test.fenda.io:80 指定了端口 把端口去掉。

  5. gitlab啓動成功後 gitlab-pages啓動失敗 報錯:auth-secret must be defined if authentication is supported 解決:指定變量GITLAB_PAGES_ACCESS_CONTROL=false。

  6. 設置允許來自列入白名單的主機的請求。默認爲127.0.0.1 解決:指定變量RACK_ATTACK_WHITELIST=127.0.0.1,xxxxxxxx(ip)。

  7. 郵件發送失敗 解決:指定變量SMTP_TLS=true 默認爲false。

  8. 初始化失敗 報錯Failed to connect to Gitaly… Error: 14:Connect Failed 解決:先啓動gitlab 拷貝/home/git/gitlab/lib/tasks/gitlab/setup.rake到本地 註釋第四行 # check_gitaly_connection 初始化時把修改後的文件掛載進容器。

  9. 從11.7.5本版升級到12.2.5版本時要先升級到11.11.0 (更新數據庫內數據)。

  10. 11.7.5版本gitlab啓動成功後 amdin/runner頁面500 報錯:

Completed 500 Internal Server Error in 175ms (ActiveRecord: 10.2ms)

ActionView::Template::Error ():
    37:
    38:     .col-sm-6
    39:       .bs-callout
    40:         = render partial: 'ci/runner/how_to_setup_runner',
    41:                  locals: { registration_token: Gitlab::CurrentSettings.runners_registration_token,
    42:                            type: 'shared',
    43:                            reset_token_url: reset_registration_token_admin_application_settings_path }

lib/gitlab/crypto_helper.rb:27:in `aes256_gcm_decrypt'
app/models/concerns/token_authenticatable_strategies/encrypted.rb:55:in `get_token'
app/models/concerns/token_authenticatable_strategies/base.rb:33:in `ensure_token!'
app/models/concerns/token_authenticatable.rb:43:in `block in add_authentication_token_field'

解決:進去gitlab容器 執行 cd /home/git/gitlab/gitlab sudo -u git -H bundle exec rails console production
在交互界面執行 ApplicationSetting.current.reset_runners_registration_token! 成功後exit退出,刷新runner頁面。
omnibus 版本應使用如下命令:gitlab-rails console production
在交互界面執行 ApplicationSetting.current.reset_runners_registration_token! 成功後exit退出,刷新runner頁面。
相關文檔鏈接:https://blog.csdn.net/weixin_43952432/article/details/89642418

  1. 11.11.0版本gitlab啓動成功後 amdin/runner及項目下ci/cd頁面500 報錯:
Completed 500 Internal Server Error in 133ms (ActiveRecord: 19.8ms)
ActionView::Template::Error ():
    16:   .table-section.section-10
    17:     .table-mobile-header{ role: 'rowheader' }= _('Runner token')
    18:     .table-mobile-content
    19:       = link_to runner.short_sha, admin_runner_path(runner)
    20:
    21:   .table-section.section-20
    22:     .table-mobile-header{ role: 'rowheader' }= _('Description')

lib/gitlab/crypto_helper.rb:27:in `aes256_gcm_decrypt'



Completed 500 Internal Server Error in 221ms (ActiveRecord: 46.1ms)
ActionView::Template::Error ():
    25:     project_clusters_path(@project),
    26:     class: 'btn btn-info'
    27:   %hr
    28:   = render partial: 'ci/runner/how_to_setup_runner',
    29:            locals: { registration_token: @project.runners_token,
    30:                      type: 'specific',
    31:                      reset_token_url: reset_registration_token_namespace_project_settings_ci_cd_path }

lib/gitlab/crypto_helper.rb:27:in `aes256_gcm_decrypt'

解決:進入postgresql 登錄數據庫 執行:

$ sudo su postgres
$ psql
postgres=# \c gitlabhq_production
gitlabhq_production=# UPDATE projects SET runners_token = null, runners_token_encrypted = null;
gitlabhq_production=# UPDATE namespaces SET runners_token = null, runners_token_encrypted = null;
gitlabhq_production=# UPDATE application_settings SET runners_registration_token_encrypted = null;
gitlabhq_production=# UPDATE ci_runners SET token = null, token_encrypted = null; 

成功後退出,刷新runner頁面。
相關文檔鏈接:https://docs.gitlab.com/ee/raketasks/backup_restore.html#when-the-secrets-file-is-lost

  1. 恢復數據報錯:
Unpacking backup ... tar: 1568898165_2019_09_19_11.7.5_gitlab_backup.tar: Cannot open: Permission denied
tar: Error is not recoverable: exiting now
unpacking backup failed

解決: chmod 777 備份文件

  1. 初始化時報錯:
Database 'gitlabhq_production' already exists
rake aborted!
ActiveRecord::ProtectedEnvironmentError: You are attempting to run a destructive action against your 'production' database.

正常現象,啓動數據庫時已經創建過庫。

  1. “項目–>設置–>集成”頁面500報錯:
ActionView::Template::Error ():
1: %li
2:   .row
3:     .col-md-8.col-lg-7
4:       %strong.light-header= hook.url    
5:       %div    
6:         - ProjectHook.triggers.each_value do |event|    
7:           - if hook.public_send(event)  
app/models/hooks/web_hook.rb:62:in `url' 

解決:啓動時把GITLAB_SECRETS_DB_KEY_BASE GITLAB_SECRETS_SECRET_KEY_BASE GITLAB_SECRETS_OTP_KEY_BASE這三個參數設置成和老版本一樣;/home/git/gitlab/config/secrets.yml文件中的值。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章