web1            192.168.1.137    sh keninit.sh安装web


vip             192.168.1.254    ip是虚拟ip


web2            192.168.1.138    sh keninit.sh安装web


mysqlA           192.168.1.140     

mysqlB           192.168.1.141     

mysqlC           192.168.1.145     

Atlas1          192.168.1.146    Atlas+keepalive+lvs

Atlas2          192.168.1.144    Atlas+keepalive+lvs

实验环境:台机器全部是centos6.5环境

以下三台数据库机器140mysqlA141mysqlB145mysqlC操作一模一样

yum install -y  gcc* gcc-c++* autoconf* automake* zlib* libxml* ncurses-devel* libgcrypt* libtool*

wget http://www.cmake.org/files/v2.8/cmake-2.8.4.tar.gz --no-check-certificate

tar -zxvf  cmake-2.8.4.tar.gz

cd  cmake-2.8.4.tar.gz

./configure  && make && make install

mkdir /opt/mysql

mkdir /opt/mysql/data

groupadd mysql

useradd -g mysql mysql

chown mysql:mysql -R /opt/mysql/data

wget http://downloads.mysql.com/archives/get/file/mysql-5.5.13.tar.gz

tar -zxvf  mysql-5.5.13.tar.gz

cd  mysql-5.5.13.tar.gz

cmake -DCMAKE_INSTALL_PREFIX=/opt/mysql \

-DSYSCONFDIR=/opt/mysql/etc \

-DMYSQL_DATADIR=/opt/mysql/data \

-DMYSQL_TCP_PORT=3306 \

-DMYSQL_UNIX_ADDR=/tmp/mysqld.sock \

-DMYSQL_USER=mysql \

-DEXTRA_CHARSETS=all \

-DWITH_READLINE=1 \

-DWITH_SSL=system \

-DWITH_EMBEDDED_SERVER=1 \

-DENABLED_LOCAL_INFILE=1 \

-DWITH_INNOBASE_STORAGE_ENGINE=1

make && make install

mkdir /opt/mysql/log

mkdir /opt/mysql/etc

cp support-files/my-medium.cnf /opt/mysql/etc/my.cnf

chmod 755 scripts/mysql_install_db

scripts/mysql_install_db --user=mysql --basedir=/opt/mysql/ --datadir=/opt/mysql/data/

mkdir /opt/mysql/init.d

cp support-files/mysql.server /opt/mysql/init.d/mysql

chmod +x /opt/mysql/init.d/mysql

 /opt/mysql/init.d/mysql start

 find / -name mysqld.sock

 /tmp/mysqld.sock

 /opt/mysql/bin/mysql -S /tmp/mysqld.sock -P 3306

 /opt/mysql/bin/mysqladmin -u root password '123456'   #设置mysql数据库密码

 /opt/mysql/bin/mysql -uroot -p'123456'               #登陆mysql数据库

[root@localhost mysql-5.5.13]# grep -Ev "^#|^$" /opt/mysql/etc/my.cnf

[client]

port        = 3306

socket        = /tmp/mysqld.sock

[mysqld]

port        = 3306

socket        = /tmp/mysqld.sock

basedir         = /opt/mysql/                 ############报错的解决方法

datadir         = /opt/mysql/data/             ###########报错的解决方法

skip-external-locking

key_buffer_size = 16M

max_allowed_packet = 1M

table_open_cache = 64

sort_buffer_size = 512K

net_buffer_length = 8K

read_buffer_size = 256K

read_rnd_buffer_size = 512K

myisam_sort_buffer_size = 8M

log-bin=mysql-bin

binlog_format=mixed

server-id    = 1

[mysqldump]

quick

max_allowed_packet = 16M

[mysql]

no-auto-rehash

[myisamchk]

key_buffer_size = 20M

sort_buffer_size = 20M

read_buffer = 2M

write_buffer = 2M

[mysqlhotcopy]

interactive-timeout

 

 

1192.168.1.140mysqlA

[root@localhost ~]#   /opt/mysql/bin/mysql -uroot -p'123456'

mysql>  GRANT REPLICATION SLAVE ON *.* to 'rep1'@'192.168.1.141' identified by "123456";   #mysqlB授权

mysql>  GRANT REPLICATION SLAVE ON *.* to 'rep1'@'192.168.1.145' identified by "123456";

mysql>  grant all on *.* to per1@'192.168.1.146' identified by '123456';     #所有主从都要给Atlas数据库授权,不然提示down的状态

mysql> flush  privileges;

mysql> show master status;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000003 |      699 |              |                  |

+------------------+----------+--------------+------------------+

##记录下 FILE  Position 的值,在后面进行从服务器操作的时候需要用到。

 

 

2192.168.1.141/145mysqlB/C从里

[root@localhost ~]#   /opt/mysql/bin/mysql -uroot -p'123456'

mysql>  grant all on *.* to per1@'192.168.1.146' identified by '123456';  #所有主从都要给Atlas数据库授权,不然提示down的状态

mysql> flush  privileges;

[root@localhost ~]# grep  server-id /opt/mysql/etc/my.cnf

server-id    = 10          ###145的改为server-id =9                    

#server-id       = 2

[root@localhost ~]# /opt/mysql/init.d/mysql restart

Shutting down MySQL.                                       [确定]

Starting MySQL..                                           [确定]

[root@localhost ~]# /opt/mysql/bin/mysql -uroot -p'123456'

mysql> change master to master_host="192.168.1.140", master_user="rep1", master_password="123456", master_log_file="mysql-bin.000003", master_log_pos=699;

mysql>  start slave;

Query OK, 0 rows affected (0.01 sec)                                                 ####注意防火墙和selinux都关掉或者开放相应的端口

 

mysql> show slave status\G

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.1.140

                  Master_User: rep1

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000005

          Read_Master_Log_Pos: 107

               Relay_Log_File: localhost-relay-bin.000005

                Relay_Log_Pos: 253

        Relay_Master_Log_File: mysql-bin.000005

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB:

          Replicate_Ignore_DB:

           Replicate_Do_Table:

       Replicate_Ignore_Table:

      Replicate_Wild_Do_Table:

  Replicate_Wild_Ignore_Table:

                   Last_Errno: 0

                   Last_Error:

                 Skip_Counter: 0

          Exec_Master_Log_Pos: 107

              Relay_Log_Space: 413

              Until_Condition: None

               Until_Log_File:

                Until_Log_Pos: 0

           Master_SSL_Allowed: No

           Master_SSL_CA_File:

           Master_SSL_CA_Path:

              Master_SSL_Cert:

            Master_SSL_Cipher:

               Master_SSL_Key:

        Seconds_Behind_Master: 0

Master_SSL_Verify_Server_Cert: No

                Last_IO_Errno: 0

                Last_IO_Error:

               Last_SQL_Errno: 0

               Last_SQL_Error:

  Replicate_Ignore_Server_Ids:

             Master_Server_Id: 1

1 row in set (0.00 sec)

3验证:在主192.168.1.140

[root@localhost log]#  /opt/mysql/bin/mysql -uroot -p'123456'

mysql>  create database mydatabases;

Query OK, 1 row affected (0.00 sec)

 

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| first_db           |

| mydatabases        |

| mysql              |

| performance_schema |

| test               |

+--------------------+

6 rows in set (0.00 sec)

 

在从数据库里192.168.1.141里查看

 

[root@localhost log]#  /opt/mysql/bin/mysql -uroot -p'123456'

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| first_db           |

| mydatabases        |

| mysql              |

| performance_schema |

| test               |

+--------------------+

6 rows in set (0.00 sec)

OK,至此主从同步完成。

4以下是常见的主从错误

1.网络不通

2.密码不对

3.pos不正确

4.ID问题

5.防火墙开端口和selinux关掉

补充:

双方的机器防火墙是否策略有限制。

ID的问题,在安装完mysql数据库的时候默认他们的server-id=1 但是在做主从同步的时候需要将ID 号码设置不一样才行。

 

二、安装Atlas

1、安装atlas

yum install -y pkg-config libevent*  glib lua

wget ftp://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz && tar zxvf  autoconf-2.69.tar.gz && cd autoconf-2.69 &&  ./configure && make && make install

wget http://ftp.gnu.org/gnu/automake/automake-1.13.2.tar.gz  &&  tar zxvf  automake-1.13.2.tar.gz  && cd automake-1.13.2 &&  ./configure && make && make install

 yum install -y libffi-devel

wget http://ftp.gnome.org/pub/gnome/sources/glib/2.36/glib-2.36.3.tar.xz && tar xvf glib-2.36.3.tar.xz && cd glib-2.36.3 && ./configure

export PKG_CONFIG_PATH=/usr/bin/pkg-config

make && make install

Atlas包下载到本地,再从本地上传到服务器里

wget  --no-check-certificate  https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm

2、修改test.cnf配置文件

vim  /usr/local/mysql-proxy/conf/test.cnf

[root@localhost conf]# /usr/local/mysql-proxy/bin/encrypt 123456     

/iZxz+0GRoA=                            #生成密钥

 

[root@localhost conf]# cat test.cnf

[mysql-proxy]

 

##号的为非必需的配置项目

 

#管理接口的用户名

admin-username = user

 

#管理接口的密码

admin-password = pwd

 

#实现管理接口的Lua脚本所在路径

admin-lua-script = /usr/local/mysql-proxy/lib/mysql-proxy/lua/admin.lua

 

#Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔

proxy-backend-addresses = 192.168.1.140:3306

 

#Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分隔

proxy-read-only-backend-addresses = 192.168.1.141:3306@1,192.168.1.145:3306@1

 

#用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密

pwds = root:/iZxz+0GRoA=                     ##和上面的密钥对应

 

#设置Atlas的运行方式,设为true时为守护进程方式,设为false时为前台方式,一般开发调试时设为false,线上运行时设为true

daemon = true

 

#设置Atlas的运行方式,设为trueAtlas会启动两个进程,一个为monitor,一个为workermonitorworker意外退出后会自动将其重启,设为false时只有worker,没有monitor,一般开发调试时设为false,线上运行时设为true

keepalive = true

 

#工作线程数,推荐设置与系统的CPU核数相等

event-threads = 4

 

#日志级别,分为messagewarningcriticalerrordebug五个级别

log-level = message

 

#日志存放的路径

log-path = /usr/local/mysql-proxy/log                   #日志查找的地方

 

#SQL日志的开关,可设置为OFFONREALTIMEOFF代表不记录SQL日志,ON代表记录SQL日志,REALTIME代表记录SQL日志且实时写入磁盘,默认为OFF

#sql-log = OFF

 

#实例名称,用于同一台机器上多个Atlas实例间的区分

instance = test

 

#Atlas监听的工作接口IP和端口

proxy-address = 0.0.0.0:1234

 

#Atlas监听的管理接口IP和端口

admin-address = 0.0.0.0:2345

 

#分表设置,此例中person为库名,mt为表名,id为分表字段,3为子表数量,可设置多项,以逗号分隔,若不分表则不需要设置该项

#tables = person.mt.id.3

 

#默认字符集,若不设置该项,则默认字符集为latin1

charset = utf8

 

#允许连接Atlas的客户端的IP,可以是精确IP,也可以是IP段,以逗号分隔,若不设置该项则允许所有IP连接,否则只允许列表中的IP连接

#client-ips = 127.0.0.1, 192.168.1

 

#Atlas前面挂接的LVS的物理网卡的IP(注意不是虚IP),若有LVS且设置了client-ips则此项必须设置,否则可以不设置

#lvs-ips = 192.168.1.1

3、启动Atlas

/usr/local/mysql-proxy/bin/mysql-proxyd test start

4、查看Atlas进程

ps aux | grep mysql-proxy | grep -v grep

5、在mysqlABC主和从数据库里查看Atlas

 /opt/mysql/bin/mysql -uuser -ppwd  -h192.168.1.144 -P2345   #查看Atlas2

mysql> select * from backends;

+-------------+--------------------+-------+------+

| backend_ndx | address            | state | type |

+-------------+--------------------+-------+------+

|           1 | 192.168.1.140:3306 | up    | rw   |

|           2 | 192.168.1.141:3306 | up    | ro   |

|           3 | 192.168.1.145:3306 | up    | ro   |

+-------------+--------------------+-------+------+

3 rows in set (0.00 sec)

 

 /opt/mysql/bin/mysql -uuser -ppwd  -h192.168.1.146 -P2345   #查看Atlas1

mysql> select * from backends;

+-------------+--------------------+-------+------+

| backend_ndx | address            | state | type |

+-------------+--------------------+-------+------+

|           1 | 192.168.1.140:3306 | up    | rw   |

|           2 | 192.168.1.141:3306 | up    | ro   |

|           3 | 192.168.1.145:3306 | up    | ro   |

+-------------+--------------------+-------+------+

3 rows in set (0.00 sec)

mysql> remove backend  3;                               ###下线数据库

Empty set (0.00 sec)

 

mysql> select * from backends;

+-------------+--------------------+-------+------+

| backend_ndx | address            | state | type |

+-------------+--------------------+-------+------+

|           1 | 192.168.1.140:3306 | up    | rw   |

|           2 | 192.168.1.141:3306 | up    | ro   |

+-------------+--------------------+-------+------+

2 rows in set (0.00 sec)

 

mysql> add slave 192.168.1.145:3306                        ###上线数据库

    -> ;

Empty set (0.00 sec)

 

mysql> select * from backends;

+-------------+--------------------+-------+------+

| backend_ndx | address            | state | type |

+-------------+--------------------+-------+------+

|           1 | 192.168.1.140:3306 | up    | rw   |

|           2 | 192.168.1.141:3306 | up    | ro   |

|           3 | 192.168.1.145:3306 | up    | ro   |

+-------------+--------------------+-------+------+

3 rows in set (0.00 sec)

 

mysql> set online 3;

+-------------+--------------------+---------+------+

| backend_ndx | address            | state   | type |

+-------------+--------------------+---------+------+

|           3 | 192.168.1.145:3306 | unknown | ro   |

+-------------+--------------------+---------+------+

1 row in set (0.00 sec)

 

mysql> select * from backends;

+-------------+--------------------+-------+------+

| backend_ndx | address            | state | type |

+-------------+--------------------+-------+------+

|           1 | 192.168.1.140:3306 | up    | rw   |

|           2 | 192.168.1.141:3306 | up    | ro   |

|           3 | 192.168.1.145:3306 | up    | ro   |

+-------------+--------------------+-------+------+

3 rows in set (0.00 sec)

6192.168.1.145mysqlc机器上

#模拟故障

[root@localhost log]# netstat -utnalp | grep mysql

tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      4284/mysqld         

tcp        0      0 192.168.1.145:54292         192.168.1.140:3306          ESTABLISHED 4284/mysqld         

[root@localhost log]# kill 4284

[root@localhost log]# netstat -utnalp | grep mysql

[root@localhost log]# ps aux | grep mysql

root      4691  0.0  0.1 103256   856 pts/0    S+   15:50   0:00 grep mysql

[root@localhost log]# /opt/mysql/bin/mysql -uuser -ppwd  -h192.168.1.144 -P2345

mysql> select * from backends;

+-------------+--------------------+-------+------+

| backend_ndx | address            | state | type |

+-------------+--------------------+-------+------+

|           1 | 192.168.1.140:3306 | up    | rw   |

|           2 | 192.168.1.141:3306 | up    | ro   |

|           3 | 192.168.1.145:3306 | down  | ro   |                      

###可以看到192.168.1.145已经down了,这个要把Atlas的配置文件

vim  /usr/local/mysql-proxy/conf/test.cnf里面的从数据库的优先级调成一样时看的更快。

#proxy-read-only-backend-addresses = 192.168.1.141:3306@1,192.168.1.145:3306@1

其中@1为优先级

192.168.1.145数据库启动

[root@localhost log]# /opt/mysql/init.d/mysql restart           ###恢复故障

Shutting down MySQL.                                       [确定]

Starting MySQL..                                           [确定]

[root@localhost log]# /opt/mysql/bin/mysql -uuser -ppwd  -h192.168.1.144 -P2345

mysql> select * from backends;

+-------------+--------------------+-------+------+

| backend_ndx | address            | state | type |

+-------------+--------------------+-------+------+

|           1 | 192.168.1.140:3306 | up    | rw   |

|           2 | 192.168.1.141:3306 | up    | ro   |

|           3 | 192.168.1.145:3306 | up    | ro   |

+-------------+--------------------+-------+------+          ####可以看到已经OK.

3 rows in set (0.00 sec)

 

7、连接自己主从数据库的命令

[root@localhost bin]#   /opt/mysql/bin/mysql  -uroot -p123456

[root@localhost bin]#   /opt/mysql/init.d/mysql restart/stop/start  

 

三、以下是架构的搭建

Mysql主从:一主三从

Atlas:2个节点做HA

Keepalived:提供VIP防止Atlas单点故障

 

1、安装keepalived

wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gz

tar zxvf keepalived-1.2.13.tar.gz -C /opt/

cd /opt/keepalived-1.2.13

./configure --prefix=/usr/local/keeplived

make && make  install

 

2新建一个配置文件,默认情况下keepalived启动时会去/etc/keepalived

[root@localhost ~]# mkdir -p /etc/keepalived

[root@localhost ~]# cp -r /usr/local/keeplived/etc/keepalived/keepalived.conf  /etc/keepalived/

 

 

3编辑配置文件

#Atlas11.146)上

 

[root@localhost ~]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

   notification_email {

     837337164@qq.com

   }

   notification_email_from 837337164@qq.com

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id Atlas_ha

}

 

vrrp_instance Atlas_ha {

    state master

    interface eth0

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.1.254

    }

}

 

virtual_server 192.168.1.254 80 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    nat_mask 255.255.255.0

    persistence_timeout 50

    protocol TCP

 

    real_server 192.168.1.137 80 {

        weight 1

        notify_down /usr/local/mysql-proxy/bin/Atlas.sh

        TCP_CHECK {

            connect_timeout 10

            nb_get_retry 3

            delay_before_retry 3

            connect_port 80

            }

       }

    real_server 192.168.1.138 80 {

        weight 1

        notify_down /usr/local/mysql-proxy/bin/Atlas.sh

        TCP_CHECK {

            connect_timeout 10

            nb_get_retry 3

            delay_before_retry 3

            connect_port 80

        }

    }

}

 

 

 

#Atlas21.144)与atlas1几乎一致,如下:

[root@localhost ~]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

   notification_email {

     837337164@qq.com

   }

   notification_email_from 837337164@qq.com

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id Atlas_ha

}

 

vrrp_instance Atlas_ha {

    state BACKUP

    interface eth0

    virtual_router_id 51

    priority 50

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.1.254

    }

}

 

virtual_server 192.168.1.254 80 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    nat_mask 255.255.255.0

    persistence_timeout 50

    protocol TCP

 

    real_server 192.168.1.137 80 {

        weight 1

        notify_down /usr/local/mysql-proxy/bin/Atlas.sh

        TCP_CHECK {

            connect_timeout 10

            nb_get_retry 3

            delay_before_retry 3

            connect_port 80

            }

      }

    real_server 192.168.1.138 80 {

        weight 1

        notify_down /usr/local/mysql-proxy/bin/Atlas.sh

        TCP_CHECK {

            connect_timeout 10

            nb_get_retry 3

            delay_before_retry 3

            connect_port 80

        }

    }

}

4创建notify_down脚本

[root@localhost bin]# cat /usr/local/mysql-proxy/bin/Atlas.sh

#!/bin/sh

pkill  keepalived

 

[root@localhost bin]# chmod +x  /usr/local/mysql-proxy/bin/Atlas.sh

 

启动keepalived

[root@localhost sbin]# /usr/local/keeplived/sbin/keepalived -D

[root@localhost sbin]# ps aux | grep keepalived | grep -v grep

root      5151  0.0  0.1  39980   776 ?        Ss   17:48   0:00 /usr/local/keeplived/sbin/keepalived -D

root      5152  0.1  0.4  42084  2068 ?        S    17:48   0:00 /usr/local/keeplived/sbin/keepalived -D

root      5153  0.2  0.2  42084  1296 ?        S    17:48   0:00 /usr/local/keeplived/sbin/keepalived -D

停止keepalived

[root@localhost sbin]# kill  5151

4查看keepalived日志,解决问题思路

tail -f  /var/log/messages

四、在192.168.1.146Atlas1)和192.168.1.144(Atlas2)上分别安装LVS

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

[root@localhost ~]# 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.1.254:80 rr persistent 50

  -> 192.168.1.137:80             Route   1      0          0         

  -> 192.168.1.138:80             Route   1      0          0     

[root@localhost ~]#  ip addr list  #用这个指令可以看到vip192.168.1.254在那台Atlas

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:ec:ff:f2 brd ff:ff:ff:ff:ff:ff

    inet 192.168.1.146/24 brd 192.168.1.255 scope global eth0

    inet 192.168.1.254/32 scope global eth0                       ###虚拟ip                             

    inet6 fe80::20c:29ff:feec:fff2/64 scope link

       valid_lft forever preferred_lft forever

 

 

 

 

 

五、192.168.1.137(web1)192.168.1.138(web2)服务器安装虚拟vipip脚本

1、安装脚本

[root@localhost ~]# cat lvs-client.sh

. /etc/rc.d/init.d/functions

VIP=(

192.168.1.254

)

function start(){

  for ((i=0;i<`echo ${#VIP[*]}`;i++))

      do

       echo ${i}  ${VIP[$i]}

       ifconfig lo:${i} ${VIP[$i]} netmask 255.255.255.255 up

       route add -host ${VIP[$i]} dev lo

       done

echo "1">/proc/sys/net/ipv4/conf/lo/arp_ignore

echo "2">/proc/sys/net/ipv4/conf/lo/arp_announce

echo "1">/proc/sys/net/ipv4/conf/all/arp_announce

echo "2">/proc/sys/net/ipv4/conf/all/arp_announce

}

function  stop(){

  for ((i=0;i<${#VIP[*]};i++))

      do

       echo ${i}  ${VIP[$i]}

       ifconfig lo:${i} ${VIP[$i]} netmask 255.255.255.255 up

       route del -host ${VIP[$i]} dev lo:${i}

       done

}

case "$1" in

   start)

       start

       exit

       ;;

   stop)

       stop

       exit

       ;;

   *)

       echo "You must use $0:stop|start"

       ;;

esac

chmod +x   lvs-client.sh

sh  lvs-client.sh  start

[root@localhost ~]# ifconfig

eth0      Link encap:Ethernet  HWaddr 00:0C:29:20:F6:A4

          inet addr:192.168.1.137  Bcast:192.168.1.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe20:f6a4/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:115144 errors:0 dropped:0 overruns:0 frame:0

          TX packets:1912 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:10232034 (9.7 MiB)  TX bytes:131917 (128.8 KiB)

 

lo        Link encap:Local Loopback

          inet addr:127.0.0.1  Mask:255.0.0.0

          inet6 addr: ::1/128 Scope:Host

          UP LOOPBACK RUNNING  MTU:16436  Metric:1

          RX packets:16 errors:0 dropped:0 overruns:0 frame:0

          TX packets:16 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0

          RX bytes:960 (960.0 b)  TX bytes:960 (960.0 b)

 

lo:0      Link encap:Local Loopback

          inet addr:192.168.1.254  Mask:255.255.255.255        ####可以看到虚拟网卡已经有了

          UP LOOPBACK RUNNING  MTU:16436  Metric:1

 

 

[root@localhost ~]# route -n

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.168.1.254   0.0.0.0         255.255.255.255 UH    0      0        0 lo

192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0

0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0

2、编写测试文件

[root@localhost ~]# cat /var/www/html/index.html

<h1>RS1</h1>

[root@localhost ~]#firefox  &

[root@localhost ~]# scp -r lvs-client.sh   root@192.168.1.138:/root

web2,192.168.1.138

[root@localhost ~]# sh lvs-client.sh  start

[root@localhost ~]#ifconfig

[root@localhost ~]#route -n

[root@localhost ~]# cat /var/www/html/index.html

<h1>RS2</h1>

[root@localhost ~]#firefox  &

六、模拟故障

1、停止192.168.1.138上面的http

service  httpd stop

2、在192.168.1.146Atlas1上面查看

[root@localhost ~]# 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.1.254:80 rr persistent 50

  -> 192.168.1.137:80             Route   1      0          1   

3、测试,现在192.168.1.144Atlas2

[root@localhost ~]# curl http://192.168.1.254

<h1>RS1</h1>

4、重启192.168.1.138http

service httpd  start

在看192.168.1.146Atlas1

[root@localhost ~]# 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.1.254:80 rr persistent 50

  -> 192.168.1.137:80             Route   1      0          0         

  -> 192.168.1.138:80             Route   1      0          0  

5、关闭192.168.1.146Atlas1上的keepalived

[root@localhost ~]# ps aux | grep keepalived | grep -v grep

root      8173  0.0  0.1  39980   772 ?        Ss   14:14   0:00 /usr/local/keeplived/sbin/keepalived -D

root      8174  0.0  0.4  42208  2080 ?        S    14:14   0:00 /usr/local/keeplived/sbin/keepalived -D

root      8175  0.0  0.2  42084  1296 ?        S    14:14   0:00 /usr/local/keeplived/sbin/keepalived -D

[root@localhost ~]# kill 8173

192.168.1.144Atlas2上面查看

[root@localhost ~]# /usr/local/keeplived/sbin/keepalived -D

[root@localhost ~]# 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.1.254:80 rr persistent 50

  -> 192.168.1.137:80             Route   1      0          0         

  -> 192.168.1.138:80             Route   1      0          0       

[root@localhost ~]# ip addr list

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:bc:de:b4 brd ff:ff:ff:ff:ff:ff

    inet 192.168.1.144/24 brd 192.168.1.255 scope global eth0

    inet 192.168.1.254/32 scope global eth0                                    ###此时192.168.1.254VIP已经被Atlas2拿到了。

    inet6 fe80::20c:29ff:febc:deb4/64 scope link

       valid_lft forever preferred_lft forever

192.168.1.146Atlas1

[root@localhost ~]#  curl http://192.168.1.254

<h1>RS2</h1>                                                    ###可以访问