DevOps 介紹
一、DevOps 介紹
1.1.1 DevOps 介紹
- DevOps介紹
DevOps 是Development和Operations的組合,也就是開發和運維的簡寫。
1、DevOps集文化理念、實踐與工具與一身,可以提高組織高速交付應用程序和服務的能力,與使用 傳統軟件開發和基礎設施管理流程相比,能夠幫助企業更快速的發展和改進產品,這種速度使企業 能夠更好地服務於客戶,並在市場上高效地參與競爭。
2、DevOps 是針對企業中的研發人員、運維人員和測試人員的工作理念,是他們在應用開發、代碼部 署和質量測試等整條生命周期中協作和溝通的最佳實踐,DevOps 強調整個組織的合作以及交付和 基礎設施變更的自動化、從而實現持續集成、持續部署和持續交付。
3、DevOps 平台:代碼托管(gitlab/svn)、項目管理(jira/禪道/Teambition)、運維平台(騰訊藍鯨/自主研發 平台)、持續交付(Jenkins/gitlab)。
- . 為什么要推廣DevOps
DevOps 強調團隊協作、相互協助、持續發展,然而傳統的模式是開發人員只顧開發程序,運維 只負責基礎環境管理和代碼部署及監控等,其並不是為了一個共同的目標而共同實現最終的目的, 而DevOps 則實現團隊作戰,即無論是開發、運維還是測試,都為了最終的代碼發布、持續部署和業 務穩定而付出各自的努力,從而實現產品設計、開發、測試和部署的良性循環,實現產品的最終持 續交付。
3.DevOps的優勢
速度:應用版本快速的迭代更新,以更好地適應不斷變化的市場需求。 快速交付:更快的將應用交付至生產環境。 可靠性:保證應用交付的結果是成功的。 規模:可以在大規模環境下且可靠的交付應用。 增強合作:建立適應DevOps文化模式的團隊,開發人員和運維人員協同工作。 安全性:在快速迭代的同時保證應用的質量。
1.1.2 CI/CD簡介
- 什么是持續集成(CI-Continuous integration)
持續集成是指多名開發者在開發不同功能代碼的過程當中,可以頻繁的將代 碼行合並到一起並切相互不影響工作。
2.什么是持續部署(CD-continuous deployment)
是基於某種工具或平台實現代碼自動化的構建、測試和部署到線上環境以實 現交付高質量的產品,持續部署在某種程度上代表了一個開發團隊的更新迭代 速率
1.1.2 Gitlab安裝與使用
- Git、Gitlab、GitHub介紹
Git: 由linux之父Linus Torvalds(林納斯·托瓦茲,1969年12月28日出生)在2005年開發的一個分 布式持續集成工具,設計之初就具備了以下優點: 可靠性:數據的上傳和下載必須是安全的、一致的,所有行為都要進行驗證,數據的變更通過不同的版本 進行邏輯隔離。 分布式:Linus Torvalds認為之前的集中式版本控制系統性能太差,於是git的代碼恢復不依賴於中央服務 器,而是每個開發電腦都是一個本地倉庫,可用於代碼提交與回滾。 高效:Git基於分布式的功能實現代碼的快速提交與秒級回滾。
Github:基於git的一個公有代碼倉庫,GitHub於2008年4月10日正式上線,2018年6月,微軟宣布通 過75億美元的股票交易收購GitHub。
Gitlab:基於git的一個私有代碼倉庫。
- Gitlab部署
[root@gitlab ~]# mkdir -p /server/tools
[root@gitlab ~]# cd /server/tools/
[root@gitlab /server/tools]# wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-11.11.8-ce.0.el7.x86_64.rpm
--2020-07-01 23:02:46-- https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-11.11.8-ce.0.el7.x86_64.rpm
[root@gitlab /server/tools]# ll
total 648548
-rw-r--r-- 1 root root 664112883 Jul 1 22:16 gitlab-ce-11.11.8-ce.0.el7.x86_64.rpm
[root@gitlab /server/tools]# yum install policycoreutils-python -y
[root@gitlab /server/tools]# rpm -ivh gitlab-ce-11.11.8-ce.0.el7.x86_64.rpm
warning: gitlab-ce-11.11.8-ce.0.el7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID f27eab47: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:gitlab-ce-11.11.8-ce.0.el7 ################################# [100%]
[root@gitlab ~]# rpm -qa gitlab-ce
gitlab-ce-11.11.8-ce.0.el7.x86_64
配置Gitlab.rb
gitlab官方文檔有各郵箱詳細配置
https://docs.gitlab.com/omnibus/settings/smtp.html
external_url 'http://10.4.7.56' #域名或IP地址
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.qq.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "XXXX@qq.com" #qq郵箱賬號
gitlab_rails['smtp_password'] = "ypyvpirefumrbjed" # #qq授權碼
gitlab_rails['smtp_domain'] = "smtp.qq.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitlab_email_from'] = XXXX@qq.com #和smtp_user_name一致
gitlab_rails['gitlab_email_enabled'] = true
重新加載配置文件
[root@gitlab ~]# gitlab-ctl reconfigure
[root@gitlab ~]# gitlab-ctl status
run: alertmanager: (pid 5965) 317s; run: log: (pid 5688) 354s
run: gitaly: (pid 5848) 322s; run: log: (pid 4910) 516s
run: gitlab-monitor: (pid 5900) 321s; run: log: (pid 5562) 387s
run: gitlab-workhorse: (pid 5879) 322s; run: log: (pid 5410) 427s
run: logrotate: (pid 5459) 412s; run: log: (pid 5474) 409s
run: nginx: (pid 5426) 423s; run: log: (pid 5440) 420s
run: node-exporter: (pid 5891) 322s; run: log: (pid 5524) 398s
run: postgres-exporter: (pid 5979) 317s; run: log: (pid 5776) 343s
run: postgresql: (pid 5065) 503s; run: log: (pid 5082) 500s
run: prometheus: (pid 5919) 321s; run: log: (pid 5629) 365s
run: redis: (pid 4839) 525s; run: log: (pid 4852) 522s
run: redis-exporter: (pid 5909) 321s; run: log: (pid 5594) 376s
run: sidekiq: (pid 5355) 441s; run: log: (pid 5372) 437s
run: unicorn: (pid 5297) 452s; run: log: (pid 5328) 449s
#gitlab安裝目錄:
/etc/gitlab #配置文件目錄
/run/gitlab #運行pid目錄
/opt/gitlab #安裝目錄
/var/opt/gitlab #數據目錄
/var/log/gitlab #日志目錄
# gitlab-ctl #客戶端命令行操作行
# gitlab-ctl stop #停止gitlab
# gitlab-ctl start #啟動gitlab
# gitlab-ctl restart #重啟gitlab
# gitlab-ctl status #查看組件運行狀態
# gitlab-ctl tail nginx #查看某個組件的日志
瀏覽器訪問:520報錯解決
chmod -R 755 /var/log/gitlab #502報錯
4. Git命令的使用
基礎命令:
git clone #克隆項目到本地
git add #添加項目到暫存區
git status #查看本地工作區和暫存區的狀態
git comit -m '' #提交項目到本地
git push #提交項目到遠程倉庫
[root@jenkins ~]# yum install git -y
[root@jenkins ~]# rpm -qa git
git-1.8.3.1-23.el7_8.x86_64
[root@jenkins ~]# cd /opt/
[root@jenkins /opt]# git clone http://10.4.7.56/magedu/devops.git
Cloning into 'devops'...
Username for 'http://10.4.7.56': zhangxiaoming
Password for 'http://zhangxiaoming@10.4.7.56':
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done.
[root@jenkins /opt]# ll
total 0
drwxr-xr-x 3 root root 36 Jul 2 21:37 devops
#注意:clone的時候項目里文件不能為空,需要代碼。否則會有坑。
編輯項目:
[root@jenkins /opt]# cd devops/
[root@jenkins /opt/devops]# vim index.html
[root@jenkins /opt/devops]# cat index.html
<h1>hello world!!</h1>
<h1>v222222222222</h1>
<h1>v333333333333</h1>
提交項目到暫存區
[root@jenkins /opt/devops]# git add index.html
查看項目
[root@jenkins /opt/devops]# git status;
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: index.html
#
提交項目到本地倉庫
[root@jenkins /opt/devops]# git commit -m 'v3' ./
[master d2f8e8f] v3
1 file changed, 1 insertion(+)
提交項目到遠程倉庫
[root@jenkins /opt/devops]# git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:
git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
git config --global push.default simple
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)
Username for 'http://10.4.7.56': zhangxiaoming
Password for 'http://zhangxiaoming@10.4.7.56':
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 273 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://10.4.7.56/magedu/devops.git
d49a0a9..d2f8e8f master -> master
- 設置全局用戶和郵箱
[root@jenkins /opt/devops]# git config --global user.name 'user1'
[root@jenkins /opt/devops]# git config --global user.email 'user1@163.com'
#注意用戶和郵箱可為真亦可為假。只要格式正確(上面設置為假)
[root@jenkins /opt/devops]# git config --global push.default current
- git 其它命令使用
git log #查看操作日
[root@jenkins /opt/devops]# git log
commit dfa58caf1adfd64962d3f200fa29e50cf526f69b
Author: root <gzchenhj@163.com>
Date: Thu Jul 2 22:22:04 2020 +0800
v4
commit d2f8e8fb622e9a6f4a2f206626add8ee569d03ae
Author: root <YOU@EXAMPLE.COM>
Date: Thu Jul 2 21:56:54 2020 +0800
v3
commit d49a0a94994496d90014891f95f9d36ab91cc4c3
Author: root <gzchenhj@163.com>
Date: Thu Jul 2 21:04:07 2020 +0800
v2
commit ba59ed9d0f3e32ac889bfe06b8ce54334714504e
Author: zhangxiaoming <gzchenhj@163.com>
Date: Thu Jul 2 20:53:50 2020 +0800
v1
.gitignore #定義忽略某些指定文件或者目錄不上傳至gitlab 默認是沒有的需要創建
比如:mysql.cnf里面有一些敏感(保密)的信息,不想上傳到Gitlab上,就可以這樣操作
[root@jenkins /opt/devops]# vim my.cnf #里面隨便一點內容做測試
[root@jenkins /opt/devops]# vim .gitignore
[root@jenkins /opt/devops]# cat .gitignore
my.cnf #忽略my.cnf文件不git push 遠程倉庫
[root@jenkins /opt/devops]# git add ./
[root@jenkins /opt/devops]# git commit -m 'config' ./
[master e846f4f] config
1 file changed, 1 insertion(+)
create mode 100644 .gitignore
[root@jenkins /opt/devops]# git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:
git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
git config --global push.default simple
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)
Username for 'http://10.4.7.56': zhangxiaoming
Password for 'http://zhangxiaoming@10.4.7.56':
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 256 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To http://10.4.7.56/magedu/devops.git
b25ff63..e846f4f master -> master
假如你的zookeeper.cnf配置文件沒有加入.gitignore里面,就會上傳到遠程倉庫
[root@jenkins /opt/devops]# cp my.cnf zookeeper.cnf
[root@jenkins /opt/devops]# git add ./
[root@jenkins /opt/devops]# git commit -m "zookeeper.cnf"
[master e7712c3] zookeeper.cnf
2 files changed, 99 insertions(+)
create mode 100644 .gitignore
create mode 100644 zookeeper.cnf
[root@jenkins /opt/devops]# git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
# (use "git push" to publish your local commits)
#
nothing to commit, working directory clean
[root@jenkins /opt/devops]# git push
Username for 'http://10.4.7.56': zhangxiaoming
Password for 'http://zhangxiaoming@10.4.7.56':
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 1.20 KiB | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To http://10.4.7.56/magedu/devops.git
4bae31f..e7712c3 HEAD -> master
沒有上傳mysql的配置文件但有zookeeper的配置文件
7. git 版本回滾命令
git reset --hard HEAD^^ #git版本回滾,HEAD為當前版本,加一個^為上一個,^^為上上一個版本
git reflog #獲取每次提交的ID,可以使用--hard根據提交的ID進行版本回退
git reset --hard 5ae4b06 #回退到指定id的版本
多添加幾個版本做測試:
[root@jenkins /opt/devops]# vim index.html
[root@jenkins /opt/devops]# git add ./
[root@jenkins /opt/devops]# git commit -m "v5"
[master e746a8f] v5
1 file changed, 1 insertion(+)
[root@jenkins /opt/devops]# git push
Username for 'http://10.4.7.56': zhangxiaoming
Password for 'http://zhangxiaoming@10.4.7.56':
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 335 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To http://10.4.7.56/magedu/devops.git
e7712c3..e746a8f HEAD -> master
[root@jenkins /opt/devops]# vim index.html
[root@jenkins /opt/devops]# git add ./
[root@jenkins /opt/devops]# git commit -m "v6"
[master a840fa7] v6
1 file changed, 1 insertion(+)
[root@jenkins /opt/devops]# git push
Username for 'http://10.4.7.56': zhangxiaoming
Password for 'http://zhangxiaoming@10.4.7.56':
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 337 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To http://10.4.7.56/magedu/devops.git
e746a8f..a840fa7 HEAD -> master
[root@jenkins /opt/devops]# vim index.html
[root@jenkins /opt/devops]# git add ./
[root@jenkins /opt/devops]# git commit -m "v7"
[master fd75b2e] v7
1 file changed, 1 insertion(+)
[root@jenkins /opt/devops]# git push
Username for 'http://10.4.7.56': zhangxiaoming
Password for 'http://zhangxiaoming@10.4.7.56':
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 335 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To http://10.4.7.56/magedu/devops.git
a840fa7..fd75b2e HEAD -> master
[root@jenkins /opt/devops]# git log
commit fd75b2ed1e3bff195f6c0356a117c903122d0b4e
Author: user1 <user1@163.com>
Date: Thu Jul 2 23:35:58 2020 +0800
v7
commit a840fa7eaab4a05402781251f81dd5a7e0175af7
Author: user1 <user1@163.com>
Date: Thu Jul 2 23:34:32 2020 +0800
v6
commit e746a8f8c98c769b6536f62f512c8f64288f05b3
Author: user1 <user1@163.com>
Date: Thu Jul 2 23:31:36 2020 +0800
v5
回到v6版本
[root@jenkins /opt/devops]# git re
rebase relink repack request-pull revert
reflog remote replace reset
[root@jenkins /opt/devops]# git reset --hard HEAD^
HEAD is now at a840fa7 v6
[root@jenkins /opt/devops]# cat index.html
<h1>hello world!!</h1>
<h1>v222222222222</h1>
<h1>v333333333333</h1>
<h1>v444444444444</h1>
<h1>v555555555555</h1>
<h1>v666666666666</h1>
跨版本回滾
[root@jenkins /opt/devops]# git pull
Username for 'http://10.4.7.56': zhangxiaoming
Password for 'http://zhangxiaoming@10.4.7.56':
Updating a840fa7..fd75b2e
Fast-forward
index.html | 1 +
1 file changed, 1 insertion(+)
[root@jenkins /opt/devops]# cat index.html
<h1>hello world!!</h1>
<h1>v222222222222</h1>
<h1>v333333333333</h1>
<h1>v444444444444</h1>
<h1>v555555555555</h1>
<h1>v666666666666</h1>
<h1>v777777777777</h1>
查看commit id 號(全局唯一不重復)
[root@jenkins /opt/devops]# git log
commit fd75b2ed1e3bff195f6c0356a117c903122d0b4e #這就是ID號
Author: user1 <user1@163.com>
Date: Thu Jul 2 23:35:58 2020 +0800
比如回滾到zookeeper
[root@jenkins /opt/devops]# git reset --hard e7712c
HEAD is now at e7712c3 zookeeper.cnf
查看commit ID號的命令還有一個git reflog (更加詳細)
[root@jenkins /opt/devops]# git reflog
e7712c3 HEAD@{0}: reset: moving to e7712c
fd75b2e HEAD@{1}: pull: Fast-forward
a840fa7 HEAD@{2}: reset: moving to HEAD^
fd75b2e HEAD@{3}: commit: v7
a840fa7 HEAD@{4}: commit: v6
e746a8f HEAD@{5}: commit: v5
e7712c3 HEAD@{6}: commit: zookeeper.cnf
4bae31f HEAD@{7}: clone: from http://10.4.7.56/magedu/devops.git
- 分支相關命令
git branch #查看當前所處的分支
git branch develop #創建新分支
git checkout -b develop #創建並切換到一個新分支
git checkout develop #切換分支
git branch -d 分支名 #刪除本地已經合並了的分支.
git branch –D 分支名 #刪除本地未合並的分支
git push origin –delete 分支名 #要刪除服務器遠端的分支。
查看當前分支
[root@jenkins /opt/devops]# git branch
* master
創建分支
[root@jenkins /opt/devops]# git checkout -b develop
Switched to a new branch 'develop'
切換分支
[root@jenkins /opt/devops]# git checkout develop
Switched to branch 'develop'
常見的分支結構: 單分支模型:只有master分支
生產/開發模型:master分支與develop類型分支
特性/發布模型:master/develop/feature類型分支
開發/發布/分離模型:master/develop/feature/release類型分支
開發/發布/缺陷分離模型:master/develop/feature/release/hotfix類型分支
8 git分支合並
換一台虛擬主機做測試:
[root@db03 /opt]# cd /opt/
[root@db03 /opt]# yum install git -y
clone指定分支:-b 分支名
[root@db03 /opt]# git clone -b develop http://10.4.7.56/magedu/devops.git
Cloning into 'devops'...
Username for 'http://10.4.7.56': zhangxiaoming
Password for 'http://zhangxiaoming@10.4.7.56':
remote: Enumerating objects: 35, done.
remote: Counting objects: 100% (35/35), done.
remote: Compressing objects: 100% (23/23), done.
remote: Total 35 (delta 8), reused 25 (delta 5)
Unpacking objects: 100% (35/35), done.
[root@db03 /opt/devops]# git branch
* develop
[root@db03 /opt/devops]# vim index.html
[root@db03 /opt/devops]# cat index.html
<h1>hello world!!</h1>
<h1>v222222222222</h1>
<h1>v333333333333</h1>
<h1>v444444444444</h1>
<h1>v555555555555</h1>
<h1>v666666666666</h1>
<h1>v777777777777</h1>
<h1>v888888888888</h1> #加這么一行
[root@db03 /opt/devops]# git config --global user.name 'user1'
[root@db03 /opt/devops]# git config --global user.email 'user1@163.com'
[root@db03 /opt/devops]# git config --global push.default current
添加代碼到暫存區
[root@db03 /opt/devops]# git add ./
提交代碼
[root@db03 /opt/devops]# git commit -m "v9"
[develop 7755ca9] v9
1 file changed, 1 insertion(+)
提交代碼到遠程倉庫
[root@db03 /opt/devops]# git push
Username for 'http://10.4.7.56': zhangxiaoming
Password for 'http://zhangxiaoming@10.4.7.56':
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 339 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote:
remote: To create a merge request for develop, visit:
remote: http://10.4.7.56/magedu/devops/merge_requests/new?merge_request%5Bsource_branch%5D=develop
remote:
To http://10.4.7.56/magedu/devops.git
fd75b2e..7755ca9 HEAD -> develop
換一台主機或者在Windows上將最新的的代碼pull到本地
小黃人@LAPTOP-FSRNDRDD MINGW64 /f/gittest/devops (master)
$ git pull
remote: Enumerating objects: 14, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 12 (delta 3), reused 0 (delta 0)
Unpacking objects: 100% (12/12), done.
From http://10.4.7.56/magedu/devops
e7712c3..fd75b2e master -> origin/master
* [new branch] develop -> origin/develop
Updating e7712c3..fd75b2e
Fast-forward
index.html | 3 +++
1 file changed, 3 insertions(+)
分支合並
小黃人@LAPTOP-FSRNDRDD MINGW64 /f/gittest/devops (master)
$ git merge origin/develop
Updating fd75b2e..7755ca9
Fast-forward
index.html | 1 +
1 file changed, 1 insertion(+)
上傳遠程倉庫
小黃人@LAPTOP-FSRNDRDD MINGW64 /f/gittest/devops (master)
$ git push
Total 0 (delta 0), reused 0 (delta 0)
To http://10.4.7.56/magedu/devops.git
fd75b2e..7755ca9 master -> master
分支版本已經合並master分支
除了在本地合並外,還可以在web頁面合並(生產常用)
開發申請合並
項目負責人登錄web合並分支
合並分支成功