服务器主机名和 IP 规划参考模板

主机名 eth0 网卡 eth1 网卡 服务简介
lb01 10.0.0.5/24 172.16.1.5/24 负载服务
lb02 10.0.0.6/24 172.16.1.6/24 负载服务
web01 10.0.0.7/24 172.16.1.7/24 phpwww 服务
web02 10.0.0.8/24 172.16.1.8/24 php www 服务
tweb01 10.0.0.9/24 172.16.1.9/24 tomcat www 服务
db01 10.0.0.51/24 172.16.1.51/24 数据库服务
nfs01 10.0.0.31/24 172.16.1.31/24 存储服务
backup 10.0.0.41/24 172.16.1.41/24 备份服务
m01 10.0.0.61/24 172.16.1.61/24 管理服务

基础优化

修改ip地址
sed -i 's#222#61#g' /etc/sysconfig/network-scripts/ifcfg-eth[01] 永久修改主机名
[root@oldboy-c7 ~]# hostnamectl set-hostname oldboyedu-cc7
[root@web01 data]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.1.5 lb01
172.16.1.6 lo02
172.16.1.7 web01
172.16.1.8 web02
172.16.1.9 sweb
172.16.1.31 nfs
172.16.1.41 backup
172.16.1.51 db
#批量推送其他主机
[root@web01 data]# scp -rp /etc/hosts root@172.16.1.31:/etc/ //2.关闭firewalld防火墙
systemctl disable firewalld
systemctl stop firewalld
systemctl status firewalld //3.关闭selinux
# 方式一
sed -ri 's#(^SELINUX=).*#\1disabled#g' /etc/selinux/config
# 方式二
sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config
# 方式三
vim /etc/selinux/config # 临时生效
setenforce 0 //4.优化ulimit
echo '* - nofile 65535' >> /etc/security/limits.conf //5 重启快照

SSH、Ansible,批量管理服务项目

1.创建密钥对
[root@m01 ~]# ssh-keygen -t rsa -C xuliangwei.com #一路回车即可
[root@m01 ~]# ls ~/.ssh/
id_rsa(钥匙) id_rsa.pub(锁头) 2#发送密钥给需要登录的用户
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.31 # 测试
#远程登录对端主机方式
[root@m01 ~]# ssh root@172.16.1.41
# 不登陆主机执行命令
[root@m01 ~]# ssh root@172.16.1.41 "hostname -i" .ansible借助公钥批量管理
#利用非交换式工具实现批量分发公钥与批量管理服务器
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.41 [root@m01 ~]# yum install ansible -y //检查ansible版本
[root@m01 ~]# ansible --version
ansible 2.6.1 配置ansible 主机清单
[root@m01 ~]# vim /etc/ansible/hosts
[lb]
172.16.1.5
172.16.1.6
[web]
172.16.1.7
172.16.1.8
[sweb]
172.16.1.9
[nfs]
172.16.1.31
[backup]
172.16.1.41
[db]
172.16.1.51 测试
# ansible是通过ssh端口探测通信
[root@m01 ~]# ansible all -m ping
#批量执行命令
[root@m01 ~]# ansible all -m command -a "df -h"
[root@m01 ~]# ansible all -m command -a "hostname"

剧本开始…………….10分钟左右

mail.yaml

- import_playbook: base.yaml
- import_playbook: rsync.yaml
- import_playbook: nfs.yaml
- import_playbook: sersync.yaml
- import_playbook: web.yaml
- import_playbook: tweb.yaml
- import_playbook: lb.yaml
- import_playbook: keepalived.yaml
- import_playbook: keepalived02.yaml
- import_playbook: mysql.yaml

base.yaml

- hosts: all
tasks:
# - name: Clear yum.repos.d
# file: path=/etc/yum.repos.d/ state=absent
#
# - name: Create yum.repos.d
# file: path=/etc/yum.repos.d/ state=directory # - name: install aliyun base
# get_url: url=http://mirrors.aliyun.com/repo/Centos-7.repo dest=/etc/yum.repos.d/CentOS-Base.repo
#
# - name: install aliyun epel
# get_url: url=http://mirrors.aliyun.com/repo/epel-7.repo dest=/etc/yum.repos.d/epel.repo - name: Push centos75
copy: src=./file/contos75.repo dest=/etc/yum.repos.d/ - name: Push ops
copy: src=./file/ops.repo dest=/etc/yum.repos.d/ # - name: Dns Client
# copy: src=./conf/resolv.conf dest=/etc/resolv.conf - name: Install base soft
yum: name=rsync,nfs-utils,net-tools,vim,tree,htop,iftop,iotop,lrzsz,sl,wget,unzip,telnet,nmap,nc,psmisc,dos2unix,bash-completion,iotop,iftop,sysstat,screen,zip state=installed - name: Create Group WWW
group: name=www gid=666 - name: Create User WWW
user: name=www uid=666 group=666 create_home=no shell=/sbin/nologin - name: Create Rsync_Client_Pass
copy: content='1' dest=/etc/rsync.pass mode=600 - name: Create Sripts Directory
file: path=/server/scripts/ recurse=yes state=directory - name: Push Scripts
copy: src=./scripts/rsync_backup_md5.sh dest=/server/scripts/ - name: Crontable Scripts
cron: name="backup scripts" hour=01 minute=00 job="/usr/bin/bash /server/scripts/rsync_backup_md5.sh &>/dev/null"

rsync.yaml

- hosts: backup
tasks: - name: Install Rsync Server
yum: name=rsync,mailx state=installed - name: Configure Rsync Server
copy: src=./conf/rsyncd.conf dest=/etc/rsyncd.conf
notify: Restart Rsync Server - name: Create Date
file: path=/data state=directory owner=www group=www mode=755 - name: Create Backup
file: path=/backup state=directory owner=www group=www mode=755 - name: Create Virt User
copy: content='rsync_backup:1' dest=/etc/rsync.password mode=600 - name: Start RsyncServer
service: name=rsyncd state=started enabled=yes - name: Push Check Scripts
copy: src=./scripts/rsync_check_backup.sh dest=/server/scripts/ - name: Crond Check Scripts
cron: name="check scripts" hour=05 minute=00 job="/usr/bin/bash /server/scripts/rsync_check_backup.sh &>/dev/null" handlers:
- name: Restart Rsync Server
service: name=rsyncd state=restarted

nfs.yaml

- hosts: nfs
tasks: - name: Installed Nfs Server
yum: name=nfs-utils state=installed - name: Configure Nfs Server
copy: src=./conf/exports dest=/etc/exports
notify: Restart Nfs Server - name: Create Share Data
file: path=/data state=directory owner=www group=www mode=755 - name: Create Share /data{}
shell: mkdir /data/{wordpress,wecenter,jpress} -p - name: Chown -R www.www /data
file: path=/data recurse=yes owner=www group=www - name: Start Nfs Server
service: name=nfs-server state=started enabled=yes handlers:
- name: Restart Nfs Server
service: name=nfs-server state=restarted

sersync.yaml

- hosts: nfs
tasks: - name: Scp Sersync
copy: src=./file/sersync2.5.4_64bit_binary_stable_final.tar.gz dest=/usr/local/sersync.tar.gz - name: Zip
shell: cd /usr/local && tar xf sersync.tar.gz && mv GNU-Linux-x86 sersync
args:
creates: /usr/local/sersync - name: configure Sersync
copy: src=./conf/confxml.xml dest=/usr/local/sersync/confxml.xml
notify: kill old sersync and restart new sersync - name: Start Sersync
shell: pgrep sersync;
[ $? -eq 0 ] || /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml handlers:
- name: kill old sersync and restart new sersync
shell: pegrep sersync | xargs kill -9;
/usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml

web.yaml

- hosts: web
tasks:
# - name: Mount NFS Server Share Date
# mount: src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=mounted - name: Install Mariadb
yum: name=mysql state=installed - name: Install nginx
yum: name=nginx state=installed - name: nginx.conf copy
copy: src=./conf/nginx.conf dest=/etc/nginx/nginx.conf
notify: Restart nginx - name: install php7.1
yum: name=php71w,php71w-cli,php71w-common,php71w-devel,php71w-embedded,php71w-gd,php71w-mcrypt,php71w-mbstring,php71w-pdo,php71w-xml,php71w-fpm,php71w-mysqlnd,php71w-opcache,php71w-pecl-memcached,php71w-pecl-redis,php71w-pecl-mongodb state=installed - name: Copy www.conf
copy: src=./conf/www.conf dest=/etc/php-fpm.d/www.conf
notify: Restart php-fpm - name: Copy php.ini
copy: src=./conf/php.ini dest=/etc/php.ini
notify: Restart php-fpm - name: Start php-fpm
service: name=php-fpm state=started enabled=yes #- name: Unzip kaoshi.zip
# unarchive: src=./file/kaoshi.zip dest=/data/ creates=/data/index.html - name: Start nginx
service: name=nginx state=started enabled=yes - name: Del /etc/nginx/conf.d/default.conf
file: path=/etc/nginx/conf.d/default.conf state=absent - name: Copy conf.d/*
unarchive: src=./conf/conf.zip dest=/etc/nginx/conf.d/ creates=/etc/nginx/conf.d/wecenter.conf - name: Copy ./file/ssl_key.zip
unarchive: src=./file/ssl_key.zip dest=/etc/nginx/ creates=/etc/nginx/ssl_key/server.crt - name: Create /code
file: path=/code/ recurse=yes state=directory mode=755 owner=www group=www - name: Copy /code.zip
unarchive: src=./file/code.zip dest=/code/ creates=/code/wordpress/index.php - name: chown www.www /code
file: path=/code owner=www group=www mode=0755 # - name: Mount data
# mount: src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=mounted - name: Mount wordpress
mount: src=172.16.1.31:/data/wordpress path=/code/wordpress/wp-content/uploads fstype=nfs opts=defaults state=mounted - name: Mount wecenter
mount: src=172.16.1.31:/data/wecenter path=/code/wecenter/uploads fstype=nfs opts=defaults state=mounted - name: Start nginx
service: name=nginx state=started enabled=yes # - name: recovery data
# shell: cp -rp /code/wecenter/uploads_bak/* /code/wecenter/uploads/ && cp -rp /code/wordpress/wp-content/uploads_bak/* /code/wordpress/wp-content/uploads/ handlers:
- name: Restart nginx
service: name=nginx state=restarted enabled=yes - name: Restart php-fpm
service: name=php-fpm state=restarted enabled=yes

tweb.yaml

- hosts: sweb
tasks:
- name: Install java jarjar
yum: name=java,jarjar-maven-plugin state=installed - name: Create /server
file: path=/server/scripts/ recurse=yes state=directory - name: Unzip tomcat8_1.zip
unarchive: src=./file/tomcat8_1.zip dest=/server/ creates=/server/tomcat8_1/bin/startup.sh - name: Configgurl copy
copy: src=./conf/server.xml dest=/server/tomcat8_1/conf/server.xml
notify: Restart tomcat - name: chown www
file: path=/server/tomcat8_1 recurse=yes owner=www group=www - name: Start tomcat8_1
# command: /server/tomcat8_1/bin/startup.sh
shell: /server/tomcat8_1/bin/startup.sh - name: Mount NFS Server Share jpress
mount: src=172.16.1.31:/data/jpress path=/server/tomcat8_1/webapps/jpress/attachment fstype=nfs opts=defaults state=mounted # - name: Recovery data
# shell: cd /server/tomcat8_1/webapps/jpress && cp -rp attachment_bak/* attachment/ # - name chown www
# shell: chown -R www.www /server/tomcat8_1/webapps # handlers:
# - name: Restart tomcat
# shell: /server/tomcat8_1/bin/shutdown.sh && /server/tomcat8_1/bin/startup.sh

lb.yaml

- hosts: lb
tasks: - name: install nginx
yum: name=nginx state=installed - name: Del /etc/nginx/conf.d/default.conf
file: path=/etc/nginx/conf.d/default.conf state=absent - name: Copy ds.conf
copy: src=./lb/ds.conf dest=/etc/nginx/conf.d/ds.conf
notify: Restart nginx - name: Copy proxy-https.conf
copy: src=./lb/proxy-https.conf dest=/etc/nginx/conf.d/proxy-https.conf
notify: Restart nginx - name: Copy ./file/ssl_key.zip
unarchive: src=./file/ssl_key.zip dest=/etc/nginx/ creates=/etc/nginx/ssl_key/server.crt - name: Copy proxy_params
copy: src=./lb/proxy_params dest=/etc/nginx/proxy_params
notify: Restart nginx - name: start nginx
service: name=nginx state=started enabled=yes handlers:
- name: Restart nginx
service: name=nginx state=restarted enabled=yes

keepalived.yaml

- hosts: lb
tasks: - name: install keepalived
yum: name=keepalived state=installed - name: Copy keepalived.conf
copy: src=./lb/keepalived.conf dest=/etc/keepalived/keepalived.conf
notify: Restart keepalived - name: start keepalived
service: name=keepalived state=started enabled=yes handlers:
- name: Restart keepalived
service: name=keepalived state=restarted enabled=yes

keepalived2.yaml

- hosts: 172.16.1.6
tasks: - name: Copy keepalived2.conf
copy: src=./lb/keepalived2.conf dest=/etc/keepalived/keepalived.conf
notify: Restart keepalived - name: start keepalived
service: name=keepalived state=started enabled=yes handlers:
- name: Restart keepalived
service: name=keepalived state=restarted enabled=yes

mysql.yaml

- hosts: db
tasks: - name: Install mysql-community
yum: name=mysql-community-server state=installed - name: Start mysqld
service: name=mysqld state=started enabled=yes # - name: copy /etc/my.cnf
# copy: src=./conf/my.cnf dest=/etc/my.cnf # - name: Restart mysqld
# service: name=mysqld state=restarted enabled=yes # - name: modify mysql passwd
# shell: mysql -uroot -se "update mysql.user set authentication_string=password('Ckh123.com') where user='root';" # - name: modify my.cnf
# shell: sed '20s#skip-grant-tables##pg' /etc/my.cnf # - name: Restart mysqld
# service: name=mysqld state=restarted enabled=yes # - name: Grant all user
# shell: mysql -uroot -pCkh123.com mysql -se "update user set host = '%' where user = 'root';" # - name: flush privileges
# shell: mysql -uroot -p'Ckh123.com' -se "flush privileges;" # - name: Create daabase
# shell: mysql -uroot -p'Ckh123.com' -se "create database wordpress;" - name: Copy backup.sql
copy: src=./file/2018-10-0613-mysql-all.sql dest=/tmp/ # - name: Input mysql
# shell: mysql -uroot -p'Ckh123.com'</root/2018-09-2417-mysql-all.sql

善后操作

#4.由于mysql5.7默认配置了默认密码, 需要过滤temporary password关键字查看对应登陆数据库密码
[root@nginx ~]# grep 'temporary password' /var/log/mysqld.log
#5.登陆mysql数据库[password中填写上一步过滤的密码]
[root@web02 ~]# mysql -uroot -p$(awk '/temporary password/{print $NF}' /var/log/mysqld.log)
#6.重新修改数据库密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Ckh123.com';
# 服务器mysql允许远程用户连接 (授权法)
grant all privileges on *.* to 'all'@'%' identified by 'Ckh123.com';
flush privileges; 1.老服务器操作
#1.指定导出对应的数据库文件。
[root@web02 ~]# mysqldump -uroot -p'Ckh123.com' --all-databases --single-transaction > `date +%F%H`-mysql-all.sql
#2.传输备份数据库文件至新服务器
[root@web02 zh]# scp 2018-10-0613-mysql-all.sql root@10.0.0.51:/tmp 2.新服务器操作
#1.导入数据库
[root@db01 ~]# cd /tmp && mysql -uroot -p'Ckh123.com' < 2018-10-0613-mysql-all.sql # 手动启动tomcat8
/usr/bin/sh /server/tomcat8_1/bin/startup.sh

扩展 zip命令使用

# 当前目录下 所有文件 压缩包
[root@web01 conf.d]# zip conf.zip ./*
# -r 递归所有目录
[root@web01 conf.d]# zip -r conf.zip ./*
2.unzip
unzip -o -d /home/sunny myfile.zip
把myfile.zip文件解压到 /home/sunny/
-o:不提示的情况下覆盖文件;
-d:-d /home/sunny 指明将文件解压缩到/home/sunny目录下; 3.其他
zip -d myfile.zip smart.txt
删除压缩文件中smart.txt文件
zip -m myfile.zip ./rpm_info.txt
向压缩文件中myfile.zip中添加rpm_info.txt文件

源码下载地址

链接:https://pan.baidu.com/s/1KqE4sdDdQxhIHJyK4QFfuw 密码:cwtn

ansible_playbook 一键搭建集群架构的更多相关文章

  1. redis在Windows下以后台服务一键搭建集群(多机器)

    redis在Windows下以后台服务一键搭建集群(多机器) 一.概述 此教程介绍如何在windows系统中多台机器之间布置redis集群,同时要以后台服务的模式运行.布置以脚本的形式,一键完成.多台 ...

  2. redis在Windows下以后台服务一键搭建集群(单机--伪集群)

    redis在Windows下以后台服务一键搭建集群(单机--伪集群) 一.概述 此教程介绍如何在windows系统中同一台机器上布置redis伪集群,同时要以后台服务的模式运行.布置以脚本的形式,一键 ...

  3. MongoDB集群架构及搭建

    MongoDB分布式集群 MongDB分布式集群能够对数据进行备份,提高数据安全性,以及提高集群提高读写服务的能力和数据存储能力.主要通过副本集(replica)对数据进行备份,通过分片(shardi ...

  4. 架构之路:nginx与IIS服务器搭建集群实现负载均衡(二)

    [前言] 在<架构之路:nginx与IIS服务器搭建集群实现负载均衡(一)>中小编简单的讲解了Nginx的原理!俗话说:光说不练假把式.接下来,小编就和大家一起来做个小Demo来体会一下N ...

  5. 架构之路:nginx与IIS服务器搭建集群实现负载均衡(三)

    参考网址:https://blog.csdn.net/zhanghan18333611647/article/details/50811980 [前言] 在<架构之路:nginx与IIS服务器搭 ...

  6. Mongodb集群搭建之 Sharding+ Replica Sets集群架构(2)

    参考http://blog.51cto.com/kaliarch/2047358 一.概述 1.1 背景 为解决mongodb在replica set每个从节点上面的数据库均是对数据库的全量拷贝,从节 ...

  7. Hadoop化繁为简-从安装Linux到搭建集群环境

    简介与环境准备 hadoop的核心是分布式文件系统HDFS以及批处理计算MapReduce.近年,随着大数据.云计算.物联网的兴起,也极大的吸引了我的兴趣,看了网上很多文章,感觉还是云里雾里,很多不必 ...

  8. linux集群架构

    Linux集群架构   根据功能划分为两大类:高可用和负载均衡 高可用集群通常为两台服务器,一台工作,另外一台作为冗余,当提供服务的机器宕机,冗余将接替继续提供服务 实现高可用的开源软件有:heart ...

  9. 网站集群架构(LVS负载均衡、Nginx代理缓存、Nginx动静分离、Rsync+Inotify全网备份、Zabbix自动注册全网监控)--技术流ken

    前言 最近做了一个不大不小的项目,现就删繁就简单独拿出来web集群这一块写一篇博客.数据库集群请参考<MySQL集群架构篇:MHA+MySQL-PROXY+LVS实现MySQL集群架构高可用/高 ...

随机推荐

  1. MySQL 数据库死锁

    数据库死锁 死锁的解决办法(1) 执行下面SQL,先查看哪些表被锁住了: select b.owner,b.object_name,a.session_id,a.locked_mode from v$ ...

  2. LOJ #6050. 「雅礼集训 2017 Day11」TRI

    完全不会的数学神题,正解留着以后填坑 将一个口胡的部分分做法,我们考虑计算格点多边形(包括三角形)面积的皮克公式: \[S=a+\frac{1}{2}b-1\text({a为图形内部节点个数,b为边界 ...

  3. ==运算符和equals()方法的区别

    Java语言程序中判断两个变量是否相等有两种方式:一是运用==运算符,二是运用equals方法. 1. ==运算符 对于==运算符来说,如果两个变量是基本类型的,并且是数值类型,则只要它们的值相等,就 ...

  4. javascript权威指南笔记[6-8]

    1.三类对象,两类属性 2.原型链只有在查询的时候才会体现 3.对象属性访问 4.属性赋值 5.delete只是断开属性和宿主对象的关系,不会去操作属性中的属性 6.Object.ke() var m ...

  5. 腾讯短信服务精简版(PHP )

    短信视乎已经被慢慢淡出平常的交流工具队列,但始终抹不去它的存在,短信验证码视乎从未被取代,此外在重要的信息通知的地位也是不可取的的.所以了解短信的使用是开发中很有必要的一环. 腾讯云的短信服务提供有1 ...

  6. 从一道面试题探究 Integer 的实现

    记得有次面试,面试官问我: 如何写一个方法交换两个 Integer 类型的值? 当时心里一惊,这是把我当小白了呀!交换两个数的值还不容易么,最简单的直接搞一个中间变量,然后就可以交换了… … 面试官随 ...

  7. Nginx的配置文件nginx.conf解析

    安装openresty的nginx.conf配置文件 0.ng运行的用户和用户组 1.ng进程数,设置为CPU总核心数 2.ng错误日志 3.进程文件,有时ng启动不了,将进程文件删除即可. 4.单进 ...

  8. qml demo分析(threading-线程任务)

    一.关键类说明 qml内置了WorkerScript组件,该组件有一个source属性,可以加载js文件,含有一个名为message的信号,意味着他有一个默认的onMessage槽函数,除此之外他还有 ...

  9. C# 《编写高质量代码改善建议》整理&笔记 --(四)资源管理&序列化

    1.显示释放资源需继承接口IDisposable 什么是资源:C#中每一个类型都代表一种资源,而资源又分为以下两类. 托管资源:由CLR管理分配和释放的资源,即从CLR里new出来的对象. 非托管资源 ...

  10. 【Android Studio安装部署系列】四十、Android Studio安装Statistic插件(统计项目总行数)

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 概述 Android Studio 是没有提提供统计代码全部行数的功能的,但是对于开发者来说,这个功能确实必备的,Statistic统计代 ...