1、结合图形描述LVS的工作原理;
LVS有四种集群类型,分别是:
(三)lvs-tun:不修改请求报文的IP首部(源IP为CIP,目标IP为VIP),而是源IP首部之外在封装一个IP首部(源IP为DIP,目标IP为RIP)
(四)lvs-fullnat:通过同时修改请求报文的源IP地址(CIP–>DIP)和目标IP地址(VIP–>RIP)进行转发
2、搭建一套LVS-DR模型的高性能集群,并实现以下功能:
(1)、wordpress程序通过nfs共享给各个realserver;
(2)、后端realserver中的nginx和php分离
# 创建共享的目录,上传WordPress源码
mkdir -pv /apps/php/
unzip wordpress.zip
mv wordpress/* ./
# 创建nginx用户和组
groupadd -g 985 nginx
useradd -u 990 -g nginx nginx
# 设置共享目录的访问权限,使得在Nginx服务器上的nginx进程可以访问共享目录,具有对应的读写权限
chown -R nginx php
# 编辑/etc/exports文件
/apps/php 192.168.23.0/24(rw,no_root_squash)
# 让内核重读共享目录的配置文件
exportfs -r
# 编辑/etc/php-fpm.d/www.conf配置文件
listen = 192.168.23.13:9000
;listen.allowed_clients = 127.0.0.1
user = nginx
group = nginx
# 启动php和NFS服务
systemctl start php-fpm nfs
# 修改WordPress配置文件
mv /apps/php/wp-config-sample.php /apps/php/wp-config.php
vi /apps/php/wp-config.php
/** WordPress数据库的名称 */
define('DB_NAME', 'wp');
/** MySQL数据库用户名 */
define('DB_USER', 'wp_user');
/** MySQL数据库密码 */
define('DB_PASSWORD', '34958729348570~');
/** MySQL主机 */
define('DB_HOST', '192.168.23.14');
# 启动mariadb
systemctl start mariadb
mysql_secure_installation
mysql -uroot -p
create database wp;
grant all on wp.* to wp_user@'192.168.23.%' identified by '394584723975~';
flush privileges;
select user,host,password from mysql.user;
显示:wp_user | 192.168.23.% | *DD5143776C2C77C09429E28D2F1784F06C39F2A4
# 编辑/etc/nginx/nginx.conf配置文件
listen 80;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
root /apps/php/;
index index.php index.html index.htm;
}
location ~ \.php$ {
fastcgi_pass 192.168.23.13:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /apps/php/$fastcgi_script_name;
include fastcgi_params;
}
# 将配置文件拷贝一份到Nginx2对应的目录中
scp -p /etc/nginx/nginx.conf root@192.168.23.12:/etc/nginx/nginx.conf
# 在本地文件系统上创建/apps/php/{index.html,index.php}
mkdir -pv /apps/php
touch /apps/php/{index.html,index.php}
# 查看php-NFS服务器的共享目录
showmount -e 192.168.23.13
显示:Export list for 192.168.23.13:
/apps/php 192.168.23.0/24
# 挂载共享目录
mount -t nfs 192.168.23.13:/apps/php/ /apps/php
# 查看挂载
mount
# 开启nginx 服务
nginx
# 如果Nginx是编译安装的,在/etc/init.d/nginx写一个Nginx服务脚本,实现chkconfig管理开启自启
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/opt/application/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/opt/application/nginx/conf/nginx.conf"lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
在这里已经实现了共享存储,下面的步骤就是配置IPVS-DR模型
# 给enp0s3网卡设置一个别名,别名的IP地址为VIP, 使得VIP可以响应请求,但是不对本地进行广播,因此可以避免IP冲突
ifconfig enp0s3:0 192.168.23.102 netmask 255.255.255.255 broadcast 192.168.23.102
# 设置ipvs-dr规则
ipvsadm -A -t 192.168.23.102:80 -s rr
ipvsadm -a -t 192.168.23.102:80 -r 192.168.23.11 -g -w 1
ipvsadm -a -t 192.168.23.102:80 -r 192.168.23.12 -g -w 2
# 写一个脚本控制arp_ignore和arp_announce的值,enp0s3和lo两个接口都要进行控制,arp.sh脚本如下
#!/bin/bash
#
case $1 in
start)
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
;;
stop)
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
;;
esac
# 运行脚本
bash arp.sh start
# 在设置了接口的arp_ignore和arp_announce值之后,再在lo本地网卡添加别名,绑定VIP
ifconfig lo:0 192.168.23.102 netmask 255.255.255.255 broadcast 192.168.23.102
# 给进来的报文设置一个入栈路由条目,入口和出口为 192.168.23.102 这样就使得目标IP为lo:0的IP,即VIP,而不会是RIP
route add -host 192.168.23.102 dev lo:0
3:在第二题的基础上增加一个keepalived 高可用ipvs集群
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node1
vrrp_mcast_group4 224.0.0.18
}
vrrp_instance VI_1 {
# 这里如果是备用的Director,要改为BACKUP
state MASTER
interface enp0s3
virtual_router_id 51
# 这里如果是备用的Director,要改为优先级比100低,取90
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
# 这里需要改为后端的RS定义的vip
192.168.23.102
}
notify_master "/root/notify.sh master"
notify_backup "/root/notify.sh backup"
notify_fault "/root/notify.sh fault"
}
# 这里的virtual_server的IP就是两个高可用主机间漂移的虚拟IP
virtual_server 192.168.23.102 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
sorry_server 127.0.0.1 80
real_server 192.168.23.11 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.23.12 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
4、基于heartbeat v2 crm实现HA LAMP组合;要求,部署wordpress,用于编辑的文章中的任何数据在节点切换后都能正常访问;
yum install -y net-snmp-libs libnet PyXML pygtk2-libglade
rpm -ivh heartbeat-2.1.4-12.el6.x86_64.rpm heartbeat-pils-2.1.4-12.el6.x86_64.rpm heartbeat-stonith-2.1.4-12.el6.x86_64.rpm
cp /usr/share/doc/heartbeat-2.1.4/{ha.cf,haresources,authkeys} /etc/ha.d/
chmod 600 /etc/ha.d/authkeys
auth 2
2 sha1 WfpBxC/WzXIWsqt5zNVMTA
logfile /var/log/heartbeat
mcast eth0 225.0.0.1 694 1 0
node node1
node node2
ping 192.168.23.1
crm on
scp -p /etc/ha.d/{authkeys,ha.cf} node2:/etc/ha.d/
rpm -ivh heartbeat-gui-2.1.4-12.el6.x86_64.rpm
yum groupinstall -y "X Window System" "Desktop"
echo "yhy3426356" | passwd --stdin hacluster
service heartbeat start
hb_gui &
mkdir -vp /mydata/{data,html}
groupadd -r -g 306 mysql
useradd -r -g mysql -u 306 mysql
chown -R mysql.mysql /mydata/data/
vi /etc/exports
/mydata 192.168.23.0/24(rw,no_root_squash)
exportfs -avr
cd /mydata/html/ 上传WordPress源码包到此目录,解压
service nfs start
在WordPress的配置文件中设置数据库登入等信息
groupadd -r -g 306 mysql
useradd -r -g mysql -u 306 mysql
tar xf mariadb-5.5.54-linux-x86_64.tar -C /usr/local
解压mariadb二进制包到/usr/local下cd /usr/local/
ln -vs mariadb-5.5.54-linux-x86_64 mysql
chown -R root.mysql mysql/*
cd mysql
cp ./support-files/my-large.cnf /etc/my.cnf
cp ./support-files/mysql.server /etc/init.d/mysqld
vi /etc/my.cnf
skip_name_resolve = ON
datadir = /mydata/data
innodb_file_per_table = ON
mkdir -pv /mydata/{data,html}
mount -t nfs 192.168.23.33:/mydata /mydata
./scripts/mysql_install_db --datadir=/mydata/data/ --user=mysql
service mysqld start
登入mysql,创建一个wp数据库,并且授权用户wp_user可以远程登入
除了对数据目录初始化外,其他都和node1操作一样
并测试mysql可以登入成功
5、基于LVS-NAT模型,设计一套负载均衡集群,使用原地址哈希调度策略实现会话保存,使用NFS实现RS的共享存储
试验步骤
yum
安装好,并且设置对应的IP地址ipvsadm -A -t 192.168.23.12:80 -s sh
ipvsadm -a -t 192.168.23.12:80 -r 192.168.10.11:80 -m -w 1
ipvsadm -a -t 192.168.23.12:80 -r 192.168.10.22:80 -m -w 1
# 需要修改的部分
server {
listen 80;
# server_name _;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
root /apps/php/;
index index.php index.html index.htm;
}
location ~ \.php$ {
fastcgi_pass 192.168.10.33:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /apps/php/$fastcgi_script_name;
include fastcgi_params;
}
# 创建nginx用户和nginx组,且uid 和 gid 应与php-fpmbash服务器的nginx的uid 和 gid 一样
groupadd -g 1000 nginx
useradd -u 1000 -g nginx nginx
# 编辑/etc/exports文件,设置共享目录和允许挂载的客户端IP地址,并且设置权限
/apps/php 192.168.10.0/24(rz,no_root_squash)
# 让内核重读配置文件
exportfs -r
# 修改服务监听的IP地址
listen = 192.168.10.33:9000
# 将允许访问的客户端这一行指令注释,表示任何主机都可以访问
;listen.allowed_clients = 127.0.0.1
# 让php-fpm服务以nginx的属主和主组的身份运行
user = nginx
group = nginx