美文网首页
第十七周作业

第十七周作业

作者: 卫清华 | 来源:发表于2019-06-17 08:59 被阅读0次

    1、搭建zabbix服务,实现监控linux和windows的内存,cpu,磁盘,网络等基础指标

    1.部署lnmp环境

    安装nginx

    源码安装tengine-2.2.2.tar.gz

    tar xf tengine-2.2.2.tar.gz

    yum -y install gcc gcc-c++ autoconf automake

    yum -y install openssl-devel pcre-devel zlib-devel

    cd tengine-2.2.2

    ./configure --prefix=/data/app/nginx/ \

    --with-http_gzip_static_module \

    --with-http_realip_module \

    --with-http_stub_status_module \

    --with-http_concat_module \

    --with-pcre

    如果这一步没有提示缺少依赖的话,说明检查通过,如果缺少依赖,请看上边安装依赖库。接下来就是编译了。

    make && make install

    cd /data/app/nginx/sbin

    ./nginx启动nginx

    安装zabbix

    wget -O zabbix-3.4.2.tar.gz http://sourceforge.net/projects/zabbix/files/ZABBIX%20Latest%20Stable/3.4.2/zabbix-3.4.2.tar.gz/download

    yum -y install net-snmp-devel libxml2-devel libcurl-deve libevent libevent-devel

    tar -zxf zabbix-3.4.2.tar.gz

    cd zabbix-3.4.2

    mkdir /data/app/zabbix

    ./configure --prefix=/data/app/zabbix --enable-server --enable-agent --with-mysql --enable-ipv6 --with-net-snmp --with-libcurl --with-libxml2

    make && make install

    groupadd zabbix

    useradd -r -g zabbix zabbix

    cd /usr/local && chown -R zabbix:zabbix zabbix

    mysql -uroot -p

    mysql> create database if not exists zabbix default character set utf8 collate utf8_general_ci;

    mysql> use zabbix;

    mysql> source /data/install/zabbix-3.4.2/database/mysql/schema.sql;

    mysql> source /data/install/zabbix-3.4.2/database/mysql/images.sql;

    mysql> source /data/install/zabbix-3.4.2/database/mysql/data.sql;

    cd /data/app/zabbix

    mkdir logs

    chown zabbix:zabbix logs

    vim ./etc/zabbix.server.conf

    LogFile=/data/app/zabbix/logs/zabbix_server.log

    PidFile=/tmp/zabbix_server.pid

    DBHost=localhost

    DBName=zabbix

    DBUser=zabbix

    DBPassword=zabbix

    DBSocket=/var/lib/mysql/mysql.sock

    Include=/data/app/zabbix/etc/zabbix_server.conf.d/*.conf

    vim ./etc/zabbix_agentd.conf

    PidFile=/tmp/zabbix_agentd.pid

    LogFile=/data/app/zabbix/logs/zabbix_agentd.log

    Include=/data/app/zabbix/etc/zabbix_agentd.conf.d/*.conf

    /data/app/zabbix/sbin/zabbix_server

    /data/app/zabbix/sbin/zabbix_agent

    PS:如果再启动的时候报错:error while loading shared libraries: libmysqlclient.so.20: cannot open shared object file: No such file or directory

    解决方法:

    ln -s /usr/local/mysql/lib/libmysqlclient.so.20 /usr/lib/

    ldconfig

    然后再次启动服务

    cp /data/zabbix-3.4.2/frontends/php/ /data/app/nginx/html/zabbix

    安装php

    wget  http://au1.php.net/get/php-5.6.30.tar.gz/from/this/mirror

    #mv mirror php-5.6.30.tar.gz

    #tar zxvf php-5.6.30.tar.gz

    #cd php-5.6.30

    # ./configure --prefix=/data/app/php --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-MySQL --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-xmlrpc --with-jpeg-dir --with-xsl --with-zlib --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-zip

    配置的过程中可能会报如下错误

    错误1:

    xml2-config not found. Please check your libxml2 installation.

    解决办法

    安装libxml2相关组件

    #yum install libxml2

    #yum install libxml2-devel -y

    错误2:

    Please reinstall the libcurl distribution -

    ​easy.h should be in /include/curl/

    安装curl相关组件

    #yum install curl curl-devel

    错误3:

    configure: error: png.h not found.

    安装libpng相关组件

    #yum install libpng

    #yum install libpng-devel

    错误4:

    freetype-config not found.

    安装freetype相关组件

    #yum install freetype-devel

    错误5:

    xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution

    安装libxslt相关组件

    #yum install libxslt-devel

    配置完成

    #make && make install

    看到这句话,表明安装完成!

    配置Nginx支持PHP

    #vim nginx.conf

    修改完,这段代码变为,红色部分为我们主机目录为/mnt/project,需要修改fastcgi_param SCRIPT_FILENAME指向对应目录即可:

    location ~ .php$ {

    root           /mnt/project;     //项目根目录

    fastcgi_pass   127.0.0.1:9000;

    fastcgi_index  index.php;

    fastcgi_param  SCRIPT_FILENAME  /mnt/project$fastcgi_script_name;在$符前面加上项目根目录

    include        fastcgi_params;

    设置主目录设置为/mnt/project。

    root   /mnt/project;      //项目根目录

            index  index.html  index.php;

    ​try_files $uri $uri/ /index.php?$uri&$args;   #用框架开发必须配置url重写配置

    ​}

    保存退出。

    /data/app/nginx/sbin/nginx -s reload

    在项目目录下创建一个index.php

    在浏览器中打开http://远程ip/phpinfo.php

    安装msyql

    yum -y install cmake bison git ncurses-devel gcc gcc-c++

    wget -c http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.14-linux-glibc2.5-x86_64.tar.gz/from/http://cdn.mysql.com/ -O mysql-5.6.14-linux-glibc2.5-x86_64.tar.gz

    groupadd mysql

    useradd -g mysql mysql

    tar zxvf mysql-5.6.14-linux-glibc2.5-x86_64.tar.gz

    mkdir /data/app/mysql

    mkdir /data/app/mysql/data

    cd mysql-5.6.14

    cmake -DCMAKE_INSTALL_PREFIX=/data/app/mysql -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DMYSQL_DATADIR=/data/app/mysql/data -DMYSQL_TCP_PORT=3306 -DMYSQL_USER=mysql -DENABLE_DOWNLOADS=1

    如果此过程出现error,则执行命令

    # rm -rf /data/install/mysql-5.6.38/CMakeCache.txt

    安装相关依赖包,再重新执行cmake

    make

    make install

    make clean

    chown -R mysql:mysql /data/app/mysql/data/

    chown -R mysql:mysql /data/app/mysql

    cd /data/app/mysql

    ./scripts/mysql_install_db --user=mysql --defaults-file=/etc/my.cnf

    service mysqld  start

    Mysql -uroot -p

    create database zabbix character set utf8 collate utf8_bin;

    grant all privileges on zabbix.* to zabbix@localhost identified by 'password';

    Quit

    访问http://IP/zabbix/index.php

    输入账号密码

    2、搭建zabbix服务,监控nginx status

    ZABBIX监测NGINX STATUS状态

    一、启用nginx status状态详解

    nginx和php-fpm一样内建了一个状态页,对于想了解nginx的状态以及监控nginx非常有帮助。为了后续的zabbix监控,我们需要先了解nginx状态页是怎么回事。

    1、启用nginx status配置

    在默认主机里面加上location或者你希望能访问到的主机里面。

    server {

        listen  *:80 default_server;

        server_name _;

        location /ngx_status

        {

            stub_status on;

            access_log off;

            #allow 127.0.0.1;

            #deny all;

        }

    }

    2.重启nginx

    请依照你的环境重启你的nginx

    # service nginx restart

    3.打开status页面

    # curl http://127.0.0.1/ngx_status

    Active connections: 11921

    server accepts handled requests

     11989 11989 11991

    Reading: 0 Writing: 7 Waiting: 42

    4. nginx status详解

    active connections– 活跃的连接数量

    server accepts handled requests— 总共处理了11989个连接 , 成功创建11989次握手, 总共处理了11991个请求

    reading— 读取客户端的连接数.

    writing— 响应数据到客户端的数量

    waiting— 开启 keep-alive 的情况下,这个值等于 active – (reading+writing), 意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接.

    nginx status信息

    # curl http://127.0.0.1/ngx_status

    Active connections: 11921

    server accepts handled requests

     11989 11989 11991

    Reading: 0 Writing: 7 Waiting: 42

    二、Zabbix监控nginx性能

    # curl http://127.0.0.1/ngx_status

    Active connections: 11921

    server accepts handled requests

     11989 11989 11991

    Reading: 0 Writing: 7 Waiting: 42

    以上为nginx性能计数,我们除了监控以上数据,还需要监控nginx进程状态,并且配置触发器!

    zabbix客户端配置

    编写客户端脚本ngx_status.sh

    #!/bin/bash

    HOST="127.0.0.1"

    PORT="80"

    #检测nginx进程是否存在

    function ping {

        /sbin/pidof nginx | wc -l

    }

    #检测nginx性能

    function active {

        /usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'

    }

    function reading {

        /usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| grep 'Reading' | awk '{print $2}'

    }

    function writing {

        /usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| grep 'Writing' | awk '{print $4}'

    }

    function waiting {

        /usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'

    }

    function accepts {

        /usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| awk NR==3 | awk '{print $1}'

    }

    function handled {

        /usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| awk NR==3 | awk '{print $2}'

    }

    function requests {

        /usr/bin/curl "http://$HOST:$PORT/ngx_status/" 2>/dev/null| awk NR==3 | awk '{print $3}'

    }

    #执行function

    zabbix客户端配置

    将自定义的UserParameter加入配置文件,然后重启agentd,如下:

    #cat /usr/local/zabbix-3.0.0/etc/zabbix_agentd.conf | grep nginx

    UserParameter=nginx.status[*],/usr/local/zabbix-3.0.0/scripts/ngx-status.sh $1

    # killall zabbix_agentd

    # /usr/local/zabbix-3.0.0/sbin/zabbix_agentd

    zabbix_get获取数据

    此步骤可以跳过,但是最好是测试一下,因为通过此命令我们可以检测配置是否正确

    # /usr/local/zabbix-3.0.0/bin/zabbix_get -s 10.10.1.121 -k 'nginx.status[accepts]'

    9570756

    # /usr/local/zabbix-3.0.0/bin/zabbix_get -s 10.10.1.121 -k 'nginx.status[ping]'

    在zabbix server服务器上执行如上命令, 10.10.1.121为agentd机器

    zabbix web端配置

    3、采用corosync v2+pacemaker实现mariadb的高可用

    实验环境:

        两台mariaDB服务器 172.16.10.20 172.16.10.21

        fip:172.16.10.28

        MariaDB文件存储共享:172.16.10.22

    实验准备:

       1、两个节点的主机名称和对应的IP地址解析服务可以正常工作,且每个节点的主机名称需要跟"uname -n“命令的结果保持一致

      vim /etc/hosts

         172.16.10.20 21.xuphoto.com 20xu

         172.16.10.21 22.xuphoto.com 21xu

         Node1:

         # sed -i 's@\(HOSTNAME=\).*@\121.xuphoto.com@g'

         # hostname 21.xuphoto.com

         Node2:

         # sed -i 's@\(HOSTNAME=\).*@\122.xuphoto.com@g'

         # hostname 22.xuphoto.com

       2、各节点时间保持一致可添加统一NTP服务器同步任务:

     crontab -e

         */3 * * * * /usr/sbin/ntpdate 172.16.0.1 &>/dev/null

       3、 各节点间可基于密钥验证SSH而无需密码

          ssh-keygen -t rsa

            ssh-copy-id-i root@172.16.10.22

           # node1 节点  

            ssh-keygen -t rsa

            ssh-copy-id-i root@172.16.10.21

           # node1 节点

     配置安装 corosync/pacemaker

        yum install corosync pacemaker -y

     [root@21~]# rpm -ql corosync     #查看 corosync文件位置

    /etc/corosync

    /etc/corosync/corosync.conf.example

    1、       配置corosync配置文件,组播地址,密钥验证等等

    vim /etc/corosync/corosync.conf

               compatibility:whitetank #是否兼容whitetank版本 保持默认即可

              totem定义底层通信层: 组播地址,线程,安全认证等等;

              secauth:on   #安全认证

             threads:0   #线程数 0表示不基于线程模式,基于进程模式工作;

    interface:

               ringnumber:0 # 保持0 类似于TTL值

              bindnetaddr:172.16.0.0 #监听的网络地址

               mcasaddr:239.255.X.X  #多播地址

              mcastport:5405       #多播端口

              TTL:1               #TTL值默认

    logging:

            to_logfile: yes

    timestamp: on             #是否记录时间戳,开启会影响性能,依系统性能而定

            logger_subsys {

                    subsys: AMF

                    debug: off

            }}

    配置pacemake以插件形式运行在corosync.conf中插入以下代码

    service {

      ver:  0

      name: pacemaker

      # use_mgmtd: yes}

    aisexec {

      user: root

      group: root}

     使用corosync-keygen生成验证密钥

    2、      使用corosync-kegen

        直接运行命令即可,但是他需要从随机数熵池中读取1024个随机数 /dev/random 如果不够可以做I/O操作

    3、拷贝 authkey corosync.conf 文件到另外一个节点上

    scp -p authkey corosync.conf 20xu:/etc/corosync

              4、  启动服务

               # service corosync start;ssh 20xu 'service corosync start'

          5404端口处于监听状态;

    #tail -f /var/log/cluster/corosync.log #监控 日志系统看看有没有异常状态;

    安装crmsh (在DC上安装即可,也可每都安装)

    先下载crmsh pssh 然后用yum 安装 解决依赖关系并不检查gpg信息,

    #yum --nogpgcheck localinstall crmsh-2.1-1.6.x86_64.rpm pssh-2.3.1-2.el6.x86_64.rpm

    安装完成:

    使用crm  进入子命令模式;

    配置全局属性

    crm(live)configure#property stonith-enabled=false

    #如果没有stonith 设备需要禁止

    crm(live)configure#property  no-quorum-policy=ignore

    crm(live)configure#verify   #检查语法:

    commit: #保存

    定义fip:172.16.10.28

    crm(live)configure#primitive mysqlip ocf:heartbeat:IPaddr params ip=172.16.10.28 nic=eth0cidr_netmask=16 op monitor interval=10s timeout=20s

    定义mysqlnfs

    crm(live)configure#primitive mysqlnfs ocf:heartbeat:Filesystem paramsdevice="172.16.10.22:/mysql" directory="/mysql"fstype="nfs" op monitor timeout=40 interval=20 op start timeout=60 opstop timeout=60

    定义mysql:

    crm(live)configure#primitive mysqld lsb:mysqld op monitor timeout=40 interval=2

    定义组:

    crm(live)configure#group mysqlgroup mysqlip mysqlnfs mysqld

     定义排序:

    crm(live)configure#order mysqlip_before_mysqlnfs Mandatory: mysqlip mysqlnfs

    crm(live)configure#order mysqlnfs_before_mysqld Mandatory: mysqlnfs mysqld

    切换节点后数据正常。

    来源:https://blog.51cto.com/nickxu/1658092

    [if !supportLists]4、[endif]采用Corosync+Pacemaker+nfs实现http高可用

    定义corosync配置信息

    [root@ansible-server ~]# vim corosync/roles/ha/files/corosync.conf

    compatibility: whitetank  #是否兼容旧版本的corosync

    totem {    #定义心跳信息传递信息

    version: 2  #定义corosync版本

    secauth: on  #是否需要安全认证

    threads: 0  #启动多少个线程处理心跳信息

            interface {

    ringnumber: 0  #起始号

    bindnetaddr: 192.168.80.0  #绑定在哪个网络地址

    mcastaddr: 226.94.1.1  #组播地址,为了与另一个节点传递心跳信息

    mcastport: 5405  #组播地址端口号

                    ttl: 1

            }

    }

    logging {  #定义日志功能

            fileline: off

    to_stderr: no  #是否将错误日志输出到终端

    to_logfile: yes  #是否启用专门的日志文件

    to_syslog: no  #是否将日志记录到linux默认日志文件中,即/var/log/messages,此项和to_logfile启动一项即可

    logfile: /var/log/cluster/corosync.log  #日志文件存放位置

    debug: off  #是否开启debug日志信息

    timestamp: on  #是否开启日志记录时间戳

            logger_subsys {

                    subsys: AMF

                    debug: off

            }

    }

    amf {

            mode: disabled

    }

    service{    #设定使用pacemaker服务

        ver:  0

        name: pacemaker

    }

    aisexec{  #定义运行时使用的用户和组

      user: root

      group: root

    }

    备注:此文件可以在已安装的corosync下/etc/corosync/下有一corosync.conf.example模板信息,做好修改之后再传递给ansible-server即可。

    定义node1和node2之间corosync所需的秘钥信息

    [root@ansible-server ~]# ls corosync/roles/ha/files/authkey

    corosync/roles/ha/files/authkey

    备注:此文件可以在已安装好的corosync上执行corosync-keygen,此时需要你输入数据来产生随机数,建议使用重复安装某个程序来加快生成速度,然后拷贝到ansibe-server即可。

    定义ha的tasks

    目标:

    安装corosync、pacemaker和httpd

    拷贝authkey认证文件和corosync配置文件到各节点

    [root@ansible-server ~]# vim corosync/roles/ha/tasks/main.yml

    - name: install corosync、pacemaker and httpd

    yum: name={{ item }} state=present  #安装对应所需的程序包

      with_items:

        - corosync

        - pacemaker

        - httpd

      tags: inst

    - name: auth key file    #拷贝认证文件到各节点

      copy: src=authkey dest=/etc/corosync/authkey owner=root group=root mode=4600

      tags: authkey

    - name: configuration file  #拷贝配置文件到各节点

      copy: src=corosync.conf dest=/etc/corosync/corosync.conf

      tags: config

    notify:  #当配置改变了,通知重启corosync

        - restart corosync

    - name: start corosync  #启动corosync服务,并设置开机不自动启动

      service: name=corosync state=started enabled=no

      tags: start

    - name: start httpd  #启动httpd服务,并设定开机不自动启动

      service: name=httpd state=started enabled=no

      tags: start

    定义ha的handlers文件

    [root@ansible-server ~]# vim corosync/roles/ha/handlers/main.yml

    - name: restart corosynce

      service: name=corosynce state=restart

    定义YAML文件

    [root@ansible-server ~]# vim corosync/ha.yml

    - name: install and config corosync

      remote_user: root

      hosts: hbhosts

      roles:

        - common

        - ha

    - name: install nfs

      remote_user: root

      hosts: nfs-Server

      roles:

    - nfs

    执行ansible-play自动部署corosync和pacemaker设置

    [root@ansible-server ~]# ansible-playbook corosync/ha.yml

    (六)使用ansible安装crmsh工具

    所需程序包:

    [root@ansible-server crmsh]# ll files/

    -rw-r--r-- 1 root root 495332 4月  27 23:44 crmsh-1.2.6-4.el6.x86_64.rpm

    -rw-r--r-- 1 root root  49960 4月  27 23:44 pssh-2.3.1-2.el6.x86_64.rpm

    使用ansible安装crmsh

    - name: copy crmsh and pssh  #拷贝程序包到各节点

      copy: src={{ item }} dest=/tmp/

      with_items:

        - crmsh-1.2.6-4.el6.x86_64.rpm

        - pssh-2.3.1-2.el6.x86_64.rpm

    - name: install crmsh and pssh  #安装两个程序包

      yum: name={{ item }} state=present

      with_items:

      - /tmp/pssh-2.3.1-2.el6.x86_64.rpm

      - /tmp/crmsh-1.2.6-4.el6.x86_64.rpm

    定义YAML文件

    [root@ansible-server ~]# vim corosync/ha.yml

    - name: install and config corosync

      remote_user: root

      hosts: hbhosts

      roles:

        - common

        - ha

        - crmsh

    - name: install nfs

      remote_user: root

      hosts: nfs-Server

      roles:

        - nfs

    执行ansible-play安装crmsh

    [root@ansible-server ~]# ansible-playbook corosync/ha.yml

    使用crmsh配置http高可用

    准备工作

    [root@node1 ~]# crm

    crm(live)# configure

    crm(live)configure# property stonith-enabled=false  #默认情况下,如果没有stonith设备,会不允许启用,所以我们要设置为安全忽略

    crm(live)configure# property no-quorum-policy=ignore  #因为我们只有2个节点,当我们其中一个节点下线了,那么其将无法定票数达不到一半以上,所有如果只有两个节点,必须将其使用安全忽略,否则节点将无法转移

    crm(live)configure# verify  #校验是配置否存在问题

    crm(live)configure# commit  #如无问题的话,提交所修改的配置

    定义资源

    包括webip,webserver,webstore

    crm(live)configure# primitive webip ocf:IPaddr params ip=192.168.80.200 op monitor interval="30s" timeout="20s"

    crm(live)configure# primitive webserver lsb:httpd op monitor interval="30s" timeout="20s"

    crm(live)configure# primitive webstore ocf:Filesystem params device="192.168.80.188:/web/htdocs" directory="/var/www/html" fstype="nfs" op monitor interval="60s" timeout="40s" op start timeout="60s" interval="0" op stop timeout="60s" interval="0"

    crm(live)configure# verify

    定义组和顺序约束

    crm(live)configure# group webservice webip webstore webserver

    crm(live)configure# order webip_before_webstore_before_webserver inf: webip webstore webserver

    crm(live)configure# verify

    crm(live)configure# commit

    检查节点和资源是否正常

    crm(live)# status

    Last updated: Fri Apr 29 05:46:15 2016

    Last change: Thu Aug 13 17:23:52 2015 via cibadmin on node1.windchaser.com

    Stack: classic openais (with plugin)

    Current DC: node2.windchaser.com - partition with quorum

    Version: 1.1.10-14.el6-368c726

    2 Nodes configured, 2 expected votes

    3 Resources configured

    Online: [ node1.windchaser.com node2.windchaser.com ]

     Resource Group: webservice

        webip  (ocf::heartbeat:IPaddr):    Started node1.windchaser.com

        webstore  (ocf::heartbeat:Filesystem):    Started node1.windchaser.com

        webserver  (lsb:httpd):    Started node1.windchaser.com

    验证

    1)、使用客户端访问webip,可以正常查看到对应的网址

    2)、将node1下线

    [root@node1 ~]# crm node standby

    3)、再次查看节点以及资源状态

    [root@node1 ~]# crm status

    Online: [ node2.windchaser.com ]

     Resource Group: webservice

        webip  (ocf::heartbeat:IPaddr):    Started node2.windchaser.com

        webstore  (ocf::heartbeat:Filesystem):    Started node2.windchaser.com

        webserver  (lsb:httpd):    Started node2.windchaser.com

    发现资源已转移至node2,重新使用客户端访问webip,发现可正常使用

    4)、将node1节点重新上线,此时可正常使用。

    [root@node1 ~]# crm node online

    相关文章

      网友评论

          本文标题:第十七周作业

          本文链接:https://www.haomeiwen.com/subject/pudofctx.html