实验:使用ansible实现keepalive集群

本文详细介绍了使用Ansible进行自动化部署的过程,包括配置Keepalived实现高可用负载均衡,安装和配置Nginx作为反向代理,以及部署Web服务器和MariaDB数据库。通过编写Playbook和角色,实现了对不同服务器的自动化管理。
摘要由CSDN通过智能技术生成
一、角色的目录结构
.
├── keepalive
│   ├── default
│   ├── files
│   │   ├── keepalived.conf.backup
│   │   └── keepalived.conf.master
│   ├── handlers
│   │   └── main.yml
│   ├── meta
│   ├── tasks
│   │   └── main.yml
│   ├── templates
│   └── vars
├── mariadb
│   ├── default
│   ├── files
│   ├── handlers
│   ├── meta
│   ├── tasks
│   │   └── main.yml
│   ├── templates
│   └── vars
├── nginx
│   ├── default
│   ├── files
│   │   └── web.conf
│   ├── handlers
│   │   └── main.yml
│   ├── meta
│   ├── tasks
│   │   └── main.yml
│   ├── templates
│   └── vars
└── websrv
    ├── default
    ├── files
    │   ├── wordpress-4.9.4-zh_CN.tar.gz
    │   └── wp-config.php
    ├── handlers
    │   └── main.yml
    ├── meta
    ├── tasks
    │   ├── httpd.yml
    │   ├── main.yml
    │   └── wp.yml
    ├── templates
    └── vars
二、编写相应的playbook
  • 添加对应的主机到ansible主机清单中
vim /etc/ansible/hosts
[lb]
192.168.30.10[2:3]

[websrv]
192.168.30.100
192.168.30.104

[dbsrv]
192.168.30.107

  • keepalive
vim keepalive/tasks/main.yml 
- name: install keepalive package
  yum: name=keepalived state=installed
  when: ansible_os_family == "RedHat"

- name: copy config file for master
  copy: src=keepalived.conf.master dest=/etc/keepalived/keepalived.conf force=yes
  when: ansible_ens33.ipv4.address == "192.168.30.102"
  notify: restart keepalive

- name: copy config file for backup
  copy: src=keepalived.conf.backup dest=/etc/keepalived/keepalived.conf force=yes
  when: ansible_ens33.ipv4.address == "192.168.30.103"
  notify: restart keepalive

- name: start keepalive service
  service: name=keepalived state=started enabled=yes
vim keepalive/handlers/main.yml 
- name: restart keepalive
  service: name=keepalived state=restarted
vim keepalive/files/keepalived.conf.backup
! Configuration File for keepalived
global_defs {
    router_id node1
    vrrp_mcast_group4 224.0.100.10
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens37
    virtual_router_id 20
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass ed93f89c
    }
    virtual_ipaddress {
        10.1.0.100/8 dev ens37
    }
}

vim keepalive/files/keepalived.conf.master
! Configuration File for keepalived
global_defs {
    router_id node2
    vrrp_mcast_group4 224.0.100.10
}
vrrp_instance VI_1 {
    state MASTER
    interface ens37
    virtual_router_id 20
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass ed93f89c
    }
    virtual_ipaddress {
        10.1.0.100/8 dev ens37
    }
}
  • nginx
vim nginx/tasks/main.yml 
- name: install nginx
  yum: name=nginx state=latest
  when: ansible_os_family == "RedHat"
- name: copy config file
  copy: src=web.conf dest=/etc/nginx/conf.d/
  notify: restart nginx
- name: start nginx
  service: name=nginx state=started enabled=yes
vim nginx/handlers/main.yml 
- name: restart nginx
  service: name=nginx state=restarted
vim nginx/files/web.conf 
upstream websrvs {
    server 192.168.30.104:80;
    server 192.168.30.100:80;
}

server {
  server_name www.ilinux.io;
  listen 80;
  location / {
    proxy_pass http://websrvs/;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header host $host;
  }
  • websrv
# 1
vim websrv/tasks/httpd.yml
- name: install httpd
  yum:
    name: ['httpd', 'php', 'php-mysql', 'php-mbstring']   # 安装多个软件的方法
    state: installed
- name: start httpd service
  service: name=httpd state=started

# 2
vim websrv/tasks/wp.yml
- name: unpack wordpress package
  unarchive: 
    src: wordpress-4.9.4-zh_CN.tar.gz
    dest: /var/www/html/
    copy: yes
- name: copy config file
  copy: 
    src: wp-config.php
    dest: /var/www/html/wordpress/

# 3
vim websrv/tasks/main.yml
- include: httpd.yml
- include: wp.yml
vim websrv/handlers/main.yml 
- name: restart httpd
  service: name=httpd state=restarted
  • mariadb
vim mariadb/tasks/main.yml 
- name: install mariadb
  yum: name=mariadb-server state=latest
  when: ansible_os_family == "RedHat"
- name: start mariadb
  service: name=mariadb state=started
- name: grant wordpress user
  shell: mysql -e "create database wordpress;grant all on wordpress.* to test@'192.168.30.%' identified by 'centos';"
三、测试
http://www.ilinux.io/wordpress/

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值