使用ansible ad-hoc部署企业集群

使用ansible部署企业架构

环境

主机名 角色 外网IP 内网IP 安装服务
db03 代理 10.0.0.53 172.16.1.53 nginx
db04 代理 10.0.0.54 172.16.1.54 nginx
lb01 负载均衡 10.0.0.51 172.16.1.51 nginx
web01 服务器 10.0.0.7 172.16.1.7 nginx php nfs-utils
web02 服务器 10.0.0.8 172.16.1.8 nginx php nfs-utils
nfs 共享存储 10.0.0.31 172.16.1.31 nfs-utils
db01 数据库 10.0.0.51 172.16.1.51 mariadb-server

推送公钥脚本

#推送过后,使用172.16.1.网段, 跳板机可以直接连接,10.0.0.网段第一次的连接只需要输入yes
#使用该脚本可以向新克隆的虚拟机推送该公钥

vim /root/jb.sh	    
#!/bin/bash 
pass='1'
        ip='172.16.1.'
        ip2='10.0.0.'

        ssh-keygen -t rsa -P "" -f /root/.ssh/id_rsa

        for i in  5 6 7 8 9 31 41 51 52 53 54 61 71 81;
        do
        sshpass -p $pass ssh-copy-id -i /root/.ssh/id_rsa.pub -o stricthostkeychecking=no root@${ip}${i}
        
        sshpass -p $pass ssh-copy-id -i /root/.ssh/id_rsa.pub -o stricthostkeychecking=no root@${ip2}${i}
        
        done
	    chmod 600 /root/jb.sh
1.安装absible
[root@m01 ~]# yum install -y ansible
2.优化ansible
[root@m01 ~]#  vim /etc/ansible/ansible.cfg		#改为
host_key_checking = False
3.创建密钥对
[root@m01 ~]# ssh-keygen
4.推送公钥
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.5
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.6
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.7
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.8
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.9
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.31
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.41
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.51
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.52
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.53
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.54
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.61
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.71
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.81

#或者使用脚本推送公钥
sh jb.sh

5.编辑主机清单
[root@m01 ~]# vim /etc/ansible/hosts
[web_group]
web01 ansible_ssh_host=172.16.1.7 asible_ssh_user=root ansible_ssh_port=22
web02 ansible_ssh_host=172.16.1.8 asible_ssh_user=root ansible_ssh_port=22
web03 ansible_ssh_host=172.16.1.9 asible_ssh_user=root ansible_ssh_port=22

[db_group]
db01 ansible_ssh_host=172.16.1.51 asible_ssh_user=root ansible_ssh_port=22
db02 ansible_ssh_host=172.16.1.52 asible_ssh_user=root ansible_ssh_port=22
db03 ansible_ssh_host=172.16.1.53 asible_ssh_user=root ansible_ssh_port=22
db04 ansible_ssh_host=172.16.1.54 asible_ssh_user=root ansible_ssh_port=22

[nfs_group]
nfs ansible_ssh_host=172.16.1.31 asible_ssh_user=root ansible_ssh_port=22

[redis_group]
redis ansible_ssh_host=172.16.1.81 asible_ssh_user=root ansible_ssh_port=22

[lb_group]
lb01 ansible_ssh_host=172.16.1.5 asible_ssh_user=root ansible_ssh_port=22
lb02 ansible_ssh_host=172.16.1.6 asible_ssh_user=root ansible_ssh_port=22

[backup_group]
backup ansible_ssh_host=172.16.1.41 asible_ssh_user=root ansible_ssh_port=22

[zabbix_group]
zabbix ansible_ssh_host=172.16.1.71 asible_ssh_user=root ansible_ssh_port=22

[m01_group]
m01 ansible_ssh_host=172.16.1.61 asible_ssh_user=root ansible_ssh_port=22

6.仪式(检测)
[root@m01 ~]# ansible '*' -m ping

部署nginx

1.部署官方yum源
[root@m01 ~]# ansible '*' -m yum_repository -a 'name=syy_add_nginx description=EE baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=no enabled=yes file=nginx'

2.下载nginx(时间不是一般的长)
[root@m01 ~]# ansible '*' -a 'yum install -y nginx'

3.启动并加入开机自启
[root@m01 ~]# ansible all -m systemd -a 'name=nginx state=started enabled=yes'

部署rsync

客户端+服务端

1.安装
[root@m01 ~]# ansible all -m yum -a 'name=rsync'
2.编辑m01本地的配置文件,再拷贝到nfs服务端
[root@m01 ~]# vim /etc/rsyncd.conf  #先删除,再拷贝
#################################### 服务相关配置 ###########################
uid = www	
#指定rsync进程启动的用户(打工的),和传送文件的用户---------useradd
gid = www			
#指定rsync进程启动的组
port = 873			
#指定rsync使用的监听端口(默认873端口,可以改)
fake super = yes	 
#无需让rsync以root身份运行,允许接收文件的  完整属性(属主属组不变)
use chroot = no		 	
#禁锢指定的目录(只能推到指定的目录,不紧固的话随便推)
max connections = 200	
#最大连接数(同时连接的主机数,减少服务端负载)
timeout = 600			
#超时时间
ignore errors			
#忽略报错
read only = false		
#不只读(可读可写)
list = false						
#不允许别人查看模块名
#################################### 命令相关配置 ###########################
auth users = bck			 
#传输文件的用户(相当于密码,没有实际意义),客户端认证1
secrets file = /etc/rsync.passwd	  
#传输用户文件的密码文件,vim或echo/600,客户端认证2
log file = /var/log/rsyncd.log		  
#日志文件,使用了rsync之后才会生成

[backupmk]								
#模块名,可以随便改,小心大写字母,空格,数字,多模块对应多主机(随便推?),客户端认证3
comment = welcome to oldboyedu backup!	   
#注释(废物)
path = /backup							 
#备份的目录-------mkdir/权限属主属组,,客户端认证4

3.推送m01的rsync配置文件到nfs服务端
[root@m01 ~]# ansible 'nfs_group' -m copy -a 'src=/etc/rsyncd.conf dest=/etc'
重启nfs服务端(restarted有开启服务的功能)
[root@m01 ~]# ansible nfs_group -m systemd -a 'name=rsyncd state=restarted'

4.创建用户www,/backup目录
[root@m01 ~]# ansible all -m group -a 'name=www gid=666 state=present'
[root@m01 ~]# ansible all -m user -a 'name=www uid=666 group=666 create_home=false'

[root@m01 ~]# ansible 'backup_group' -m file -a 'path=/backup state=directory owner=www group=www mode=0755'

5.创建rsync服务端密码文件
[root@m01 ~]# ansible backup_group -m copy -a "content='bck:123\n' dest=/etc/rsync.passwd mode=0600"

6.把客户端密码加入到环境变量
[root@m01 ~]# ansible all -m copy -a "content='export RSYNC_PASSWORD=123\n' dest=/etc/profile.d/rsync_passwd.sh"
[root@m01 ~]# ansible all -m shell -a 'source /etc/profile.d/rsync_passwd.sh'

7.启动,并加入开自启动
[root@m01 ~]# ansible all -m systemd -a 'name=rsyncd state=started enabled=yes'

部署nfs

1.安装
[root@m01 ~]# ansible web_group -m yum -a 'name=nfs-utils state=present'
[root@m01 ~]# ansible nfs_group -m yum -a 'name=nfs-utils'
2.编辑nfs服务端配置文件
[root@m01 ~]# ansible 'nfs_group' -m copy -a 'content="/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)\n" dest=/etc/exports'
3.重载nfs服务端配置文件(选做)
[root@m01 ~]# ansible nfs_group -a 'exportfs -r'

4.创建nfs服务端备份目录
[root@m01 ~]# ansible 'nfs_group' -m file -a 'path=/data state=directory owner=www group=www mode=0755'

5.启动,并加入开自启动
[root@m01 ~]# ansible web_group -m systemd -a 'name=nfs state=started enabled=yes'
[root@m01 ~]# ansible nfs_group -m systemd -a 'name=nfs state=started enabled=yes'
6.挂载
[root@m01 ~]# ansible web_group -m mount -a 'path=/mnt src=172.16.1.31:/data fstype=nfs state=absent'

7.取消挂载(开机挂载容易出现问题)
[root@m01 ~]# ansible web_group -m mount -a 'path=/mnt src=172.16.1.31:/data fstype=nfs state=unmounted'

部署PHP


部署mariadb


开启防火墙

#永久启动防火墙,并设置开机自启动(使用systemd或service模块)
ansible 'web_group' -m systemd -a 'name=firewalld state=started enabled=yes'

#永久开启指定服务(某些服务系统自动开启)
[root@m01 ~]# ansible web_group -m firewalld -a "port=873/tcp  permanent=yes state=enabled"

#重启服务器(忽略红色报错)
[root@m01 ~]# ansible web_group -a 'reboot'

selinux

#修改配置文件关闭selinux(永久关闭),必须重启,才能生效
[root@m01 ~]# ansible web_group -m selinux -a 'state=disabled'

#重启服务器
[root@m01 ~]# ansible web_group -a 'reboot'
posted @ 2020-06-11 16:31  看萝卜在飘  阅读(178)  评论(0编辑  收藏  举报