nginx负载均衡一部署(ansible操作),nginxansible
nginx负载均衡一部署(ansible操作),nginxansible
- 环境:
- 一、部署ansible环境
- 1.安装ansible
- 2.配置免秘钥
- 3.配置ansible主机清单
- 二、nginx环境部署
- 1.给两台client安装httpd服务
- 2.关闭所有虚拟机的防火墙
- 3.发送index.html首页到client
- 三、安装nginx
- 1.上传nginx镜像到lb两台虚拟机
- 2.安装依赖
- 3.解压编译安装
- 4.创建目录与软链接
- 5.设置环境变量,启动nginx
环境:
四台机器(lb-01使用ansible管理)
lb-01---10.0.0.10
lb-02---10.0.0.11
clent-01---10.0.0.12
clent-02---10.0.0.13
一、部署ansible环境
1.安装ansible
[root@lb-01 ~]# yum install epel-release -y
[root@lb-01 ~]# yum install ansible -y
2.配置免秘钥
[root@lb-01 ~]# ssh-keygen
[root@lb-01 ~]# for ip in 10 11 12 13;do ssh-copy-id -i /root/.ssh/id_rsa.pub 10.0.0.$ip;done
3.配置ansible主机清单
[root@lb-01 ~]# cat /etc/ansible/hosts
[lb]
10.0.0.10
10.0.0.11
[client]
10.0.0.12
10.0.0.13
[root@lb-01 ~]# ansible all -m ping
10.0.0.11 | SUCCESS => {
"changed": false,
"ping": "pong"
}
10.0.0.12 | SUCCESS => {
"changed": false,
"ping": "pong"
}
10.0.0.13 | SUCCESS => {
"changed": false,
"ping": "pong"
}
10.0.0.10 | SUCCESS => {
"changed": false,
"ping": "pong"
}
二、nginx环境部署
1.给两台client安装httpd服务
[root@lb-01 ~]# ansible client -m yum -a "name=httpd state=present"
[root@lb-01 ~]# ansible client -m service -a "name=httpd state=started enabled=yes"
2.关闭所有虚拟机的防火墙
[root@lb-01 ~]# ansible all -m service -a "name=iptables state=stopped enabled=no"
[root@lb-01 ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux
[root@lb-01 ~]# ansible all -m shell -a "setenforce 0"
[root@lb-01 ~]# ansible all -m copy -a "src=/etc/sysconfig/selinux dest=/etc/sysconfig/selinux"
3.发送index.html首页到client
[root@lb-01 test]# cat httpd.index.html
{{ansible_default_ipv4.address}} ###调用IP,这里取出来的是client两个虚拟机的IP
[root@lb-01 test]# cat http.yml
---
- hosts: client
tasks:
- name: scp index.html
template: src=/test/httpd.index.html dest=/var/www/html/index.html
[root@lb-01 test]# ansible-playbook http.yml
查看效果
[root@lb-01 test]# ansible client -m shell -a "cat /var/www/html/index.html"
10.0.0.12 | SUCCESS | rc=0 >>
10.0.0.12 #/var/www/html/index.html 文件里的内容
10.0.0.13 | SUCCESS | rc=0 >>
10.0.0.13 #/var/www/html/index.html 文件里的内容
[root@lb-01 test]# curl 10.0.0.12
10.0.0.12
[root@lb-01 test]# curl 10.0.0.13
10.0.0.13
三、安装nginx
1.上传nginx镜像到lb两台虚拟机
[root@lb01 ~]# ansible lb -m shell -a "ls -lh /root/nginx-1.6.2.tar.gz"
10.0.0.11 | SUCCESS | rc=0 >>
-rw-r--r-- 1 root root 786K Aug 12 15:17 /root/nginx-1.6.2.tar.gz
10.0.0.10 | SUCCESS | rc=0 >>
-rw-r--r-- 1 root root 786K Mar 24 2015 /root/nginx-1.6.2.tar.gz
2.安装依赖
[root@lb01 test]# ansible lb -m yum -a "name=gcc state=present"
[root@lb01 test]# ansible lb -m yum -a "name=pcre-devel state=present"
[root@lb01 test]# ansible lb -m yum -a "name=openssl-devel state=present"
3.解压编译安装
[root@lb01 test]# cat nginx.yml
---
- hosts: lb###指定模块,这个模块包含了lb两台虚拟机
tasks:
- name: add user ###添加nginx用户
user: name=nginx group=nginx createhome=no shell=/sbin/nologin
- name: tar nginx ###解压nginx
unarchive: src=/root/nginx-1.6.2.tar.gz dest=/root
- name: make install###编译安装
shell: chdir=/root/nginx-1.6.2 ./configure --user=nginx \
--group=nginx --prefix=/usr/local/nginx-1.6.2 \
--with-http_stub_status_module --with-http_ssl_module \
&& make && make install
===>注:ansible-plabook里不能有斜线 需要删掉
[root@lb01 test]# ansible-playbook nginx.yml
4.创建目录与软链接
[root@lb01 ~]# ansible lb -m file -a "path=/application state=directory"
[root@lb01 ~]# ansible lb -m shell -a "ln -s /usr/local/nginx-1.6.2 /application/"
5.设置环境变量,启动nginx
[root@lb01 ~]# ansible lb -m shell -a "cp /application/nginx-1.6.2/sbin/nginx /usr/local/sbin/“
[root@lb01 ~]# ansible lb -m shell -a "nginx"
[root@lb01 ~]# ansible lb -m shell -a "netstat -lntup|grep nginx"
10.0.0.11 | SUCCESS | rc=0 >>
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8570/nginx
10.0.0.10 | SUCCESS | rc=0 >>
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 14554/nginx
评论暂时关闭