轉載:http://blog.csdn.net/abcdocker/article/details/53840449
持續集成概念
持續集成Continuous Integration
持續交付Continuous Delivery
持續部署Continuous Deployment
1.1 什么是持續集成:
持續集成是指開發者在代碼的開發過程中,可以頻繁的將代碼部署集成到主干,並進程自動化測試
1.3 什么是持續交付:
持續交付指的是在持續集成的環境基礎之上,將代碼部署到預生產環境
1.4 持續部署:
在持續交付的基礎上,把部署到生產環境的過程自動化,持續部署和持續交付的區別就是最終部署到生產環境是自動化的。
1.5 部署代碼上線流程
1.代碼獲取(直接了拉取)
2.編譯 (可選)
3.配置文件放進去
4.打包
5.scp到目標服務器
6.將目標服務器移除集群
7.解壓並放置到Webroot
8.Scp 差異文件
9.重啟 (可選)
10.測試
11.加入集群
運維必知OWASP
Jenkins上OWASP 插件介紹: 它是開放式Web應用程序安全項目[OWASP,Open Web Application Secunity Project
]
它每年會出一個top10的安全漏洞,我們需要知道當前top10的漏洞有哪些
https://www.owasp.org/images/5/57/OWASP_Proactive_Controls_2.pdf
https://www.owasp.org/index.php/Top_10_2013-Top_10
Gitlab介紹
GitLab是一個利用 Ruby on Rails
開發的開源應用程序,實現一個自托管的Git項目倉庫,可通過Web界面進行訪問公開的或者私人項目。
GitLab擁有與Github類似的功能,能夠瀏覽源代碼,管理缺陷和注釋。可以管理團隊對倉庫的訪問,它非常易於瀏覽提交過的版本並提供一個文件歷史庫。它還提供一個代碼片段收集功能可以輕松實現代碼復用,便於日后有需要的時候進行查找。
環境准備
[root@linux-node1 ~]# cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core) [root@linux-node1 ~]# uname -r 3.10.0-514.2.2.el7.x86_64 下載epel源 [root@linux-node1 ~]# wget http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm [root@linux-node1 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 關閉 NetworkManager 和防火牆 [root@linux-node1 ~]#systemctl stop firewalld.service systemctl disable firewalld systemctl disable NetworkManager 關閉SELinux並確認處於關閉狀態 sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config grep SELINUX=disabled /etc/selinux/config setenforce 0 更新系統並重啟 [root@linux-node1 ~]# yum update -y && reboot
我們一共有2台:192.168.56.11
和192.168.56.12
我們安裝在192.168.56.11上
[root@linux-node1 /]# yum install curl policycoreutils openssh-server openssh-clients postfix -y [root@linux-node1 /]# systemctl start postfix [root@linux-node1 /]# curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash [root@linux-node1 /]# yum install -y gitlab-ce
#由於網絡問題,國內用戶,建議使用清華大學的鏡像源進行安裝
[root@linux-node1 ~]# cat /etc/yum.repos.d/gitlab-ce.repo [gitlab-ce] name=gitlab-ce baseurl=http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7 repo_gpgcheck=0 gpgcheck=0 enabled=1 gpgkey=https://packages.gitlab.com/gpg.key [root@linux-node1 ~]# yum makecache [root@linux-node1 /]# yum install -y gitlab-ce
在安裝一個git客戶端
[root@linux-node1 /]# yum install -y git
配置並啟動gitlab-ce
[root@linux-node1 ~]# gitlab-ctl reconfigure #時間可能比較長,耐心你等待即可!---- gitlab常用命令: 關閉gitlab:[root@linux-node2 ~]# gitlab-ctl stop 啟動gitlab:[root@linux-node2 ~]# gitlab-ctl start 重啟gitlab:[root@linux-node2 ~]# gitlab-ctl restart
可以使用gitlab-ctl
管理gitlab,例如查看gitlab狀態:
[root@linux-node1 /]# gitlab-ctl status run: gitlab-workhorse: (pid 7437) 324s; run: log: (pid 7436) 324s run: logrotate: (pid 7452) 316s; run: log: (pid 7451) 316s run: nginx: (pid 8168) 2s; run: log: (pid 7442) 318s run: postgresql: (pid 7293) 363s; run: log: (pid 7292) 363s run: redis: (pid 7210) 369s; run: log: (pid 7209) 369s run: sidekiq: (pid 7479) 265s; run: log: (pid 7426) 326s run: unicorn: (pid 7396) 327s; run: log: (pid 7395) 327s
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
提示: 我們要保證80
端口不被占用
我們可以查看一下端口
[root@linux-node1 /]# gitlab-ctl restart ok: run: gitlab-workhorse: (pid 8353) 0s ok: run: logrotate: (pid 8360) 1s ok: run: nginx: (pid 8367) 0s timeout: down: postgresql: 0s, normally up, want up ok: run: redis: (pid 8437) 0s ok: run: sidekiq: (pid 8445) 0s ok: run: unicorn: (pid 8450) 0s [root@linux-node1 /]# lsof -i:80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 8367 root 7u IPv4 54972 0t0 TCP *:http (LISTEN) nginx 8368 gitlab-www 7u IPv4 54972 0t0 TCP *:http (LISTEN)
Web:訪問:192.168.56.11
提示:啟動gitlab需要時間!
Web
頁面提示我們需要設置一個賬號密碼(我們要設置最少8
位數的一個賬號密碼)我們設置密碼為:12345678
我們在后面的頁面設置用戶名
我們現在是以管理員的身份登陸
我們點擊右上角管理區域
第一步:我們關閉自動注冊,因為我們內部使用不需要用戶自己注冊,由運維分配用戶即可
提示:Save
在頁面最下放!!!!!! 記得點保存!!!!!!!!!!!!
現在在查看首頁就沒有注冊頁面了
第二步:我們創建一個用戶,在創建一個項目
先創建一個組
**提示:**gitlab上面有一個項目跟組的概念,我們要創建一個組,才可以在創建一個項目。因為gitlab的路徑上首先是ip地址,其次是組
點擊下方Create group
然后我們在組里面創建項目
下一步:
創建完成之后它提示我們可以創建一個key對它進行管理
我們點擊上面的README
然后我們隨便在里面寫點東西
填寫完成我們點擊前面進行查看
我們要做免密驗證,現在去192.168.56.11復制下面的.ssh/id_rsa.pub
[www@linux-node1 ~]$ cat .ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC8wfTSQcSyhlsGYDSUtuxZNb1Gl3VU56nAPuxAEF2wP2ZWZ2yva354ZdKOOb6rZx2yZxqy5XIj7opBJPbhraXap+NtCH5qWyktR7dH19RBmCS7vUGgvk/5RQC0mVFrC8cztBp0M/5HxMuhVir6mD1rhbDvvaLL6S5y4gljzC1Gr2VRHIb4Et9go/38c2tqMjYCike7WWbFRyL9wTal6/146+9uREZ/r69TBTKrGuRqF44fROQP8/ly02XFjlXyl6J5NnGTk6AU855pwasX0W9aNPce3Ynrpe1TBTubmfQhrH1BwteEmg+ZXNRupxjumA+tPWfBUX+u51r/w7W/d4PD www@linux-node1 #提示:需要提前做秘鑰認證
設置Keys
添加完之后我們就可以使用www用戶,就可以拉了
點擊Projects 選擇SSH
,我們要將代碼拉去下來
[www@linux-node1 ~]$ cd /deploy/code/
[www@linux-node1 code]$ ls
web-demo
[www@linux-node1 code]$ rm -rf web-demo/
[www@linux-node1 ~]$ git clone git@linux-node1:web/web-demo.git
Cloning into 'web-demo'... The authenticity of host 'linux-node1 (192.168.56.11)' can't be established. ECDSA key fingerprint is b5:74:8f:f1:03:2d:cb:7d:01:28:30:12:34:9c:35:8c. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'linux-node1' (ECDSA) to the list of known hosts. remote: Counting objects: 3, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0) Receiving objects: 100% (3/3), done. [www@linux-node1 ~]$ ls web-demo/ README.md #git clone是克隆的意思
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
我們來模擬開發繼續寫代碼提交
[www@linux-node1 ~]$ mkdir -p /web-demo [www@linux-node1 ~]$ vim web-demo/index.html [www@linux-node1 ~]$ cd web-demo/ [www@linux-node1 web-demo]$ [www@linux-node1 web-demo]$ ls index.html README.md [www@linux-node1 web-demo]$ git add * [www@linux-node1 web-demo]$ git commit -m "add index.html" *** Please tell me who you are. Run git config --global user.email "you@example.com" git config --global user.name "Your Name" to set your account's default identity. Omit --global to set the identity only in this repository. fatal: empty ident name (for <www@linux-node1.(none)>) not allowed
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
需要身份驗證:
[www@linux-node1 web-demo]$ git config --global user.email "you@example.com" [www@linux-node1 web-demo]$ git config --global user.name "Your Name" [www@linux-node1 web-demo]$ git commit -m "add index.html" [master be8a547] add index.html 1 file changed, 169 insertions(+) create mode 100644 index.html
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
git push命令用於將本地分支的更新,推送到遠程主機。它的格式與git pull命令相仿。
[www@linux-node1 web-demo]$ 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) Counting objects: 4, done. Delta compression using up to 4 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 7.66 KiB | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To git@linux-node1:web/web-demo.git 0c1d357..be8a547 master -> master
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
我們的gitlab
安裝在opt/gitlab
gitlab配置文件存放在etc/gitlab/gitlab.rb
#現在git 需要加上主機名,我們可以修改配置文件,讓它使用IP進行訪問
編輯配置文件
[root@linux-node1 ~]# vim /etc/gitlab/gitlab.rb external_url 'http://192.168.56.11' [root@linux-node1 ~]# gitlab-ctl reconfigure #提示:修改完需要從新配置才會生效
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
我們從新登陸進行查看
咦! 為啥還沒改呢! 我們從新創建一個項目在試一下
友情提示:
關於Git可以查看徐布斯博客 or 廖雪峰Git
自動化運維之DevOps
DevOps(英文Development(開發
)和Operations(技術運營
)的組合)是一組過程、方法與系統的統稱,用於促進開發(應用程序/軟件工程)、技術運營和質量保障(QA)部門之間的溝通、協作與整合。
它的出現是由於軟件行業日益清晰地認識到:為了按時交付軟件產品和服務,開發和運營工作必須緊密合作
簡單的來說DevOps是一種文化,是讓開發開發、運維、測試能夠之間溝通和交流
自動化運維工具:saltstack、jenkins、等。因為他們的目標一樣,為了我們的軟件、構建、測試、發布更加的敏捷、頻繁、可靠
如果運維對git不熟,是無法做自動化部署。因為所有的項目都受制於開發
Jenkins 介紹
Jenkins
只是一個平台,真正運作的都是插件。這就是jenkins流行的原因,因為jenkins什么插件都有 Hudson
是Jenkins的前身,是基於Java開發的一種持續集成工具,用於監控程序重復的工作,Hudson后來被收購,成為商業版。后來創始人又寫了一個jenkins
,jenkins在功能上遠遠超過hudson
Jenkins官網:https://jenkins.io/
安裝
安裝JDK
Jenkins是Java編寫的,所以需要先安裝JDK,這里采用yum安裝,如果對版本有需求,可以直接在Oracle官網下載JDK。
[root@linux-node1 ~]# yum install -y java-1.8.0
- 1
- 1
安裝jenkins
[root@linux-node1 ~]# cd /etc/yum.repos.d/ [root@linux-node1 yum.repos.d]# wget http://pkg.jenkins.io/redhat/jenkins.repo [root@linux-node1 ~]# rpm --import http://pkg.jenkins.io/redhat/jenkins.io.key [root@linux-node1 ~]# yum install -y jenkins [root@linux-node1 ~]# systemctl start jenkins #本文使用yum進行安裝,大家也可以使用編譯安裝。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
新版本的jenkins為了保證安全,在安裝之后有一個鎖,需要設置密碼之后才可以解鎖
Jenkins Web
訪問地址:192.168.56.11:8080
友情提示:jenkins如果跟gitlab在一台服務器需要將jenkins的端口進行修改,需要將jenkins的8080
修改為8081
[root@linux-node1 ~]# cat /var/lib/jenkins/secrets/initialAdminPassword 490a2f35a2df49b6b8787ecb27122a3a
- 1
- 2
- 1
- 2
復制這個文件下面的ID,否則不可以進行安裝。
我們選擇推薦安裝即可
它會給我們安裝一些基礎的插件
設置用戶名密碼:
點擊保存並退出
早期jenkins
默認是不需要登陸的
我們先來介紹一下jenkins基礎功能
我們點擊新建
這里就是構建一個項目
用戶界面:主要是一些用戶的管理
可以看到當前登陸用戶及用戶權限等
任務歷史:可以查看到所有構建過的項目的歷史
#之所以叫構建,是因為都是java,因為如果不是java程序就沒有構建這個詞。但是我們也可以把一些工作稱之為構建
系統管理:存放jenkins所有的配置
My Views:視圖功能,我們可以自己創建一個自己的視圖
構建隊列:如果當前有視圖任務都會顯示在這里
構建執行狀態:顯示在正構建的任務
系統管理:-系統設置
設置Jenkins全局設置&路徑
Jenkins系統管理比較重要的就是插件管理
了
#因為jenkins所有的東西都需要靠插件來完成,
點擊已安裝可以查看我們的安裝
我們想安裝什么插件,我們可以選擇可選插件
我們為了和gitlab和在一起,我們需要安裝一個插件
查看還可以去jenkins官網下載,然后上傳插件
因為很多插件需要翻牆才可以繼續下載,jenkins還提供了代理的設置
還是在服務器目錄下進行上傳插件 目錄路徑= /var/lib/jenkins/plugins/ 這個目錄下是我們安裝所有的插件
- 1
- 2
- 3
- 1
- 2
- 3
個人博客:www.abcdocker.com