当前位置:文档之家› 搭建一个服务器集群

搭建一个服务器集群

搭建一个服务器集群包含负载均衡,HA高可用,MySQL主从复制,备份服务器,和监控服务器,服务用discuz 论坛演示服务器配置如下服务器名服务器ip服务器作用backup192.168.199.180备份+zabbix监控+NFSNginx1192.168.199.142主DirectorNginx2192.168.199.145从DirectorApache1192.168.199.200Apache1Apache2192.168.199.210Apache2Apache3192.168.199.233Apache3Mysql1192.168.199.126主mysqlMysql2192.168.199.131从mysqlMysql3192.168.199.197从mysqlVIP192.168.199.3Apache负载均衡VIP在所有服务器上操作#关闭selinuxsed-i's/SELINUX=enforcing/SELINUX=disabled/'/etc/selinux/config&&setenforce0;#清空iptablesiptables-F&&service iptables save;#安装nfs服务yum install-y nfs-utils epel-release配置backup服务器mkdir-p/data/discuz#建立discuz应用目录mkdir/opt/backup#建立backup目录#设置目录的属主和属组chown-R shared:shared/data/discuzchown-R shared:shared/opt/backupvi/etc/exports#设置共享目录/data/discuz/192.168.199.0/24(rw,sync,all_squash,anonuid=500,anongid=500)/opt/backup/192.168.199.0/24(rw,sync,all_squash,anonuid=500,anongid=500)/etc/init.d/rpcbind start;/etc/init.d/nfs start#启动NFS服务配置mysql服务器#挂载NFS服务器backup目录mount-t nfs-onolock192.168.199.180:/opt/backup/optvi/etc/fstab192.168.199.180:/opt/backup/opt nfs nolock00安装MySQL#在3台mysql服务器上下载mysql5.7的二进制安装文件curl-O /mysql/MySQL-5.7/mysql-5.7.11-linux-glibc2.5-x86_64.tar.gztar-zxvf mysql-5.7.11-linux-glibc2.5-x86_64.tar.gz#解压mysql文件useradd-s/sbin/nologin-M mysql#建立mysql用户mv mysql-5.7.11-linux-glibc2.5-x86_64/usr/local/mysql#移动mysql文件chown-R mysql:mysql/usr/local/mysql#设置mysql目录的属主和属组mkdir-p/data/mysql#新建mysql的datadir目录chown-R mysql:mysql/data/mysql#设置mysql的datadir目录的属主和属组#初始化mysql./bin/mysqld--initialize--user=mysql--datadir=/data/mysql./bin/mysql_ssl_rsa_setup--datadir=/data/mysqlcp support-files/f/etc/f#复制mysql配置文件cp support-files/mysql.server/etc/init.d/mysqld#复制mysql启动文件#修改mysql配置文件sed-i-e's?#basedir=.....?basedir=/usr/local/mysql?g;s?#datadir=.....?datadir= '/data/mysql'?g;s?#port=.....?port=3306?g;s?#socket=.....?socket= /tmp/mysql.sock?g;s?^\[mysqld\]$?\[mysqld\]\nskip-grant-tables?g'/etc/f#修改root密码/usr/local/mysql/bin/mysql-uroot#在mysql里执行命令use mysql;update user set authentication_string=password('111111')where user='root';delete from user where not(user='root');delete from user where user='root'and host='';flush privileges;Quit#修改配置文件sed-i's?skip-grant-tables??g'/etc/f配置主从修改每台MYSQL服务器的配置文件f1.首先开启每台服务器的Bin日志(必须的)[code]log-bin=mysql-bin[/code]2.要记录日志的数据库[code]binlog-do-db=db1[/code]3.给每一个服务器一个Id,多台服务器的Id不能重复,要求是唯一的只要不相同就行[code]server-id=1[/code]4.下面指令是只有在环形结构时候才需要加下面项,主从的时候不需要加[code]log-slave-updates=true[/code]5.关于账号的建立5.1如果是(主从)的话那么要在主服务器创建一个账号SLAVE5.2如果是(环形)的话,那么在每台服务器上都创建一个账号SLAVE这个账号就专门用来做复制用的5.3创建语法:[code]GRANT REPLICATION SLAVE ON*.*TO'username'@'%'IDENTIFIED BY'password';[/code] 6.在服务器上的信息(查看主的)(环形都弄)(mysql里执行)[code]FLUSH TABLES WITH READ LOCK;[/code]保证当前只有自己操作,上线项目建议用上[code]show master status;//查看状态[/code]记录下其中的File和Psition7.从服务器上设置主服务器的IP地址等信息(mysql操作)[code]CHANGE MASTER TOMASTER_HOST='xx.xx.xx.xx',#主服务器ip/环形的就是上一个的MASTER_USER='账名',#主服务器上slave账户名/环形的就是上一个的MASTER_PASSWORD='密码',#主服务器上slave密码/环形的就是上一个的MASTER_LOG_FILE='mysql-bin.000001',#主服务器上记录的file/环形的就是上一个的MASTER_LOG_POS=251;#主服务器上记录的Position/环形的就是上一个的[/code]8.启动slave[code]start slave;[/code]9.主从同步检查[code]show slave status\G[/code][code]Slave_IO_Running:yesSlave_SQL_Running:yes[/code]上面两个字段都是yes表示连接成功PS:Slave_IO_Running:连接到主库,并读取主库的日志到本地,生成本地日志文件Slave_SQL_Running:读取本地日志文件,并执行日志里的SQL命令。

Apache服务器配置mkdir-p/data/discuz/#创建discuz应用目录#挂载NFS服务器discuz应用目录mount-t nfs-onolock192.168.199.180:/data/discuz/data/discuzvi/etc/fstab192.168.199.180:/data/discuz/data/discuz nfs nolock00安装Apache服务yum install-y gcc make compat-libstdc++-33libxml2-devel openssl openssl-devel bzip2 bzip2-devel libpng libpng-devel freetype freetype-devel libcurl libcurl-devel libjpeg-turbo libjpeg-turbo-devel libmcrypt libmcrypt-devel#安装apr和apr-utilcurl-O /apache/apr/apr-1.5.2.tar.gztar-zxvf apr-1.5.2.tar.gzcd apr-1.5.2./configure--prefix=/usr/local/apr&&make&&make installcurl-O /apache/apr/apr-util-1.5.4.tar.gztar-zxvf apr-util-1.5.4.tar.gzcd apr-util-1.5.4./configure--prefix=/usr/local/apr--with-apr=/usr/local/apr&&make&&make install#安装Apache2.4yum install-y pcre-develcurl-O /apache/httpd/httpd-2.4.18.tar.gztar-zxvf httpd-2.4.18.tar.gzcd httpd-2.4.18./configure\--prefix=/usr/local/apache\--sysconfdir=/etc/httpd\--enable-so\--enable-ssl\--enable-rewrite\--enable-cgi\--with-zlib\--with-pcre\--with-apr=/usr/local/apr/\--with-apr-util=/usr/local/apr/\--enable-modules=most\--enable-mpms-shared=all\&&make&&make install#配置Apache2.4sed-i-e's#AddType application/x-gzip.gz.tgz#&\n AddType application/x-httpd-php.php#g' /etc/httpd/httpd.confsed-i's#DirectoryIndex index.html#&index.htm index.php#g'/etc/httpd/httpd.confsed-i's#\#ServerName :80#ServerName localhost:80#g' /etc/httpd/httpd.conf安装phpcurl-O /distributions/php-5.6.17.tar.gztar-zxvf php-5.6.17.tar.gzcd php-5.6.17yum install-y mysql./configure--prefix=/usr/local/php\--with-apxs2=/usr/local/apache/bin/apxs\--with-config-file-path=/usr/local/php/etc\--with-mysql\--with-libxml-dir\--with-gd\--with-jpeg-dir\--with-png-dir\--with-freetype-dir\--with-iconv-dir\--with-zlib-dir\--with-bz2\--with-openssl\--with-mcrypt\--enable-soap\--enable-gd-native-ttf\--enable-mbstring\--enable-sockets\--enable-exif\--disable-ipv6\&&make&&make install修改Apache的默认主机目录vim/etc/httpd/httpd.conf修改DocumentRootDocumentRoot"/data/discuz"<Directory"/data/discuz">设置Apache负载均衡LVS/DR配置四台服务器:Director(eth0dip:192.168.199.142,vip eth0:0:192.168.199.3) Real server1(eth0rip:192.168.199.200,vip lo:0:192.168.199.3) Real server2(eth0rip:192.168.199.210,vip lo:0:192.168.199.3) Real server3(eth0rip:192.168.199.233,vip lo:0:192.168.199.3) Directo服务器上vim/usr/local/sbin/lvs_dr.sh#!/bin/bashecho1>/proc/sys/net/ipv4/ip_forwardipv=/sbin/ipvsadmvip=192.168.199.3rs1=192.168.199.200rs2=192.168.199.210rs3=192.168.199.233ifconfig eth0:0$vip broadcast$vip netmask255.255.255.255up route add-host$vip dev eth0:0$ipv-C$ipv-A-t$vip:80-s rr$ipv-a-t$vip:80-r$rs1:80-g-w1$ipv-a-t$vip:80-r$rs2:80-g-w1$ipv-a-t$vip:80-r$rs3:80-g-w1执行:bash/usr/local/sbin/lvs_dr.shReal server服务器上vim/usr/local/sbin/lvs_dr_rs.sh#!/bin/bashvip=192.168.199.3ifconfig lo:0$vip broadcast$vip netmask255.255.255.255up route add-host$vip lo:0echo"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_ignore echo"2">/proc/sys/net/ipv4/conf/all/arp_announce执行:bash/usr/local/sbin/lvs_dr_rs.sh浏览器测试访问http://192.168.199.3测试增加HA高可用LVS/DR+keepalived配置ifconfig eth0:0downyum install-y keepalivedvim/etc/keepalived/keepalived.conf!Configuration File for keepalivedglobal_defs{notification_email{acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.loc smtp_server192.168.200.1smtp_connect_timeout30router_id LVS_DEVEL}vrrp_instance VI_1{state MASTERinterface eth0virtual_router_id51priority100advert_int1authentication{auth_type PASSauth_pass1111}virtual_ipaddress{192.168.199.3}}virtual_server192.168.199.380{delay_loop6lb_algo wlclb_kind DRpersistence_timeout60protocol TCPreal_server192.168.199.20080{weight100TCP_CHECK{connect_timeout10nb_get_retry3delay_before_retry3connect_port80}}real_server192.168.199.21080{weight100TCP_CHECK{connect_timeout10nb_get_retry3delay_before_retry3connect_port80}}real_server192.168.199.23380{weight100TCP_CHECK{connect_timeout10nb_get_retry3delay_before_retry3connect_port80}}}以上为主director的配置文件,从director的配置文件只需要修改state MASTER->state BACKUPpriority100->priority90配置完keepalived后,需要开启端口转发(主从都要做):echo1>/proc/sys/net/ipv4/ip_forward然后,Real server上执行/usr/local/sbin/lvs_dr_rs.sh脚本最后,两个director上启动keepalived服务(先主后从):/etc/init.d/keepalived start配置zabbix监控Backup主机上配置安装rpm包的lamp环境yum install-y httpd mysql mysql-libs php php-mysql mysql-server php-bcmath php-gd php-mbstring安装zabbix服务端:yum install zabbix20zabbix20-agent zabbix20-server zabbix20-server-mysql zabbix20-web zabbix20-web-mysql net-snmp-devel修改一下mysql配置文件vim/etc/f//修改或增加如下内容[mysql]default-character-set=utf8[mysqld]character_set_server=utf8启动mysql服务/etc/init.d/mysqld start建库,导入数据mysql-uroot-p-e"create database zabbix"mysql-uroot-p--default-character-set=utf8zabbix</usr/share/zabbix-mysql/schema.sql mysql-uroot-p--default-character-set=utf8zabbix</usr/share/zabbix-mysql/images.sqlmysql-uroot-p--default-character-set=utf8zabbix</usr/share/zabbix-mysql/data.sqlmysql-uroot-p-e"grant all privileges on zabbix.*to zabbixuser@'localhost'identified by 'zabbixpass'"配置zabbix server端的文件,定义数据库的IP、用户名、密码vim/etc/zabbix/zabbix_server.confDBHost=192.168.199.180DBName=zabbixDBUser=zabbixuserDBPassword=zabbixpass启动apache服务跟zabbix服务/etc/init.d/zabbix-server start;/etc/init.d/zabbix-agent start/etc/init.d/httpd start;浏览器访问http://192.168.199.180/zabbix,解决相关的报错信息,点retry(vim/etc/php.ini),设置date.timezone=“Asia/Shanghai”输入mysql相关信息,首先要测试一下,如果不通过,则需要调试,测试通过后,点next Name写127.0.0.1(可以自定义)点next,再点next,最后点finish默认管理员账号为admin:zabbix3.接入要监控的主机在客户端上yum install zabbix20-agentvim/etc/zabbix_agentd.conf//更改Server=192.168.199.180ServerActive=0.0.0.0:10050Hostname=apache1(自定义,但要唯一)启动客户端/etc/init.d/zabbix-agent start服务端上命令行测试:zabbix_get-s192.168.199.200-p10050-k"apache1"在web界面下,点”configuration”-->“host”-->右上角点”Create Host”其中host name, visible name自定义,可以选择groups,这里默认即可,ip address写入客户端ip配置监控项目模板:点“templates”,点add,在弹出的小窗口中选择Template OS Linux,然后点select,最后点save。

相关主题