架构师第五周作业

avatar
作者
猴君
阅读量:0

目录

1、使用keepalived做nginx和haproxy高可用。

1.1 安装keeplived

1.2 Nginx高可用

1.3 haproxy高可用

2、做三个节点的redis集群

 2.1 安装redis

 2.2 修改redis配置

 2.3 创建集群


1、使用keepalived做nginx和haproxy高可用。

1.1 安装keeplived

keepalived的安装可通过包安装或编译安装

例:编译安装

#部署编译环境

root@ubuntu2004:~# apt update && apt -y install make gcc ipvsadm build-essential pkg-config automake autoconf libipset-dev libnl-3-dev libnl-genl-3-dev libssl-dev libxtables-dev libip4tc-dev libip6tc-dev libmagic-dev libsnmp-dev libglib2.0-dev libpcre2-dev libnftnl-dev libmnl-dev libsystemd-dev

#离线或在线下载tar文件并解压缩

root@ubuntu2004:~# wget https://keepalived.org/software/keepalived-2.0.20.tar.gzroot@ubuntu2004:~# tar xvf keepalived-2.0.20.tar.gz -C /usr/local/srcroot@ubuntu2004:~# cd /usr/local/src/keepalived-2.0.20/ #编译 root@ubuntu2004:~# ./configure --prefix=/usr/local/keepalivedroot@ubuntu2004:~# make && make install #查看版本

root@ubuntu2004:~# /usr/local/keepalived/sbin/keepalived -v

Keepalived v2.0.20 (01/22,2020)

Copyright(C) 2001-2020 Alexandre Cassen, <acassen@gmail.com>

Built with kernel headers for Linux 5.4.255
Running on Linux 5.4.0-169-generic #187-Ubuntu SMP Thu Nov 23 14:52:28 UTC 2023

configure options: --prefix=/usr/local/keepalived

Config options:  NFTABLES LVS VRRP VRRP_AUTH OLD_CHKSUM_COMPAT FIB_ROUTING
 

#服务配置文件#下默认源码目录会自动生成unit文件 root@ubuntu2004:~#  cp ./keepalived/keepalived.service /lib/systemd/system/ root@ubuntu2004:~#  cat /usr/lib/systemd/system/keepalived.service [Unit] Description=LVS and VRRP High Availability Monitor After=network-online.target syslog.target Wants=network-online.target [Service] Type=forking PIDFile=/run/keepalived.pid KillMode=process EnvironmentFile=-/usr/local/keepalived/etc/sysconfig/keepalived ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS ExecReload=/bin/kill -HUP $MAINPID [Install] WantedBy=multi-user.target #配置文件root@ubuntu2004:~# mkdir /etc/keepalived root@ubuntu2004:~# cp /usr/local/keepalived/etc/keepalived/keepalived.conf  /etc/keepalived/keepalived.conf#启动服务root@ubuntu2004:~# systemctl enable --now keepalived.service 
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /lib/systemd/system/keepalived.service.

1.2 Nginx高可用

#Nginx配置文件 root@ubuntu2004:~# vim /etc/nginx/conf.d/www.huang.org.conf

upstream webservers {
   server 10.0.0.202:80;
   server 10.0.0.203:80;
}

server {
   listen 10.0.0.199:80;
   server_name www.huang.org;
   location / {
       proxy_pass http://webservers;
      }
}

#两个节点都需要配置

root@ubuntu2004:~# scp /etc/nginx/conf.d/www.huang.org.conf 10.0.0.201:/etc/nginx/conf.d/www.huang.org.conf
 

#Keepalived 配置文件

root@ubuntu2004:~# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   router_id ka1.huang.org   #另一个节点为ka2.huang.org
   vrrp_mcast_group 230.0.0.0
}

vrrp_script check_down {
     fall 3
     rise 2
     timeout 2
}

vrrp_script check_nginx{
     script "/usr/bin/killall -0 nginx"
     interval 1
     weight -30
     fall 3
     rise 2  
     timeout 2
}
include /etc/keepalived/conf.d/*.conf

root@ubuntu2004:~# cat /etc/keepalived/conf.d/www.huang.org.conf 
vrrp_instance VI_1 {
     state MASTER  #另一个节点为backup
     interface eth1
     virtual_router_id 51
     priority 100   #另一个节点为80
     advert_int 1
     authentication {
          auth_type PASS
          auth_pass 123456
     }
     virtual_ipaddress {
         10.0.0.199/24 dev eth0 label eth0:1
     }
     track_interface {
          eth0
     }
     notify_master "/etc/keepalived/notify.sh master"
     notify_backup "/etc/keepalived/notify.sh backup
     notify_fault "/etc/keepalived/notify.sh fault
     track_script {
          check_nginx
     }
}

root@ubuntu2004:~# systemctl restart keepalived.service

1.3 haproxy高可用

#haproxy配置

[root@ka1 ~]#cat /etc/haproxy/haproxy.cfg listen web_http   bind 10.0.0.199:80   server web1 10.0.0.202:80 check   server web2 10.0.0.203:80 check listen stats   mode http   bind 10.0.0.8:9999   stats enable   log global   stats uri     /haproxy-status   stats auth   haadmin:123456 #在两个ka1和ka2两个节点启用内核参数 [root@ka1,2 ~]#vim /etc/sysctl.conf net.ipv4.ip_nonlocal_bind = 1 [root@ka1,2 ~]#sysctl -p #keepalived配置

root@ubuntu2004:~# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   router_id ka1.huang.org #另一个节点为ka2.huang.org
   vrrp_mcast_group 230.0.0.0
}

vrrp_script check_down {
     fall 3
     rise 2
     timeout 2
}

vrrp_script check_haproxy{
     script "/usr/bin/killall -0 haproxy"
     interval 1
     weight -30
     fall 3
     rise 2  
     timeout 2
}
include /etc/keepalived/conf.d/*.conf

root@ubuntu2004:~# cat /etc/keepalived/conf.d/www.huang.org.conf 
vrrp_instance VI_1 {
     state MASTER   #另一个节点为backup
     interface eth1
     virtual_router_id 51
     priority 100      #另一个节点为80
     advert_int 1
     authentication {
          auth_type PASS
          auth_pass 123456
     }
     virtual_ipaddress {
         10.0.0.199/24 dev eth0 label eth0:1
     }
     track_interface {
          eth0
     }
     notify_master "/etc/keepalived/notify.sh master"
     notify_backup "/etc/keepalived/notify.sh backup
     notify_fault "/etc/keepalived/notify.sh fault
     track_script {
          check_haproxy
     }
}

root@ubuntu2004:~# systemctl restart keepalived.service

2、做三个节点的redis集群

2.1 安装redis

准备6台主机

主: 10.0.0.100  10.0.0.200  10.0.0.201

从: 10.0.0.201  10.0.0.202  10.0.0.203

         

例:编译安装

#编译环境

[root@ubuntu2004 ~]#apt update & apt -y install make gcc libjemalloc-dev libsystemd-dev #下载源码 [root@ubuntu2004 ~]#wget http://download.redis.io/releases/redis-6.2.4.tar.gz  #解压安装[root@ubuntu2004 ~]tar xvf redis-6.2.4.tar.gz#编译安装 [root@ubuntu2004 ~]#cd redis-6.2.4/ [root@ubuntu2004redis-6.2.4]#make -j 2 PREFIX=/apps/redis install #指定redis安装目录 #如果支持systemd,需要执行下面 [root@ubuntu2004 redis-6.2.4]#make -j 2 USE_SYSTEMD=yes PREFIX=/apps/redis install #配置环境变量 [root@ubuntu2004 ~]#echo 'PATH=/apps/redis/bin:$PATH' > /etc/profile.d/redis.sh [root@ubuntu2004 ~]#. /etc/profile.d/redis.sh#准备相关目录和配置文件 [root@ubuntu2004 ~]#mkdir /apps/redis/{etc,log,data,run} #创建配置文件、日志、数据等目录 [root@ubuntu2004 ~]#cp redis.conf /apps/redis/etc/

2.2 修改redis配置

每个节点都要修改redis配置

[root@redis-node1 ~]vim /etc/redis.conf bind 0.0.0.0 masterauth 123456   #建议配置,否则后期的master和slave主从复制无法成功,还需再配置 requirepass 123456 cluster-enabled yes  #取消此行注释,必须开启集群,开启后 redis 进程会有cluster标识 cluster-config-file nodes-6379.conf #取消此行注释,此为集群状态数据文件,记录主从关系 及slot范围信息,由redis cluster 集群自动创建和维护 cluster-require-full-coverage no   #默认值为yes,设为no可以防止一个节点不可用导致整 个cluster不可用#或者执行下面命令,批量修改

[root@redis-node1 ~]#sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456' - e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-requirefull-coverage yes/c cluster-require-full-coverage no' /etc/redis.conf#如果是编译安装可以执行下面操作 [root@redis-node1 ~]#sed -i.bak -e '/masterauth/a masterauth 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-requirefull-coverage yes/a cluster-require-full-coverage no' /apps/redis/etc/redis.conf

 2.3 创建集群

#命令redis-cli的选项 --cluster-replicas 1 表示每个master对应一个slave节点,注意:所有节点 数据必须清空 [root@redis-node1 ~]#redis-cli -a 123456 --cluster create 10.0.0.100:6379   10.0.0.200:6379 10.0.0.201:6379   10.0.0.202:6379   10.0.0.203:6379   10.0.0.204:6379 --cluster-replicas 1#观察以上结果,可以看到3master/slave master:10.0.0.100---slave:10.0.0.202master:10.0.0.200---slave:10.0.0.203master:10.0.0.201---slave:10.0.0.204#如果节点少于3个会出下面提示错误 [root@node1 ~]#redis-cli -a 123456 --cluster create 10.0.0.100:6379   10.0.0.200:6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. *** ERROR: Invalid configuration for cluster creation. *** Redis Cluster requires at least 3 master nodes.#使用选项-c 以集群模式连接 [root@centos8 ~]#redis-cli -c -h 10.0.0.100 -a 123456 --no-auth-warning

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!