DevOps Gitlab环境部署

DevOps 介绍

一、DevOps 介绍

1.1.1 DevOps 介绍

  1. DevOps介绍

DevOps 是Development和Operations的组合,也就是开发和运维的简写。
1、DevOps集文化理念、实践与工具与一身,可以提高组织高速交付应用程序和服务的能力,与使用 传统软件开发和基础设施管理流程相比,能够帮助企业更快速的发展和改进产品,这种速度使企业 能够更好地服务于客户,并在市场上高效地参与竞争。
2、DevOps 是针对企业中的研发人员、运维人员和测试人员的工作理念,是他们在应用开发、代码部 署和质量测试等整条生命周期中协作和沟通的最佳实践,DevOps 强调整个组织的合作以及交付和 基础设施变更的自动化、从而实现持续集成、持续部署和持续交付。
3、DevOps 平台:代码托管(gitlab/svn)、项目管理(jira/禅道/Teambition)、运维平台(腾讯蓝鲸/自主研发 平台)、持续交付(Jenkins/gitlab)。

  1. . 为什么要推广DevOps

DevOps 强调团队协作、相互协助、持续发展,然而传统的模式是开发人员只顾开发程序,运维 只负责基础环境管理和代码部署及监控等,其并不是为了一个共同的目标而共同实现最终的目的, 而DevOps 则实现团队作战,即无论是开发、运维还是测试,都为了最终的代码发布、持续部署和业 务稳定而付出各自的努力,从而实现产品设计、开发、测试和部署的良性循环,实现产品的最终持 续交付。

3.DevOps的优势

速度:应用版本快速的迭代更新,以更好地适应不断变化的市场需求。 快速交付:更快的将应用交付至生产环境。 可靠性:保证应用交付的结果是成功的。 规模:可以在大规模环境下且可靠的交付应用。 增强合作:建立适应DevOps文化模式的团队,开发人员和运维人员协同工作。 安全性:在快速迭代的同时保证应用的质量。

1.1.2 CI/CD简介

  1. 什么是持续集成(CI-Continuous integration)

持续集成是指多名开发者在开发不同功能代码的过程当中,可以频繁的将代 码行合并到一起并切相互不影响工作。

2.什么是持续部署(CD-continuous deployment)
是基于某种工具或平台实现代码自动化的构建、测试和部署到线上环境以实 现交付高质量的产品,持续部署在某种程度上代表了一个开发团队的更新迭代 速率


1.1.2 Gitlab安装与使用

  1. 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的一个私有代码仓库。

  1. 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
  1. 设置全局用户和邮箱
[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

  1. 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
  1. 分支相关命令
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合并分支


合并分支成功

posted @ 2020-07-04 00:27  海上月  阅读(2150)  评论(0编辑  收藏  举报