本文經過Docker + Gitlab + Gitlab CI實現容器的自動化部署。html
環境以下:linux
ip | role | domain |
---|---|---|
192.168.1.51 | prod server | none |
192.168.1.55 | dns | none |
192.168.1.56 | gitlab ci/dev server | none |
192.168.1.57 | gitlab server | gitlab.lzxlinux.cn |
192.168.1.59 | harbor server | harbor.lzxlinux.cn |
注意:爲了方便後面實驗,建議自定義的域名不要與外部域名衝突,不然在鏈接外網狀況下容易解析錯誤。nginx
Harbor是一個用於存儲Docker鏡像的企業級Registry服務。git
# systemctl stop firewalld && systemctl disable firewalld# setenforce 0 && sed -i 's/=enforcing/=disabled/g' /etc/selinux/config
# curl http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker.repo# yum makecache fast# yum install -y docker-ce# systemctl start docker && systemctl enable docker
提升docker pull速度github
# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io# systemctl restart docker
# curl -L https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose# chmod +x /usr/local/bin/docker-compose
github地址:https://github.com/goharbor/harbor/releasesdocker
# cd /software# wget https://storage.googleapis.com/harbor-releases/release-1.9.0/harbor-offline-installer-v1.9.1.tgz# tar zxf harbor-offline-installer-v1.9.1.tgz
# cd harbor/# vim harbor.ymlhostname: harbor.lzxlinux.cn #域名harbor_admin_password: Harbor12345 #admin用戶初始密碼data_volume: /data #數據存儲路徑,自動建立log: level: info local: rotate_count: 50 rotate_size: 200M location: /var/log/harbor #日誌路徑 # sh install.sh
# echo '192.168.1.59 harbor.lzxlinux.cn' >> /etc/hosts# docker login harbor.lzxlinux.cnUsername: admin Password: Error response from daemon: Get https://harbor.lzxlinux.cn/v2/: dial tcp 192.168.1.59:443: connect: connection refused
# vim /usr/lib/systemd/system/docker.serviceExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --insecure-registry=harbor.lzxlinux.cn# systemctl daemon-reload && systemctl restart docker.service
harbor服務器shell
# cd /software/harbor# docker-compose down -v# docker-compose up -d
# docker login harbor.lzxlinux.cnUsername: admin Password: WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-storeLogin Succeeded
# docker pull busybox# docker tag busybox:latest harbor.lzxlinux.cn/public/busybox# docker push harbor.lzxlinux.cn/public/busybox
刷新harbor頁面,能夠看到剛推送到harbor倉庫的鏡像json
以Centos7爲例,準備一臺至少內存爲4G的機器。vim
# systemctl stop firewalld && systemctl disable firewalld# setenforce 0 && sed -i 's/=enforcing/=disabled/g' /etc/selinux/config
# yum install -y curl policycoreutils openssh-server openssh-clients postfix# systemctl start postfix && systemctl enable postfix #啓動postfix郵件服務
若是在國內的話,能夠嘗試使用清華大學的源。centos
# vim /etc/yum.repos.d/gitlab-ce.repo[gitlab-ce]name=Gitlab CE Repository baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/ gpgcheck=0 enabled=1
若是在國外的話,可使用
# curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.rpm.sh | sudo bash
# vim /etc/hosts192.168.1.57 gitlab.lzxlinux.cn
在Windows電腦hosts文件中添加本地dns:
192.168.1.57 gitlab.lzxlinux.cn
# yum install -y gitlab-ce
# mkdir -p /etc/gitlab/ssl# openssl genrsa -out "/etc/gitlab/ssl/gitlab.lzxlinux.cn.key" 2048# openssl req -new -key "/etc/gitlab/ssl/gitlab.lzxlinux.cn.key" -out "/etc/gitlab/ssl/gitlab.lzxlinux.cn.csr"Country Name (2 letter code) [XX]:cn State or Province Name (full name) []:hz Locality Name (eg, city) [Default City]:hz Organization Name (eg, company) [Default Company Ltd]: Organizational Unit Name (eg, section) []: Common Name (eg, your name or your server's hostname) []:gitlab.lzxlinux.cn Email Address []:admin@lzxlinux.cn Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []:123456 An optional company name []:
# openssl x509 -req -days 365 -in "/etc/gitlab/ssl/gitlab.lzxlinux.cn.csr" -signkey "/etc/gitlab/ssl/gitlab.lzxlinux.cn.key" -out "/etc/gitlab/ssl/gitlab.lzxlinux.cn.crt"# openssl dhparam -out /etc/gitlab/ssl/dhparams.pem 2048# chmod 600 /etc/gitlab/ssl/*# ll /etc/gitlab/ssltotal 16 -rw------- 1 root root 424 Oct 29 10:45 dhparams.pem -rw------- 1 root root 1281 Oct 29 10:44 gitlab.lzxlinux.cn.crt -rw------- 1 root root 1074 Oct 29 10:38 gitlab.lzxlinux.cn.csr -rw------- 1 root root 1679 Oct 29 10:37 gitlab.lzxlinux.cn.key
# vim /etc/gitlab/gitlab.rb #修改下面內容external_url 'https://gitlab.lzxlinux.cn'nginx['redirect_http_to_https'] = truenginx['ssl_certificate'] = "/etc/gitlab/ssl/gitlab.lzxlinux.com.cn"nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/gitlab.lzxlinux.cn.key"nginx['ssl_dhparam'] = "/etc/gitlab/ssl/dhparams.pem"
# gitlab-ctl reconfigure# vim /var/opt/gitlab/nginx/conf/gitlab-http.conf #第一個 server_name gitlab.lzxlinux.cn; 下添加該行rewrite ^(.*)$ https://$host$1 permanent;# gitlab-ctl restart #重啓gitlab
打開https://gitlab.lzxlinux.cn/
修改root用戶密碼,而後使用root和新密碼登錄。
Gitlab CI服務器建議另選一臺服務器搭建,不要與Gitlab服務器部署在同一臺機器上。
# systemctl stop firewalld && systemctl disable firewalld# setenforce 0 && sed -i 's/=enforcing/=disabled/g' /etc/selinux/config
# curl http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker.repo# yum makecache fast# yum install -y docker-ce# systemctl start docker && systemctl enable docker
提升docker pull速度
# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io# systemctl restart docker
# echo '54.153.54.194 packages.gitlab.com' >> /etc/hosts# curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash# yum install -y gitlab-ci-multi-runner
查看是否運行正常
# gitlab-ci-multi-runner statusgitlab-runner: Service is running!
爲了能讓gitlab-runner能正確的執行docker命令,須要把gitlab-runner用戶添加到docker group裏, 而後重啓docker和gitlab ci runner。
# usermod -aG docker gitlab-runner# systemctl restart docker# gitlab-ci-multi-runner restart
到gitlab上查看runner的註冊token
# gitlab-ci-multi-runner registerRunning in system-mode. Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/): https://gitlab.lzxlinux.cn/ #輸入gitlab urlPlease enter the gitlab-ci token for this runner: 4kr9ZmLMWasYxqB2tSzQ #輸入tokenPlease enter the gitlab-ci description for this runner:[worker4]: #輸入該runner的描述信息Please enter the gitlab-ci tags for this runner (comma separated): test,demo #輸入該runner的tagsWhether to run untagged builds [true/false]:[false]: #是否運行沒有打上tag的構建Whether to lock Runner to current project [true/false]:[false]: #是否將該runner鎖定到當前項目ERROR: Registering runner... failed runner=4kr9ZmLM status=couldn't execute POST against https://gitlab.lzxlinux.cn/api/v4/runners: Post https://gitlab.lzxlinux.cn/api/v4/runners: x509: certificate signed by unknown authority PANIC: Failed to register this runner. Perhaps you are having network problems
報錯,證書籤名錯誤,這裏由於前面gitlab服務器作了https。
# mkdir -p /etc/gitlab/ssl# scp root@gitlab.lzxlinux.cn:/etc/gitlab/ssl/gitlab.lzxlinux.cn.crt /etc/gitlab/ssl# gitlab-ci-multi-runner register \ --tls-ca-file=/etc/gitlab/ssl/gitlab.lzxlinux.cn.crt \ --url "https://gitlab.lzxlinux.cn/" \ --registration-token "4kr9ZmLMWasYxqB2tSzQ" \ --tag-list "test,demo" \ --run-untagged \ --locked="false" \ --executor "shell"
# gitlab-ci-multi-runner listListing configured runners ConfigFile=/etc/gitlab-runner/config.toml worker4 Executor=shell Token=aLzTn6bfk1tXNBLRBbSD URL=https://gitlab.lzxlinux.cn/
能夠看到,成功地註冊了一個runner到gitlab服務器。
gitlab上新建一個組test
,在test
組中新建一個項目helloworld
,而後項目中新建一個README.md
文件。
再新建一個.gitlab-ci.yml
文件,
打開CI/CD
→ 流水線
,能夠看到剛剛的提交已經成功完成,
整個pipeline按順序執行,若是前面的stage出錯,後面的stage不會執行。
另選同網段的一臺主機搭建一個dns服務器,讓同網段任意機器及其上的容器均可以解析到自定義的域名(如 gitlab.lzxlinux.cn)。
首先,在gitlab ci服務器上把gitlab.lzxlinux.cn
從/etc/hosts
裏刪除,此時在gitlab ci服務器上是ping不通gitlab.lzxlinux.cn
的。
# systemctl stop firewalld && systemctl disable firewalld# setenforce 0 && sed -i 's/=enforcing/=disabled/g' /etc/selinux/config
# curl http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker.repo# yum makecache fast# yum install -y docker-ce# systemctl start docker && systemctl enable docker
提升docker pull速度
# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io# systemctl restart docker
# docker run -d -p 53:53/tcp -p 53:53/udp --cap-add=NET_ADMIN --name dns-server andyshinn/dnsmasq# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bcaf93792614 andyshinn/dnsmasq "dnsmasq -k" 10 seconds ago Up 9 seconds 0.0.0.0:53->53/tcp, 0.0.0.0:53->53/udp dns-server
# docker exec -it dns-server sh/ # vi /etc/resolv.dnsmasqnameserver 223.5.5.5 nameserver 114.114.114.114 / # vi /etc/dnsmasqhosts192.168.1.57 gitlab.lzxlinux.cn 192.168.1.59 harbor.lzxlinux.cn / # vi /etc/dnsmasq.confresolv-file=/etc/resolv.dnsmasq addn-hosts=/etc/dnsmasqhosts# docker restart dns-server
gitlab ci服務器上操做
# vim /etc/resolv.conf #增長一行,且放在公網dns前面nameserver 192.168.1.55 #該ip爲dns容器所在主機ipnameserver 223.5.5.5# ping gitlab.lzxlinux.cnPING gitlab.lzxlinux.cn (192.168.1.57) 56(84) bytes of data. 64 bytes from gitlab.lzxlinux.cn (192.168.1.57): icmp_seq=1 ttl=64 time=0.276 ms 64 bytes from gitlab.lzxlinux.cn (192.168.1.57): icmp_seq=2 ttl=64 time=0.310 ms 64 bytes from gitlab.lzxlinux.cn (192.168.1.57): icmp_seq=3 ttl=64 time=0.380 ms 64 bytes from gitlab.lzxlinux.cn (192.168.1.57): icmp_seq=4 ttl=64 time=0.319 ms ^C --- gitlab.lzxlinux.cn ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3001ms rtt min/avg/max/mdev = 0.276/0.321/0.380/0.039 ms# ping harbor.lzxlinux.cnPING harbor.lzxlinux.cn (192.168.1.59) 56(84) bytes of data. 64 bytes from harbor.lzxlinux.cn (192.168.1.59): icmp_seq=1 ttl=64 time=0.307 ms 64 bytes from harbor.lzxlinux.cn (192.168.1.59): icmp_seq=2 ttl=64 time=0.430 ms 64 bytes from harbor.lzxlinux.cn (192.168.1.59): icmp_seq=3 ttl=64 time=0.383 ms 64 bytes from harbor.lzxlinux.cn (192.168.1.59): icmp_seq=4 ttl=64 time=0.346 ms ^C --- harbor.lzxlinux.cn ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3000ms rtt min/avg/max/mdev = 0.307/0.366/0.430/0.049 ms
# docker run -it --rm busybox sh/ # ping gitlab.lzxlinux.cnPING gitlab.lzxlinux.cn (192.168.1.57): 56 data bytes 64 bytes from 192.168.1.57: seq=0 ttl=63 time=0.388 ms 64 bytes from 192.168.1.57: seq=1 ttl=63 time=0.472 ms ^C --- gitlab.lzxlinux.cn ping statistics --- 2 packets transmitted, 2 packets received, 0% packet loss round-trip min/avg/max = 0.388/0.430/0.472 ms / # ping harbor.lzxlinux.cnPING harbor.lzxlinux.cn (192.168.1.59): 56 data bytes 64 bytes from 192.168.1.59: seq=0 ttl=63 time=0.580 ms 64 bytes from 192.168.1.59: seq=1 ttl=63 time=0.332 ms 64 bytes from 192.168.1.59: seq=2 ttl=63 time=0.401 ms ^C --- harbor.lzxlinux.cn ping statistics --- 3 packets transmitted, 3 packets received, 0% packet loss round-trip min/avg/max = 0.332/0.437/0.580 ms
能夠看到,在搭建dns服務器後,即便gitlab ci服務器的/etc/hosts
刪除本地dns,主機和主機上的容器仍能夠解析咱們自定義的域名。