mysql02

Haproxy 负载均衡搭建

一主二从一个负载均衡节点

主:192.168.239.140
负载均衡:192.168.239.141
从一:192.168.239.142
从二:192.168.239.144

1.创建用户,并赋予权限
master

create user 'test'@'192.168.239.%' identified by 'test';
grant all on *.* to 'test'@'192.168.239.%';

编译

yum install gcc-c++

make TARGET=linux26(内核版本号)
make install PREFIX=/usr/local/haproxy

创建配置文件

cd /usr/local/haproxy
mkdir conf
vi haproxy.cnf

如下

 1 global
 2     daemon     # 后台方式运行
 3     nbproc 1
 4           pidfile /usr/local/haproxy/conf/haproxy.pid
 5 
 6 defaults
 7     mode tcp  #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
 8     retries 2    #两次连接失败就认为是服务器不可用,也可以通过后面设置
 9     option redispatch    #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器
10     option abortonclose    #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
11     maxconn 4096   #默认的最大连接数
12     timeout connect 5000ms   #连接超时
13     timeout client 30000ms      #客户端超时
14     timeout server 30000ms     #服务器超时
15     #timeoutcheck 2000    #=心跳检测超时
16     log 127.0.0.1 local0 err#[err warning info debug]
17 
18 #test1
19 listen test1   #这里是配置负载均衡,test1是名字,可以任意
20     bind 0.0.0.0:33060        #这里是监听的IP地址和端口,端口号可以在0-65535之间,要避免端口冲突
21     mode tcp    #连接的协议,这里是tcp协议
22     #maxconn 4086
23     #log 127.0.0.1 local0 debug
24     server s1 192.168.239.142:3306    #负载的机器
25     server s2 192.168.239.144:3306   #负载的机器,负载的机器可以有多个,往下排列即可

启动haproxy

../sbin/haporxy -f /usr/local/haproxy/conf/haproxy.cnf

haproxy算法

有如下8种
一、roundrobin,表示简单的轮询,这个不多说,这个是负载均衡基本都具备的;
二、static-rr,表示根据权重;
三、leastconn,表示最少连接者先处理;
四、source,表示根据请求源IP;
五、uri,表示根据请求的URI;
六、url_param,表示根据请求的URl参数'balance url_param' requires an URL parameter name
七、hdr(name),表示根据HTTP请求头来锁定每一次HTTP请求;
八、rdp-cookie(name),表示根据据cookie(name)来锁定并哈希每一次TCP请求。

例:balance source

监控

vi haproxy.cnf

listen admin_stats
    bind:0.0.0.0:8888
    mode http
    stats uri /test_haproxy
    stats auth admin:admin

http://192.168.239.141:8888/test_haproxy  

  username:admin password:admin

Status   not check 方法

server s1 192.168.239.142:3306 check port 3306
server s2 192.168.239.144:3306 check port 3306

Keepalived高可用集群搭建

1.主主复制

主1 192.168.239.140
主2 192.168.239.145

首先关闭防火墙

service iptables stop

主2

show master status;

file
mysql-bin.000009 Position 154

主1

change master to master_host = '192.168.239.145',master_port=3306,master_user='repl',master_password='repl',master_log_file='mysql-bin.000009',master_log_pos=154

start slave

show master status

file
mysql-bin.000008 Position 154

主2

change master to master_host = '192.168.239.145',master_port=3306,master_user='repl',master_password='repl',master_log_file='mysql-bin.000009',master_log_pos=154

start slave;

2.Keepalive搭建

上传keepalive 的压缩包

1 tar -zxvf keepalived-1.3.5.tar.gz
2 cd keepalived
3 yum install gcc-c++
4 yum install openssl openssl-devel
5 
6 ./configure
7 make && make install

配置文件及可执行文件位置

配置文件
cd /usr/lcoal/etc/keepalived
vi keepalived.conf

可运行程序
pwd
/usr/local/sbin

主1

vi keepalived.conf

global_defs {
     router_id LVS_MASTER
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.239.150/24   //虚拟ip
         }
}

主2

global_defs {
     router_id LVS_SLAVE
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.239.150/24  //虚拟ip
         }
}

启动

cd ../../sbin
./keepalived -D -f /usr/local/etc/keepalived/keepalived.conf

手动把主1 kill -9 keepalive的id 号后, 虚拟ip会自动转换绑定到主2上

配置文件的信息

 1 global_defs {         
 2      router_id LVS_SLAVE  #router_id  安装keeplieved设置不同即可
 3 }
 4 
 5 vrrp_instance VI_1 { 
 6     state BACKUP      #MASTER 和 BACKUP 两种状态
 7     interface eth0    
 8     virtual_router_id 51   #虚拟路由id节点
 9     priority 100    #抢占主节点的概率
10     advert_int 1          #发送的时间间隔(秒)
11     authentication {
12         auth_type PASS   #授权
13         auth_pass 1111
14     }
15     virtual_ipaddress {
16         192.168.239.150/24   #虚拟ip地址,指定地址加到 VI_1的网卡上
17          }
18 }


主节点的选择问题
priority 的值大的优先

keepalived配置邮件
1.在keepalieved配置文件中进行配置
2.在keepalived调用shell脚本
3.才用第三方的监控程序

步骤:

1.安装postfix

yum insatll postfix

cd /etc/postfix
vi main.cnf  修改配置文件

 1 myhostname = mail.qyy.cn
 2 mydomain = qyy.cn
 3 
 4 myorigin = $myhostname
 5 myorigin = $mydomain
 6 
 7 inet_interfaces = all
 8 
 9 mydestination = $myhostname, $mydomain
10 
11 
12 mynetworks = 192.168.239.0/28 ,127.0.0.0/8
13 
14 reply_domains = $mydestination

service postfix start

ps-ef |grep master

2.安装 dovecot

 1 yum install dovecot
 2 
 3 cd /etc/dovecot
 4 
 5 vi dovecot.conf
 6 
 7 protocols = imap pop3 lmtp
 8 
 9 
10 service dovecot start
11 
12 netstat -tunlp | grep 110
13 
14 
15 yum install mail

3.

 1 新增用户
 2 useradd user1
 3 passwd user1
 4 
 5 useradd user2
 6 passwd user2
 7 
 8 
 9 centos 关闭setlinux
10 
11 临时关闭
12 setenforce 0
13 
14 永久关闭
15 
16 /etc/setlinux/config
17 
18 SELINUX = disabled

cmd 验证

 1 telnet 192.168.239.140  25
 2 
 3 mail from:uer1@qyy.cn  指定发件人
 4   
 5 rcpt to:user2@qyy.cn   指定接受人
 6 
 7 data  开始编辑发送数据 
 8 
 9 hello
10 . (发送数据,以点表示结束)
11 
12 quit  退出
13 
14 
15 
16 登陆 user2
17 su user2
18 mail
19 
20 >N 1 user1@qyy.cn 
21 &1
22 
23 hello

步骤概要

service iptables stop
service postfix start
service dovecot start

shell 脚本配置邮件报警

service mysqld stop

vi mysql.sh

1 #!/bin/bash
2 
3 nc -w2 localhost 3306
4 if   [  $? -ne 0   ]
5 then
6    echo "mysql's 3306 port is down" | mail root@qyy.cn -s "mysql is down"
7 fi

chmod u+x mysql.sh
yum install nc

./mysql.sh

 

1.通过计划任务进行邮件报警

1 crontab -e
2 
3 */2 * * * * /root/mysql.sh      两分钟执行一次


crontab -l 查看当前的计划任务

crontab -r 删除当前的计划任务

2.删除计划任务,修改配置文件

vim keeplieved.conf

 4 vrrp_script chk_mysql {
 5       script "/root/mysql.sh"
 6        interval    10
 7 }
 8 
 9 
10 track_script {
11       chk_mysql
12 }

原文地址:https://www.cnblogs.com/quyangyang/p/11420769.html