使用ansible-playbook批量部署Nginx-web 集群服务

使用ansible-playbook批量部署Nginx-web 集群服务

所有操作在 jumpserver上进行

1. 先上拓扑图

使用ansible-playbook批量部署Nginx-web 集群服务_第1张图片

2. 配置jumpserver

配置 基本环境

mkdir -p /etc/yum.repos.d/repo_bak/
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/repo_bak/
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.myhuaweicloud.com/repo/CentOS-Base-7.repo
yum repolist
yum -y install bash-completion lrzsz

配置免密登录其他主机

ssh-keygen -f /root/.ssh/id_rsa  -N ''
ssh-copy-id root@主机ip

配置DNS服务 (内网中同步/etc/hosts 文件,可以不需要)

暂不配置,使用/etc/hosts文件

配置vsftp服务和提供yum源 , 把local.repo同步到其他主机

yum -y install vsftpd
yum -y install createrepo
mkdir -p /var/ftp/localrepo
cd /var/ftp/localrepo
createrepo .

systemctl restart vsftpd
systemctl enable vsftpd
vim /etc/yum.repos.d/local.repo
[local_repo]
name=local - Base
baseurl="ftp://192.168.1.100/localrepo/"
enabled=1
gpgcheck=0

配置ansible服务

yum -y install  ansible

3. 制做Nginx的RPM包

安装RPM-build软件包

yum -y install  rpm-build

生成rpmbuild目录结构

rpmbuild -ba nginx.spec

把家目录下的Nginx源码包(需要提前准备nginx源码包)复制到SOURCES下

cp ~/nginx-1.12.2.tar.gz   ~/rpmbuild/SOURCES/

家目录下创建nginx.service ,方便制作的RPM包能用系统管理相应服务

vim  ~/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target

创建并修改SPEC配置文件

vim ~/rpmbuild/SPECS/nginx.spec
Name:nginx
Version:1.12.2
Release:10
Summary:nginx web server software

Group:nginx
License:GPL
URL:www.test.com
Source0:nginx-1.12.2.tar.gz

#BuildRequires:
#Requires:      

%description
nginx [engine x] is an HTTP and reverse proxy server.

%post
useradd -s /sbin/nologin nginx

%prep
%setup -q

%build
./configure --user=nginx --group=nginx
make %{?_smp_mflags}

%install
make install DESTDIR=%{buildroot}
mkdir -p  %{buildroot}/lib/systemd/system
cp /root/nginx.service %{buildroot}/lib/systemd/system/

%files
%doc
/usr/local/nginx/*
/lib/systemd/system/nginx.service

%changelog

安装依赖包

yum -y install gcc pcre-devel openssl-devel

生成RPM包

rpmbuild -ba  ~/rpmbuild/SPECS/nginx.spec

查看生成的RPM包

ls ~/rpmbuild/RPMS/x86_64/

把RPM包发送到 jumpserver 的RPM仓库 (修改 jumpserver 的对应 IP )

cp   ~/rpmbuild/RPMS/x86_64/nginx-1.12.2-10.x86_64.rpm   /var/ftp/localrepo/

更新RPM包仓库信息

cd   /var/ftp/localrepo/
createrepo --update   .

4. Nginx_web集群

修改本地/etc/hosts 并同步到其他elasticsearch主机

vim /etc/hosts
192.168.1.100   ecs-jump-server
192.168.1.51    ecs-web-0001
192.168.1.52    ecs-web-0002
192.168.1.53    ecs-web-0003
192.168.1.54    ecs-web-0004
192.168.1.55    ecs-web-0005
192.168.1.56    ecs-web-0006
192.168.1.57    ecs-web-0007
192.168.1.58    ecs-web-0008
for i in    ecs-web-000{1..8}
do
    scp   /etc/hosts   ${i}:/etc/
done

准备nginx配置文件 nginx.conf
请自行准备

准备web的测试页面文件
index.html文件,自行准备

准备ansible运行环境

vim ansible.cfg
[defaults]
inventory      = hosts
host_key_checking = False
vim hosts
[web]
ecs-web-0001
ecs-web-0002
ecs-web-0003
ecs-web-0004
ecs-web-0005
ecs-web-0006
ecs-web-0007
ecs-web-0008

#[all:vars]
#ansible_ssh_private_key_file='/root/.ssh/key'

创建 nasible-playbook 文件

vim nginx_setup.yaml
---
- hosts: web
  remote_user: root
  tasks:
    - name: install Nginx
      yum:
        name: "{{item}}"
        state: installed
      with_items:
        - nginx

    - copy:
        src: index.html
        dest: /usr/local/nginx/html/index.html
        owner: root
        group: root
        mode: 0644

    - template:
        src: nginx.conf
        dest: /usr/local/nginx/conf/nginx.conf
        owner: root
        group: root
        mode: 0644
      notify: reload nginx
      tags: nginxconf

    - service:
        name: nginx
        enabled: yes
  handlers:
    - name: reload nginx
      service:
        name: nginx
        state: restarted

运用ansible-playbook 部署nginx web服务

ansible-playbook   nginx_setup.yaml

你可能感兴趣的:(shell,ansible)