akyou56 发表于 2018-12-28 13:12:05

LVS+keepalived+LNMP架构

  DR模型原理:

  客户端请求负载均衡器,负载均衡器根据调度算法,从后端realserver集群中选择一台机器,然后更改请求报文MAC地址,将目的MAC地址改为后端服务器的MAC地址,源MAC改为自己的MAC地址。后端服务器收到请求报文,然后处理,最后直接响应给客户端。
  

  注意问题:
1.director和realserver都是同一个网段。
2.director和realserver都配置VIP地址。
3.director和realserver都配置route add VIP路由
4.director的vip配置在eth0:0上
5.realserver的vip配置在lo:0上。
6.realserver禁止响应arp数据包。  

  一:架构图
http://s4.运维网.com/wyfs02/M02/78/49/wKiom1Z5YgrQ8GdcAAFUO6aJrh8413.png
  

  

  二:Realserver安装软件
192.168.1.119
192.168.1.121  

  1)nginx-1.6.1.tar.gz安装
  http://yujianglei.blog.运维网.com/7215578/1725587

  

  2)php-5.5.4.tar.gz安装
  A:安装依赖包
cd /home/mcc/tools
yum -y install libmcrypt-devel mhash-devel libxslt-devel\
libjpeglibjpeg-devellibpnglibpng-devel\
freetypefreetype-devellibxml2libxml2-devel\
zlibzlib-develglibc glibc-devel glib2 glib2-devel bzip2 \
bzip2-devel ncurses ncurses-devel curl curl-devel\
e2fsprogse2fsprogs-develkrb5krb5-devellibidn \
libidn-developensslopenssl-develbzip2-devellibcurl-devel  

  B:安装第三方依赖包
wget http://www.atomicorp.com/installers/atomic
sh ./atomic
yuminstallphp-mcryptlibmcryptlibmcrypt-devel   -y  C:编译安装
./configure \
      --prefix=/app/php\
      --with-config-file-path=/app/php/etc \
      --enable-sockets \
      --with-openssl \
      --with-mhash   \
      --with-mysql \
      --with-mysqli \
      --with-iconv-dir \
      --with-freetype-dir \
      --with-jpeg-dir \
      --with-png-dir \
      --with-zlib \
      --with-curl \
      --with-libxml-dir=/usr \
      --enable-xml \
      --with-xmlrpc \
      --with-bz2 \
      --enable-fpm\
      --enable-zip \
      --enable-soap \
      --enable-sysvsem \
      --enable-inline-optimization \
      --with-gd \
      --with-gettext \
      --with-mcrypt \
      --disable-rpath \
      --enable-mbstring \
      --enable-ftp\
      --enable-bcmath \
      --enable-shmop \
      --enable-pcntl \
      --enable-gd-native-ttf \
      --without-pear
make   &&make   installcpsapi/fpm/init.d.php-fpm   /etc/rc.d/init.d/php-fpm
chmod +x /etc/rc.d/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on
cp/app/php/etc/php-fpm.conf.default   /app/php/etc/php-fpm.conf  D:配置php-fpm.conf
pm.max_children = 150
pm.start_servers = 8
pm.min_spare_servers = 5
pm.max_spare_servers = 10  E:启动php-fpm
servicephp-fpmstart  F:配置nginx,整合php环境
  两台机器nginx配置相同
worker_processes 1;
worker_rlimit_nofile 655360;
events {
      useepoll;
      worker_connections 1024;
}
http{
    include       mime.types;
    default_typeapplication/octet-stream;
    sendfile          on;
    tcp_nopush      on;
    tcp_nodelay       on;
    server_tokens   off;   
    keepalive_timeout 65;
server {
      listen      80;
      server_name   localhost;
      location /{
                roothtml;
                index index.htmlindex.htm index.php;
      }
      location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|ico|txt|js|css)$ {
            root      html;
            expires   3d;
      }
      location ~ \.php$ {
                root   html;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_indexindex.php;
                fastcgi_paramSCRIPT_FILENAME   $document_root$fastcgi_script_name;
                include      fastcgi_params;
      }
      error_page 500502503504/50x.html;
      location = /50.html {
            root   html;
      }
}
}  

  F:代码上线,先上一台机器,安装完成后在把安装完成的discuz包拷贝到另外一台机器上。
192.168.1.121
cd/app/nginx/html
rz  

  安装时:不要用VIP访问。用192.168.1.121访问。
  

  

  三:配置Realserver的LVS功能
  启动脚本:
#!/bin/bash
#
# chkconfig: - 90 10
# description: LVS DR RealServer
. /etc/rc.d/init.d/functions
VIP=192.168.1.200
case $1 in
start)
      /sbin/ifconfig lo down
      /sbin/ifconfig lo up
      /sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP up
      /sbin/route add -host $VIP dev lo:0
      echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
      echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
      echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
      echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
      sysctl-p 2>&1 >/dev/null
      echo "RealServer Start OK!"
;;
stop)
      /sbin/ifconfig lo:0 down
      /sbin/route del $VIP 2>&1 >/dev/null
      echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
      echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
      echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
      echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
      sysctl-p 2>&1 >/dev/null
      echo "RealServer Stop OK!"
;;
*)
      printf "Usage: $0 {start|stop}\n"
      exit 1
esac  2) 拷贝realserver脚本到/etc/init.d/
cprealserver/etc/ini.d/  3)使用
server realserver stop
server realserver start  4)开启启动
chkconfig –add    realserver
chkconfig    realserveron  启动问题:
# ./realserverstart
error: "net.bridge.bridge-nf-call-ip6tables" is an unknown key
error: "net.bridge.bridge-nf-call-iptables" is an unknown key
error: "net.bridge.bridge-nf-call-arptables" is an unknown key
解决:
modprobe bridge
lsmod | grepbridge  

  四:Director安装软件
192.168.1.116
192.168.1.117  安装ipvsadm-1.24.tar.gz
mkdir   /home/mcc/tools
wget -c http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
ln -s /usr/src/kernels/2.6.* /usr/src/linux
tar xf ipvsadm-1.24.tar.gz
cd ipvsadm-1.24
make
makeinstall  安装keepalived-1.2.12.tar.gz
  http://yujianglei.blog.运维网.com/7215578/1725586

  

  配置文件:
192.168.1.116master
192.168.1.117bakcup主配置文件:
! Configuration File for keepalived
global_defs {
   notification_email {
    yujianglei@singulax.com
   }
   notification_email_from 15614119390@163.com
   smtp_server smtp.163.com
   smtp_connect_timeout 30
   router_id LVS
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 10
    priority 100
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass jidian123
    }
    virtual_ipaddress {
      192.168.1.200
    }
}
virtual_server 192.168.1.200 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.121 80 {
      weight 1
      TCP_CHECK {
            connect_timeout10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
      }
    }
    real_server 192.168.1.119 80 {
      weight 1
      TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
      }
    }
}从配置文件:
! Configuration File for keepalived
global_defs {
   notification_email {
    yujianglei@singulax.com
   }
   notification_email_from 15614119390@163.com
   smtp_server smtp.163.com
   smtp_connect_timeout 30
   router_id LVS
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 10
    priority 99
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass jidian123
    }
    virtual_ipaddress {
      192.168.1.200
    }
}
virtual_server 192.168.1.200 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.121 80 {
      weight 1
      TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
      }
    }
    real_server 192.168.1.119 80 {
      weight 1
      TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
      }
    }
}  五:配置Director的LVS功能
  启动脚本:
#!/bin/bash
#
VIP=192.168.1.200
RIP1=192.168.1.119
RIP2=192.168.1.121
PORT=80
case $1 in
start)
      /sbin/ifconfig eth0:0 $VIP broadcast $VIPnetmask 255.255.255.255 up
      /sbin/routeadd -host $VIP dev eth0:0
      echo "1">/proc/sys/net/ipv4/ip_forward
      /sbin/iptables-F
      /sbin/ipvsadm   -C
      /sbin/ipvsadm   -A -t $VIP:$PORT -s rr
      /sbin/ipvsadm   -a -t $VIP:$PORT -r $RIP1 -g
      /sbin/ipvsadm   -a -t $VIP:$PORT -r $RIP2 -g
      touch/var/lock/subsys/director.lock
      echo "Director Start OK!"
;;
stop)
      /sbin/ifconfig eth0:0 down
      /sbin/ipvsadm -C
      echo "0">/proc/sys/net/ipv4/ip_forward
      rm-fr/var/lock/subsys/director.lock
      echo "Director Stop OK!"
;;
status)
      if [ -e /var/lock/subsys/director.lock ];then
                echo "Director is Running!"
      else
                echo "Director is Down!"
      fi
;;
*)
      printf "Usage: $0 {start|stop}\n"
      exit 1
esac  

  一:安装MySQL需要依赖的包
yum install ncurses-devel -y  二:建立数据库启动账号
# cat /etc/passwd | grepmysql
# groupadd mysql
# useradd -s /sbin/nologin -g mysql -M mysql  三:获取MySQL软件
dev.mysql.com获取mysql-5.5.45.tar.gz、cmake-2.8.8.tar.gz  四:上传MySQL软件到服务器
  1.建立软件包目录
mkdir-p /home/mcc/tools
cd /home/mcc/tools
rz  

  五:创建MySQL实例的数据文件目录和日志目录
mkdir -p /mydata55/data
mkdir -p /mydata55/{bin_log,error_log,relay_log}
tree /mydata55  六:创建MySQL实例的安装文件目录
mkdir /app55/mysql-5.5.45 -pv  

  

  七:安装MySQL软件
  1.解压安装cmake-2.8.8.tar.gz软件包
tar xf cmake-2.8.8.tar.gz
cd cmake-2.8.8
./configure
gmake
gmake install  2.解压编译安装mysql-5.5.45.tar.gz软件包
tar xf mysql-5.5.45.tar.gz
cd mysql-5.5.45
cmake . -DCMAKE_INSTALL_PREFIX=/app55/mysql-5.5.45\
-DMYSQL_DATADIR=/mydata55/data\
-DMYSQL_UNIX_ADDR=/mydata55/mysql.sock\
-DDEFAULT_CHARSET=utf8\
-DDEFAULT_COLLATION=utf8_general_ci-DEXTRA_CHARSETS=all\
-DENABLED_LOCAL_INFILE=ON\
-DWITH_INNOBASE_STORAGE_ENGINE=1\
-DWITH_FEDERATED_STORAGE_ENGINE=1\
-DWITH_BLACKHOLE_STORAGE_ENGINE=1\
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1\
-DWITHOUT_PARTITION_STORAGE_ENGINE=1\
-DWITH_FAST_MUTEXES=1\
-DENABLED_LOCAL_INFILE=1 \
-DWITH_READLINE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DWITH_DEBUG=0
make &&makeinstall  

  2.生成不带版本号的软连接/app/mysql,即mysql安装路径
cd /app55
ln -smysql-5.5.45mysql  

  八:准备配置文件
# cp /app55/mysql/support-files/my-small.cnf /etc/my.cnf
cp: overwrite `/etc/my.cnf'? yvi/etc/my.cnf
port = 3306
socket = /mydata55/mysql.sock
default-character-set=utf8

#No-auto-rehash
default-character-set=utf8

user = mysql
port = 3306
socket = /mydata55/mysql.sock
character-set-server=utf8
basedir = /app55/mysql
datadir = /mydata55/data
open_files_limit = 1024
back_log = 600
max_connections = 800
max_connect_errors = 3000
table_cache = 614
external-locking = FALSE
max_allowed_packet = 8M
sort_buffer_size = 1M
join_buffer_size = 1M
thread_cache_size = 100
thread_concurrency = 2
query_cache_size = 2M
query_cache_limit = 1M
query_cache_min_res_unit = 2k
default-storage-engine=InnoDB
#default_table_type = InnoDB
thread_stack = 192k
transaction_isolation = READ-COMMITTED
tmp_table_size = 2M
max_heap_table_size = 2M
long_query_time = 1
#log_long_format
log-error = /mydata55/error_log/3306.err
pid-file = /mydata55/3306.pid
#二进制日志
log-bin = /mydata55/bin_log/mysql-bin
log-bin-index = /mydata55/bin_log/mysql-bin.index
binlog_format = mixed
relay-log = /mydata55/relay_log/relay-bin
relay-log-info-file = /mydata55/ relay_log /relay-log.info
binlog_cache_size = 1M
max_binlog_cache_size = 1M
max_binlog_size = 2M
expire_logs_days = 10
#MyISAM引擎
key_buffer_size = 16M
read_buffer_size = 1M
#read_md_buffer_size = 1M
bulk_insert_buffer_size = 1M
myisam_sort_buffer_size = 1M
myisam_max_sort_file_size = 10G
#myisam_max_extra_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover
lower_case_table_names = 1
skip-name-resolve
slave-skip-errors = 1032,1062
replicate-ignore-db = mysql
server-id = 1
#InnoDB引擎
innodb_additional_mem_pool_size = 4M
innodb_buffer_pool_size = 32M
innodb_data_file_path = ibdata1:128M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 4M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 1

quick
max_allowed_packet = 2M
default-character-set=utf8

log-error = /mydata55/error_log/3306.err
pid-file = /mydata55/3306.pid
default-character-set=utf8  九:配置环境变量
vi /etc/profile
export PATH=/app55/mysql/bin:$PATH
source/etc/profile  

  

  提示:
  在配置环境变量时,一定要将安装路径配置在PATH前面,否则系统中万一存在rpm安装的mysql。那么系统就会去寻找/usr/local/bin/下关于mysql的有关命令。
  

  十:更改权限
chown-Rmysql.mysql   /mydata55
chmod-R1777 /tmp  

  十一:初始化数据库
cd/app55/mysql/scripts
./mysql_install_db --basedir=/app55/mysql --datadir=/mydata55/data--user=mysql  十二:数据库启动脚本
cd /app55/mysql/support-files
cp mysql.server /etc/init.d/mysqld  

  十三:授权,加密,优化
/etc/init.d/mysqld start
mysql -uroot -p -e "grant all privileges on *.* to admin@'localhost' identified by 'jidian123' with grant option"
mysql -uroot -p -e "grant all privileges on *.* to admin@'%' identified by 'jidian123' with grant option"
mysql -uroot -p -e "flushprivileges;"
mysql -uroot -p -e "drop database test;"
mysql -uroot -p -e "delete from mysql.user where User='root'"
mysql -uadmin -p'jidian123' -e "delete from mysql.user where User=''"
mysql -uadmin -p'jidian123' -e "flushprivileges;"  

  十三:报错解决
  

  1、启动报错日志:
1508193:12:24 Plugin 'InnoDB' init function returned error.
1508193:12:24 Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
1508193:12:24 Unknown/unsupported storage engine: InnoDB
1508193:12:24 Aborting  解决方案:
移除数据目录下的ib_logfile0ib_logfile1
mv/mydata55/data/ib_logfile*   /home  

  重新启动:
/etc/init.d/mysqldstart  

  2、导入报错日志
ERROR 1665 (HY000): Cannot execute statement:
impossible to write to binary log since BINLOG_FORMAT = STATEMENT and at least one table uses a storage engine limited to row-based logging.  【报错原因】
  innodb的事务隔离级别是read commited或者read uncommited模式时,binlog不可以使用statement模式。
  

  解决:
set global binlog_format=mixed  




页: [1]
查看完整版本: LVS+keepalived+LNMP架构