技术流ken

运维拯救世界

DAY8:河北科技大学课堂记录–git+gitlab,keepalived+lvs

写一个脚本: 实现自动化一键部署NFS服务器端和客户端

#!/bin/bash
ip=192.168.64.
yum install nfs-utils rpcbind -y &>/dev/null
if [ $? -ne 0 ];then
echo “下载失败!”
exit 8
fi
if [ ! -e /ken ];then
mkdir /ken
fi
echo “/ken *(rw)” > /etc/exports
systemctl restart rpcbind nfs
for i in 5 7
do
ssh $ip$i mount /dev/cdrom /mnt &>/dev/null
ssh $ip$i yum install nfs-utils -y &>/dev/null
ssh $ip$i mkdir /ken
ssh $ip$i mount -t nfs ${ip}4:/ken /ken
ssh $ip$i df -h
done

第二个脚本:实现批量化检测当前教室主机在线状况,在线主机保存至一个文件中

#!/bin/bash
. /etc/init.d/functions
ip=192.168.64.
for i in {1..255}
do
if ping -c 1 -w 1 $ip$i &>/dev/null;then
echo “$ip$i” >> /root/online_host
echo -n “$ip$i”
success
echo “”
else
echo -n “$ip$i”
failure
echo “”
fi

done


第三个脚本:实现批量化创建100个用户,并创建8位随机密码,且可登陆

#!/bin/bash
for i in {1..100}
do
useradd user$i
pass=`echo $RANDOM | md5sum | cut -c 1-8`
echo “$pass” | passwd –stdin user$i
echo -e “账户:user$i \n密码:$pass” >> /root/user_info
done

第四个脚本:找出系统中含有某个关键词的文件,并输出到终端,关键词用户输入指定

#!/bin/bash
key=$1
for i in `find / -type f`
do
if grep “$key” $i &>/dev/null;then
echo “$i”
sleep 1
fi
done

第五个脚本:批量判断当前目录下所有文件类型

#!/bin/bash
for i in `ls /root`
do
type=`ls -ld $i | cut -c 1`
if [ “$type” == “-” ];then
echo “$i—–>普通文件”
elif [ “$type” == “d” ];then
echo “$i—–>目录”
else
echo “无法识别!”
fi
done

 

 

git

 

git下载

yum install git -y

 

git使用演示

第一步:创建目录

[root@ken ~]# mkdir /ken2

[root@ken ~]# cd /ken2

 

第二步:初始化git工作目录

[root@ken ken2]# git init
Initialized empty Git repository in /ken2/.git/
[root@ken ken2]# ls -a
. .. .git

 

第三步:创建普通文件

[root@ken ken2]# echo “111” > test

[root@ken ken2]# cat test
111

 

第四步:提交至暂存区域

[root@ken ken2]# git add .

 

第五步:提交至仓库

[root@ken ken2]# git config –global user.email “you@example.com”
[root@ken ken2]# git config –global user.name “Your Name”
[root@ken ken2]# git commit -m “v1”

 

第六步:查看历史版本

[root@ken ken2]# git log
commit 76381cb8b768643d42bf481b9544abeb43064598
Author: Your Name <you@example.com>
Date: Fri Jun 28 10:10:51 2019 +0800

v1

 

第七步:再次提交新的版本

[root@ken ken2]# echo “222” >> test
[root@ken ken2]# git add .
[root@ken ken2]# git commit -m “v2”
[master c87fcbd] v2
1 file changed, 1 insertion(+)
[root@ken ken2]# git log
commit c87fcbdf12c541485a9ad1503fb0ed012b83ae27
Author: Your Name <you@example.com>
Date: Fri Jun 28 10:11:49 2019 +0800

v2

commit 76381cb8b768643d42bf481b9544abeb43064598
Author: Your Name <you@example.com>
Date: Fri Jun 28 10:10:51 2019 +0800

v1

 

第八步:回滚到v1版本

[root@ken ken2]# git reset –hard 76381cb8b
HEAD is now at 76381cb v1
[root@ken ken2]#
[root@ken ken2]#
[root@ken ken2]# cat test
111

 

git常用操作

add 添加文件至暂存区域

branch 查看和设置分支

checkout 回滚及切换分支

clone 克隆远程仓库

commit 提交暂存区域的文件至仓库

init 初始化工作目录

log 查看历史版本

merge 合并分支

push 推送本地文件至远程仓库

reset 版本回滚

status 查看文件状态

 

例子1:撤销工作区的内容

[root@ken ken2]# echo “333” >> test
[root@ken ken2]# git status
# On branch master
# Changes not staged for commit:
# (use “git add <file>…” to update what will be committed)
# (use “git checkout — <file>…” to discard changes in working directory)
#
# modified: test
#
no changes added to commit (use “git add” and/or “git commit -a”)
[root@ken ken2]# status ^C
[root@ken ken2]# git checkout — test
[root@ken ken2]# git status
# On branch master
nothing to commit, working directory clean
[root@ken ken2]# cat test
111

 

例子2:撤销暂存区域的文件

[root@ken ken2]# echo “333” >> test
[root@ken ken2]# git add .
[root@ken ken2]# git status
# On branch master
# Changes to be committed:
# (use “git reset HEAD <file>…” to unstage)
#
# modified: test
#
[root@ken ken2]# git reset HEAD test
Unstaged changes after reset:
M test
[root@ken ken2]# git status
# On branch master
# Changes not staged for commit:
# (use “git add <file>…” to update what will be committed)
# (use “git checkout — <file>…” to discard changes in working directory)
#
# modified: test
#
no changes added to commit (use “git add” and/or “git commit -a”)
[root@ken ken2]# git checkout — test
[root@ken ken2]# git status
# On branch master
nothing to commit, working directory clean
[root@ken ken2]# cat test
111

 

例子3:版本回滚

[root@ken ken2]# echo “333” >> test
[root@ken ken2]# git add .
[root@ken ken2]# git commit -m “v3”
[master f0752d2] v3
1 file changed, 1 insertion(+)
[root@ken ken2]# git log
commit f0752d209969a2e79114277d9a20ece1c2c27efe
Author: Your Name <you@example.com>
Date: Fri Jun 28 10:34:49 2019 +0800

v3

commit 76381cb8b768643d42bf481b9544abeb43064598
Author: Your Name <you@example.com>
Date: Fri Jun 28 10:10:51 2019 +0800

v1

 

[root@ken ken2]# cat test
111
333
[root@ken ken2]# git reset –hard 76381cb
HEAD is now at 76381cb v1
[root@ken ken2]# cat test
111

 

 

再次恢复到v2版本

[root@ken ken2]# git reflog
76381cb HEAD@{0}: reset: moving to 76381cb
f0752d2 HEAD@{1}: commit: v3
76381cb HEAD@{2}: reset: moving to 76381cb8b
c87fcbd HEAD@{3}: commit: v2
76381cb HEAD@{4}: commit (initial): v1

[root@ken ken2]# git reset –hard c87fcbd
HEAD is now at c87fcbd v2
[root@ken ken2]# cat test
111
222

 

恢复到v3版本

[root@ken ken2]# git reflog
c87fcbd HEAD@{0}: reset: moving to c87fcbd
76381cb HEAD@{1}: reset: moving to 76381cb
f0752d2 HEAD@{2}: commit: v3
76381cb HEAD@{3}: reset: moving to 76381cb8b
c87fcbd HEAD@{4}: commit: v2
76381cb HEAD@{5}: commit (initial): v1
[root@ken ken2]# git reset –hard f0752d2
HEAD is now at f0752d2 v3
[root@ken ken2]# cat test
111
333

 

git分支

 

例子1:分支使用

[root@ken ken2]# git branch
* master

 

[root@ken ken2]# git branch ken
[root@ken ken2]# git branch
ken
* master

 

[root@ken ken2]# git checkout ken
Switched to branch ‘ken’
[root@ken ken2]# git branch
* ken
master

 

[root@ken ken2]# echo “ken” >> test
[root@ken ken2]# git add .
[root@ken ken2]# git commit -m “v4”
[ken 11651cb] v4
1 file changed, 1 insertion(+)

 

[root@ken ken2]# git checkout master
Switched to branch ‘master’
[root@ken ken2]# cat test
111
333
[root@ken ken2]# git log
commit f0752d209969a2e79114277d9a20ece1c2c27efe
Author: Your Name <you@example.com>
Date: Fri Jun 28 10:34:49 2019 +0800

v3

commit 76381cb8b768643d42bf481b9544abeb43064598
Author: Your Name <you@example.com>
Date: Fri Jun 28 10:10:51 2019 +0800

v1

 

[root@ken ken2]# git merge ken
Updating f0752d2..11651cb
Fast-forward
test | 1 +
1 file changed, 1 insertion(+)
[root@ken ken2]# cat test
111
333
ken
[root@ken ken2]# git log
commit 11651cb4f8687699b2875ab4c90eb1ebcb2efaec
Author: Your Name <you@example.com>
Date: Fri Jun 28 10:52:08 2019 +0800

v4

commit f0752d209969a2e79114277d9a20ece1c2c27efe
Author: Your Name <you@example.com>
Date: Fri Jun 28 10:34:49 2019 +0800

v3

commit 76381cb8b768643d42bf481b9544abeb43064598
Author: Your Name <you@example.com>
Date: Fri Jun 28 10:10:51 2019 +0800

v1

 

搭建gitlab

 

第一步:下载gitlab软件包

[root@ken ~]# yum install gitlab-ce-8.9.5-ce.0.el7.x86_64.rpm java -y

 

第二步:修改配置文件

[root@ken ~]# vim /etc/gitlab/gitlab.rb

external_url ‘http://192.168.64.4’

 

第三步:重新配置gitlab

[root@ken ~]# gitlab-ctl reconfigure

 

第四步:访问gitlab

ip:80

 

实现负载均衡加高可用方案

 

方案一:nginx+keepalived

方案二:lvs+keepalived

 

 

LVS负载均衡

 

作用:实现负载均衡

 

高负载集群实战之LVS负载均衡-技术流ken

lvs简介

 

LVS的英文全称是Linux Virtual Server,即Linux虚拟服务器。

LVS采用IP负载均衡技术和基于内容请求分发技术。调度器具有很好的吞吐率,将请求均衡地转移到不同的服务器上执行,且调度器自动屏蔽掉服务器的故障,从而将一组服务器构成一个高性能的、高可用的虚拟服务器。整个服务器集群的结构对客户是透明的,而且无需修改客户端和服务器端的程序。为此,在设计时需要考虑系统的透明性、可伸缩性、高可用性和易管理性

 

特点

跨平台:window,linux

 

作用

 

实现负载均衡

 

核心组件

ip_vs:linux的内核功能模块,工作在内核,依赖该内核模块实现负载均衡功能

ipvsadm:应用层程序,该程序可以和内核中的ip_vs模块通信,实现对负载均衡的管理和控制

 

 

ipvsadm参数详解

 

-A:添加一个虚拟服务(添加vip)
-C:清空规则表
-a:指定在虚拟服务中添加RS节点
-t service-address[:port]:该规则是对tcp协议的请求做调度
-r: 指定真实服务器的地址(remote)
-g:指定用DR模式(默认)

 

-s 指定调度算法

 

实例1:LVS的DR模式使用

 

环境准备:

lvs服务器:192.168.64.4

后端节点1:192.168.64.5

后端节点2:192.168.64.7

VIP:192.168.64.10

 

第一步:lvs服务器端下载ipvsadm

[root@ken ~]# yum install ipvsadm -y

 

第二步:给lvs服务器端绑定VIP

[root@ken ~]# ip a a 192.168.64.10/24 dev eth0

 

第三步:ipvsadm添加调度规则

[root@ken ~]# ipvsadm -A -t 192.168.64.10:80 -s rr

 

第四步:查看ipvsadm规则

[root@ken ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.64.10:80 rr

 

第五步:添加后端节点

[root@ken ~]# ipvsadm -a -t 192.168.64.10:80 -r 192.168.64.5:80 -g

[root@ken ~]# ipvsadm -a -t 192.168.64.10:80 -r 192.168.64.7:80 -g

 

第六步:再次查看ipvsadm规则

[root@ken ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.64.10:80 rr
-> 192.168.64.5:80 Route 1 0 0
-> 192.168.64.7:80 Route 1 0 0

 

第七步:配置后端节点,先下载httpd

[root@ken ~]# yum install httpd -y

 

第八步:启动后端节点的apache

[root@ken ~]# systemctl restart httpd

 

第九步:执行下面的脚本

#!/bin/bash
ip a a 192.168.64.10/32 dev lo:0
echo “1”>/proc/sys/net/ipv4/conf/lo/arp_ignore
echo “1”>/proc/sys/net/ipv4/conf/all/arp_ignore
echo “2”>/proc/sys/net/ipv4/conf/lo/arp_announce
echo “2”>/proc/sys/net/ipv4/conf/all/arp_announce

 

第十步:访问VIP

 

 

 

keepalived

实例2:keepalived+lvs

 

主调度器:192.168.64.4

备调度器:192.168.64.8

后端节点1: 192.168.64.5

后端节点2: 192.168.64.7

VIP: 192.168.64.10

 

第一步:两个调度节点需要执行下面的操作

[root@ken ~]# yum install ipvsadm keepalived -y

 

第二步:配置主调度器的keepalived配置文件

! Configuration File for keepalived

global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id d1   #每台节点确保名称不一致
vrrp_skip_check_adv_addr
#vrrp_strict #注释改行
vrrp_garp_interval 0
vrrp_gna_interval 0
}

vrrp_instance VI_1 {
state MASTER  #主节点不需要更改
interface eth0
virtual_router_id 51
priority 150  #优先级要比备节点高于50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.64.10/24    #VIP地址
}
}

virtual_server 192.168.64.10 80 {  #后端节点IP
delay_loop 6
lb_algo rr
lb_kind DR   #改成DR模式
persistence_timeout 50
protocol TCP

real_server 192.168.64.5 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.64.7 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}

 

 

第三步:把主节点配置好的keepalived的文件发送到备用节点

[root@ken ~]# scp /etc/keepalived/keepalived.conf 192.168.64.8:/etc/keepalived/

 

第四步:在备用节点修改keepalived文件

只需要修改三处

router_id d2

state BACKUP

priority 100

 

第五步:清空之前配置的ipvsadm规则及VIP

[root@ken ~]# ipvsadm -C

[root@ken ~]# ip a d 192.168.64.10/24 dev eth0

 

第六步:重启主调度器及备调度器keepalived

[root@ken ~]# ip a

[root@ken ~]# ipvsadm -L -n

 

第七步:验证

关掉主调度器

 

 

 

9 thoughts on “DAY8:河北科技大学课堂记录–git+gitlab,keepalived+lvs

  1. Thank you a lot for providing individuals with remarkably wonderful chance to check tips from this blog. It is always so lovely and jam-packed with a lot of fun for me and my office acquaintances to search your blog more than three times a week to read through the latest secrets you have. And indeed, I’m certainly amazed concerning the mind-boggling secrets you serve. Some 1 areas in this post are without a doubt the simplest I’ve had.

  2. My spouse and i felt cheerful Jordan could deal with his investigations with the ideas he gained from your web pages. It’s not at all simplistic just to continually be offering techniques other folks could have been trying to sell. And now we fully understand we have got you to be grateful to for that. All the illustrations you made, the straightforward site menu, the friendships you will assist to foster – it is mostly overwhelming, and it is letting our son and us do think this subject matter is amusing, which is very pressing. Thank you for the whole thing!

  3. I must express my passion for your kind-heartedness for all those that absolutely need help on this topic. Your very own dedication to getting the solution all over had become quite helpful and has consistently helped those just like me to achieve their ambitions. Your new warm and helpful key points entails much to me and substantially more to my mates. Thank you; from everyone of us.

  4. I wish to show my thanks to the writer for rescuing me from such a trouble. Because of scouting through the the net and seeing opinions that were not pleasant, I thought my entire life was over. Existing without the presence of approaches to the difficulties you’ve fixed by way of your entire article is a critical case, as well as those which might have in a wrong way damaged my career if I had not noticed your blog. Your good training and kindness in playing with all the stuff was precious. I’m not sure what I would have done if I hadn’t encountered such a step like this. I am able to at this point look forward to my future. Thanks so much for the high quality and sensible guide. I won’t be reluctant to suggest the sites to anybody who would like support on this area.

  5. I wish to show my thanks to you just for rescuing me from this particular condition. Just after looking out through the internet and getting basics that were not helpful, I believed my entire life was gone. Being alive devoid of the answers to the difficulties you have solved by way of your entire article is a serious case, as well as the ones that might have badly affected my entire career if I had not come across your web page. Your own personal mastery and kindness in touching every part was helpful. I’m not sure what I would’ve done if I had not discovered such a step like this. I am able to now look forward to my future. Thank you very much for this specialized and result oriented help. I will not think twice to propose your web site to anyone who ought to have guidance on this problem.

  6. I as well as my guys were found to be going through the best secrets found on the website and then before long came up with a horrible suspicion I never thanked the website owner for those strategies. All the young boys were as a result warmed to study them and now have really been taking advantage of these things. Thanks for getting so helpful as well as for picking these kinds of quality subjects most people are really desperate to understand about. My personal sincere apologies for not expressing gratitude to sooner.

  7. My husband and i got really contented Chris managed to deal with his web research while using the precious recommendations he discovered using your web pages. It’s not at all simplistic to just continually be giving out things people today have been making money from. We really do know we now have you to give thanks to for this. The illustrations you made, the straightforward site menu, the friendships you will make it easier to create – it’s everything remarkable, and it is assisting our son in addition to our family reckon that that concept is enjoyable, which is highly serious. Thank you for all the pieces!

  8. I simply wanted to write a simple remark to express gratitude to you for these awesome advice you are giving out on this website. My time intensive internet investigation has finally been compensated with beneficial suggestions to write about with my companions. I ‘d express that many of us visitors actually are very lucky to exist in a notable network with many awesome professionals with insightful points. I feel somewhat grateful to have seen your website and look forward to some more exciting moments reading here. Thanks once again for all the details.

发表评论

电子邮件地址不会被公开。