keepalived双机热备服务高可用

keepalived+nginx,场景适用于非高并发,但又偏核心业务

VIP: 192.168.3.111
Node1: 192.168.3.171
Node2: 192.168.3.175

  1. 用户
  2.                         ||
  3.                         ISP
  4.                         ||
  5.                 Virtual IP [192.168.3.111]
  6.                 |                |
  7. Node1[192.168.3.171]        Node2[192.168.3.175]
  1. Node1配置文件:
  2. [root@localhost ~]# cat /etc/keepalived/keepalived.conf
  3. vrrp_script check_nginx {              
  4.         script "/usr/bin/killall -0 nginx"
  5.         interval 1 #每秒钟检查一次
  6.         weight 2 #每次priority增加2 如果状态正常的话
  7. }                                        
  8. vrrp_instance VI_1 {
  9.         interface eth1
  10.         state MASTER
  11.         virtual_router_id 51
  12.         priority 101 #Master为101,Slave为100
  13.         authentication {
  14.             auth_type PASS
  15.             auth_pass 123456
  16.         }
  17.         track_script {
  18.                 check_nginx
  19.         }
  20.         virtual_ipaddress {
  21.                 192.168.3.111 dev eth1
  22.         }
  23. }
  24.  
  25. Node2配置文件:
  26. [root@localhost ~]# cat /etc/keepalived/keepalived.conf
  27. vrrp_script check_nginx {
  28.         script "/usr/bin/killall -0 nginx"
  29.         interval 1
  30.         weight 2
  31. }
  32. vrrp_instance VI_1 {
  33.         interface eth1
  34.         state BACKUP
  35.         virtual_router_id 51
  36.         priority 100
  37.         authentication {
  38.             auth_type PASS
  39.             auth_pass 123456
  40.         }
  41.         track_script {
  42.                 check_nginx
  43.         }
  44.         virtual_ipaddress {
  45.                 192.168.3.111 dev eth1
  46.         }
  47. }
  1. Note:
  2. 停止NginxKeepalived本身,两台都会切换;
  3. 在负载较高的服务器上,如果没有对“服务”进行冗余而频繁切换IP是不严谨的,nopreempt可以防止来回抢占IP
  4. nopreempt需要stateBACKUP模式下才允许,并且给它们相同的route_id,两台间优先级较高的将优先取得VIP
  5.  
  6. /etc/sysctl.conf
  7. net.ipv4.ip_nonlocal_bind=1

yum Error: rpmdb open failed 的解决办法

  1. rm -f /var/lib/rpm/__db.*
  2. yum clean all
  3. rpm --rebuilddb
  4. yum makecache

nginx查看php-fpm状态信息

  1. #查看php-fpm状态的信息,修改php-fpm.conf
  2. pm.status_path = /php-status
  3.  
  4. #修改nginx.conf
  5. server {
  6.     listen       $IP:$Port;
  7.     server_name  _;
  8.  
  9. location ~ ^/php-status$ {
  10.                 stub_status on;
  11.                 access_log off;
  12.                 #allow all;
  13.                 allow $IP;
  14.                 deny all;
  15.                 include fastcgi_params;
  16.                 fastcgi_pass 127.0.0.1:9000;
  17.                 fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
  18.         }
  19. }
  20.  
  21. #查看方式 http://$IP/php-status
  22. #full list: http://$IP/php-status?full
  23.  
  24. #输出方式支持html、json、xml
  25. #http://$IP/php-status?html #默认
  26. ##http://$IP/php-status?full&html
  27.  
  28. #http://$IP/php-status?json #json
  29. ##http://$IP/php-status?full&json
  30.  
  31. #http://$IP/php-status?xml #xml
  32. ##http://$IP/php-status?full&xml
  33.  
  34.  
  35. #查看nginx状态
  36. server {
  37.     listen       $IP:$Port;
  38.     server_name  _;
  39.         location = /nginx-status {
  40.                 stub_status on;
  41.                 access_log off;
  42.                 allow $IP;
  43.                 deny all;
  44.         }
  45. }
  46. #查看方式 http://$IP/nginx-status

zabbix历史数据清理

  1. mysql> SELECT TABLE_NAME AS "Table", round(((data_length + index_length) / 1024 / 1024), 2) AS Size_in_MB FROM information_schema.TABLES  WHERE table_schema = 'zabbix' ORDER BY Size_in_MB DESC LIMIT 10;         
  2. +-----------------------+------------+
  3. | Table                 | Size_in_MB |
  4. +-----------------------+------------+
  5. | history_uint          |    7195.09 |
  6. | history               |    3494.61 |
  7. | trends_uint           |     341.44 |
  8. | trends                |     252.44 |
  9. | alerts                |      24.70 |
  10. | events                |      14.06 |
  11. | history_str           |       3.52 |
  12. | items                 |       2.08 |
  13. | images                |       1.53 |
  14. | auditlog              |       0.58 |
  15.  
  16. 可以看出history_uinthistory这两个表堆积了大量的历史数据,两种方法删除zabbix的历史数据
  17.  
  18. 第一种是直接清空表(历史记录全删)
  19. truncate table history_uint;
  20. optimize table history_uint;
  21. truncate table history;
  22. optimize table history;
  23.  
  24. 第二种是删除指定时间段内的数据,timestamp可以用date命令来取, date "+%s" -d "年月日",比如
  25. [root@localhost ~]# date "+%s" -d "20150127"
  26. 1422334800
  27.  
  28. 删除2015127日前zabbix的历史记录
  29. DELETE FROM `history_uint` WHERE `clock` < 1422334800;
  30. optimize table history_uint;
  31. DELETE FROM `history` WHERE `clock` < 1422334800;
  32. optimize table history;

查询修改MySQL表的存储引擎

查MySQL某个表的引擎:

  1. 方法1)
  2. show table status where name='$Table_Name'
  3.  
  4. 方法2)
  5. xxd -l 4 tablename.frm
  6. 0000000: fe01 0909
  7.  
  8. xxd -l 4 tablename.frm
  9. 0000000: fe01 0a0c
  10.  
  11. 前面4位固定为fe01,紧接着4位,0CInnoDB09MyISAM

查看全局引擎支持类型

  1. show engines;

MyISAM限制:
没有外键更新删除
无法回滚
没有事务
行数最多为4284867296
每一行最大的索引为64个

InnoDB限制:
不能全文索引,最新版貌似可以英文

MyISAM使用表级别的锁,而InnoDB使用行级别的锁。
更改全局引擎:

  1. SET storage_engine=$Engine

建表指定为InnoDB,默认为MyISAM

  1. CREATE TABLE table_name (rid INT) ENGINE = INNODB;
  2. CREATE TABLE table_name (rid INT) TYPE = INNODB;

转换一个表的存储引擎为InnoDB:

  1. ALTER TABLE table_name ENGINE = INNODB;