docker部署gitlab-ci环境部署discourse

环境信息:

hostnameip地址cpu内存磁盘系统版本域名
discourse192.168.0.1744c4g20gubuntu1804discourse.onap.vip
gitlab-runner192.168.0.1754c4g20gubuntu1804gitlab.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"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值