ansible自动部署集群服务

上面的思路大致是:

首先配置yum仓库,之后搭建http+php,之后搭建数据库,其次搭建nginx反代,最后设置keepalived自动化安装。

[[email protected] ansible]# pwd

/etc/ansible

[[email protected] ansible]# tree -L 3 roles/

roles/

├── base

│   ├── files

│   │   ├── mage6.repo

│   │   └── mage7.repo

│   └── tasks

│       └── main.yml

├── db

│   ├── files

│   │   ├── my6.cnf

│   │   └── my7.cnf

│   ├── handlers

│   │   └── main.yml

│   └── tasks

│       └── main.yml

├── http+php

│   ├── handlers

│   │   └── main.yml

│   ├── tasks

│   │   └── main.yml

│   ├── templates

│   │   ├── httpd.conf6.j2

│   │   └── httpd.conf7.j2

│   └── vars

│       └── main.yml

├── keepalived

│   ├── handlers

│   │   └── main.yml

│   ├── tasks

│   │   └── main.yml

│   └── templates

│       └── keepalived.conf.j2

├── nginx

│   ├── handlers

│   │   └── main.yml

│   ├── tasks

│   │   └── main.yml

│   ├── templates

│   │   └── nginx.conf.j2

│   └── vars

│       └── main.yml

└── webdata

├── files

│   ├── index.html

│   ├── index.php

│   └── wordpress

└── tasks

└── main.yml

最后建立完成要有这些文件

首先修改ansible主配置文件

[[email protected] ansible]# vim hosts

只留一下部分:

[keepnginx]

172.16.1.3 hhname=kepnx1.zou.com state=MASTER pri=100

172.16.1.5 hhname=kepnx2.zou.com state=BACKUP pri=98

[httphp]

172.16.1.11 hhname=hp1.zou.com

172.16.1.8 hhname=hp2.zou.com

[db]

172.16.1.12 hhname=db.zou.com

base

├── files

│   ├── mage6.repo

│   └── mage7.repo

└── tasks

└── main.yml

[[email protected] roles]# vim base/tasks/main.yml


- name: install repo-file

copy: src=mage7.repo dest=/etc/yum.repos.d/

when:  ansible_distribution_major_version == "7"

- name: install repo source for yum

copy: src=mage6.repo dest=/etc/yum.repos.d/

when:  ansible_distribution_major_version == "6"

- name: rm some file of repos

shell: rm -rf /etc/yum.repos.d/C*

- name: set hostname

hostname: name={{ hhname }}

tags: sethostname

- name: install killall for ckeck servers‘s state

yum: name=psmisc state=latest

- name: install bash-completion

yum: name=bash-completion state=latest

之后准备好两个可以yum安装册仓库源设置好mage6.repo 和  mage7.repo

http+php/

├── handlers

│   └── main.yml

├── tasks

│   └── main.yml

├── templates

│   ├── httpd.conf6.j2

│   └── httpd.conf7.j2

└── vars

└── main.yml

[[email protected] roles]# vim http+php/handlers/main.yml


- name: restart httpd

service: name=httpd state=restarted

[[email protected] roles]# vim http+php/tasks/main.yml


- name: install http

yum: name=httpd state=latest

- name: install php

yum: name=php state=latest

- name: install php-mysql

yum: name=php-mysql state=latest

- name: install php-gd

yum: name=php-gd state=latest

- name: install php-mbsting

yum: name=php-mbstring state=latest

when: ansible_distribution_major_version == "7"

- name: install php-xml

yum: name=php-xml state=latest

- name: mkdir web‘ file

file: path={{ htdocumentroot }} state=directory

- name: install httpd.conf

template: src=httpd.conf6.j2 dest=/etc/httpd/conf/httpd.conf

notify: restart httpd

tags: rehttpdconf

when: ansible_distribution_major_version == "6"

- name: install httpd.conf

template: src=httpd.conf7.j2 dest=/etc/httpd/conf/httpd.conf

notify: restart httpd

tags: rehttpdconf

when: ansible_distribution_major_version == "7"

- name: start httpd

service: name=httpd state=started

[[email protected] roles]# vim http+php/templates/httpd.conf6.j2

修改:

Listen {{ htport }}

DocumentRoot "{{ htdocumentroot }}"

<Directory "{{ htdocumentroot }}">

ErrorLog {{ htdocumentroot }}/error_log

CustomLog {{ htdocumentroot }}/access_log combined

[[email protected] roles]# vim http+php/templates/httpd.conf7.j2

修改:

Listen {{ htport }}

User {{ htuser }}

Group {{ htgroup }}

ServerName {{ hhname }}:80

DocumentRoot "{{ htdocumentroot }}"

<Directory "{{ htdocumentroot }}">

<Directory "{{ htdocumentroot }}">

ErrorLog "{{ htdocumentroot }}/error_log"

CustomLog "{{ htdocumentroot }}/access_log" combined

[[email protected] roles]# vim http+php/vars/main.yml


htuser: apache

htgroup: apache

htport: 80

htdocumentroot: /data/www

db

├── files

│   ├── my6.cnf

│   └── my7.cnf

├── handlers

│   └── main.yml

└── tasks

└── main.yml

[[email protected] db]# vim files/my6.cnf


[mysqld]

datadir=/data/db

socket=/var/lib/mysql/mysql.sock

user=mysql

innodb_file_per_table=ON

skip_name_resolve=ON

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

[[email protected] db]# vim files/my7.cnf


[mysqld]

datadir=/data/db

socket=/var/lib/mysql/mysql.sock

innodb_file_per_table=ON

skip_name_resolve=ON

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

# Settings user and group are ignored when systemd is used.

# If you need to run mysqld under a different user or group,

# customize your systemd unit file for mariadb according to the

# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]

log-error=/var/log/mariadb/mariadb.log

pid-file=/var/run/mariadb/mariadb.pid

#

# include all files from the config directory

#

!includedir /etc/my.cnf.d

vim handlers/main.yml


- name: restart mariadb

service: name=mariadb state=restarted

- name: restart mysql

service: name=mysqld state=restarted

[[email protected] db]# vim tasks/main.yml


- name: install mariadb-server

yum: name=mariadb-server state=latest

when: ansible_distribution_major_version == "7"

- name: install mysql-server

yum: name=mysql-server state=latest

when: ansible_distribution_major_version == "6"

- name: build data file

file: path=/data/db owner=mysql group=mysql state=directory

- name: install mariadb conf

copy: src=my7.cnf dest=/etc/my.cnf

notify: restart mariadb

tags: remariadbconf

when: ansible_distribution_major_version == "7"

- name: install mysql conf

copy: src=my6.cnf dest=/etc/my.cnf

notify: restart mysql

tags: remysqlconf

when: ansible_distribution_major_version == "6"

- name: start mariadb

service: name=mariadb state=started

when: ansible_distribution_major_version == "7"

- name: start mysql

service: name=mysqld state=started

when: ansible_distribution_major_version == "6"

webdata/

├── files

│   ├── index.html

│   ├── index.php

│   └── wordpress

└── tasks

└── main.yml

[[email protected] roles]# vim webdata/tasks/main.yml


- name: web of index.html for test

copy: src=index.html dest=/data/www

- name: web of index.php for test

copy: src=index.php dest=/data/www

- name: web of wordpress

copy: src=wordpress dest=/data/www/

tags: copywordpress

[[email protected] roles]# vim webdata/files/index.html

web form {{ hhname }} the version is {{ ansible_distribution_major_version }};

[[email protected] roles]# vim webdata/files/index.ph

<?php

$conn=mysql_connect(‘172.16.1.12‘,‘zou‘,‘123.comer‘);

if($conn)

echo ok;

echo the web from {{ hhname }};

else

echo fault;

mysql_close();

phpinfo()

?>

之后准备好wordpress网页压缩包解压缩只有放到这个响应的位置,并编辑好wp-config.php

nginx

├── handlers

│   └── main.yml

├── tasks

│   └── main.yml

├── templates

│   └── nginx.conf.j2

└── vars

└── main.yml

[[email protected] nginx]# vim tasks/main.yml


- name: install nginx package

yum: name=nginx state=present

- name: install conf file

template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf

notify: restart nginx

tags: reinstallconf

- name: start nginx

service: name=nginx state=started enabled=true

[[email protected] nginx]# cat handlers/main.yml

- name: restart nginx

service: name=nginx state=restarted

[[email protected] nginx]# cat vars/main.yml

username: nginx

[[email protected] nginx]# grep -v ‘^[[:space:]]\+#‘ templates/nginx.conf.j2


user  {{ username }};

worker_processes  {{ ansible_processor_vcpus }};

error_log  /var/log/nginx/error.log warn;

pid        /var/run/nginx.pid;

events {

worker_connections  1024;

}

http {

include       /etc/nginx/mime.types;

default_type  application/octet-stream;

log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘

‘$status $body_bytes_sent "$http_referer" ‘

‘"$http_user_agent" "$http_x_forwarded_for"‘;

access_log  /var/log/nginx/access.log  main;

sendfile        on;

keepalive_timeout  65;

gzip  on;

upstream backend {

server 172.16.1.8;

server 172.16.1.11 weight=2;

}

include /etc/nginx/conf.d/*.conf;

#########################################################################

#sorry nginx      #

###################

server {

listen       80;

server_name  {{ hhname }};

#

location / {

proxy_pass http://backend;

index index.html index.php;

}

error_page   500 502 503 504  /50x.html;

}

}

keepalived/

├── handlers

│   └── main.yml

├── tasks

│   └── main.yml

└── templates

└── keepalived.conf.j2

[[email protected] keepalived]# vim tasks/main.yml


- name: install the keepalived

yum: name=keepalived state=latest

- name: install ntpdate

yum: name=ntpdate state=latest

- name: make time to equal

shell: ntpdate 172.16.0.1

- name: install the conf_file

template: src=keepalived.conf.j2 dest=/etc/keepalived/keepalived.conf

notify: restart keepalived

tags: rekeepconf

- name: start keepalived

service: name=keepalived state=started enabled=true

[[email protected] keepalived]# vim handlers/main.yml


- name: restart keepalived

service: name=keepalived state=restarted

[[email protected] keepalived]# cat templates/keepalived.conf.j2

global_defs {

notification_email {

[email protected]

}

notification_email_from [email protected]

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id {{ hhname }}

vrrp_mcast_group4 224.0.101.1

}

vrrp_script chk_nginx {

script "killall -0 nginx && exit 0 || exit 1"

interval 1

weight -5

}

track_script {

chk_nginx

}

vrrp_instance VI_1 {

state {{ state }}

interface eno16777736

virtual_router_id 101

priority {{ pri }}

advert_int 1

authentication {

auth_type PASS

auth_pass 123.com

}

virtual_ipaddress {

172.16.1.4

}

track_script {

chk_nginx

}

}

基本定义完成角色,但是要想要生效还要调用角色,执行才能实现集群的部署

/root/myansible/

├── base.yml

├── db.yml

├── hp+webdata.yml

├── http+php.yml

└── keng.yml

[[email protected] myansible]# cat base.yml

- hosts: all

remote_user: root

roles:

- base

[[email protected] myansible]# cat http+php.yml

- hosts: httphp

remote_user: root

roles:

- http+php

[[email protected] myansible]# cat db.yml

- hosts: db

remote_user: root

roles:

- db

[[email protected] myansible]# cat hp+webdata.yml

- hosts: httphp

remote_user: root

roles:

- webdata

[[email protected] myansible]# cat keng.yml

- hosts: keepnginx

remote_user: root

roles:

- keepalived

- { role: nginx, username: nginx, when: "ansible_distribution_major_version == ‘7‘" }

ansible是不同启动的,安装完毕,配置好hosts文件即可使用,这就是安装了一个命令

[[email protected] myansible]# ansible-playbook base.yml --check

[[email protected] myansible]# ansible-playbook base.yml

[[email protected] myansible]# ansible-playbook http+php.yml --check

[[email protected] myansible]# ansible-playbook http+php.yml

[[email protected] myansible]# ansible-playbook db.yml --check

[[email protected] myansible]# ansible-playbook db.yml

[[email protected] myansible]# ansible-playbook hp+webdata.yml --check

[[email protected] myansible]# ansible-playbook hp+webdata.yml

[[email protected] myansible]# ansible-playbook keng.yml --check

[[email protected] myansible]# ansible-playbook keng.yml

时间: 2024-08-07 04:33:27

ansible自动部署集群服务的相关文章

kubernetes有状态集群服务部署与管理

有状态集群服务的两个需求:一个是存储需求,另一个是集群需求.对存储需求,Kubernetes的解决方案是:Volume.Persistent Volume .对PV,除了手动创建PV池外,还可以通过Storage Class来让存储系统自动创建. 对集群需求,Kubernetes的解决方案是Pet Set.Pet Set 又通过Init Container来做集群初始化,通过Headless Service来为集群成员提供稳定 的网络身份. 在K8S运行的服务,从简单到复杂可以分成三类:无状态服

Kubernetes 部署集群内部DNS服务

Kubernetes 部署集群内部DNS服务 部署官网:https://github.com/kubernetes/kubernetes/tree/master/cluster/addons/dns/coredns 为服务提供名称域名的访问. - DNS服务监视Kubernetes API,为每一个Service创建DNS记录用于域名解析.- ClusterIP A记录格式:<service-name>.<namespace-name>.svc.cluster.local示例:my

kubernetes使用ansible快速构建集群

软硬件限制: 1)cpu和内存 master:至少1c2g,推荐2c4g:node:至少1c2g 2)linux系统 内核版本至少3.10,推荐CentOS7/RHEL7 3)docker 至少1.9版本,推荐1.12+ 4)etcd 至少2.0版本,推荐3.0+ kubernetes官方github地址 https://github.com/kubernetes/kubernetes/releases 高可用集群所需节点规划: 部署节点------x1 : 运行这份 ansible 脚本的节点

Linux下使用Apache的Httpd+Mod_jk+Tomcat搭建Web集群服务

Linux下使用Apache的Httpd+Mod_jk+Tomcat搭建Web集群服务 目的 ?? 使用多个tomcat服务器来对请求进行分流,防止单个服务器压力过重.这里为了简单,只使用两个tomcat. 软件 apache httpd-2.2.31(下载地址:https://httpd.apache.org/download.cgi) apache tomcat-7.0.69(下载地址:https://tomcat.apache.org/download-70.cgi) tomcat-con

使用Codis搭建redis集群服务

转(http://www.jianshu.com/p/f8e968e57863) 一. 应用场景 redis 作为数据结构存储引擎,有着很多优点 高性能单机引擎可以达到5-10W qps 数据结构全面,支持快速开发业务string,list,set,sorted set, hashes 问题: 存储容量受限单机最大容量即为单机内存最大容量 单机数据的持久化依赖aof和rdb机制,如果机器整个down掉,服务不可用 二. redis集群选型 正是由于单机redis引擎有着这样的问题,所以,基本每个

WebLogic部署集群和代理服务器

应公司要求,最近在学习weblogic集群这块的知识,下面我把我这几天学到的,以及过程中遇到的问题及如何解决的,分享给大家.首先,weblogic是Orcale公司的一款产品,至于其作用,我想就不用我说了.但是关于weblogic里面的几个专业名词,我想还是有必要去说一下的. 1.Server 用过tomcat的朋友应该清楚,当我们有多个项目部署在同一个tomcat服务器下的时候,我们只需要启动这个tomcat就可以根据目录的不同,访问这些应用,此时我们用的端口号不需要改变.在weblogic服

实现MySQL读写分离 部署集群基础环境(有图)

实现MySQL读写分离 部署集群基础环境 1 实现MySQL读写分离1.1 问题 本案例要求配置2台MySQL服务器+1台代理服务器,实现MySQL代理的读写分离: 用户只需要访问MySQL代理服务器,而实际的SQL查询.写入操作交给后台的2台MySQL服务器来完成 其中Master服务器允许SQL查询.写入,Slave服务器只允许SQL查询 1.2 方案 使用4台RHEL 7.2虚拟机,如图-1所示.其中192.168.4.10.192.168.4.20分别作为MySQL主.从服务器,是整个服

从零开始,使用Docker Swarm部署集群教程

本文首先从Dockerfile创建了一个简单web镜像 然后将web镜像推送到了远程仓库,以备后面集群中不同机器自动下载 之后使用docker-compose.yml配置了一个应用 而后新建了2台虚拟机作为swarm节点,并部署应用的5个实例在这两台虚拟机上 最后还讲了如何如果更改集群配置.如何扩容您的集群和如重新发布您的应用 一.创建一个简单web镜像,并推送到docker仓库 1.创建Dockerfile 创建一个空目录, 然后CD到新目录,创建名为Dockerfile的文件,将以下内容复制

[转贴]CentOS7.5 Kubernetes V1.13(最新版)二进制部署集群

CentOS7.5 Kubernetes V1.13(最新版)二进制部署集群 http://blog.51cto.com/10880347/2326146 一.概述 kubernetes 1.13 已发布,这是 2018 年年内第四次也是最后一次发布新版本.Kubernetes 1.13 是迄今为止发布间隔最短的版本之一(与上一版本间隔十周),主要关注 Kubernetes 的稳定性与可扩展性,其中存储与集群生命周期相关的三项主要功能已逐步实现普遍可用性. Kubernetes 1.13 的核心