Ansible implements keepalived)

Source: Internet
Author: User
Tags aliyun

Ansible is an extremely simple it automated O & M tool. It is developed based on Python and integrates the advantages of many O & M tools (puppet, cfengine, Chef, func, fabric, implements batch system configuration, batch program deployment, and batch running commands. Ansible works based on modules and does not have the ability to deploy them in batches. It is actually a module running by ansible. ansible only provides a framework. Ansible mainly undertakes the following tasks: configuration management, real-time service activation, application deployment, Process Orchestration, monitoring and alarms, and logging.

Basic architecture of ansible:

  • Core Module: There are two modules in the module library. One is the core module and the other is the custom module ). The core modules are all built-in modules of ansible. The module resources are distributed to remote nodes to execute specific tasks or match a specific status;
  • Custom modules: If ansible cannot meet your needs, add custom modules here;
  • Plugins: assists the module in completing a function;
  • Playbooks: defines a series of tasks to be executed on a remote host;
  • Connectior plugins: ansible connects to the target machine based on SSH by default, but also supports different connection methods. In this case, you need to connect the plug-in to help us complete the connection;
  • Host inventory: defines the host to be managed. In a small environment, you only need to write the Host IP address to the host file, however, in medium and large environments, we may need to use static inventory or dynamic host list to generate the target host we need to execute.
Lab Topology

The two nginx servers are used as the web proxy, and keepalived is configured as the master and slave servers. The backend servers are two Apache servers, one is Apache + PHP, and the other is Apache + MySQL.

Building 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 [[email protected] ~] # Yum-y install ansible [[email protected] ~] # Vim/etc/ansible/hosts... [hasrvs] [websrvs]
[MySQL] [[email protected] ~] # Vim/etc/hosts... nginx1 nginx2 apache1 apache2 [[email protected] ~] # Ssh-keygen-t rsa-n'' # generate a key pair to enable SSH password-free login to 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/ [[email protected] ~] # Ssh-copy-ID-I. Ssh/ [email protected] # copy the public key to each remote host [[email protected] ~] # Ssh-copy-ID-I. Ssh/ [email protected] [[email protected] ~] # Ssh-copy-ID-I. Ssh/ [email protected] [[email protected] ~] # Ssh-copy-ID-I. Ssh/ [email protected]
Test connectivity
[[email protected] ~]# ansible all -m ping | SUCCESS => {        "changed": false,         "ping": "pong"    } | SUCCESS => {        "changed": false,         "ping": "pong"    } | SUCCESS => {        "changed": false,         "ping": "pong"    } | SUCCESS => {        "changed": false,         "ping": "pong"    }
Synchronization time, disable firewalld and 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 &>/dev/null" name = updatetime '# synchronize every 3 minutes [email protected] ~] # Ansible all-M shell-A 'systemctl stop firewalld. Service; systemctl disable firewalld. Service; setenfore 0'
Configure the roles of the Apache service
[[Email protected] ~] # Mkdir-PV/etc/ansible/roles/Apache/{files, templates, tasks, handlers, vars, Meta, default} # create related directories [[email protected] ~] # Vim/etc/ansible/roles/Apache/templates/vhost. conf. j2 # Apache host template <virtualhost *: 80> servername directoryindex index.html index. PHP DocumentRoot/var/www/html proxyrequests off proxypassmatch ^ /(. *\. PHP) $ fcgi: // 9000/var/www/html/$1 proxypassmatch ^/(Ping | status) $ fcgi: // 9000/$1 <directory/> options followsymlinks AllowOverride none require all granted </ Directory> </virtualhost> [[email protected] ~] # Vim/etc/ansible/roles/Apache/templates/index.html # Apache homepage Configure the roles of the PHP-FPM Service
[[Email protected] ~] # Mkdir-PV/etc/ansible/roles/PHP-FPM/{files, templates, tasks, handlers, vars, Meta, default} # create related directories [[email protected] ~] # Cp/etc/php-fpm.d/www. conf/etc/ansible/roles/PHP-FPM/templates/www. conf. j2 # directly copy the pre-prepared configuration template [[email protected] ~] # Vim/etc/ansible/roles/PHP-FPM/templates/www. conf. j2 # modify the following configuration: listem = 9000; listen. allowed_clients = PM. status_path =/status Ping. path =/ping. response = Pong [[email protected] ~] # Vim/etc/ansible/roles/PHP-FPM/tasks/main. yml # define the 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
Configure the MySQL service roles
[[Email protected] ~] # Mkdir-PV/etc/ansible/roles/MySQL/{files, templates, tasks, handlers, vars, Meta, default} # create related directories [[email protected] ~] # Cp/etc/My. CNF/etc/ansible/roles/MySQL/templates/My. CNF. J2 # copy the prepared template [email protected] ~] # Vim/etc/ansible/roles/MySQL/templates/My. CNF. j2 # Add the following configuration skip-name-resolve = on InnoDB-file-per-table = on [email protected] ~] # Vim/etc/ansible/roles/MySQL/tasks/main. yml # define the 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
Configure the roles of the nginx Service
[[Email protected] ~] # Mkdir-PV/etc/ansible/roles/nginx/{files, templates, tasks, handlers, vars, Meta, default} # create related directories [[email protected] ~] # Cp/etc/nginx. CONF/etc/ansible/roles/nginx/templates/nginx. conf. J2 # copy the prepared template [[email protected] ~] # Vim/etc/ansible/roles/nginx/templates/nginx. conf. J2
# Modify the configuration HTTP {... upstream websrvs {server 80; server 80; server 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 # define the local nginx Service Server {Listen 80; root/usr/share/nginx/html; index index.html;} [[email protected] ~] # Vim/etc/ansible/roles/nginx/templates/index.html Configure the roles of the keepalived Service
[[Email protected] keepalived] # mkdir-PV/etc/ansible/roles/keepalived/{files, templates, tasks, handlers, vars, Meta, default} # create the related directory [[email protected] keepalived] # Vim/etc/ansible/roles/keepalived/templates/keepalived. conf. j2 # keepalived configuration file global_defs {notification_email {[email protected]} Listen [email protected] smtp_server Limit 30 router_id {region} route} vrrp_instance vip_1 {state} interface eno16777736 virtual_router_id 1 priority {region} advert_int 1 authentication {auth_type pass auth_pass % & hhjj99} virtual_ipaddress { Dev eno16777736 label eno167736: 0} [[email protected] keepalived] # Vim/etc/ansible/hosts # Add variable... [hasrvs] keepalived_role = Master keepalived_pri = 100 keepalived_role = backup keepalived_pri = 99... [[email protected] keepalived] # Vim/etc/ansible/roles/keepalived/tasks/main. yml # define the task-Name: Install keepalived Yum: name = keepalived state = Latest-Name: Copy config template: src = keepalived. conf. j2 DEST =/etc/keepalived. conf-Name: Start keepalived service: Name = keepalived state = started
Configure the playbook of the Apache + PhP Service
[[Email protected] keepalived] # mkdir/etc/ansible/playbooks # create a playbook storage directory [[email protected] roles] # Vim/etc/ansible/playbooks/ap1.yml defines 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 # Check for syntax errors [[email protected] roles] # ansible-playbook/etc/ansible/playbooks/ap1.yml # Run
Configure the playbook of the Apache + MySQL Service
[[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
Configure the playbook of the nginx + keepalived Service
[[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
Client Test Access
[[email protected] ~]# vim /etc/hosts    ...    ...[[email protected] ~]# for i in {1..10};do curl;done    Episode:

[[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> = 100 '))

An error is reported when you run the ansible command. The Python cryptography version must be greater than or equal to 1.1.


[[Email protected] ~] # Yum-y install Python-pip

[[Email protected] ~] # Pip install -- upgrade Cryptography

Ansible implements keepalived)

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.