环境准备
pubserver:eth0:192.168.88.240,eth1:192.168.99.240
client1:eth0:192.168.88.10
lvs1: eth0 : 192.168.88.5;eth1:192.168.99.5
web1:eth1:192.168.99.100
web2:eth1:192.168.99.200
1.虚拟机已关闭selinux和防火墙 ,在pubserver上准备管理环境(已做免密)
#创建主配置文件
[root@pubserver cluster]# vim ansible.cfg
[defaults]
inventory = inventory
host_key_checking = false
2.创建主机清单文件及相关变量
[root@pubserver cluster]# vim inventory
[clients]
client1 ansible_host=192.168.88.10
[webservers]
web1 ansible_host=192.168.99.100
web2 ansible_host=192.168.99.200
[lb]
lvs1 ansible_host=192.168.88.5
3.1编写yum配置文件
[root@pubserver cluster]vim files/local88.repo
[BaseOS]
name = BaseOS
baseurl = ftp://192.168.88.240/dvd/BaseOS
gpgcheck = 0
[AppStream] name = AppStream
baseurl = ftp://192.168.88.240/dvd/AppStream
gpgcheck = 0
[rpms] #自定义仓库
name = rpms
baseurl = ftp://192.168.88.240/rpms
gpgcheck = 0
3.2编写用于上传yum配置文件的playbook
[root@pubserver cluster] vim upload-repo.yml
---
- name: config repos.d
hosts: all
tasks:
- name: delete repos.d # 删除repos.d目录(脚本创建虚拟机自带一个)
file:
path: /etc/yum.repos.d
state: absent
- name: create repos.d # 创建repos.d目录
file:
path: /etc/yum.repos.d
state: directory mode: '0755'
- name: config local88 # 上传repo文件到88网段
hosts: clients,lb
tasks:
- name: upload local88
copy:
src: files/local88.repo
dest: /etc/yum.repos.d/
#99网段同理
4.配置2台web服务器
#创建首页文件,文件中包含ansible facts变量
[root@pubserver cluster] vim files/index.html
Welcome from {{ansible_hostname}}
#配置web服务器
[root@pubserver cluster] vim webservers.yml
---
- name: config webservers
hosts: webservers
tasks:
- name: install nginx # 安装nginx
yum:
name: nginx
state: present
- name: upload index # 上传首页文件到web服务器
template:
src: files/index.html
dest: /usr/share/nginx/html/index.html
- name: start nginx # 启动服务
service:
name: nginx
state: started
enabled: yes
[root@pubserver cluster] ansible-playbook webservers.yml
#在lvs1上测试到web服务器的访问
[root@lvs1 ~] curl http://192.168.99.100
Welcome from web1
[root@lvs1 ~] curl http://192.168.99.200
Welcome from web2
5.确保lvs1的ip转发功能已经打开(该功能需要改变内核参数),安装LVS
[root@lvs1 ~] sysctl -a | grep ip_forward # 查看ip_foward参数
net.ipv4.ip_forward = 1 # 1表示打开转发,0表示关闭转发
#安装LVS
[root@pubserver cluster] vim lvs.yml
---
- name: install lvs
hosts: lb
tasks:
- name: install lvs # 安装lvs
yum:
name: ipvsadm
state: present
[root@pubserver cluster]# ansible-playbook lvs.yml
6.配置LVS
#为web服务器创建虚拟服务器,使用rr调度算法
[root@lvs1 ~] ipvsadm -A -t 192.168.88.5:80 -s rr
#查看配置
[root@lvs1 ~] ipvsadm -Ln
#向虚拟服务器中添加RIP
[root@lvs1 ~] ipvsadm -a -t 192.168.88.5:80 -r 192.168.99.100 -w 1 -m
[root@lvs1 ~] ipvsadm -a -t 192.168.88.5:80 -r 192.168.99.200 -w 2 -m
#查看配置
[root@lvs1 ~] ipvsadm -Ln
7.验证
[root@client1 ~] for i in {1..6}
for i in {1..6}
> do
> curl http://192.168.88.5
> done
Welcome from web2
Welcome from web1
Welcome from web2
Welcome from web1
Welcome from web2
Welcome from web1
#在NAT模式下,客户端的请求首先被发送到虚拟服务器(VIP)然后IPVS修改请求的目的地址为某个真实服务器的地址,并将请求转发给该服务器。
响应则经过反向NAT过程,从真实服务器返回到客户端。