云计算day13-Git 版本控制系统
2019-09-23 本文已影响0人
Linux丶晨星
git版本监控组
1.安装和配置git
[root@k8s-node2 ~]# yum install git -y
[root@k8s-node2 ~]# git --version
git version 1.8.3.1
git config --global user.name "lcx"
git config --global user.email "245684979@qq.com"
git config --global color.ui "true"
2. 创建版本库
git init
3.git仓库添加文件
git add . #添加文件到缓冲区
git commit -m 'ad three file'
4. git 修改文件名称并提交
git mv file1 file4
git commit -m 'change file name'
5. git的文件对比
git diff file3 #本地目录和缓冲去文件对比
git diff --cached file3 #缓冲区文件和仓库文件对比
6. 实现回退功能
[root@k8s-node2 ~]# git log --oneline
58bea1a add three file
fe9e8c2 add two file
[root@k8s-node2 ~]# cat file2
456456456
8888888
[root@k8s-node2 ~]# git reset --hard 58bea1a
HEAD is now at 58bea1a add three file
[root@k8s-node2 ~]# cat file2
456456456
#查看所有历史版本
[root@k8s-node2 ~]# git reflog --oneline
fe9e8c2 HEAD@{0}: reset: moving to fe9e8c2
58bea1a HEAD@{1}: commit: add three file
fe9e8c2 HEAD@{2}: commit (initial): add two file
[root@k8s-node2 ~]# git reset --hard 58bea1a
撤回
[root@k8s-node2 ~]# echo 8888888 > file2
[root@k8s-node2 ~]# git diff file2
diff --git a/file2 b/file2
index 7d7a948..80f456a 100644
--- a/file2
+++ b/file2
@@ -1 +1 @@
-456456456
+8888888
[root@k8s-node2 ~]# git checkout file2
[root@k8s-node2 ~]# git diff file2
[root@k8s-node2 ~]# cat file2
456456456
撤销修改的文件
提交到缓冲区的文件撤回
git reset HEAD file2
未add到缓冲区的文件撤回
git checkout -- file2
7. git分支管理
#查看分支
git branch ----> * master
#增加一个新分支
git branch dev01
#切换分支
git checkout dev01
#合并分支
git merge master
#在开发分支上合并主分支
git merge master -m '注释'
#切回主分支
git checkout master
#在主分支次合并开发分支
git merge dev01 -m '注释'
8. git标签管理
标签就是给commit起一个别名,容易记忆
#添加标签
git log --oneline
git tag v1.2 ----> 给当前版本打标签
git tag -a v1.0 -m '注释,优化了修复了什么' <commit的ID>
#删除标签
git tag -d v1.2
[root@k8s-node2 demo]# git log --oneline
b8e21cc add test.txt
9d5ab15 add test.txt
382f080 add test.txt
ff6047d add file6 file7
b693580 add file6 file7
58bea1a add three file
fe9e8c2 add two file
[root@k8s-node2 demo]# git tag
[root@k8s-node2 demo]# git tag v1.2
[root@k8s-node2 demo]# git tag
v1.2
[root@k8s-node2 demo]# git show v1.2
commit b8e21cc519dfd918ee880831e37585afcdf73410
Author: lcx <245684979@qq.com>
Date: Mon Sep 23 12:03:35 2019 +0800
add test.txt
diff --git a/demo/test.txt b/demo/test.txt
index 69f66d3..d99f9f0 100644
--- a/demo/test.txt
+++ b/demo/test.txt
@@ -1,3 +1,3 @@
11111
-aaaaa
+22222
33333
[root@k8s-node2 demo]# git tag -a v1.0 -m 'v1.0 add 后裔' fe9e8c2
[root@k8s-node2 demo]# git tag
v1.0
v1.2
[root@k8s-node2 demo]# git show v1.0
tag v1.0
Tagger: lcx <245684979@qq.com>
Date: Mon Sep 23 12:18:45 2019 +0800
v1.0 add 后裔
commit fe9e8c25cfb61eb63da6681dc09d054cd5739243
Author: lcx <245684979@qq.com>
Date: Mon Sep 23 10:44:54 2019 +0800
add two file
diff --git a/file1 b/file1
new file mode 100644
index 0000000..5721413
--- /dev/null
+++ b/file1
@@ -0,0 +1 @@
+423
扩展
Git 打补丁-- patch 和 diff 的使用
diff
patch 打补丁,创建补丁,卸载补丁
git克隆 码云网站复制项目链接
9. git远程仓库gitee
ssh-keygen -t rsa
cat /root/.ssh/id_rsa.pub
cd /root/demo/
ll
git remote add origin git@gitee.com:linuxcx/linux.git
git push -u origin master
10. gitlab安装
#上传gitlab-ce-11.9.11-ce.0.el7.x86_64.rpm到当前目录
echo "192.168.37.202 mirrors.aliyun.com" >>/etc/hosts
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum localinstall gitlab-ce-11.9.11-ce.0.el7.x86_64.rpm -y
vim /etc/gitlab/gitlab.rb
#设置访问url和关闭prometheus
external_url 'http://http://gitlablcx.com'
prometheus_monitoring['enable'] = false
#配置gitlab通过smtp发送邮件
[root@k8s-node2 demo]# vim /etc/gitlab/gitlab.rb
gitlab_rails['gitlab_email_enabled'] = true
gitlab_rails['gitlab_email_from'] = '245684979@qq.com'
gitlab_rails['gitlab_email_display_name'] = 'linux_gitlab'
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.qq.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "245684979"
gitlab_rails['smtp_password'] = "lvlina.199625"
gitlab_rails['smtp_domain'] = "qq.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
#重新配置
gitlab-ctl reconfigure
11.gitlab汉化
image#下载汉化包
https://gitlab.com/xhang/gitlab
gitlab-ctl stop
tar xf gitlab-11-9-stable-zh.tar.gz
\cp -a gitlab-11-9-stable-zh/* /opt/gitlab/embedded/service/gitlab-rails/
image
12. gitlab-用户-用户组-项目之间的关系
先创建用户组,在基于用户组创建项目,最后创建用户,编辑用户组,添加成员,注意权限
13. gitlab备份和恢复
#备份
gitlab-rake gitlab:backup:create
#恢复
gitlab-ctl stop
gitlab-rake gitlab:backuip:restore BACKUP=1510472027_2019_09_23_9.4.5
gitlab-ctl start
vim /etc/gitlab/gitlab.rb
gitlab_rails['manage_backup_path'] = true
gitlab_rails['backup_path'] = "/var/opt/gitlab/backups"
gitlab_rails['backup_keep_time'] = 604800
#迁移云主机
先备份,云主机安装相同版本,恢复
#更新配置
gitlab-ctl reconfigure
14.gitlab版本升级
gitlab升级,要小版本跨 10--->11--->12
扩展内容:
1.安装svn服务器,将svn中的数据迁移到git仓库
git-svn
git clone svn
svn是一个中心化的版本控制工具,git是分布式的,每个人都可以有完整的代码仓库
#安装svn
yum install subversion -y
mkdir /svn/repo -p
#初始化仓库
svnadmin create /svn/repo
cd /svn/repo/conf
vim svnserve.conf
[general]
anon-access = none
auth-access = write
password-db = passwd
authz-db = authz
realm = /svn/repo
[sasl]
vim passwd
[users]
admin = 123456
vim authz
[/]
admin = rw
test = r
启动svn服务
svnserve -d -r /svn/repo
#svn迁移到git
yum install git-svn -y
git svn clone svn://10.0.0.13 --username=admin --no-metadata --authors-file=password.txt git
cat password.txt
admin=<245684979@qq.com>
2.找回
gitlab
的root密码
2. 持续部署
gitlab, lb(部署服务器), web01,web02
#lb01,web01,web02都按照nginx
echo "192.168.37.202 mirrors.aliyun.com" >>/etc/hosts
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum install nginx -y
#创建upstream资源池文件
vim /etc/nginx/upstream_monitor
server 10.0.0.7;
server 10.0.0.8;
#配置lb01负载均衡
vim /etc/nginx/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream monitor {
include /etc/nginx/upstream_monitor;
}
server {
listen 80;
server_name monitor.lcx.com;
location / {
proxy_pass http://monitor;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}
nginx -t
systemctl restart nginx
systemctl enable nginx
#配置web01和web02,文件内容相同
vi /etc/nginx/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name monitor.lcx.com;
location / {
root /code/web;
index index.html index.htm;
}
}
}
nginx -t
systemctl restart nginx
systemctl enable nginx
#在web01和web02创建首页并创建软连接
[root@lcx01 nginx]# cd /code/
[root@cx01 code]# mkdir web_v1
[root@cx01 code]# echo web01 >web_v1/index.html
[root@cx01 code]# ln -s web_v1 web
[root@cx01 code]# ll
total 0
lrwxrwxrwx 1 root root 6 Sep 24 11:11 web -> web_v1
drwxr-xr-x 2 root root 24 Sep 24 11:11 web_v1
[root@web02 code]# cd /code/
[root@web02 code]# mkdir web_v1
[root@web02 code]# echo web02 >web_v1/index.html
[root@web02 code]# ln -s web_v1 web
[root@web02 code]# ll
total 0
lrwxrwxrwx 1 root root 6 Sep 24 11:12 web -> web_v1
drwxr-xr-x 2 root root 24 Sep 24 11:12 web_v1
#在windows上做hosts解析
C:\Windows\System32\drivers\etc\hosts
10.0.0.5 http://monitor.lcx.com/
image
新建项目
image
流程
image
#在gitlab上
git config --global user.name "Administrator"
git config --global user.email "admin@example.com"
mkdir /code
cd /code/
ls
wget http://192.168.37.202/linux59/monitor_html.tar.gz
ls
tar xf monitor_html.tar.gz
cd monitor/
ls
ls .git/
git branch
git log --oneline
[root@k8s-node2 monitor]# mkdir -p /server/scripts
[root@k8s-node2 monitor]# cd /server/scripts/
============================================================
[root@lb scripts]# cat auto_deploy.sh
#!/bin/bash
serverlist="10.0.0.7 10.0.0.8"
#代码更新
fabu() {
cd /code/monitor/ && \
git pull -u origin
version=`git tag|tail -1`
echo $version
tar zcf /code/monitor_${version}.tar.gz *
for ip in $serverlist
do
scp -rp /code/monitor_${version}.tar.gz root@${ip}:/code
ssh root@${ip} "mkdir -p /code/web_${version} &&
tar xf /code/monitor_${version}.tar.gz -C /code/web_${version} && \
rm -fr /code/web && ln -s /code/web_${version} /code/web && rm -fr /code/monitor_${version}.tar.gz "
done
}
huigun() {
cd /code/monitor/
version=`git tag|tail -1`
last_version=`git tag|tail -2|head -1`
for ip in $serverlist
do
ssh root@${ip} "rm -fr /code/web && ln -s /code/web_${last_version} /code/web"
done
}
case $1 in
deploy)
fabu;
;;
rollback)
huigun;
;;
*)
echo '无效的参数';
esac
========================================================
[root@lb ~]# ssh-keygen -t rsa
[root@lb ~]# cat /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCsPPhatOoCoYEt/PBoKeC6Kd8Uj+Ll+dD0Bnrw3aiYsL7fWc83bCyp45wub31Wqt7sCcoqxuqwbidk6vlsZyb3CbGKxVcroMln91ASD+bmaYIJkl4KINAqktTzBIIDLRG0GUYRZw9npNq5d0MCc1XLTSmoAWpWpvuwIEpCFJcSYwSlUWkEXWtEFkJX5oWCNJX77iCfCp00JJI7TF59gReUojZ2YcXzxBSNz/lAvcvudcflJvu/rc9nBXMnwtXEYiY/QfXk2I9igqfR1lfU2FnfptJOKHKHHibajkuGVaovMypiBQxm66fHG88z7ZL+05+IDk/+LBj5In/tkYi1VWi5 root@lb
``
3. jenkins安装
#安装jenkins
yum install -y java
wget http://192.168.37.202/linux59/jenkins/jenkins-2.176.1-1.1.noarch.rpm
wget http://192.168.37.202/linux59/jenkins/jenkins_2.176_plugins.tar.gz
yum localinstall *.rpm
rpm -ql jenkins
systemctl restart jenkins
route del -net 0.0.0.0/0 gw 10.0.0.254
#将语言包导入
[root@lb tools]# ls
jenkins-2.176.1-1.1.noarch.rpm jenkins_2.176_plugins.tar.gz plugins
tar xf jenkins_2.176_plugins.tar.gz
\cp -a plugins/* /var/lib/jenkins/plugins/
systemctl restart jenkins.service
systemctl restart network
cd /var/lib/jenkins/
ls
image
访问10.0.0.5:8080汉化成功
升级插件
image
image image
java项目代码上线
思路
1:创建数据库
2:编译java代码 maven编译
mvn clean package
3:scp 推送war包 10.0.0.7,10.0.0.8,重启tomcat
4: 通知机制
gitlab 拉取代码
编译代码
本地打包
for 循环
do
scp 目标主机 传输压缩包
从负载均衡上摘下来
ssh 目标主机 解压压缩包, 删除软连接,新建软连接
curl 访问 200
从负载均衡上加回去
done