环境信息:
hostname | ip地址 | cpu | 内存 | 磁盘 | 系统版本 | 域名 |
---|---|---|---|---|---|---|
discourse | 192.168.0.174 | 4c | 4g | 20g | ubuntu1804 | discourse.onap.vip |
gitlab-runner | 192.168.0.175 | 4c | 4g | 20g | ubuntu1804 | gitlab.onap.vip |
一、部署docker(两个节点都安装)
apt-get update
apt-get -y install vim apt-transport-https ca-certificates curl software-properties-common
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
apt-get -y update
apt-get -y install docker-ce
mkdir -p /etc/docker
cat > /etc/docker/daemon.json <<EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"registry-mirrors": ["https://t3xbrfwz.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl start docker
systemctl enable docker
二、部署gitlab和gitlab runner
1、部署gitlab
1>部署gitlab
docker run -itd --restart=always --name gitlab -p 443:443 -p 80:80 -p 2222:22 -v /opt/gitlab-ce/config:/etc/gitlab -v /opt/gitlab-ce/logs:/var/log/gitlab -v /opt/gitlab-ce/data:/var/opt/gitlab -v /etc/localtime:/etc/localtime registry.baidubce.com/docker-hub/gitlab/gitlab-ce-zh:latest
2>解决gitlab使用中遇到的问题
解决gitlab clone时gitlab地址是容器id问题
进入容器
docker exec -it gitlab bash
修改容器内/opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml的host字段为主机地址
重启gitlab服务
gitlab-ctl restart
解决gitlab无法加载头像问题
修改容器内/opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml的plain_url字段
plain_url: http://cdn.libravatar.org/avatar/%{hash}?s=%{size}&d=identicon
重启gitlab服务清空缓存
gitlab-ctl restart
gitlab-rake cache:clear RAILS_ENV=production
2、登录gitlab设置密码并新建群组和项目
3、clone github项目同步到新建的仓库
1>获取源码
git clone http://gitlab.onap.vip/duanshuaixing/discourse_docker.git
git clone http://gitlab.onap.vip/duanshuaixing/discourse_docker.git /var/discourse
mv /var/discourse/* discourse_docker/
cd discourse_docker/
git add .
git commit -m "project init"
git push origin master
2>针对项目进行配置
4、部署gitlab runner
1>通过容器部署runner
docker run -itd --restart=always --name gitlab-runner -v /opt/gitlab-runner/config:/etc/gitlab-runner -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner:latest
2>注册runner
在项目的cicd的Runner中获取专用runner的url和注册令牌
在gitlab runner容器中注册到gitlab
root@discourse-gitlab:~# docker exec -it gitlab-runner bash
root@a7cbe2f57e97:/# gitlab-runner register
Runtime platform arch=amd64 os=linux pid=30 revision=54944146 version=13.10.0
Running in system-mode.
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://gitlab.onap.vip/
Enter the registration token:
UA8gUrxxBAxf5PvP8uhh
Enter a description for the runner:
[a7cbe2f57e97]: discourse-deploy
Enter tags for the runner (comma-separated):
discourse-runner #gitlab-ci.yml的tag中需要用这个参数
Registering runner... succeeded runner=UA8gUrxx
Enter an executor: kubernetes, parallels, shell, ssh, docker+machine, docker-ssh+machine, custom, docker, docker-ssh, virtualbox:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
root@a7cbe2f57e97:/#
三、部署通过gitlab runner部署discourse
1、配置runner和部署环境互信
docker exec -it gitlab-runner
ssh-copy-id root@192.168.0.174
2、在项目的cicd的Runner中添加变量并保存变量
SSH_HOST 192.168.0.174
SSH_KNOWN_HOSTS 值是容器中cat ~/.ssh/known_hosts的值
SSH_PRIVATE_KEY 值是容器中cat ~/.ssh/id_rsa
SSH_USER root
3、添加gitlab-ci.yml
stages:
- ssh-check
- git-checkout
- deploy-app
deploy_discourse:
stage: ssh-check
tags:
- discourse-runner
before_script:
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
script:
- ssh $SSH_USER@$SSH_HOST "hostname"
only:
- master
stage: git-checkout
tags:
- discourse-runner
script:
- ssh $SSH_USER@$SSH_HOST "cd /var/discourse && git pull"
stage: deploy-app
tags:
- discourse-runner
script:
- ssh $SSH_USER@$SSH_HOST "cd /var/discourse && ./launcher rebuild app && docker ps -a"