docker-compose+gitlab部署CICD過程
1.概念
CICD,顧名思義就是持續(xù)集成(Continuous Integration)和持續(xù)部署(Continuous Deployment)簡稱,指在開發(fā)過程中自動執(zhí)行一系列腳本來減低開發(fā)引入 bug 的概率,在新代碼從開發(fā)到部署的過程中,盡量減少人工的介入。
CICD,其實可以使用jenkinsfile,就象gitlab的 .gitlab-ci.yaml文件,把CICD的流程控制和步驟也作為開發(fā)的一部分,由開發(fā)去維護。并且可以很快的部署到多個環(huán)境。
持續(xù)集成:
持續(xù)集成指在和向遠程倉庫 push 代碼后,在這次提交合并入主分支前進行一系列測試,構建等流程。
持續(xù)部署:
持續(xù)部署在持續(xù)集成的基礎上更進一步,指將推送指倉庫默認分支的部署至產(chǎn)品環(huán)境。如果這部分需要手動觸發(fā),這就是一個持續(xù)交付(Continuous Delivery)環(huán)節(jié)。
2.需要準備環(huán)境
192.168.78.129:docker、docker-compose、gitlab
192.168.78.130:docker、docker-compoe、gitrunner
每臺服務器的CPU至少要4G及以上
3.部署過程
- 兩臺機器都要執(zhí)行的腳本:stop_safe.sh,docker_install.sh
- 兩臺機器都要部署 docker-compose
清理Centos7環(huán)境
[root@gitlab ~]# mkdir /data/
[root@gitlab data]# vim stop_safe.sh
#!/bin/bash
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
rpm -qa | grep ntpdate &>/dev/null || yum install ntpdate -y
ntpdate ntp1.aliyun.com #同步時間
systemctl stop NetworkManager && systemctl disable NetworkManager
systemctl stop firewalld && systemctl disable firewalld #關閉防火墻
grep -x "SELINUX=disabled" /etc/selinux/config &>/dev/null
if [[ $? != 0 ]];then
sed -i 's/=enforcing/=disabled/' /etc/selinux/config && setenforce 0
fi
grep swap /etc/fstab | grep ^# &>/dev/null
if [[ $? != 0 ]];then
sed -i '/swap/s/^/#/g' /etc/fstab && swapoff -a
fi
sed -i 's/^GSSAPIAuthentication/#GSSAPIAuthentication/g' /etc/ssh/sshd_config
echo 'GSSAPIAuthentication no' >> /etc/ssh/sshd_config
grep "UseDNS *no" /etc/ssh/sshd_config &>/dev/null || echo 'UseDNS no' >> /etc/ssh/sshd_config
systemctl restart sshd
yum install -y wget epel-release bash-completion.noarch net-tools
yum install -y python3-3.6.8-18.el7.x86_64
[root@gitlab data]# bash stop_safe.sh部署docker
[root@gitlab data]# cat docker_install.sh
#!/bin/bash
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'ABC'
{
"registry-mirrors": ["https://hiz48418.mirror.aliyuncs.com"]
}
ABC
# 獲取本機IP
ETH_NAME=`route -n | awk '($1 ~ "0.0.0.0"){print $NF}'`
IP=`ifconfig $ETH_NAME | awk 'NR==2{print $2}'`
# 清空yum環(huán)境
rm -f /var/lib/rpm/__*
rpm --rebuilddb -v -v &>/dev/null
yum clean dbcache
yum clean metadata
yum clean rpmdb
yum clean headers
yum clean all
rm -rf /var/cache/yum/timedhosts.txt
rm -rf /var/cache/yum/*
install_docker() {
yum install -y yum-utils
#使用阿里的下載地址
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sed -i 's/gpgcheck=1/gpgcheck=0/g' /etc/yum.repos.d/docker-ce.repo
yum --enablerepo=docker-ce-stable clean metadata
yum makecache
yum install -y docker-ce docker-ce-cli containerd.io
systemctl start docker
systemctl enable docker
echo -e "\033[32m######## docker version ########\033[0m"
echo -n "$IP"
docker --version
echo -e "\033[32m############# END ##############\033[0m"
}
rpm -qa | grep docker-ce &>/dev/null && echo -e "\033[32m$IP docker already installed......\033[0m" || install_docker
[root@gitlab data]# bash docker_install.sh
[root@gitlab data]# systemctl status docker部署docker-compose
[root@gitlab ~]# pip3 install docker-compose #Centos7默認的python版本是2.7,本次部署需要python3以上的
#出現(xiàn)以下錯誤
=============================DEBUG ASSISTANCE==========================
If you are seeing an error here please try the following to
successfully install cryptography:
Upgrade to the latest pip and try again. This will fix errors for most
users. See: https://pip.pypa.io/en/stable/installing/#upgrading-pip
=============================DEBUG ASSISTANCE==========================
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-build-cfg40vu0/cryptography/setup.py", line 14, in <module>
from setuptools_rust import RustExtension
ModuleNotFoundError: No module named 'setuptools_rust'
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-cfg40vu0/cryptography/
#以上錯誤的解決方法
[root@gitlab ~]# python3 -m pip install --upgrade --force pip
[root@gitlab ~]# pip3 install docker-compose
[root@gitlab ~]# docker-compose --version
docker-compose version 1.29.2, build unknown
部署gitlab
在192.168.78.129主機上使用docker 創(chuàng)建docker-compose.yml文件
[root@gitlab data]# cat docker-compose.yml
version: '3'
services:
gitlab:
image: 'gitlab/gitlab-ce:latest'
restart: always
container_name: "gitlab"
privileged: true
hostname: "192.168.78.129"
environment:
GITLAB_OMNIBUS_CONFIG: |
external_url 'http://192.168.78.129' #切記要改成自己主機的IP
gitlab_rails["time_zone"] = "Asia/Shanghai"
gitlab_rails["gitlab_shell_ssh_port"] = 1222
nginx["listen_port"] = 80
ports:
- "80:80"
- "8443:443"
- "1222:22"
volumes:
- /data/gitlab/config:/etc/gitlab
- /data/gitlab/data:/var/opt/gitlab
- /data/gitlab/logs:/var/log/gitlab
- "/etc/localtime:/etc/localtime:ro"
[root@gitlab data]# docker-compose up -d #創(chuàng)建并后臺運行所有容器
[root@gitlab data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d3049dd0a779 gitlab/gitlab-ce:latest "/assets/wrapper" 2 hours ago Up 2 hours (healthy) 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:1222->22/tcp, :::1222->22/tcp, 0.0.0.0:8443->443/tcp, :::8443->443/tcp gitlab
- 等待gitab加載完成,就可以在瀏覽器上輸入IP 訪問
- [root@gitlab data]# docker logs -f gitlab #查看gitlab的日志
- 首次訪問如下,需要設置密碼即可登入gitlab管理界面
- 地址:http://IP
- 賬號:root
- 密碼:首次登入設置

非首次登入:

非首次登入,忘記密碼的,需要修改用戶、密碼方法如下: [root@localhost ~]# docker exec -it gitlab bash root@192:/# cd /opt/gitlab/bin/ root@192:/opt/gitlab/bin# gitlab-rails console -e production -------------------------------------------------------------------------------- Ruby: ruby 2.7.5p203 (2021-11-24 revision f69aeb8314) [x86_64-linux] GitLab: 14.6.1 (661d663ab2b) FOSS GitLab Shell: 13.22.1 PostgreSQL: 12.7 -------------------------------------------------------------------------------- Loading production environment (Rails 6.1.4.1) irb(main):001:0> u=User.where(id:1).first => #<User id:1 @root> irb(main):002:0> u.password='12345678' => "12345678" irb(main):003:0> u.password_confirmation='12345678' => "12345678" irb(main):004:0> u.save => true irb(main):005:0> exit
安裝gitlab-runner
在192.168.78.130主機上創(chuàng)建docker-compose.yml
[root@runner data]# cat docker-compose.yml
version: '3'
services:
gitlab-runner:
container_name: gitlab-runner
restart: always
privileged: true
image: 'gitlab/gitlab-runner'
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
- "/data/gitlab-runner/config:/etc/gitlab-runner"
- "/etc/localtime:/etc/localtime:ro"
[root@runner data]# docker-compose up -d
[root@runner data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
462973a1c654 gitlab/gitlab-runner "/usr/bin/dumb-init …" About an hour ago Up About an hour gitlab-runner
獲取注冊token
在項目的settings-CI/CD-RUNNER中找到token
注冊gitlab-runner
[root@runner data]# docker run --rm -t -i -v /data/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register Runtime platform arch=amd64 os=linux pid=8 revision=5316d4ac version=14.6.0 Running in system-mode. Enter the GitLab instance URL (for example, https://gitlab.com/): http://192.168.78.129/ Enter the registration token: 9jT_yH5aK_HTBGgTyQX- Enter a description for the runner: [3f4049807752]: Enter tags for the runner (comma-separated): docker Registering runner... succeeded runner=9jT_yH5a Enter an executor: docker, docker-ssh, parallels, ssh, docker+machine, docker-ssh+machine, custom, shell, virtualbox, kubernetes: shell Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
查看注冊信息
docker exec gitlab-runner cat /etc/gitlab-runner/config.toml
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
docker沒有錯誤日志,鏡像服務卻啟動不成功的問題以及排查方式
這篇文章主要介紹了docker沒有錯誤日志,鏡像服務卻啟動不成功的問題以及排查方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
Docker 修改docker存儲位置 修改容器鏡像大小限制操作
這篇文章主要介紹了Docker 修改docker存儲位置 修改容器鏡像大小限制操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Docker使用Calico網(wǎng)絡模式配置及問題處理方法
這篇文章主要介紹了Docker使用Calico網(wǎng)絡模式配置及問題處理,設計思想是Calico不使用隧道或者NAT來實現(xiàn)轉發(fā),而是巧妙的把所有二三層流量轉換成三層流量,并通過host上路由配置完成跨host轉發(fā),需要的朋友可以參考下2022-11-11
docker安裝mysqld-exporter的實現(xiàn)
本文主要介紹了docker安裝mysqld-exporter的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-05-05

