[关闭]
@Great-Chinese 2016-12-28T09:56:11.000000Z 字数 6649 阅读 886

Linux系统架构

Linux系统架构


1.1 HA集群配置-1 High Availability Cluster

1,配置主服务器 192.168.31.127

  1. # 临时修改hostname为master
  2. hostname master
  3. bash # 测试一下
  4. # 永久性更改hostname,修改下面的配置文件
  5. vim /etc/sysconfig/network
  6. # 关闭防火墙
  7. iptables -F
  8. getenforce
  9. vim /etc/hosts # 修改指向
  10. 192.168.31.127 master
  11. 192.168.31.116 slave
  12. # 下载epel包
  13. wget www.lishiming.net/data/attachment/forum/epel-release-6-8_64.noarch.rpm
  14. # 安装epel包
  15. rpm -ivh epel-release-6-8_64.noarch.rpm
  16. # 安装heartbeat
  17. yum install -y heartbeat
  18. # 安装libnet
  19. yum install -y libnet

2,配置备选端 192.168.31.116

  1. # 临时修改hostname为slave
  2. hostname slave
  3. bash # 测试一下
  4. # 永久性更改hostname,修改下面的配置文件
  5. vim /etc/sysconfig/network
  6. # 关闭防火墙
  7. iptables -nvL
  8. getenforce
  9. vim /etc/hosts # 增加内容如下
  10. 192.168.31.127 master
  11. 192.168.31.116 slave
  12. bash 测试一下
  13. # 下载epel包
  14. wget www.lishiming.net/data/attachment/forum/epel-release-6-8_64.noarch.rpm
  15. # 安装epel包
  16. rpm -ivh epel-release-6-8_64.noarch.rpm
  17. # 安装heartbeat
  18. yum install -y heartbeat
  19. # 安装libnet
  20. yum install -y libnet

1.2 HA集群配置

1,配置主服务器 192.168.31.127

  1. cd /usr/share/doc/heartbeat-3.0.4/
  2. cp authkeys ha.cf haresources /etc/ha.d/
  3. cd /etc/ha.d
  4. # 主从验证
  5. vim authkeys
  6. auth 3
  7. #1 crc
  8. #2 sha1 HI!
  9. 3 md5 Hello!
  10. chmod 600 authkeys
  11. # 配置haresources
  12. vim haresources
  13. master 192.168.31.110/24/eth0:0 nginx
  14. # 配置ha.cf
  15. vim ha.cf
  16. debugfile /var/log/ha-debug
  17. logfile /var/log/ha-log
  18. logfacility local0
  19. keepalive 2
  20. deadtime 30
  21. warntime 10
  22. initdead 60
  23. udpport 694
  24. ucast eth0 192.168.31.116
  25. auto_failback on
  26. node master
  27. node slave
  28. ping 192.168.31.1
  29. respawn hacluster /usr/lib64/heartbeat/ipfail
  30. # 配置好上面的3个文件后,就上传到备选端
  31. scp authkeys haresources ha.cf slave:/etc/ha.d/
  32. # 安装nginx
  33. yum install -y nginx

2,配置备选端 192.168.31.116

  1. authkeys haresources # 这两个文件不用更改
  2. cd /usr/share/doc/heartbeat-3.0.4/
  3. cp authkeys ha.cf haresources /etc/ha.d/
  4. cd /etc/ha.d
  5. # 配置ha.cf
  6. vim ha.cf # 其它不变,只更改下面的ip地址为对方的地址
  7. ucast eth0 192.168.31.127
  8. # 安装nginx
  9. yum install -y nginx

3,测试

  1. # 在主服务器上启动heartbeat
  2. /etc/init.d/heartbeat start
  3. ifconfig
  4. ps aux |grep nginx
  5. # 在主服务器上编辑
  6. echo "master melody0113" > /usr/share/nginx/html/index.html
  7. 那么在web网上打开192.168.31.110 显示master melody0113
  8. # 在备选端编辑
  9. echo "salve gary88" > /usr/share/nginx/html/index.html
  10. 那么在eb网上打开192.168.31.110 显示salve gary88
  11. # 在主服务器上关闭iptables
  12. iptables -A INPUT -p icmp -j DROP
  13. 然后再刷新网业就会跳转到备选端,显示salve gary88
  14. # 在主服务器上删除iptables这条命令
  15. iptables -D INPUT -p icmp -j DROP
  16. # 停止启动heartbeat
  17. /etc/init.d/heartbeat stop

1.3 LB集群之LVS介绍 --load balance 负载均衡集群

负载均衡开源软件有 nginx、lvs、keepalived
商业的硬件负载设备 F5、Netscale

1.3.1 LVS 三种模式

NAT模式-网络地址转换 Virtualserver via Network address translation(VS/NAT)
TUN模式 virtual server via ip tunneling模式
DR模式-直接路由模式 Virtual server via direct routing (vs/dr)

LVS由前端的负载均衡器(Load Balancer,LB)和后端的真实服务器(Real Server,RS)群组成

1.3.2 LVS的调度算法

RR:轮循调度(Round Robin)
WRR:加权轮循(Weight RR)
LC:最少链接(Least Connections)
WLC:加权最少连接(默认采用的就是这种)(Weighted Least Connections)

1.4 LVS的NAT模式

1.4.1 编辑dir --192.168.139.111(新增的网卡)

  1. hostname dir
  2. bash
  3. yum install -y ipvsadm
  4. # 编辑此配置文件
  5. vim /usr/local/sbin/lvs_nat.sh # 增加内容如下
  6. #! /bin/bash
  7. # director 服务器上开启路由转发功能:
  8. echo 1 > /proc/sys/net/ipv4/ip_forward
  9. # 关闭icmp的重定向
  10. echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects
  11. echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects
  12. echo 0 > /proc/sys/net/ipv4/conf/eth0/send_redirects
  13. echo 0 > /proc/sys/net/ipv4/conf/eth1/send_redirects
  14. # director 设置nat防火墙
  15. iptables -t nat -F
  16. iptables -t nat -X
  17. iptables -t nat -A POSTROUTING -s 192.168.139.0/24 -j MASQUERADE
  18. # director设置ipvsadm
  19. IPVSADM='/sbin/ipvsadm'
  20. $IPVSADM -C
  21. $IPVSADM -A -t 192.168.139.111:80 -s rr
  22. $IPVSADM -a -t 192.168.139.111:80 -r 192.168.31.116:80 -m
  23. $IPVSADM -a -t 192.168.139.111:80 -r 192.168.31.127:80 -m
  24. # 执行此配置文件
  25. sh /usr/local/sbin/lvs_nat.sh
  26. ipvsadm -ln # 查看具体IP
  27. # 修改权重
  28. $IPVSADM -A -t 192.168.139.111:80 -s wrr
  29. $IPVSADM -a -t 192.168.139.111:80 -r 192.168.31.116:80 -m -w 2
  30. $IPVSADM -a -t 192.168.139.111:80 -r 192.168.31.133:80 -m -w 1

1.4.2 编辑rs1 --192.168.31.116

  1. hostname rs1
  2. bash
  3. # 编辑此文件
  4. vim /etc/sysconfig/network-scripts/ifcfg-eth0 # 更改下面的网关地址
  5. GATEWAY=192.168.31.133(主机内网ip)
  6. # 启动网卡
  7. ifdown eth0; ifup eth0
  8. # 启动80端口
  9. /etc/init.d/nginx start
  10. curl localhost

1.4.3 编辑rs2 --198.168.31.127

  1. hostname rs2
  2. bash
  3. # 编辑此文件
  4. vim /etc/sysconfig/network-scripts/ifcfg-eth0 # 更改下面的网关地址
  5. GATEWAY=192.168.31.133(主机内网ip)
  6. # 启动网卡
  7. ifdown eth0; ifup eth0
  8. # 启动80端口
  9. /etc/init.d/nginx start
  10. curl localhost

1.5 LVS的DR设置(3个公用IP + 1个VIP)

1.5.1 编辑dir --198.168.31.127

  1. ipvsadm -C # 先清除nat的所有规则
  2. ipvsadm -ln # 查看
  3. iptables -t nat -F # 再清除防火墙规则
  4. ifdown eth1; # 再关闭多的网卡
  5. # 编辑此配置文件
  6. vim /usr/local/sbin/lvs_dr.sh # 增加内容如下
  7. #! /bin/bash
  8. echo 1 > /proc/sys/net/ipv4/ip_forward
  9. ipv=/sbin/ipvsadm
  10. vip=192.168.31.111 # 虚拟IP
  11. rs1=192.168.31.128
  12. rs2=192.168.31.129
  13. ifconfig eth0:0 $vip broadcast $vip netmask 255.255.255.255 up
  14. route add -host $vip dev eth0:0
  15. $ipv -C
  16. $ipv -A -t $vip:80 -s rr
  17. $ipv -a -t $vip:80 -r $rs1:80 -g -w 1
  18. $ipv -a -t $vip:80 -r $rs2:80 -g -w 1
  19. # 执行此文件
  20. sh /usr/local/sbin/lvs_dr.sh
  21. # 修改权重 先down再开启--2
  22. ifconfig eth0:0 down
  23. ifconfig eth0:0 $vip broadcast $vip netmask 255.255.255.255 up
  24. route add -host $vip dev eth0:0
  25. $ipv -C
  26. $ipv -A -t $vip:80 -s wrr
  27. $ipv -a -t $vip:80 -r $rs1:80 -g -w 3
  28. $ipv -a -t $vip:80 -r $rs2:80 -g -w 1
  29. # 执行此文件--2
  30. sh /usr/local/sbin/lvs_dr.sh
  31. # 查看权重是否成功
  32. ipvsadm -ln

1.5.2 编辑rs1 --192.168.31.128 操作方式同下

1.5.3 编辑rs2 --198.168.31.129

  1. # 编辑此配置文件
  2. vim /usr/local/sbin/lvs_rs.sh #增加内容如下
  3. #! /bin/bash
  4. vip=192.168.31.111
  5. ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
  6. route add -host $vip lo:0
  7. echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
  8. echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
  9. echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
  10. echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
  11. # 执行此配置文件
  12. sh /usr/local/sbin/lvs_rs.sh
  13. # 启动vip,lo:0会出现
  14. ifconfig
  15. # 在web网业上测试,输入vip地址192.168.31.111

1.6 LVS结合keepalived配置(负载均衡+高可用)lb+HA

1.6.1 主服务器端--192.168.31.127(masterdir)

  1. # 先清除ipvsadm所有规则
  2. ipvsadm -C
  3. # 安装keepalived
  4. yum install -y keepalived
  5. vim /etc/keepalived/keepalived.conf
  6. vrrp_instance VI_1 {
  7. state MASTER #备用服务器上为 BACKUP
  8. interface eth0
  9. virtual_router_id 51
  10. priority 100 #备用服务器上为90
  11. advert_int 1
  12. authentication {
  13. auth_type PASS
  14. auth_pass 1111
  15. }
  16. virtual_ipaddress {
  17. 192.168.31.111
  18. }
  19. }
  20. delay_loop 6 # (每隔10秒查询realserver状态)
  21. lb_algo rr # (lvs 算法)
  22. lb_kind DR # (Direct Route)
  23. persistence_timeout 50 # (同一IP的连接60秒内被分配到同一台realserver)
  24. protocol TCP # (用TCP协议检查realserver状态)
  25. real_server 192.168.31.128 80 {
  26. weight 100 # (权重)
  27. TCP_CHECK {
  28. connect_timeout 10 # (10秒无响应超时)
  29. nb_get_retry 3
  30. delay_before_retry 3
  31. connect_port 80
  32. }
  33. }
  34. real_server 192.168.31.129 80 {
  35. weight 100 # (权重)
  36. TCP_CHECK {
  37. connect_timeout 10 # (10秒无响应超时)
  38. nb_get_retry 3
  39. delay_before_retry 3
  40. connect_port 80
  41. }
  42. }
  43. }
  44. # 复制此文件到备选服务端上
  45. scp /etc/keepalived/keepalived.conf 192.168.31.128:/etc/keepalived/keepalived.conf
  46. # 查看没有有虚拟IP
  47. ifconfig
  48. ipvsadm -ln
  49. ifconfig eth0:0 down
  50. # 启动keepalived,vip会自动启动
  51. /etc/init.d/keepalived start
  52. # 在另一台机器上测试
  53. curl 192.168.31.111

1.6.2 备选服务端--192.168.31.128(rs2)

  1. # 安装keepalived
  2. yum install -y keepalived
  3. # 编辑此配置文件
  4. vim /etc/keepalived/keepalived.conf # 修改下面2行,其它内容不变
  5. state BACKUP #备用服务器上为 BACKUP
  6. priority 99 #备用服务器上为90
  7. # 启动keepalived
  8. /etc/init.d/keepalived start
  9. # 测试,如果有一台机器down,系统会自动踢出掉
  10. curl 192.168.31.111

1.7 nginx的负载均衡集群-可针对域名转发,而LVS不能,只能针对IP转发

  1. cd /usr/local/nginx/conf/vhosts
  2. # 针对域名
  3. vim lb.conf # 增加内容如下
  4. upstream melody {
  5. server 192.168.31.128:80 weight=2;
  6. server 192.168.31.129:80 weight=1;
  7. }
  8. server {
  9. listen 80;
  10. server_name www.123.com;
  11. location / {
  12. proxy_pass http://melody/;
  13. proxy_set_header Host $host;
  14. }
  15. }
  16. # 启动nginx
  17. /etc/init.d/nginx start
  18. # 测试
  19. curl -xlocalhost:80 www.123.com ??????????????????????????????????????????
  20. curl -x192.168.31.128:80 www.123.com
  21. curl -x192.168.31.129:80 www.123.com
  1. # 针对目录
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注