• Ansible自动化运维、ZABBIX监控


    先准备管理机器(Boss)

    1.装好环境:

    1. yum install epel-release -y
    2. yum install ansible libselinux-python -y

    2.查看版本

    ansible --version

    准备被管理节点(node)

    1.准备环境node节点:

    yum install epel-release libselinux-python -y

    master节点配置host:

    vim /etc/ansible/hosts

     拉到最后、格式

    [bobo]这样的得命名格式好管理

    1. [bobo]
    2. 172.28.229.146
    3. 172.28.229.145

    尝试用ansible连接:

    ansible bobo -m command -a "hostname" -k -u root

    输入密码,连接成功,执行成功

     如果提示失败用ssh root@ip连一遍就行了

    ansible的执行模板:

    ansible    bobo  -m     模块名  -a "shell命令"

    ansible chaoge -m command -a "uptime"

    配置免密登陆ssh密钥

    1.在master节点创建密钥对

    ssh-keygen -f ~/.ssh/id_rsa -P "" > /dev/null 2>&1

    2.检查公钥文件私钥文件

    cd ~/.ssh/
    1. ls
    2. authorized_keys id_rsa id_rsa.pub known_hosts

    3.master节点编写公钥分发脚本:

    vim kry.sh
    1. #!/bin/bash
    2. rm -rf ~/.ssh/id_rsa*
    3. ssh-keygen -f ~/.ssh/id_rsa -P "" > /dev/null 2>&1
    4. SSH_Pass=111111
    5. Key_Path=~/.ssh/id_rsa.pub
    6. for ip in 138 139
    7. do
    8. sshpass -p$SSH_Pass ssh-copy-id -i $Key_Path "-o
    9. StrictHostKeyChecking=no" 192.168.178.$ip
    10. done

     4.运行key.sh

    5.利用ansible分测试:

    ansible bobo -m command -a "uname -a"

    ansible-doc命令:

    1.列出所有ansible支持的模块

    ansible-doc -l |grep ^command
    ັ查看该模块支持的参数
    ansible-doc -s command

     command模块是默认模块

    shell模块

    shell模块是经常使用的模块:梨子例子

     ansible bobo -m shell -a "ps -ef|grep vim|grep -v grep"

    批量执行脚本:

    1. ansible chaoge -m shell -a "mkdir -p /server/myscripts/;echo
    2. 'hostname' > /server/myscripts/hostname.sh;chmod +x
    3. /server/myscripts/hostname.sh;bash
    4. /server/myscripts/hostname.sh warn=False"

    script模块---重点:

    master写一遍脚本可在所有节点执行:格式差不多,该脚本不用再客户机存在

    ansible chaoge -m script -a "/myscripts/local_hostname.sh"

    copy模块:把数据推到从节点

    ansible-doc -s copy  #查看参数的用法

    例子:批量拷贝文件:

    1. ansible chaoge -m copy -a "src=/etc/hosts
    2. dest=/tmp/m01_hosts owner=learn_ansible group=learn_ansible
    3. mode=0666"

    检查拷贝后的信息:

     ansible chaoge -m command -a "ls -l /tmp/m01_hosts"

    yum模块练习:

    批量安装nginx:

    ansible chaoge -m yum -a "name=nginx state=installed"

    批量检查:

    ansible chaoge -m shell -a "rpm -qa nginx warn=false"

    批量卸载nginx:

    ansible chaoge -m yum -a "name=nginx  state=absent"

    批量升级:

    ansible chaoge -m yum -a "name='nginx' state=latest"

    playbook剧本:

    第一个剧本:yaml格式

     host主机部分:

     剧本的编写规范:

     剧本的执行:

    ansible-playbook nginx.yaml

    查看剧本的详细输出:

    ansible-playbook nginx.yaml --verbose

    playbook部署rsyns实战题目:

     实际写法:

     运行剧本:

    1. ansible-playbook install_rsync.yaml -C
    2. ansible-playbook install_rsync.yaml

    ZABBIXj监控:

    内存要求2g以上:

    getenforce  #查看防火墙状态
    iptables -L  #查看iptablees
    free -m  #服务端内存,尽量给大点

    获取官方下载源:

    rpm -Uvh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm

    修改镜像源:

    sed -i 's#http://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#' /etc/yum.repos.d/zabbix.repo

    安装zabbix  server和agent:

    yum install zabbix-server-mysql zabbix-agent -y

    安装Software Collections:

    yum install centos-release-scl -y

    修改zabbix前段源:

    1. vim /etc/yum.repos.d/zabbix.repo
    2. [zabbix-frontend]
    3. name=Zabbix Official Repository frontend - $basearch
    4. baseurl=https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/$b
    5. asearch/frontend
    6. enabled=1 #修改这里
    7. gpgcheck=1
    8. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591

    安装zabbix前端环境:

    yum install zabbix-web-mysql-scl zabbix-apache-conf-scl -y

    安装zabbix数据库:

    yum install mariadb-server -y

    启动数据库,配置开机自弃:

    systemctl enable --now mariadb

    设置数据库密码:按提示:

    mysql_secure_installation

    连接数据库:

    [root@master ~]# mysql -u root -p

    创建数据库连接:很重要

    1. create database zabbix character set utf8 collate utf8_bin;
    2. create user zabbix@localhost identified by 'chaoge666';
    3. grant all privileges on zabbix.* to zabbix@localhost;
    4. flush privileges;
    5. quit;

     导入数据库信息:用户zabbix:

    -p后面是数据库名字:密码chaoge666

    zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix

    尝试登陆:

    1. [root@master ~]# mysql -uzabbix -pchaoge666
    2. Welcome to the MariaDB monitor. Commands end with ; or \g.
    3. Your MariaDB connection id is 24
    4. Server version: 5.5.68-MariaDB MariaDB Server
    5. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    6. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    7. MariaDB [(none)]>

    查看database :

    1. MariaDB [(none)]> show databases;
    2. +--------------------+
    3. | Database |
    4. +--------------------+
    5. | information_schema |
    6. | zabbix |
    7. +--------------------+
    8. 2 rows in set (0.01 sec)
    9. MariaDB [(none)]>

    use zabbix数据库:

    1. MariaDB [(none)]> use zabbix
    2. Reading table information for completion of table and column names
    3. You can turn off this feature to get a quicker startup with -A
    4. Database changed
    5. MariaDB [zabbix]>
    show tables;

    修改配置文件密码:

    /etc/zabbix/zabbix_server.conf

    搜索:

     检查是否改好了:

    [root@zabbix-server01 data]# grep 'timezone' /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf

    修改时区:

    /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf

     启动zabbix服务:

     设置开机自启:

    1. systemctl restart zabbix-server zabbix-agent httpd rh-php72-php-fpm
    2. systemctl enable zabbix-server zabbix-agent httpd rh-php72-php-fpm

     

  • 相关阅读:
    Hystrix 服务熔断
    Android 获取短信验证
    ABBYY15切换中文版教程及离线下载安装教程
    《算法基础》基础数据结构
    优化华为云服务器采用Key登陆
    【LeetCode热题100】--169.多数元素
    C语言描述数据结构 —— 二叉树(1)
    判断文件为文本文件还是二进制文件
    APP破解去广告
    IP协议从0到1
  • 原文地址:https://blog.csdn.net/weixin_42435798/article/details/126111663