一看必会系列:docker 练习 11 docker部署gitlab及gitlab迁移方法

No Comments Docker

gitlab docker安装

前提
关闭selinux:
# sed -i ‘s/enforcing/disabled/’ /etc/selinux/config
# setenforce 0
关闭swap:
# swapoff -a  # 临时
# vim /etc/fstab  # 永久
同步时间:
# yum install ntpdate -y
# ntpdate  ntp.api.bz

# step 1: 安装必要的一些系统工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
sudo yum-config-manager –add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3: 更新并安装 Docker-CE
sudo yum makecache fast
yum install docker-ce-17.03.3.ce -y
如果提示container-selinux依赖问题,先安装ce-17.03匹配版本:
# yum localinstall https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.3.ce-1.el7.noarch.rpm
# Step 4: 开启Docker服务
# systemctl enable docker && systemctl start docker

# docker info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 17.03.3-ce   #版本信息

部署容器
sudo docker run –detach \
  –hostname git03.jdccie.com \
  –publish 4430:443 –publish 803:80 –publish 223:22 \
  –name gitlab03 \
  –restart always \
  –volume /data/service/gitlab/config:/etc/gitlab \
  –volume /data/service/gitlab/logs:/var/log/gitlab \
  –volume /data/service/gitlab/data:/var/opt/gitlab \
  gitlab/gitlab-ce:latest
 
–restart 标志会检查容器的退出代码,并据此来决定是否要重启容器,默认是不会重启。
–restart的参数说明
always:无论容器的退出代码是什么,Docker都会自动重启该容器。
on-failure:只有当容器的退出代码为非0值的时候才会自动重启。另外,该参数还接受一个可选的重启次数参数,
–restart=on-fialure:5`表示当容器退出代码为非0时,Docker会尝试自动重启该容器,最多5次。

 
验证
root@docker01:~# docker ps
CONTAINER ID        IMAGE                                    COMMAND                                 
PORTS                                                             NAMES
b4a6652f3a4d        gitlab/gitlab-ce:latest                  "/assets/wrapper"         
0.0.0.0:223->22/tcp, 0.0.0.0:803->80/tcp, 0.0.0.0:4430->443/tcp   gitlab03

目录说明
Local location    Container location    Usage
/srv/gitlab/data    /var/opt/gitlab    For storing application data
/srv/gitlab/logs    /var/log/gitlab    For storing logs
/srv/gitlab/config    /etc/gitlab    For storing the GitLab configuration files

 

#gitlab 迁移及恢复数据
1.容器外复制备份文件到 logs目录
cp ../15531jdccie.com_2019_03_21_10.7.3_gitlab_backup.tar logs/
2.
docker exec -it a622702ec855 /bin/bash
root@git03:/#
3. 容器内
root@git03:/var/log/gitlab# cp 15531jdccie.com_2019_03_21_10.7.3_gitlab_backup.tar /var/opt/gitlab/backups
4. 修改权限
chmod 666 15531jdccie.com_2019_03_21_10.7.3_gitlab_backup.tar
5  恢复数据
gitlab-rake gitlab:backup:restore RAILS_ENV=production BACKUP=15531jdccie.com_2019_03_21_10.7.3
其中有两处需要 输入yes,注意一下
Do you want to continue (yes/no)? yes

5 。修改 project的域名信息
修改前http://1.1.1.1/root/git03.git
修改后
http://git03.jdccie.com:803/root/git03.git

修改方法
1.进容器
2.在容器里修改
vim /var/opt/gitlab/gitlab-rails/etc/gitlab.yml

修改内容
  gitlab:
    ## Web server settings (note: host is the FQDN, do not include http://)
    host: git03.jdccie.com:803   #这时取巧了。因为容器对外的端口是803但容器内的端口是80所以改成这样
    port: 80                     #这个不要动,会启不动,当然也有另外的方法看最后
    https: false

3.在容器里重启
gitlab-ctl restart

访问验证即可

———-其它信息

如果在selinux环境用以下命令
sudo docker run –detach \
  –hostname gitlab.example.com \
  –publish 443:443 –publish 80:80 –publish 22:22 \
  –name gitlab \
  –restart always \
  –volume /srv/gitlab/config:/etc/gitlab:Z \
  –volume /srv/gitlab/logs:/var/log/gitlab:Z \
  –volume /srv/gitlab/data:/var/opt/gitlab:Z \
  gitlab/gitlab-ce:latest
 
 
sudo docker exec -it gitlab /bin/bash

You can also just edit /etc/gitlab/gitlab.rb:

sudo docker exec -it gitlab vi /etc/gitlab/gitlab.rb

Once you open /etc/gitlab/gitlab.rb make sure to set the external_url to point to a valid URL.

To receive e-mails from GitLab you have to configure the SMTP settings because the GitLab Docker image doesn’t have an SMTP server installed.

------中间广告---------

You may also be interested in Enabling HTTPS.

After you make all the changes you want, you will need to restart the container in order to reconfigure GitLab:

sudo docker restart gitlab

升级新版本

1 sudo docker stop gitlab
2 sudo docker rm gitlab
3 sudo docker pull gitlab/gitlab-ce:latest
4 sudo docker run –detach \
–hostname gitlab.example.com \
–publish 443:443 –publish 80:80 –publish 22:22 \
–name gitlab \
–restart always \
–volume /srv/gitlab/config:/etc/gitlab \
–volume /srv/gitlab/logs:/var/log/gitlab \
–volume /srv/gitlab/data:/var/opt/gitlab \
gitlab/gitlab-ce:latest
 
 
这里可以直接改域名信息
Create a docker-compose.yml file (or download an example):

web:
   image: ‘gitlab/gitlab-ce:latest’
   restart: always
   hostname: ‘gitlab.example.com’
   environment:
     GITLAB_OMNIBUS_CONFIG: |
       external_url ‘https://gitlab.example.com’
       # Add any other gitlab.rb configuration here, each on its own line
   ports:
     – ’80:80′
     – ‘443:443’
     – ’22:22′
   volumes:
     – ‘/srv/gitlab/config:/etc/gitlab’
     – ‘/srv/gitlab/logs:/var/log/gitlab’
     – ‘/srv/gitlab/data:/var/opt/gitlab’
 
 
———-报错

"couldn’t deduce an advertise address: no private IP found, explicit advertise addr not provided"

#恢复报错
Unpacking backup … tar: 15531jdccie.com_2019_03_21_10.7.3_gitlab_backup.tar: Cannot open: Permission denied

处理方式 修改权限
root@git03:/var/opt/gitlab/backups# ll
total 430188
drwx——.  2 git  root      4096 Mar 21 06:03 ./
drwxr-xr-x. 19 root root      4096 Mar 21 04:52 ../
-rw——- #原来的权限.  1 root root 440504320 Mar 21 06:03 15531jdccie.com_2019_03_21_10.7.3_gitlab_backup.tar
root@git03:/var/opt/gitlab/backups# chmod 666 15531jdccie.com_2019_03_21_10.7.3_gitlab_backup.tar
root@git03:/var/opt/gitlab/backups# ll
total 430188
drwx——.  2 git  root      4096 Mar 21 06:03 ./
drwxr-xr-x. 19 root root      4096 Mar 21 04:52 ../
-rw-rw-rw-#修改后的.  1 root root 440504320 Mar 21 06:03 15531jdccie.com_2019_03_21_10.7.3_gitlab_backup.tar

root@git03:/var/opt/gitlab/backups# gitlab-rake gitlab:backup:restore RAILS_ENV=production BACKUP=15531jdccie.com_2019_03_21_10.7.3                                                                              
Unpacking backup … done
GitLab version mismatch:
  Your current GitLab version (11.8.3) differs from the GitLab version in the backup!
  Please switch to the following version and try again:
  version: 10.7.3

[root@jdccie.comgitlab-001 logs]# docker pull gitlab/gitlab-ce:10.7.3-ce.0

删除原容器后,创建新容器报错
Error response from daemon: service endpoint with name gitlab03 already exists

原因,是容器被删了。但网络占用仍在占用
解决
1.确认删除容器  docker rm -f 容器ID
2.确认容器使用的网络也被删除
docker network inspect 网络模式
3.强制断开该网络 docker network disconnect –force 网络模式 xxx
docker network disconnect –force bridge gitlab03
4.验证 不出现gitlab03即可
[root@jdccie.comgitlab-001 ~]# docker network inspect bridge
5.重建容器即可

这是之前的信息
[root@jdccie.comgitlab-001 ~]# docker network inspect bridge
        "Name": "bridge",
                "Name": "gitlab03",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",

https://docs.gitlab.com/omnibus/docker/README.html

#
[root@jdccie.comgitlab-001 ops]# docker ps
CONTAINER ID        IMAGE                          COMMAND             CREATED             STATUS                         PORTS                                                             NAMES
0ce0e6599588        gitlab/gitlab-ce:10.7.3-ce.0   "/assets/wrapper"   About an hour ago   Up About an hour (unhealthy)   0.0.0.0:223->22/tcp, 0.0.0.0:803->80/tcp, 0.0.0.0:4430->443/tcp   gitlab03
[root@jdccie.comgitlab-001 ops]#

docker inspect 0ce0e6599588

                    {
                        "Start": "2019-03-21T09:14:29.322227599Z",
                        "End": "2019-03-21T09:14:59.322409713Z",
                        "ExitCode": -1,
                        "Output": "Health check exceeded timeout (30s)"
                    }

查看日志是nginx配置文件改的有问题

==> /var/log/gitlab/unicorn/unicorn_stderr.log <==
I, [2019-03-21T09:15:22.477559 #945]  INFO — : reaped #<Process::Status: pid 1177 exit 0> worker=0
I, [2019-03-21T09:15:22.477894 #945]  INFO — : reaped #<Process::Status: pid 1180 exit 0> worker=1
I, [2019-03-21T09:15:22.477985 #945]  INFO — : reaped #<Process::Status: pid 1183 exit 0> worker=2
I, [2019-03-21T09:15:22.478067 #945]  INFO — : reaped #<Process::Status: pid 1189 exit 0> worker=4
I, [2019-03-21T09:15:22.478318 #945]  INFO — : reaped #<Process::Status: pid 3199 exit 0> worker=3
I, [2019-03-21T09:15:22.478480 #945]  INFO — : master complete
ok: down: unicorn: 0s, normally up

从容器里把有问题的配置文件搞出来
docker cp 0ce0e6599588:/var/opt/gitlab/nginx/conf/gitlab-http.conf .
[root@jdccie.comgitlab-001 ops]# ls
gitlab-http.conf
修改
[root@jdccie.comgitlab-001 ops]# vim gitlab-http.conf
再搞回去
[root@jdccie.comgitlab-001 ops]# docker cp gitlab-http.conf 0ce0e6599588:/var/opt/gitlab/nginx/conf/gitlab-http.conf
重新启动服务器

发表评论