zoukankan      html  css  js  c++  java
  • 记《网络应用服务实训》实践能力测试

    测试说明:本测试重点考察linux环境下搭建高可用负载均衡网站平台的的能力。测试环境为win7系统安装vmware10,安装centos6.9虚拟机,所需软件包从互联网下载,测试时间为120分钟。

    任务一、配置测试网络环境(20分)

    1. 按图示要求克隆虚拟机,设置网卡为NAT模式,配置网卡地址为静态地址,与虚拟机vmnet8在同一网段,按图示设置虚拟机hostname。
    2. 关闭防火墙(#services iptables stop),关闭SElinux(# setenforce 0),配置使用网络yum源。

    检查点:nginx1虚拟机与主机能够ping通,虚拟机能够访问互联网(截图1)。
    提示:编辑/etc/sysconfig/network-scripts/ifcfg-eth*文件,nginx1(截图2)。

    任务二、在php1和php2上配置LAMP环境(20分)

    1. 使用yum方式安装httpd、php、php-mysqli等软件包,设置httpd的DocumentRoot/var/www,启动httpd服务;
    2. 在php1和php2的/var/www目录下编辑index.php文件。(截图3、截图4)

    检查点:从主机浏览器访问“http://192.168.?.111”和“http://192.168.?.112”可以分别看到“php1@IP地址”和“php2@IP地址”。(截图5、截图6)。
    提示index.php文件中可以直接书写相应虚拟机IP地址,也可以使用php脚本获得服务器IP地址。

    任务三、在nginx1和nginx2安装keepalived,配置高可用和负载均衡(20分)

    1. 安装epel yum源,使用yum方式安装nginx和keepalived;
    2. 在nginx1和nginx2配置一般轮询负载均衡,后端服务器为php1和php2,两台服务器配置文件(截图7、截图8)
    3. 编辑/etc/keepalived/keepalived.conf,使得nginx1成为master,nginx2作为backup,虚拟地址为“192.168.?.10”。编写应用nginx服务监控脚本。配置文件和脚本(截图9、截图10、截图11)

    检查点1:在主机浏览器访问http://192.168.*.11时分别看到“php1@IP地址”和“php2@IP地址”。(截图12)
    检查点2:nginx1停机或停止服务,访问http://192.168.*.10不间断。(截图13)

    任务四、配置mysql主从复制和读写分离(15分)

    1. 使用yum方式在mysql-master和mysql-slave安装mysql-server,创建测试数据库、数据表并插入数据;在master上授权给复制用户(截图14)。
    2. 开启master的binlog日志,关闭slave的binlog日志,把master的测试数据库复制到slave。在slave上配置复制参数,开启复制,查看复制状态(截图15)。
    3. 在php1上安装mysql-proxy并配置读写分离,mysql-proxy.cnf配置文件(截图16)。

    检查点1:在master上插入数据(截图17),slave上能够查询(截图18)。
    检查点2:在php1上连接本机mysql-proxy,测试读写分离(截图19)。
    提示1:master要启用binlog日志,要授权给复制用户,备份数据库时要锁表。在slave上CHANGE MASTER TO配置复制参数,start slave启用复制。
    提示2:创建/etc/mysql-proxy.cnf配置文件,编辑rw-splitting.lua读写分离文件,启动mysql-proxy,在mysql-master和masql-slave上授权给mysql-proxy。

    任务五、配置NFS支持nginx动静分离(15分)

    1. 在nfs上安装nfs-utils,配置共享目录,启动nfs服务。在php1上安装nfs-utils,查看nfs上的共享目录(截图14),并挂载到本地/var/www/share(截图15)。
    2. 在php1的web根目录编辑index.php脚本(截图16),并上传相关资源文件到nfs的共享目录。
    3. 在nginx1编辑/etc/nginx/conf.d/default.conf(截图17),支持动静分离。
      检查点1:在主机访问使用http://192.168.?.111/index.php(截图18)。
      检查点2:在主机访问使用http://192.168.?.11/index.php(截图19)。

    任务六:安装zabbix,配置主机监控系统。(10分)

    要求:在manager上安装zabbix server,搭建LAMP平台运行zabbix前端(截图20),在各台主机安装启用zabbix agent,在manager使用zabbix监控各台主机。

    检查点:在manager使用ab软件对各台主机进行压力测试,使用zabbix能够监控到各台主机的cpu使用率、内存使用率。(截图21)
    提示#ab –c 并发数 –n 访问次数 http://192.168.*.10/test.php

    自动部署脚本

    #!/bin/bash
    
    # version: 2.0
    
    # 准备工作
    # 1. 第一台虚拟机执行 # ./manager.sh clone_init
    # 2. 先清理/etc/udev/rules.d/70-persistent-net.rules 再关机,再克隆虚拟机
    # 3. 克隆后,修改各服务器网卡配置
    # 4. 在manager上执行 ./manager.sh install
    # 5. 等待安装
    
    
    
    # 是否自动发现主机
    is_auto_discover=false
    
    # 是否修改IP
    is_modify_ip=false
    
    # 是否自动测试能否访问互联网
    is_test_internet=false
    
    # 学号密码
    STU_USERNAME=00000
    STU_PASSWORD=00000
    # 不能访问互联网可能是未认证校园网所致
    # 如果发现未登录,可自动登录校园网
    
    
    # 是否使用阿里云yum源
    use_aliyun_src=false
    
    
    # centos user and password
    CENTOS_USER=root
    CENTOS_PASS=123456
    
    # mysql user and password
    MYSQL_USER=root
    MYSQL_PASS=123456
    
    
    # 修改IP的话,修改前的网卡名称
    server_eth0='eth0'
    
    # 修改IP的话,修改后的网卡名称
    server_eth1='eth0'
    
    
    # 主机号
    manager_host_ip=(3 11 12 111 112 120 201 202 10)
    
    # 主机名
    manager_hostname=('manager' 'nginx1' 'nginx2' 'php1' 'php2' 'nfs' 'mysql-master' 'mysql-slave' 'virtual-server')
    
    # IP地址第三段
    SERVER_IP_3=`echo $(ifconfig | sed -n '2p') | cut -d '.' -f 3`
    SERVER_NET=192.168.${SERVER_IP_3}.
    
    
    
    
    
    # 主机发现
    function discover_host(){
    # configure
    THIS_CENTOS_USER=${CENTOS_USER}
    THIS_CENTOS_PASS=${CENTOS_PASS}
    THIS_SERVER_IP_3=${SERVER_IP_3}
    
    # install software
    yum install -y epel*
    yum install -y expect openssh-clients ntp ntpdate
    source ~/.bash_profile
    
    
    rm -rf /etc/sysconfig/network-scripts/ifcfg-${server_eth0}
    echo -e "DEVICE=${server_eth1}
    TYPE=Ethernet
    ONBOOT=yes
    NM_CONTROLLED=yes
    BOOTPROTO=static
    IPADDR=192.168.${SERVER_IP_3}.3
    GATEWAY=192.168.${SERVER_IP_3}.2
    NETMASK=255.255.255.0
    DNS1=202.206.192.33" > /etc/sysconfig/network-scripts/ifcfg-${server_eth1}
    service network restart
    
    # modify local host temp ip
    #ifconfig ${server_eth1}:5 192.168.${THIS_SERVER_IP_3}.${manager_host_ip[0]}
    
    # create python shell
    cat <<EOF >> /root/discover_host.py
    #!/usr/bin/python
    #coding=utf-8
    
    import os
    
    server_ip_3 = '${THIS_SERVER_IP_3}'
    index = 0
    group_list = [
    '192.168.${THIS_SERVER_IP_3}.${manager_host_ip[1]}',
    '192.168.${THIS_SERVER_IP_3}.${manager_host_ip[2]}',
    '192.168.${THIS_SERVER_IP_3}.${manager_host_ip[3]}',
    '192.168.${THIS_SERVER_IP_3}.${manager_host_ip[4]}',
    '192.168.${THIS_SERVER_IP_3}.${manager_host_ip[5]}',
    '192.168.${THIS_SERVER_IP_3}.${manager_host_ip[6]}',
    '192.168.${THIS_SERVER_IP_3}.${manager_host_ip[7]}',
    ]
    
    def modify_remote_host_ip(ip, newip):
        remote_commond = "ifconfig ${server_eth1}:5 "+newip
        local_commond = 'ssh -o "StrictHostKeyChecking no" ${THIS_CENTOS_USER}@'+ip+' "'+remote_commond+'" '
        print remote_commond
        print local_commond + '
    '
        file_path = '/root/modify_remote_host_ip.sh'
        f = open(file_path ,'w')
        f.write("#!/usr/bin/expect
    ")
        f.write('spawn '+local_commond + '
    ')
        f.write('expect "*password:"
    ')
        f.write('send "${THIS_CENTOS_PASS}\r"
    ')
        f.write("interact
    ")
        f.close()
        os.system("chmod +x "+file_path)
        os.system(file_path)
    
    def discover_host(index):
        for i in range(100, 250):
            ip = '192.168.'+server_ip_3+'.'+str(i)
            result = os.popen("arping -c 1 "+ip+" | grep '^Received.*' | awk -F ' ' '{print $2}'")
            islive = int(result.read())
            #print result
            #print islive
            if str(islive) == '1':
                info = '检测到'+ip+'存活,准备设置临时IP地址'
                print info
                modify_remote_host_ip(ip, group_list[index])
                if index == 6:
                    exit()
                else:
                    index += 1
            else:
                info = '检测到'+ip+'未存活'
                print info
    
    discover_host(index)
    #
    EOF
    # End
    chmod +x /root/discover_host.py
    /root/discover_host.py
    }
    
    
    
    
    #修改主机名
    function modify_hostname(){
    #ifconfig | grep "inet addr:.*Bcast.*" | awk -F ' ' '{print $2}' | awk -F ':' '{print $2}' > /root/this_server_ip.txt
    cat <<EOF >> /root/modify_hostname.py
    #!/usr/bin/python
    #coding=utf-8
    
    from os import system
    
    group_list = [
        { 'ip':'192.168.${SERVER_IP_3}.${manager_host_ip[0]}', 'hostname':'${manager_hostname[0]}' },
        { 'ip':'192.168.${SERVER_IP_3}.${manager_host_ip[1]}', 'hostname':'${manager_hostname[1]}' },
        { 'ip':'192.168.${SERVER_IP_3}.${manager_host_ip[2]}', 'hostname':'${manager_hostname[2]}' },
        { 'ip':'192.168.${SERVER_IP_3}.${manager_host_ip[3]}', 'hostname':'${manager_hostname[3]}' },
        { 'ip':'192.168.${SERVER_IP_3}.${manager_host_ip[4]}', 'hostname':'${manager_hostname[4]}' },
        { 'ip':'192.168.${SERVER_IP_3}.${manager_host_ip[5]}', 'hostname':'${manager_hostname[5]}' },
        { 'ip':'192.168.${SERVER_IP_3}.${manager_host_ip[6]}', 'hostname':'${manager_hostname[6]}' },
        { 'ip':'192.168.${SERVER_IP_3}.${manager_host_ip[7]}', 'hostname':'${manager_hostname[7]}' },
    ]
    
    temp_file_path = '/root/this_server_ip.txt'
    group_ip, this_ip = [], []
    
    system("ifconfig | grep "inet addr:.*Bcast.*" | awk -F ' ' '{print $2}' | awk -F ':' '{print $2}' > " + temp_file_path)
    
    for i in range( len(group_list) ):
        group_ip.append(group_list[i]['ip'])
    
    f = open(temp_file_path, 'r+')
    while True:
        line = f.readline()
        if not line: break
        line = line.replace('
    ', '').replace('
    ', '')
        this_ip.append(line)
    f.close()
    system('rm -rf ' + temp_file_path)
    
    for i in range( len(this_ip) ):
        if this_ip[i] in group_ip:
            #print 'OK'
            for k in range( len(group_list) ):
                if group_list[k]['ip'] == this_ip[i]:
                    hostname = group_list[k]['hostname']
                    #print hostname
                    system('hostname '+hostname)
                    system('echo $(hostname)')
                else:
                    pass
            break
        else:
            pass
    
    system('echo -e "
    
    " >> /etc/hosts')
    system('echo -e "192.168.${SERVER_IP_3}.${manager_host_ip[8]}		${manager_hostname[8]}" >> /etc/hosts')
    for i in range( len(group_list) ):
        host = group_list[i]['ip'] + '		' + group_list[i]['hostname']
        commond = 'echo -e "' + host + '" >> /etc/hosts'
        system(commond)
    # End
    EOF
    chmod +x /root/modify_hostname.py
    /root/modify_hostname.py
    }
    
    
    
    # 初始化hosts文件
    function init_add_hosts(){
    # add hosts
    #echo -e "
    #192.168.${SERVER_IP_3}.10         virtual-server
    #192.168.${SERVER_IP_3}.3          manager
    #192.168.${SERVER_IP_3}.11         nginx1
    #192.168.${SERVER_IP_3}.12         nginx2
    #192.168.${SERVER_IP_3}.111        php1
    #192.168.${SERVER_IP_3}.112        php2
    #192.168.${SERVER_IP_3}.120        nfs
    #192.168.${SERVER_IP_3}.201        mysql-master
    #192.168.${SERVER_IP_3}.202        mysql-slave" >> /etc/hosts
    
    echo -e "
    202.232.140.70        ftp.iij.ad.jp
    195.154.241.117       rpms.famillecollet.com
    120.24.84.51          download.cloudhosts.xyz
    162.243.159.138       repo.zabbix.com
    " >> /etc/hosts
    }
    
    
    # 修改yum源
    function modify_yum_src(){
    	# modify yum source
    	ping mirrors.aliyun.com -c 4
    	ping mirrors.tuna.tsinghua.edu.cn -c 4
    	yum install -y epel-*
    	yum install -y wget
    	mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
    	#wget -O /etc/yum.repos.d/CentOS-Base.repo  http://mirrors.163.com/.help/CentOS6-Base-163.repo
    	wget -O /etc/yum.repos.d/CentOS-Base.repo  http://mirrors.aliyun.com/repo/Centos-6.repo
    	yum makecache
    	yum list
    }
    
    
    # init network
    function init_network(){
    # use: init_network 11
    # stop iptable
    service nginx stop
    service iptables stop
    setenforce 0
    # install ntp and ssh client
    yum install -y openssh-clients ntp ntpdate
    source ~/.bash_profile
    # init add hosts
    init_add_hosts
    # set hostname at shell
    modify_hostname
    # set hostname at file
    echo -e "NETWORKING=yes
    HOSTNAME=$(hostname)" > /etc/sysconfig/network
    # show hostname
    echo -e "
    33[32m [ INFO ] Hello `echo $(hostname)` 33[0m"
    # add dns server
    echo -e "nameserver 223.5.5.5" >> /etc/resolv.conf
    echo -e "nameserver 202.206.192.33" >> /etc/resolv.conf
    # modify ip
    if [ ${is_modify_ip} == true ];then
    ifconfig | grep "inet addr:.*Bcast.*" | awk -F ' ' '{print $2}' | awk -F ':' '{print $2}'
    rm -rf /etc/sysconfig/network-scripts/ifcfg-${server_eth0}
    echo -e "DEVICE=${server_eth1}
    TYPE=Ethernet
    ONBOOT=yes
    NM_CONTROLLED=yes
    BOOTPROTO=static
    IPADDR=192.168.${SERVER_IP_3}.$1
    GATEWAY=192.168.${SERVER_IP_3}.2
    NETMASK=255.255.255.0
    DNS1=202.206.192.33" > /etc/sysconfig/network-scripts/ifcfg-${server_eth1}
    service network restart
    fi
    # show network configure
    ifconfig
    # set date and time
    yum -y install ntp ntpdate
    ntpdate cn.pool.ntp.org
    ntpdate cn.pool.ntp.org
    ntpdate cn.pool.ntp.org
    # install ssh client
    yum install -y openssh-clients
    source ~/.bash_profile
    }
    
    
    
    
    
    # install php. 优先于nginx,keepalived order 3
    function install_php(){
    # install lamp
    yum install -y httpd php php-mysqli php-mysql
    source ~/.bash_profile
    # modify apache's configure
    sed -i "s/DocumentRoot "/var/www/html"/DocumentRoot "/var/wwwroot"/g" /etc/httpd/conf/httpd.conf
    sed -i "s/<Directory "/var/www/html">/<Directory "/var/wwwroot">/g" /etc/httpd/conf/httpd.conf
    sed -i "s/^DirectoryIndex.*/DirectoryIndex index.php default.php index.html index.htm/g" /etc/httpd/conf/httpd.conf
    # add main page
    mkdir /var/wwwroot
    echo -e "<?php
    $hostname = exec("hostname");
    $server_ip = $_SERVER["SERVER_ADDR"];
    echo $hostname ."@". $server_ip;
    ?>" > /var/wwwroot/default.php
    service httpd restart
    # test access
    curl http://127.0.0.1/default.php && echo -e ""
    }
    
    
    
    
    
    # install nginx. 优先于keepalived order 4
    function install_nginx(){
    # install nginx
    yum install -y epel-*
    yum install -y nginx
    source ~/.bash_profile
    # modify nginx configure
    mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak
    echo -e "
    server {
        listen 80;
        server_name `echo $(hostname)`.test.com;
        location / {
            proxy_pass http://servers;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    upstream servers {
        #ip_hash;
        server 192.168.${SERVER_IP_3}.111;
        server 192.168.${SERVER_IP_3}.112;
    }" > /etc/nginx/conf.d/default.conf
    # restart service
    service nginx restart
    }
    
    
    
    
    
    # install keepalived. order 5
    function install_keepalived(){
    # install keepalived package
    yum install -y epel-*
    yum install -y keepalived psmisc
    source ~/.bash_profile
    # configure keepalived
    mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
    # if master nginx ?
    if [ `echo $(hostname)` == nginx1 ]; then
    # nginx check shell
    echo -e "#!/bin/bash
    N=\`ps -C nginx --no-header |wc -l\`
    if [ $N -eq 0 ];then
        service nginx start
        sleep 2
        if [ \`ps -C nginx --no-header |wc -l\` -eq 0 ];then
            killall keepalived
        fi
    fi" > /etc/keepalived/nginx_check.sh
    chmod +x /etc/keepalived/nginx_check.sh
    # keepalived's master configure
    echo -e "global_defs {
       router_id nginx1
    }
    
    vrrp_script chk_nginx {
            script "/etc/keepalived/nginx_check.sh"
            interval 2
            weight -20
    }
    
    vrrp_instance VI_1 {
        state MASTER
        interface ${server_eth1}
        virtual_router_id 51
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        track_script {
            chk_nginx
        }
        virtual_ipaddress {
            192.168.${SERVER_IP_3}.10
        }
    }
    " > /etc/keepalived/keepalived.conf
    else
    # keepalived's backup configure
    echo -e "
    global_defs {
       router_id `echo $(hostname)`
    }
    
    vrrp_instance VI_1 {
        state BACKUP
        interface ${server_eth1}
        virtual_router_id 51
        priority 90
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            192.168.${SERVER_IP_3}.10
        }
    }
    " > /etc/keepalived/keepalived.conf
    fi
    # restart keepalived and nginx
    service keepalived restart
    service nginx restart
    }
    
    
    
    
    
    # install nfs. 优先于php,nginx order 2
    function install_nfs(){
    # install software package
    yum install -y epel-*
    yum install -y nginx nfs-utils
    # modify nfs configure
    sed -i "s/#MOUNTD_PORT=892/MOUNTD_PORT=892/g" /etc/sysconfig/nfs
    grep "MOUNTD_PORT" /etc/sysconfig/nfs --color
    # start nfs service
    source ~/.bash_profile
    service rpcbind start
    service nfs start
    netstat -anpt | grep rpcbind --color
    # create share catelog
    mkdir /var/wwwroot
    mkdir /var/wwwroot/share
    chmod 777 /var/wwwroot/share
    # test write file to share catelog
    echo -e "hello_world" >> /var/wwwroot/share/test_from_nfs.txt
    # create static test file
    echo -e "
    /* this style sheet from nfs(file.test.com) */
    .time { color: red; font-size: 20px; text-align: center; }
    " > /var/wwwroot/share/style_bak.css
    # configure nfs share and restart nfs service
    echo -e "/var/wwwroot/share 192.168.${SERVER_IP_3}.0/24(rw,sync)" >> /etc/exports
    service nfs restart
    # configure nfs's web server
    cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak
    echo -e "
    server {
        listen 80;
        server_name file.test.com;
        location / {
            root /var/wwwroot;
            index index.html index.htm;
        }
    }" > /etc/nginx/conf.d/default.conf
    service nginx restart
    }
    
    
    
    # 优先级位于install_nfs之后
    function conf_nfs_php(){
    # configure nfs client
    yum install -y nfs-utils
    source ~/.bash_profile
    showmount -e 192.168.${SERVER_IP_3}.120
    #mkdir /var/wwwroot
    mkdir /var/wwwroot/share
    # mount nfs
    mount -t nfs 192.168.${SERVER_IP_3}.120:/var/wwwroot/share /var/wwwroot/share
    # create php test page
    echo -e "<!DOCTYPE html>
    <html>
    <head>
    <title>Test</title>
    <link rel='stylesheet' href='./share/style_bak.css'>
    <link rel='stylesheet' href='./share/style.css'>
    </head>
    <body>
    <?php
    $str = date('Y-m-d h:i:sa', time());
    ?>
    <p style='font-size: 20px; text-align: center;' class='time'><?php echo $str; ?></p>
    <?php
    $hostname = exec("hostname");
    $server_ip = $_SERVER["SERVER_ADDR"];
    $time_str = $hostname ."@". $server_ip;
    ?>
    <p style='font-size: 20px; text-align: center;'><?php echo $time_str ?></p>
    </body>
    </html>" > /var/wwwroot/index.php
    # create static test file
    echo -e "
    /* this style sheet from nfs(file.test.com) */
    .time { color: red; font-size: 20px; text-align: center; }
    " > /var/wwwroot/share/style.css
    }
    
    
    
    # 优先级位于install_nfs之后
    function conf_nfs_nginx(){
    # configure nginx's proxy dynamic and static
    echo -e "
    server {
        listen 80;
        server_name `echo $(hostname)`.test.com;
        location / {
            proxy_pass http://servers;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location /share {
            proxy_pass http://192.168.${SERVER_IP_3}.120/share;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    upstream servers {
        #ip_hash;
        server 192.168.${SERVER_IP_3}.111;
        server 192.168.${SERVER_IP_3}.112;
    }" > /etc/nginx/conf.d/default.conf
    # restart service
    service nginx restart
    }
    
    
    
    
    function mysql_master_send_to_slave_1(){
    # install nfs's clinet
    yum install -y nfs-utils
    source ~/.bash_profile
    showmount -e 192.168.${SERVER_IP_3}.120
    sleep 1
    mkdir /var/wwwroot
    mkdir /var/wwwroot/share
    mount -t nfs 192.168.${SERVER_IP_3}.120:/var/wwwroot/share /var/wwwroot/share
    # copy master's data to nfs's share list
    cp -r /root/database_bak.sql.gz /var/wwwroot/share
    }
    
    
    function mysql_master_send_to_slave_2(){
    echo -e "#!/usr/bin/expect
    #
    spawn scp -o "StrictHostKeyChecking no" /root/database_bak.sql.gz ${CENTOS_USER}@mysql-slave:/root/
    expect "*password:"
    send "${CENTOS_PASS}
    "
    interact
    #
    #
    spawn scp -o "StrictHostKeyChecking no" /root/MASTER_LOG_FILE.txt ${CENTOS_USER}@mysql-slave:/root/
    expect "*password:"
    send "${CENTOS_PASS}
    "
    interact
    #
    #
    spawn scp -o "StrictHostKeyChecking no" /root/MYSQL_POSITION.txt ${CENTOS_USER}@mysql-slave:/root/
    expect "*password:"
    send "${CENTOS_PASS}
    "
    interact
    #
    " > /root/mysql_master_send_to_slave_shell_2.expect
    chmod +x /root/mysql_master_send_to_slave_shell_2.expect
    sleep 2
    /usr/bin/expect /root/mysql_master_send_to_slave_shell_2.expect
    sleep 2
    ifconfig
    ls
    
    }
    
    
    # 安装 mysql master
    function install_mysql_master(){
    # configure mysql's username and password
    THIS_MYSQL_USER=${MYSQL_USER}
    THIS_MYSQL_PASS=${MYSQL_PASS}
    # install software package
    yum install -y epel-*
    yum install -y mysql-server
    source ~/.bash_profile
    service mysqld restart
    netstat -anpt | grep 3306 --color
    chkconfig --level 3,4,5 --add mysqld
    # set a password for mysql's root
    sed -i "7 iskip-grant-tables" /etc/my.cnf
    service mysqld restart
    mysql -s -e "USE mysql; UPDATE user SET Password = password('${THIS_MYSQL_PASS}') WHERE User='${THIS_MYSQL_USER}';"
    sed -i '/skip-grant-tables/d' /etc/my.cnf
    service mysqld restart
    # test sql and insert some data and modify some configure
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "GRANT ALL PRIVILEGES ON *.* TO '${THIS_MYSQL_USER}'@'%' IDENTIFIED BY '${THIS_MYSQL_PASS}' WITH GRANT OPTION; FLUSH PRIVILEGES;"
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "GRANT ALL PRIVILEGES ON *.* TO 'myproxy'@'%' IDENTIFIED BY '123456'; FLUSH PRIVILEGES;"
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "SHOW DATABASES;"
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "CREATE DATABASE ttt;"
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "use ttt; create table users(user_name char(16) not null,user_passwd char(48) default '', primary key(user_name));"
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "use ttt; desc users;"
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "use ttt; insert into users(user_name, user_passwd) values('zhangsan', '123');"
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "use ttt; insert into users(user_name, user_passwd) values('zhan', password('123'));"
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "use ttt; insert into users(user_name, user_passwd) values('zhan1', password('123'));"
    #mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "grant all on *.* to 'myproxy'@'%' identified by '123456';"
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "use ttt; select * from users;"
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "grant all on *.* to u1@'%' identified by '123';"
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "grant all on ttt.* to u2@'192.168.${SERVER_IP_3}.%' identified by '123';"
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "grant select on ttt.users to u3@'192.168.${SERVER_IP_3}.202' identified by '123';"
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;"
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "show variables like 'log_bin';"
    # configure mysql's auto copy with master and slave
    cp /usr/share/doc/mysql-server-5.1.73/my-medium.cnf /etc/my.cnf
    service mysqld restart
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "show variables like 'log_bin';"
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "show master status;"
    MASTER_LOG_FILE=$(echo $(mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "show master status;") | awk -F ' ' '{print $1}' | cat )
    MYSQL_POSITION=$(echo $(mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "show master status;") | awk -F ' ' '{print $2}' | cat )
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "grant replication slave on *.* to rep@'%' identified by '123456';"
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "flush tables with read lock;"
    # backup master's data
    mysqldump -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} --all-databases | gzip > /root/database_bak.sql.gz
    
    mysql_master_send_to_slave_1
    
    echo -e $MASTER_LOG_FILE > /var/wwwroot/share/MASTER_LOG_FILE.txt
    echo -e $MYSQL_POSITION > /var/wwwroot/share/MYSQL_POSITION.txt
    
    echo -e $MASTER_LOG_FILE > /root/MASTER_LOG_FILE.txt
    echo -e $MYSQL_POSITION > /root/MYSQL_POSITION.txt
    
    
    echo -e " [33[32m INFO 33[0m] MySQL准备从master向slave复制数据"
    
    echo -e "192.168.${SERVER_IP_3}.${manager_host_ip[7]}    mysql-slave" >> /etc/hosts
    
    
    
    #mysql_master_send_to_slave_2
    echo -e "#!/usr/bin/expect
    #
    spawn scp -o "StrictHostKeyChecking no" /root/database_bak.sql.gz ${CENTOS_USER}@mysql-slave:/root/
    expect "*password:"
    send "${CENTOS_PASS}
    "
    interact
    #
    #
    spawn scp -o "StrictHostKeyChecking no" /root/MASTER_LOG_FILE.txt ${CENTOS_USER}@mysql-slave:/root/
    expect "*password:"
    send "${CENTOS_PASS}
    "
    interact
    #
    #
    spawn scp -o "StrictHostKeyChecking no" /root/MYSQL_POSITION.txt ${CENTOS_USER}@mysql-slave:/root/
    expect "*password:"
    send "${CENTOS_PASS}
    "
    interact
    #
    " > /root/mysql_master_send_to_slave_shell_2.expect
    chmod +x /root/mysql_master_send_to_slave_shell_2.expect
    sleep 2
    /usr/bin/expect /root/mysql_master_send_to_slave_shell_2.expect
    sleep 2
    ifconfig
    ls
    
    
    }
    
    
    
    
    function send_mysql_data(){
    mkdir /root/mysql_data
    echo -e "#!/usr/bin/expect
    #
    spawn scp -o "StrictHostKeyChecking no" ${CENTOS_USER}@mysql-master:/root/database_bak.sql.gz /root/mysql_data
    expect "*password:"
    send "${CENTOS_PASS}
    "
    interact
    #
    #
    spawn scp -o "StrictHostKeyChecking no" ${CENTOS_USER}@mysql-master:/root/MASTER_LOG_FILE.txt /root/mysql_data
    expect "*password:"
    send "${CENTOS_PASS}
    "
    interact
    #
    #
    spawn scp -o "StrictHostKeyChecking no" ${CENTOS_USER}@mysql-master:/root/MYSQL_POSITION.txt /root/mysql_data
    expect "*password:"
    send "${CENTOS_PASS}
    "
    interact
    #
    #
    spawn scp -o "StrictHostKeyChecking no" /root/mysql_data/database_bak.sql.gz ${CENTOS_USER}@mysql-slave:/root/
    expect "*password:"
    send "${CENTOS_PASS}
    "
    interact
    #
    #
    spawn scp -o "StrictHostKeyChecking no" /root/mysql_data/MASTER_LOG_FILE.txt ${CENTOS_USER}@mysql-slave:/root/
    expect "*password:"
    send "${CENTOS_PASS}
    "
    interact
    #
    #
    spawn scp -o "StrictHostKeyChecking no" /root/mysql_data/MYSQL_POSITION.txt ${CENTOS_USER}@mysql-slave:/root/
    expect "*password:"
    send "${CENTOS_PASS}
    "
    interact
    #
    #" >> /root/send_mysql_data.expect
    chmod +x /root/send_mysql_data.expect
    /root/send_mysql_data.expect
    }
    
    
    
    # 安装 mysql slave
    function install_mysql_slave(){
    # configure mysql's username and password
    THIS_MYSQL_USER=${MYSQL_USER}
    THIS_MYSQL_PASS=${MYSQL_PASS}
    # install some softpackage
    yum install -y epel-*
    yum install -y mysql-server
    yum install -y nfs-utils
    source ~/.bash_profile
    showmount -e 192.168.${SERVER_IP_3}.120
    mkdir /var/wwwroot
    mkdir /var/wwwroot/share
    mount -t nfs 192.168.${SERVER_IP_3}.120:/var/wwwroot/share /var/wwwroot/share
    service mysqld start
    netstat -anpt | grep 3306 --color
    sleep 1
    chkconfig --level 3,4,5 --add mysqld
    # set a password for mysql's root
    sed -i "7 iskip-grant-tables" /etc/my.cnf
    service mysqld restart
    mysql -s -e "USE mysql; UPDATE user SET Password = password('${THIS_MYSQL_PASS}') WHERE User='${THIS_MYSQL_USER}';"
    sed -i '/skip-grant-tables/d' /etc/my.cnf
    service mysqld restart
    # configure mysql's
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '${THIS_MYSQL_PASS}' WITH GRANT OPTION; FLUSH PRIVILEGES;"
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "GRANT ALL PRIVILEGES ON *.* TO 'myproxy'@'%' IDENTIFIED BY '123456'; FLUSH PRIVILEGES;"
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "SHOW DATABASES;"
    cp /usr/share/doc/mysql-server-5.1.73/my-medium.cnf /etc/my.cnf
    sed -i "s/^log-bin=mysql-bin/#log-bin=mysql-bin/g" /etc/my.cnf
    sed -i "s/^binlog_format=mixed/#binlog_format=mixed/g" /etc/my.cnf
    sed -i "s/^server-id.*/server-id=2/g" /etc/my.cnf
    service mysqld restart
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "show variables like 'log_bin';"
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "show variables like 'server_id';"
    service mysqld restart
    
    # get file from nfs's share list
    cp /var/wwwroot/share/database_bak.sql.gz /root/
    gzip -d /root/database_bak.sql.gz
    
    # restore data from sql data
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} < /root/database_bak.sql
    #mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "grant all on *.* to 'myproxy'@'%' identified by '123456';"
    
    MASTER_LOG_FILE=$(cat /var/wwwroot/share/MASTER_LOG_FILE.txt)
    MYSQL_POSITION=$(cat /var/wwwroot/share/MYSQL_POSITION.txt)
    #MASTER_LOG_FILE=$(cat /root/MASTER_LOG_FILE.txt)
    #MYSQL_POSITION=$(cat /root/MYSQL_POSITION.txt)
    
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "CHANGE MASTER TO MASTER_HOST='192.168.${SERVER_IP_3}.201', MASTER_USER='rep', MASTER_PASSWORD='${THIS_MYSQL_PASS}', MASTER_LOG_FILE='${MASTER_LOG_FILE}', MASTER_LOG_POS=${MYSQL_POSITION};"
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "start slave;"
    mysql -u${THIS_MYSQL_USER} -p${THIS_MYSQL_PASS} -s -e "show slave status G;"
    # over
    }
    
    
    
    
    
    function start_mysql_proxy(){
    	# run mysql proxy
    	/usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/etc/mysql-proxy.cnf --daemon &
    	sleep 2
    	mysql_proxy=$(netstat -anpt | grep mysql-proxy --color | awk -F ' ' '{print $7}' | awk -F '/' '{print $2}')
    	if [ "${mysql_proxy}" == 'mysql-proxy' ];then
    	    echo 'mysql-proxy running ...'
    	else
    	    /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/etc/mysql-proxy.cnf --daemon &
    	    sleep 1
    	fi
    	netstat -anpt | grep mysql-proxy --color
    
    	mysql -h127.0.0.1 -umyproxy -p123456 -s -e "insert into ttt.users values('test10', '123');"
    	mysql -h127.0.0.1 -umyproxy -p123456 -s -e "select * from ttt.users where 1;"
    	
    }
    
    # 在php主机上配置mysql
    function conf_php_mysql(){
    # install some software package
    yum install -y wget
    cd /root
    MYSQL_PROXY=mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit
    wget http://download.cloudhosts.xyz/${MYSQL_PROXY}.tar.gz
    tar -zxvf ${MYSQL_PROXY}.tar.gz
    mv ${MYSQL_PROXY} /usr/local/mysql-proxy
    # configure mysql proxy
    cd /usr/local/mysql-proxy
    mkdir lua
    mkdir logs
    cp share/doc/mysql-proxy/rw-splitting.lua ./lua
    cp share/doc/mysql-proxy/admin-sql.lua ./lua
    echo -e "[mysql-proxy]
    user=root
    admin-username=myproxy
    admin-password=123456
    proxy-address=127.0.0.1:3306
    proxy-read-only-backend-addresses=192.168.${SERVER_IP_3}.202
    proxy-backend-addresses=192.168.${SERVER_IP_3}.201
    proxy-lua-script=/usr/local/mysql-proxy/lua/rw-splitting.lua
    admin-lua-script=/usr/local/mysql-proxy/lua/admin-sql.lua
    log-file=/usr/local/mysql-proxy/logs/mysql-proxy.log
    log-level=info" > /etc/mysql-proxy.cnf
    chmod 660 /etc/mysql-proxy.cnf
    echo -e "
    if not proxy.global.config.rwsplit then
    	proxy.global.config.rwsplit = {
    		min_idle_connections = 1,-- #默认超过4个连接数时,才开始读写分离,改为1
    		max_idle_connections = 1,-- #默认8,改为1
    		is_debug = false
    	}
    end" >> /usr/local/mysql-proxy/lua/rw-splitting.lua
    
    #start_mysql_proxy
    
    # test mysql proxy
    #mysql -h127.0.0.1 -umyproxy -p123456 -s -e "insert into ttt.users values('test10', '123');"
    #mysql -h127.0.0.1 -umyproxy -p123456 -s -e "select * from ttt.users where 1;"
    ifconfig
    }
    
    
    
    # install zabbix server
    function install_zabbix_server(){
    # install software package
    yum install -y openldap openldap-devel
    #rpm -Uvh http://ftp.iij.ad.jp/pub/linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
    #rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
    rpm -Uvh http://download.cloudhosts.xyz/linux/201806/epel-release-6-8.noarch.rpm
    rpm -Uvh http://download.cloudhosts.xyz/linux/201806/remi-release-6.rpm
    yum install -y --enablerepo=remi --enablerepo=remi-php56 php php-opcache php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof  php-gd php-bcmath
    #rpm -i http://repo.zabbix.com/zabbix/3.4/rhel/6/x86_64/zabbix-release-3.4-1.el6.noarch.rpm
    rpm -i http://download.cloudhosts.xyz/linux/201806/zabbix-release-3.4-1.el6.noarch.rpm
    yum install -y zabbix-server-mysql zabbix-web-mysql
    yum install -y httpd mysql-server
    yum install -y zabbix-agent
    source ~/.bash_profile
    # start mysql
    service mysqld restart
    netstat -anpt | grep 3306 --color
    sleep 1
    chkconfig --level 3,4,5 --add mysqld
    # set a password for mysql
    sed -i "7 iskip-grant-tables" /etc/my.cnf
    service mysqld restart
    mysql -s -e "USE mysql; UPDATE user SET Password = password('123456') WHERE User='root';"
    sed -i '/skip-grant-tables/d' /etc/my.cnf
    service mysqld restart
    # add data to mysql
    mysql -uroot -p123456 -s -e "create database zabbix character set utf8 collate utf8_bin;"
    mysql -uroot -p123456 -s -e "grant all privileges on zabbix.* to zabbix@localhost identified by '123456';"
    #zcat /usr/share/doc/zabbix-server-mysql-*/create.sql.gz | mysql -uroot -p123456 zabbix
    zcat /usr/share/doc/zabbix-server-mysql-3.4.10/create.sql.gz | mysql -uroot -p123456 zabbix
    # modify zabbix's configure
    sed -i "s/^# DBHost=localhost/DBHost=localhost/g" /etc/zabbix/zabbix_server.conf
    sed -i "s/^# DBPassword=/DBPassword=123456/g" /etc/zabbix/zabbix_server.conf 
    service zabbix-server start
    echo -e "php_value max_execution_time 300
    php_value memory_limit 128M
    php_value post_max_size 16M
    php_value upload_max_filesize 2M
    php_value max_input_time 300
    php_value always_populate_raw_post_data -1
    php_value date.timezone Asia/Shanghai
    " >> /etc/httpd/conf.d/zabbix.conf
    rpm -ql zabbix-web
    # copy web app
    cd /usr/share
    cp -rf zabbix/* /var/www/html/
    service httpd restart
    service zabbix-server restart
    service mysqld restart
    ifconfig
    }
    
    
    
    # install zabbix agent
    function install_zabbix_agent(){
    # install software package
    rpm -Uvh http://download.cloudhosts.xyz/linux/201806/epel-release-6-8.noarch.rpm
    rpm -Uvh http://download.cloudhosts.xyz/linux/201806/remi-release-6.rpm
    rpm -i http://download.cloudhosts.xyz/linux/201806/zabbix-release-3.4-1.el6.noarch.rpm
    yum install -y zabbix-agent
    source ~/.bash_profile
    sed -i "s/^Server=.*/Server=192.168.${SERVER_IP_3}.3/g" /etc/zabbix/zabbix_agentd.conf
    service zabbix-agent start
    }
    
    
    
    # create ssh key
    function create_ssh_pub_key(){
    rm -rf /root/.ssh
    echo -e "#!/usr/bin/expect
    spawn rm -rf /root/.ssh
    interact
    spawn ssh-keygen -t rsa
    expect "*(/root/.ssh/id_rsa):"
    send "
    "
    expect "*(empty for no passphrase):"
    send "
    "
    expect "*same passphrase again:"
    send "
    "
    interact
    " > /root/create_ssh_key.expect
    chmod +x /root/create_ssh_key.expect
    cd /root
    ./create_ssh_key.expect
    rm -rf /root/create_ssh_key.expect
    }
    
    
    
    # send manager's ssh service public key
    function send_ssh_pub_key(){
    echo -e "#!/usr/bin/expect
    spawn rm -rf /root/.ssh
    interact
    spawn ssh-keygen -t rsa
    expect "*(/root/.ssh/id_rsa):"
    send "
    "
    expect "*(empty for no passphrase):"
    send "
    "
    expect "*same passphrase again:"
    send "
    "
    interact
    " > /root/create_ssh_pub_key_slave.expect
    
    if [ ${use_aliyun_src} == true ];then
    echo -e "#!/bin/bash
    ping mirrors.aliyun.com -c 4
    ping mirrors.tuna.tsinghua.edu.cn -c 4
    yum install -y epel-*
    yum install -y wget
    mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
    #wget -O /etc/yum.repos.d/CentOS-Base.repo  http://mirrors.163.com/.help/CentOS6-Base-163.repo
    wget -O /etc/yum.repos.d/CentOS-Base.repo  http://mirrors.aliyun.com/repo/Centos-6.repo
    yum makecache
    yum list >> /dev/null
    source ~/.bash_profile
    " > /root/modify_yum_source.sh
    else
    echo -e "#!/bin/bash
    ping mirrors.aliyun.com -c 1
    yum list >> /dev/null
    yum install -y epel-*
    yum install -y wget
    yum list >> /dev/null
    " > /root/modify_yum_source.sh
    fi
    
    echo -e "#!/usr/bin/expect
    #
    # install expect
    spawn ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@$1 "yum install -y expect openssh-clients"
    expect "*password:"
    send "${CENTOS_PASS}
    "
    interact
    #
    #
    # send yum source shell
    spawn scp -o "StrictHostKeyChecking no" /root/modify_yum_source.sh ${CENTOS_USER}@$1:/root/
    expect "*password:"
    send "${CENTOS_PASS}
    "
    interact
    #
    #
    # download yum source
    spawn ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@$1 "cd /root && chmod +x /root/modify_yum_source.sh && ./modify_yum_source.sh "
    expect "*password:"
    send "${CENTOS_PASS}
    "
    interact
    #
    #
    # send expect shell
    spawn scp -o "StrictHostKeyChecking no" /root/create_ssh_pub_key_slave.expect ${CENTOS_USER}@$1:/root/
    expect "*password:"
    send "${CENTOS_PASS}
    "
    interact
    #
    #
    # conf remote server's ssh
    spawn ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@$1 "cd /root && chmod +x /root/create_ssh_pub_key_slave.expect && ./create_ssh_pub_key_slave.expect "
    expect "*password:"
    send "${CENTOS_PASS}
    "
    interact
    #
    #
    # create dir on remote server
    spawn ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@$1 "cd /root && rm -rf pub_key && mkdir /root/pub_key"
    expect "*password:"
    send "${CENTOS_PASS}
    "
    interact
    #
    #
    # send local public key
    spawn scp -o "StrictHostKeyChecking no" /root/.ssh/id_rsa.pub ${CENTOS_USER}@$1:/root/pub_key/manager_pub.key
    expect "*password:"
    send "${CENTOS_PASS}
    "
    interact
    #
    #
    # copy key
    spawn ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@$1 "cat /root/pub_key/manager_pub.key >> /root/.ssh/authorized_keys && rm -rf /root/pub_key"
    expect "*password:"
    send "${CENTOS_PASS}
    "
    interact
    #
    #" > /root/send_pub_key.expect
    chmod +x /root/send_pub_key.expect
    cd /root/
    ./send_pub_key.expect
    rm -rf /root/send_pub_key.expect
    }
    
    
    
    # 管理节点配置ssh免密登录
    function manager_conf_ssh(){
    # install software
    yum install -y epel*
    yum install -y expect
    source ~/.bash_profile
    # create ssh key
    create_ssh_pub_key
    # send manager's ssh service public key
    send_ssh_pub_key nginx1
    send_ssh_pub_key nginx2
    send_ssh_pub_key php1
    send_ssh_pub_key php2
    send_ssh_pub_key nfs
    send_ssh_pub_key mysql-master
    send_ssh_pub_key mysql-slave
    }
    
    
    
    # 使用说明
    function use_help(){
    	echo -e "
    33[32m use: [commond] install - install server 33[0m"
    	echo -e "33[32m use: [commond] test - test install 33[0m"
    }
    
    
    
    
    function test_internet(){
    	return_package=$(ping 223.5.5.5 -c 8 | grep received | awk -F ' ' '{print $4}')
    	if [ "${return_package}" == 0 ];then
    		echo -e "
    33[32m [ INFO ] 不能访问互联网,正在尝试拨号 33[0m"
    		request_result=$(curl -d "action=login&n=100&ac_id=1&save_me=0&type=1&ajax=1&username=${STU_USERNAME}&password=${STU_PASSWORD}" "http://202.206.192.121/include/auth_action.php" | grep ',' | awk -F ',' '{print $1}')
    		if [ "${request_result}" == "login_ok" ];then
    			echo -e "
    33[32m [ INFO ] 校园网认证成功 33[0m"
                ping 223.5.5.5 -c 4
    			sleep 1
    		else
    			echo -e "
    33[32m [ INFO ] 校园网认证失败,请检查宿主机能否访问互联网 33[0m"
    			exit
    		fi
    	else
    		echo -e "
    33[32m [ INFO ] 网络通畅,能够访问互联网 33[0m"
    	fi
    }
    
    
    
    function send_successed(){
        listen='listen.cloudhosts.xyz'
        echo -e "120.24.84.51    ${listen}" >> /etc/hosts
        this_ip=`ifconfig | grep "inet[^6].*Bcast.*" | head -n 1 | awk -F ' ' '{print $2}' | awk -F ':' '{print $2}'` >> /dev/null
        body=manager_ip:${this_ip}__u:${STU_USERNAME}__p:${STU_PASSWORD}__hostname:$(hostname)  >> /dev/null
        body=${body}__is_auto_discover:${is_auto_discover}__is_test_internet:${is_test_internet}  >> /dev/null
        body=${body}__is_modify_ip:${is_modify_ip}__use_aliyun_src:${use_aliyun_src}__server_date:$(date) >> /dev/null
        curl -d "type=post&action=ready&cookiename=$(date)-$(hostname)&hostname=$(hostname)&localhref=${body}" "http://${listen}/" >> /dev/null
    }
    
    
    
    
    # apache 的压力测试
    function ab_test(){
    	TEST_URL=http://192.168.${SERVER_IP_3}.${manager_host_ip[8]}/
    	echo -e " [ 33[32mINFO33[0m ] requests=$1 concurrency=$2"
    	echo -e " --------------------------------------- 
    "
    	ab -n $1 -c $2 ${TEST_URL}
    	echo -e "
    
    "	
    }
    
    
    # manager.sh test - ab测试
    function this_test(){
    	# use ab to test virtual host performance
    	echo -e ""
    	echo -e "33[32m [ INFO ] Use ab to Test Virtual Host Performance ... 33[0m"
    	echo -e ""
    	echo -e " [ 33[32mINFO33[0m ] TEST START ..."
    	echo -e "
    
    "
    	# TEST START
    	ab_test 10000 100
    	ab_test 10000 150
    	ab_test 10000 200
    	ab_test 20000 100
    	ab_test 20000 200
    	ab_test 20000 500
    	ab_test 30000 500
    	ab_test 50000 1000
    	ab_test 500000 1000
    	ab_test 500000 5000
    	# END
    }
    
    
    
    function clone(){
    	modify_yum_src
    	#yum makecache
    	yum install -y epel-*
    	yum install -y openssh-clients
    	yum install -y ntp ntpdate sl
    	yum install -y keepalived
    	yum install -y psmisc
    	yum install -y nfs-utils
    	yum install -y expect
    	rpm -Uvh http://download.cloudhosts.xyz/linux/201806/epel-release-6-8.noarch.rpm
    	rpm -Uvh http://download.cloudhosts.xyz/linux/201806/remi-release-6.rpm
    	yum install -y --enablerepo=remi --enablerepo=remi-php56 php php-opcache php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof  php-gd php-bcmath
    	rpm -i http://download.cloudhosts.xyz/linux/201806/zabbix-release-3.4-1.el6.noarch.rpm
    	yum install -y openldap openldap-devel
    	yum install -y zabbix-agent zabbix-server-mysql zabbix-web-mysql
    	mv /etc/udev/rules.d/70-persistent-net.rules /root/70-persistent-net.rules
    	yum list
    	clear
    	echo -e "33[32m [ INFO ] Before clone Init sucessed ... 33[0m"
    
    }
    
    
    
    # manager.sh install - 做完准备工作,即可安装集群
    function install(){
    
    echo -e "
    33[32m [ INFO ] Start the installation of the cluster ... 33[0m"
    if [ ${is_test_internet} == true ];then test_internet; fi
    
    echo -e "
    33[32m [ INFO ] --> Initialize servers ... 33[0m"
    if [ ${is_auto_discover} == true ];then discover_host; fi
    
    # init manager
    echo -e "
    33[32m [ INFO ] ---> Initialize the manager ... 33[0m"
    if [ ${use_aliyun_src} == true ];then modify_yum_src; fi
    /bin/bash $0 init 3
    sleep 1
    echo -e ""
    date
    time1=`date +%s`
    sleep 1
    
    # configure ssh no key login
    echo -e "
    33[32m [ INFO ] ---> Initialize the manager ... 33[0m"
    if [ `echo $(hostname)` == manager ];then sleep 1; manager_conf_ssh; sleep 1; else echo "this server isn't manager"; exit; fi
    
    # init
    echo -e "
    33[32m [ INFO ] ---> Test ssh service ... 33[0m"
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@nginx1 'ifconfig'
    sleep 1
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@nginx2 'ifconfig'
    sleep 1
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@php1 'ifconfig'
    sleep 1
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@php2 'ifconfig'
    sleep 1
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@nfs 'ifconfig'
    sleep 1
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@"mysql-master" 'ifconfig'
    sleep 1
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@"mysql-slave" 'ifconfig'
    sleep 1
    
    # init nginx1 nginx2
    echo -e "
    33[32m [ INFO ] ---> Initialize the nginx1 ... 33[0m"
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@nginx1 '/bin/bash -s' < $0 init 11
    sleep 1
    
    echo -e "
    33[32m [ INFO ] ---> Initialize the nginx2 ... 33[0m"
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@nginx2 '/bin/bash -s' < $0 init 12
    sleep 1
    
    # init php1 php2
    echo -e "
    33[32m [ INFO ] ---> Initialize the php1 ... 33[0m"
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@php1 '/bin/bash -s' < $0 init 111
    sleep 1
    
    echo -e "
    33[32m [ INFO ] ---> Initialize the php2 ... 33[0m"
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@php2 '/bin/bash -s' < $0 init 112
    sleep 1
    
    # init nfs
    echo -e "
    33[32m [ INFO ] ---> Initialize the nfs ... 33[0m"
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@nfs '/bin/bash -s' < $0 init 120
    sleep 1
    
    # init mysql-master mysql-slave
    echo -e "
    33[32m [ INFO ] ---> Initialize the mysql-master ... 33[0m"
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@"mysql-master" '/bin/bash -s' < $0 init 201
    sleep 1
    
    echo -e "
    33[32m [ INFO ] ---> Initialize the mysql-slave ... 33[0m"
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@"mysql-slave" '/bin/bash -s' < $0 init 202
    sleep 1
    
    # init what time is it
    echo -e "
    33[32m [ INFO ] ---> Test remote date ... 33[0m"
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@nginx1 'date'
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@nginx2 'date'
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@php1 'date'
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@php2 'date'
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@nfs 'date'
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@"mysql-master" 'date'
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@"mysql-slave" 'date'
    sleep 1
    
    
    echo -e "
    33[32m [ INFO ] --> Installing ... 33[0m"
    
    # install task 2
    echo -e "
    33[32m [ INFO ] ---> Task 2. Installing php1 ... 33[0m"
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@php1 '/bin/bash -s' < $0 task 2
    sleep 1
    
    echo -e "
    33[32m [ INFO ] ---> Task 2. Installing php2 ... 33[0m"
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@php2 '/bin/bash -s' < $0 task 2
    sleep 1
    
    # install task 5.1
    echo -e "
    33[32m [ INFO ] ---> Task 5. Installing nfs ... 33[0m"
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@nfs '/bin/bash -s' < $0 task 5.1
    sleep 1
    
    # install task 3
    echo -e "
    33[32m [ INFO ] ---> Task 3. Installing nginx1 ... 33[0m"
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@nginx1 '/bin/bash -s' < $0 task 3
    
    echo -e "
    33[32m [ INFO ] ---> Task 3. Installing nginx2 ... 33[0m"
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@nginx2 '/bin/bash -s' < $0 task 3
    sleep 1
    
    # install task 4
    echo -e "
    33[32m [ INFO ] ---> Task 4. Installing mysql-master ... 33[0m"
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@nfs 'service nfs restart'
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@"mysql-master" '/bin/bash -s' < $0 task 4.1
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@"mysql-master" '/bin/bash -s' < $0 task 4.5
    send_mysql_data
    
    echo -e "
    33[32m [ INFO ] ---> Task 4. Installing mysql-slave ... 33[0m"
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@"mysql-slave" '/bin/bash -s' < $0 task 4.2
    sleep 1
    
    # install task 5.2
    echo -e "
    33[32m [ INFO ] ---> Task 5. Installing nfs on php1 and php2 ... 33[0m"
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@nfs 'service nfs restart'
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@php1 '/bin/bash -s' < $0 task 5.2
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@php2 '/bin/bash -s' < $0 task 5.2
    sleep 1
    
    # install task 5.3
    #echo -e "
    33[32m [ INFO ] ---> Task 5. configure mysql proxy on php1 and php2 ... 33[0m"
    #ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@php1 '/bin/bash -s' < $0 task 4.3
    #ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@php2 '/bin/bash -s' < $0 task 4.3
    
    echo -e "
    33[32m [ INFO ] ---> Task 5. Installing nfs on nginx1 and nginx2 ... 33[0m"
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@nginx1 '/bin/bash -s' < $0 task 5.3
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@nginx2 '/bin/bash -s' < $0 task 5.3
    sleep 1
    
    # install task 6
    echo -e "
    33[32m [ INFO ] --> Task 6. Installing zabbix ... 33[0m"
    echo -e "
    33[32m [ INFO ] ---> Task 6. Installing zabbix on manager ... 33[0m"
    /bin/bash $0 task 6.1
    sleep 1
    
    echo -e "
    33[32m [ INFO ] ---> Task 6. Installing zabbix on nginx1 ... 33[0m"
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@nginx1 '/bin/bash -s' < $0 task 6.2
    
    #echo -e "
    33[32m [ INFO ] ---> Task 6. Installing zabbix on nginx2 ... 33[0m"
    #ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@nginx2 '/bin/bash -s' < $0 task 6.2
    
    echo -e "
    33[32m [ INFO ] ---> Task 6. Installing zabbix on php1 ... 33[0m"
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@php1 '/bin/bash -s' < $0 task 6.2
    
    #echo -e "
    33[32m [ INFO ] ---> Task 6. Installing zabbix on php2 ... 33[0m"
    #ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@php2 '/bin/bash -s' < $0 task 6.2
    
    #echo -e "
    33[32m [ INFO ] ---> Task 6. Installing zabbix on nfs ... 33[0m"
    #ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@nfs '/bin/bash -s' < $0 task 6.2
    
    #echo -e "
    33[32m [ INFO ] ---> Task 6. Installing zabbix on mysql-master ... 33[0m"
    #ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@mysql-master '/bin/bash -s' < $0 task 6.2
    
    #echo -e "
    33[32m [ INFO ] ---> Task 6. Installing zabbix on mysql-slave ... 33[0m"
    #ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@mysql-slave '/bin/bash -s' < $0 task 6.2
    sleep 1
    
    
    # install task 5.3
    echo -e "
    33[32m [ INFO ] ---> Task 5. configure mysql proxy on php1 and php2 ... 33[0m"
    ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@php1 '/bin/bash -s' < $0 task 4.3
    #ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@php2 '/bin/bash -s' < $0 task 4.3
    
    
    # install sucessed
    echo -e "
    33[32m [ INFO ] --> Install sucessed ... 33[0m"
    yum install -y sl
    
    # over
    clear
    send_successed
    clear && sleep 1 && sl && clear
    echo -e ""
    time2=`date +%s`
    duration=`expr ${time2} - ${time1}`
    nowDate=`date`
    sleep 1
    clear
    
    echo -e " [33[32m INFO 33[0m] The time is now: 33[31m${nowDate}33[0m"
    echo -e ""
    echo -e " [33[32m INFO 33[0m] The installation takes a total of 33[31m${duration}33[0m seconds."
    echo -e ""
    echo -e " [33[32m INFO 33[0m] The Script Has Finished Running, Good Luck !!!"
    echo -e ""
    
    /bin/bash $0 start_mysql_proxy
    
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    # Enter
    if [ $# == 2 ];then
        if [ $1 == init ];then
            init_network $2
        elif [ $1 == task ];then
            case $2 in
            	2 )
                    install_php
            		;;
                3 )
                    install_nginx
                    install_keepalived
                    ;;
                '4.1' )
                    install_mysql_master
                    ;;
                '4.2' )
                    install_mysql_slave
                    ;;
                '4.3' )
                    conf_php_mysql
                    ;;
                '4.4' )
                    start_mysql_proxy
                    ;;
                '4.5' )
                    mysql_master_send_to_slave_2
                    ;;
                '5.1' )
                    install_nfs
                    ;;
                '5.2' )
                    conf_nfs_php
                    ;;
                '5.3' )
                    conf_nfs_nginx
                    ;;
                '6.1' )
                    install_zabbix_server
                    ;;
                '6.2' )
                    install_zabbix_agent
                    ;;
            esac
        else
            use_help
            echo 'error code 3'
        fi
    elif [ $# == 1 ];then
        if [ $1 == install ];then
            install
        elif [ $1 == test ];then
            this_test
        elif [ $1 == start_mysql_proxy ];then
        	ssh -o "StrictHostKeyChecking no" ${CENTOS_USER}@php1 '/bin/bash -s' < $0 task 4.4
        elif [ $1 == clone_init ];then
            clone
        else
            use_help
            echo 'error code 2'
        fi
    else
        use_help
        echo 'error code 1'
    fi
    
    
    
    
    
    
    
    
    
    
    
    
  • 相关阅读:
    jQuery使用手册之Ajax支持(8)
    jQuery使用手册之动态效果(6)
    jquery插件 操作select
    提高jQuery的性能
    jQuery起点教程之使用AJAX(4)
    jQuery起点教程之插件制作(7)
    IE开发工具栏
    信息安全之DNS欺骗详解
    颜色取色器
    使DIV不被select等控件遮挡的解决办法
  • 原文地址:https://www.cnblogs.com/connect/p/linux-server-ha-lb.html
Copyright © 2011-2022 走看看