Ansible实现主备模式的高可用(Keepalived)

前言

  Ansible是一款极其简单的IT自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。Ansible是基于模块工作的,本身没有批量部署的能力,真正具有批量部署的是Ansible所运行的模块,Ansible只是提供一种框架。Ansible主要承担的工作包括:配置管理、服务即时开通、应用部署、流程编排、监控告警、日志记录等。

  Ansible的基本架构:

  • 核心模块(Core Module):在模块库(Module Library)中分为两块,一个是核心模块,另外一个就是自定义模块(Custom Modules)。核心模块中都是Ansible自带的模块,模块资源分发到远程节点使其执行特定任务或匹配一个特定的状态;
  • 自定义模块(Custom Modules):如果Ansible满足不了你所需求的模块,则在此处添加自定义化的模块;
  • 插件(Plugins):协助模块来完成某个功能;
  • 剧本(Playbooks):定义需要给远程主机执行的一系列任务;
  • 连接插件(Connectior Plugins):Ansible默认基于SSH连接到目标机器上执行操作的,但也支持不同的连接方法,此时需要连接插件来帮助我们完成连接;
  • 主机清单(Host Inventory):定义需要管理的主机,小型环境中我们只需要在host文件中写入主机的IP地址即可,但到了中大型环境我们有可能需要使用静态inventory或者动态主机清单来生成我们所需要执行的目标主机。

实验拓扑图

  两台Nginx作为Web Proxy,配置实现KeepAlived做主备;后端两台Apache,一台部署Apache+PHP,一台部署Apache+MySQL。

搭建Ansible

[[email protected] ~]# systemctl stop firewalld.service
[[email protected] ~]# systemctl disable firewalld.serive
[[email protected] ~]# vim /etc/selinux/config
    ...
    SELINUX=disabled
    ...
[[email protected] ~]# init 6
[[email protected] ~]# ntpdate ntp1.aliyun.com
[[email protected] ~]# yum -y install ansible
[[email protected] ~]# vim /etc/ansible/hosts
    ...
    [hasrvs]
    192.168.4.117
    192.168.4.118
    [websrvs]
    192.168.4.119
    192.168.4.120  [php]  192.168.4.119  [mysql]  192.168.4.120
[[email protected] ~]# vim /etc/hosts
    ...
    192.168.4.117 nginx1
    192.168.4.118 nginx2
    192.168.4.119 apache1
    192.168.4.120 apache2
[[email protected] ~]# ssh-keygen -t rsa -N ‘‘  #生成密钥对,实现ssh免密码登录
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa):
    Created directory ‘/root/.ssh‘.
    Your identification has been saved in /root/.ssh/id_rsa.
    Your public key has been saved in /root/.ssh/id_rsa.pub
[[email protected] ~]# ssh-copy-id -i .ssh/id_rsa.pub [email protected]  #复制公钥到各远程主机
[[email protected] ~]# ssh-copy-id -i .ssh/id_rsa.pub [email protected]
[[email protected] ~]# ssh-copy-id -i .ssh/id_rsa.pub [email protected]
[[email protected] ~]# ssh-copy-id -i .ssh/id_rsa.pub [email protected]

测试连通性

[[email protected] ~]# ansible all -m ping
    192.168.4.117 | SUCCESS => {
        "changed": false,
        "ping": "pong"
    }
    192.168.4.118 | SUCCESS => {
        "changed": false,
        "ping": "pong"
    }
    192.168.4.120 | SUCCESS => {
        "changed": false,
        "ping": "pong"
    }
    192.168.4.119 | SUCCESS => {
        "changed": false,
        "ping": "pong"
    }

同步时间,关闭firewalld及selinux

[[email protected] ~]# ansible all -m shell -a ‘echo "TZ=‘Asia/Shanghai‘; export TZ" >> /etc/profile‘
[[email protected] ~]# ansible all -m cron -a ‘minute=*/5 job="/usr/sbin/ntpdate ntp1.aliyun.com &> /dev/null" name=UpdateTime‘  #每隔3分钟同步一次时间
[[email protected] ~]# ansible all -m shell -a ‘systemctl stop firewalld.service; systemctl disable firewalld.service; setenfore 0‘

配置Apache服务的roles

[[email protected] ~]# mkdir -pv /etc/ansible/roles/apache/{files,templates,tasks,handlers,vars,meta,default}  #创建相关目录
[[email protected] ~]# vim /etc/ansible/roles/apache/templates/vhost.conf.j2  #Apache主机模板
    <virtualhost *:80>
        ServerName www.test.org
        DirectoryIndex index.html index.php
        DocumentRoot /var/www/html
        ProxyRequests off
        ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.4.119:9000/var/www/html/$1
        ProxyPassMatch ^/(ping|status)$ fcgi://192.168.4.119:9000/$1
        <Directory />
        options FollowSymlinks
        Allowoverride None
        Require all granted
        </Directory>
    </virtualhost>
[[email protected] ~]# vim /etc/ansible/roles/apache/templates/index.html  #Apache主页
    <h1> This is {{ ansible_hostname }} </h1>
[[email protected] ~]# vim /etc/ansible/roles/apache/files/index.php
    <?php
        phpinfo();
    ?>
[[email protected] ~]# vim /etc/ansible/roles/apache/tasks/main.yml  #定义实现Apache的task
    - name: install apache
      yum: name=httpd state=latest
    - name: copy conf
      template: src=vhost.conf.j2 dest=/etc/httpd/conf.d/vhost.conf
    - name: copy index.html
      template: src=index.html dest=/var/www/html/index.html
    - name: copy index.php
      copy: src=index.php dest=/var/www/html/index.php
    - name: start httpd
      service: name=httpd state=started

配置php-fpm服务的roles

[[email protected] ~]# mkdir -pv /etc/ansible/roles/php-fpm/{files,templates,tasks,handlers,vars,meta,default}  #创建相关目录
[[email protected] ~]# cp /etc/php-fpm.d/www.conf /etc/ansible/roles/php-fpm/templates/www.conf.j2  #直接复制事先准备的配置模板
[[email protected] ~]# vim /etc/ansible/roles/php-fpm/templates/www.conf.j2
    #修改如下配置
    listem = 0.0.0.0:9000
    ;listen.allowed_clients = 127.0.0.1
    pm.status_path = /status
    ping.path = /ping
    ping.response = pong
[[email protected] ~]# vim /etc/ansible/roles/php-fpm/tasks/main.yml  #定义实现php-fpm的task
    - name: install php
      yum: name={{ item }} state=latest
      with_items:
      - php-fpm
      - php-mysql
      - php-mbstring
      - php-mcrypt
    - name: copy config
      template: src=www.conf.j2 dest=/etc/php-fpm.d/www.conf
    - name: create directory
      file: path=/var/lib/php/session group=apache owner=apache state=directory
    - name: start php-fpm
      service: name=php-fpm state=started

配置MySQL服务的roles

[[email protected] ~]# mkdir -pv /etc/ansible/roles/mysql/{files,templates,tasks,handlers,vars,meta,default}  #创建相关目录
[[email protected] ~]# cp /etc/my.cnf /etc/ansible/roles/mysql/templates/my.cnf.j2  #复制事先准备的模板
[[email protected] ~]# vim /etc/ansible/roles/mysql/templates/my.cnf.j2
    #添加如下配置
    skip-name-resolve=ON
    innodb-file-per-table=ON
[[email protected] ~]# vim /etc/ansible/roles/mysql/tasks/main.yml  #定义实现MySQL的task
    - name: install mysql
      yum: name=mariadb-server state=latest
    - name: copy config
      template: src=my.cnf.j2 dest=/etc/my.cnf
    - name: start mysql
      service: name=mariadb state=started

配置Nginx服务的roles

[[email protected] ~]# mkdir -pv /etc/ansible/roles/nginx/{files,templates,tasks,handlers,vars,meta,default}  #创建相关目录
[[email protected] ~]# cp /etc/nginx/nginx.conf /etc/ansible/roles/nginx/templates/nginx.conf.j2  #复制事先准备的模板
[[email protected] ~]# vim /etc/ansible/roles/nginx/templates/nginx.conf.j2  #修改配置
    http {
        ...
        upstream websrvs {
            server 192.168.4.119:80;
            server 192.168.4.120:80;
            server 127.0.0.1:80 backup;
        }
        server {

            listen 80;
            include /etc/nginx/default.d/*.conf;

            location / {
                proxy_pass http://websrvs;
                proxy_set_header host $http_host;
                proxy_set_header X-Forward-For $remote_addr;
            }
            ...
        }
        ...
    }
[[email protected] ~]# vim /etc/ansible/roles/nginx/templates/localhost.conf.j2  #定义本地的Nginx服务
    server {
        listen 127.0.0.1:80;
        root /usr/share/nginx/html;
        index index.html;
    }
[[email protected] ~]# vim /etc/ansible/roles/nginx/templates/index.html
    <h1> Balance Server {{ ansible_hostname }} </h1>
[[email protected] ~]# vim /etc/ansible/roles/nginx/tasks/main.yml  #定义实现Nginx的task
    - name: install nginx
      yum: name=nginx state=latest
    - name: copy nginx conf
      template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
    - name: copy local conf
      template: src=localhost.conf.j2 dest=/etc/nginx/conf.d/localhost.conf
    - name: copy index
      template: src=index.html dest=/usr/share/nginx/html/index.html
    - name: start nginx
      service: name=nginx state=started

配置KeepAlived服务的roles

[[email protected] keepalived]# mkdir -pv /etc/ansible/roles/keepalived/{files,templates,tasks,handlers,vars,meta,default}  #创建相关目录
[[email protected] keepalived]# vim /etc/ansible/roles/keepalived/templates/keepalived.conf.j2  #KeepAlived配置文件
    global_defs {
           notification_email {
             [email protected]
           }
           notification_email_from [email protected]
           smtp_server 127.0.0.1
           smtp_connect_timeout 30
           router_id {{ ansible_nodename }}
           vrrp_skip_check_adv_addr
           vrrp_mcast_group4 224.0.0.10
    }

    vrrp_instance VIP_1 {
        state {{ keepalived_role }}
        interface eno16777736
        virtual_router_id 1
        priority {{ keepalived_pri }}
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass %&hhjj99
        }
        virtual_ipaddress {
          192.168.4.155/24 dev eno16777736 label eno16777736:0
        }
    }
[[email protected] keepalived]# vim /etc/ansible/hosts  #添加变量
        ...
    [hasrvs]
    192.168.4.117 keepalived_role=MASTER keepalived_pri=100
    192.168.4.118 keepalived_role=BACKUP keepalived_pri=99
    ...
[[email protected] keepalived]# vim /etc/ansible/roles/keepalived/tasks/main.yml  #定义实现Keepalived的task
    - name: install keepalived
      yum: name=keepalived state=latest
    - name: copy config
      template: src=keepalived.conf.j2 dest=/etc/keepalived/keepalived.conf
    - name: start keepalived
      service: name=keepalived state=started

配置Apache+PHP服务的playbook

[[email protected] keepalived]# mkdir /etc/ansible/playbooks  #创建playbook存放目录
[[email protected] roles]# vim /etc/ansible/playbooks/ap1.yml  定义实现Apache+php-fpm的playbook
    - hosts: php
      remote_user: root
      roles:
      - apache
      - php-fpm
[[email protected] roles]# ansible-playbook --syntax-check /etc/ansible/playbooks/ap1.yml  #检查是否有语法错误
[[email protected] roles]# ansible-playbook /etc/ansible/playbooks/ap1.yml  #执行

配置Apache+MySQL服务的playbook

[[email protected] ~]# vim /etc/ansible/playbooks/ap2.yml
    - hosts: mysql
      remote_user: root
      roles:
      - apache
      - mysql
[[email protected] ~]# ansible-playbook --syntax-check /etc/ansibleplaybooks/ap2.yml
[[email protected] ~]# ansible-playbook /etc/ansibleplaybooks/ap2.yml

配置Nginx+Keepalived服务的playbook

[[email protected] ~]# vim /etc/ansible/playbooks/ha.yml
    - hosts: hasrvs
      remote_user: root
      roles:
      - nginx
      - keepalived
[[email protected] ~]# ansible-playbook --syntax-check /etc/ansible/playbooks/ha.yml
[[email protected] ~]# ansible-playbook /etc/ansible/playbooks/ha.yml

客户端测试访问

[[email protected] ~]# vim /etc/hosts
    ...
    192.168.4.155 www.test.org
    ...
[[email protected] ~]# for i in {1..10};do curl http://www.test.org;done
    <h1> This is apache2 </h1>
    <h1> This is apache1 </h1>
    <h1> This is apache2 </h1>
    <h1> This is apache1 </h1>
    <h1> This is apache2 </h1>
    <h1> This is apache1 </h1>
    <h1> This is apache2 </h1>
    <h1> This is apache1 </h1>
    <h1> This is apache2 </h1>
    <h1> This is apache1 </h1>

小插曲:

  [[email protected] ~]# ansible all -m ping --list-hosts
  ERROR! Unexpected Exception, this is probably a bug: (cryptography 0.8.2 (/usr/lib64/python2.7/site-packages), Requirement.parse(‘cryptography>=1.1‘))

  运行ansible命令时报错,python的cryptography版本必须>=1.1。

  解决办法:

  [[email protected] ~]# yum -y install python-pip

  [[email protected] ~]# pip install --upgrade cryptography

原文地址:https://www.cnblogs.com/walk1314/p/9836624.html

时间: 2024-11-05 15:57:00

Ansible实现主备模式的高可用(Keepalived)的相关文章

Keepalived+MySQL主主互备模式实现高可用 &nbsp; &nbsp; &nbsp;

MySQL的高可用方案很多,这是一个配置简单而且实用的高可用解决方案. 实验架构图: 修改DB1配置文件增加以下几项: [mysqld] log-bin=mysql-bin relay-log=mysql-relay-bin server-id=1 replicate-wild-ignore-table=mysql.% replicate-wild-ignore-table=test.% replicate-wild-ignore-table=information_schema.% 修改DB2

Nginx + Keepalived(主备模式)实现负载均衡高可用浅析

概述 目前关于负载均衡和高可用的架构方案能找到相当多且详尽的资料,此篇是自己学习相关内容的一个总结,防止将来遗忘再次重新查找资料,也避免踩相同的坑. 此次配置的负载均衡与高可用架构:Nginx + Keepalived(主备模式),Nginx 使用反向代理实现七层负载均衡. 众所周知,Nginx 是一款自由的.开源的.高性能HTTP服务器和反向代理服务器,也是一个IMAP.POP3.SMTP代理服务器. 也就是说Nginx本身就可以托管网站(类似于Tomcat一样),进行HTTP服务处理,也可以

[转帖]【MySQL+keepalived】用keepalived实现MySQL主主模式的高可用

[MySQL+keepalived]用keepalived实现MySQL主主模式的高可用 https://www.jianshu.com/p/8694d07595bc 一.实验说明 MySQL主主模式,是两台MySQL数据库互为主从. 此实验是用keepalived实现MySQL主主模式的高可用,基于已经安装好了主主架构的MySQL,然后配置keepalived,验证高可用性! 二.实验环境 操作系统:CentOS 7.5 serverA:192.168.1.104 serverB: 192.1

centos7 搭建双网卡bond1(主备模式)实例

前景须知: 在redhat6 中网卡叫bond,在redhat7及centos7中改名team,此处只记录centos7中双网卡主备搭建过程. 应用情景:实现网络的高可用,防止一条网线或交换机故障影响该物理机进行数据交互 此次环境是由于在上线业务之前是没有做Team的 ,现在由于要撤掉交换机过程期间需要将网线连接到另外一台交换机,为了防止数据中断,因此采用Team主备模式,这样断掉一个网卡就不影响业务,测试发现重启网卡时候会有3到4个丢包 环境:centos7  网卡1 ens192  网卡2 

3.redis单节点及主备模式

1.单节点模式 单节点模式的配置,使用redis通用配置即可. (1)启动命令: 1 /path/to/redis-server /path/to/redis-6379.conf 注:配置文件名称只是示例,一般一台机器不止启动一个redis实例,使用端口区分配置文件是比较好的方式 (2)关闭命令: 1 /path/to/redis-cli -h <host> -p <port> -a '<password>' shutdown save 注:建议不要直接kill进程,会

L10 keepalived 基本使用(主备模式)

配置keepalive基本主备模式 配置说明: 要求默认情况下由节点node1提供服务,当节点node1不可用时,由节点node2提供服务(即虚拟IP漂移至节点node2). 节点node1 192.168.0.20 (主节点) 节点node2 192.168.0.21(备用节点) 虚拟IP(对外提供服务的IP 192.168.0.60 ping node 192.168.0.22(用于节点自身状态监测) 内容: 1,节点node1上的配置文件 2,节点node2配置 3,使用脚本防止脑裂. 4

apache+inotify-tools+keepalived+lvs-DR模式配置高可用负载均衡集群

环境:虚拟机VMware workstation 9 操作系统:Redhat 5.6 i386 一.keepalived+LVS-DR模式配置高可用负载均衡 拓扑如下: 二.服务器IP配置信息 四台服务器均有VMware虚拟机实现,两台HA主机.两台web服务器.网络方式都设置为NAT模式 1.master:192.168.80.145 2.slaver:192.168.80.137 3.web1:192.168.80.144 4.web2:192.168.80.134 5.VIP:192.16

基于keepalived实现多种模式的高可用集群网站架构

一. 实现主从服务器高可用技术. 大概网络拓扑图: 前提准备条件: 准备四台机器,一台为keepalived的master,一台为keepalived的backup,一台为rs1,一台为rs2,同时都关闭防火墙和selinux. ①安装包. yum install keepalived ②修改位置文件(keepalived的master). vim /etc/keepalived/keepalived.cf ③修改keepalived的主配置文件(keepalived的backup)  ④给RS

CentOS7和rhel7配置主备模式端口聚合实现冗余目的

一.实施步骤 1.备份网卡目录# cp -r /etc/sysconfig/network-scripts/    /etc/sysconfig/network-scripts.bak 2.查看需要聚合的端口是否为“UP”状态:ethtool eth0 |grep "Link detected: yes" 2.创建脚本文件 3.chmox +x test.sh 4.运行脚本文件 ./test.sh 5.由于NetWorkManager服务和network服务两者有冲突,需关闭NetWo