华为云2

新建2台虚拟机做负载均衡load-balance
load-balance-0001 192.168.1.54
load-balance-0002 192.168.1.29


[root@ecs-abc ~]# vim /etc/ansible/hosts
... ...
[web]
192.168.1.123
192.168.1.160
192.168.1.88
192.168.1.130
192.168.1.77

[balance] 追加这个,负载均衡组的虚拟机
192.168.1.54
192.168.1.29

[root@ecs-abc ~]# ansible balance --list-host 查看负载均衡组的虚拟机有哪些
hosts (2):
192.168.1.54
192.168.1.29

[root@ecs-abc ~]# ansible balance -m shell -a 'echo 123456 | passwd --stdin root' 改负载均衡组的虚拟机的密码

[root@ecs-abc ~]# ansible balance -m shell -a 'echo "net.ipv4.ip_forward = 1" >> sysctl.conf' 开启路由转发功能
192.168.1.54 | CHANGED | rc=0 >>
192.168.1.29 | CHANGED | rc=0 >>
#################################################################################
公网IP,139.159.146.212,绑定跳板机虚拟机abc
公网IP,139.9.60.12,绑定VIP,192.168.1.200

[root@room9pc01 ~]# ssh -i '/root/桌面/abc.pem' 139.159.146.212

[root@ecs-abc ~]# ansible balance -m yum -a 'name=haproxy state=installed' 给负载均衡组的虚拟机安装haproxy

[root@ecs-abc ~]# ssh 192.168.1.54
[root@load-banlance-0001 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
router_id balance1
}

vrrp_instance VI_1 {
state BACKUP
interface eth0
track_interface {
eth0
}
virtual_router_id 51
priority 100
! nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.1.200/24 brd 192.168.1.255 dev eth0 label eth0:1
}
}

[root@load-banlance-0001 ~]# scp /etc/keepalived/keepalived.conf 192.168.1.29:/etc/keepalived/keepalived.conf
root@192.168.1.29's password:
keepalived.conf 100% 419 1.0MB/s 00:00

[root@load-banlance-0001 ~]# exit
[root@ecs-abc ~]# ssh 192.168.1.29
#################################################################################

[root@load-banlance-0002 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
router_id balance1
}

vrrp_instance VI_1 {
state BACKUP
interface eth0
track_interface {
eth0
}
virtual_router_id 51
priority 200
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.1.200/24 brd 192.168.1.255 dev eth0 label eth0:1
}
}


[root@load-banlance-0002 ~]# exit
[root@ecs-abc ~]# ansible balance -m service -a 'name=keepalived state=restarted'

[root@ecs-abc ~]# ssh 192.168.1.54
[root@load-banlance-0001 ~]# vim /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2

chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon

# turn on stats unix socket
stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
#option httplog 注释这行
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#--------------------------------------------------------------------- 删掉这行之后的所有原内容,手打下面内容
listen my_web *:80
mode http
balance roundrobin
server web1 192.168.1.160:80 check
server web2 192.168.1.77:80 check
server web3 192.168.1.88:80 check
server web4 192.168.1.123:80 check
server web5 192.168.1.130:80 check

listen my_ssh *:10022 当连接本机的10022端口时
mode tcp
option tcpka #使用长连接
balance leastconn #最小连接调度算法
server myssh_01 192.168.1.252:22 check inter 3000 rise 1 maxconn 1000 fall 3 转发到后端192.168.1.252的22端口

 

[root@load-banlance-0001 ~]# systemctl restart haproxy
[root@load-banlance-0001 ~]# systemctl status haproxy
... ...
Active: active (running) since 一 2019-01-07 15:49:56 CST; 6s ago
... ...

 


[root@load-banlance-0001 ~]# exit
[root@ecs-abc ~]# ssh 192.168.1.54 -p 10022 连接192.168.1.54的10022端口
[root@ecs-abc ~]# 连接上来后,发现成功的转发连接到了虚拟机abc,192.168.1.252的22端口

[root@ecs-abc ~]# exit

[root@ecs-abc ~]# ssh 192.168.1.54
[root@load-banlance-0001 ~]# scp /etc/haproxy/haproxy.cfg 192.168.1.29:/etc/haproxy/haproxy.cfg 传配置文件给另一个负载均衡虚拟机
[root@load-banlance-0001 ~]# exit

[root@ecs-abc ~]# ansible balance --list-host
hosts (2):
192.168.1.54
192.168.1.29

[root@ecs-abc ~]# ansible balance -m service -a 'name=haproxy state=restarted'


[root@ecs-abc ~]# ssh 192.168.1.29 -p 10022 连接192.168.1.29的10022端口
[root@ecs-abc ~]# 连接上来后,发现成功的转发连接到了虚拟机abc,192.168.1.252的22端口

[root@ecs-abc ~]# exit
[root@ecs-abc ~]# exit


[root@room9pc01 ~]# ssh -i '/root/桌面/abc.pem' 139.9.60.12 -p 10022 连接139.9.60.12,当前正在关联的vip负载均衡组的虚拟机的10022端口
[root@ecs-abc ~]# 连接上来后,发现成功的转发连接到了虚拟机abc,192.168.1.252的22端口

[root@ecs-abc ~]# ssh 192.168.1.190 连接nfs虚拟机192.168.1.190
[root@ecs-nfs ~]# yum -y install nfs-utils 安装nfs
[root@ecs-nfs ~]# mkdir /var/webroot
[root@ecs-nfs ~]# echo hello > /var/webroot/index.html

[root@ecs-nfs ~]# vim /etc/exports
/var/webroot 192.168.0.0/16(rw,no_root_squash)

[root@ecs-nfs ~]# man exports 如果不记得就man帮助,看example怎么写
no_root_squash 不压榨root权限

[root@ecs-nfs ~]# systemctl restart rpcbind
[root@ecs-nfs ~]# systemctl restart nfs

[root@ecs-nfs ~]# exportfs -rv
exporting 192.168.0.0/16:/var/webroot

[root@ecs-nfs ~]# exit
[root@ecs-abc ~]# ansible web -m yum -a 'name=nfs-utils state=installed' 给web组的虚拟机安装nfs
[root@ecs-abc ~]# ansible web -m service -a 'name=nfs state=restarted' 给web组的虚拟机启动nfs

[root@ecs-abc ~]# ssh 192.168.1.160 连接到其中一台web虚拟机
[root@ecs-web-0001 ~]# cat /etc/passwd 查看apache的uid和gid
... ...
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin 查看到apache的uid和gid,是48


[root@ecs-web-0001 ~]# exit
[root@ecs-abc ~]# ssh 192.168.1.190 连接到nfs服务器
[root@ecs-nfs ~]# cd /var/
[root@ecs-nfs var]# chown 48.48 /var/webroot/ 修改网页根目录权限,哪怕这台主机没有apache这个用户,但是能暴力修改所有者/组为id是48
[root@ecs-nfs var]# ll /var/
... ...
drwxr-xr-x 2 48 48 4096 1月 7 16:15 webroot 这样看才能看到这个目录的所有者/组变成了uid为48的用户

[root@ecs-nfs ~]# exit
[root@ecs-abc ~]# ssh 192.168.1.160 进入任意一台web主机
[root@ecs-web-0001 ~]# showmount -e 192.168.1.190 查看到nfs主机共享的目录
Export list for 192.168.1.190:
/var/webroot 192.168.0.0/16

[root@ecs-web-0001 ~]# mount -t nfs 192.168.1.190:/var/webroot /var/www/html/ 挂载
[root@ecs-web-0001 ~]# sudo -u apache -s
bash-4.2$ id apache
uid=48(apache) gid=48(apache) 组=48(apache)
bash-4.2$ cd /var/www/html/
bash-4.2$ ls
index.html
bash-4.2$ mkdir abc 试试看有没有写的权限
bash-4.2$ ls
abc index.html
bash-4.2$ rmdir abc 试试看有没有删除的权限
bash-4.2$ ls
index.html
bash-4.2$ exit
exit

[root@ecs-web-0001 ~]# umount /var/www/html/ 卸载
[root@ecs-web-0001 ~]# exit
[root@ecs-abc ~]#

[root@ecs-abc ~]# ansible web -m shell -a 'mount -t nfs 192.168.1.190:/var/webroot /var/www/html' 统一给web组挂载nfs

[root@ecs-abc ~]# ansible web -m service -a 'name=httpd state=restarted enabled=yes' 重启httpd服务

[root@ecs-abc ~]# ansible web -m shell -a 'ss -tunlp | grep :80' 查看有端口80了

[root@ecs-abc ~]# scp 192.168.1.160:/etc/httpd/conf/httpd.conf ./ 复制要一台web主机的httpd配置文件到本地当前目录
[root@ecs-abc ~]# ls
httpd.conf sysctl.conf


[root@ecs-abc ~]# vim httpd.conf 修改配置文件
... ...
ServerName localhost 取消95行的注释,并改为这个样子
... ...
Header add MyHeader "{{ansible_hostname}}" 追加最后一行,在头部信息显示当前提供页面的主机


[root@ecs-abc ~]# vim httpd.yml 写一个yml文件
---
- hosts: web
remote_user: root
tasks:
- template:
src: httpd.conf 把当前目录的httpd.conf覆盖传送给web组的主机的/etc/httpd/conf/httpd.conf
dest: /etc/httpd/conf/httpd.conf
owner: root
group: root
mode: 0644
- service:
name: httpd
state: restarted
enabled: yes


[root@ecs-abc ~]# ansible-playbook httpd.yml 执行这个yml脚本,把当前目录的httpd.conf传送给web组的主机


[root@ecs-abc ~]# exit
[root@room9pc01 ~]# curl -i 139.9.60.12
... ...
MyHeader: ecs-web-0003 这行显示头部信息,这是web3提供的页面
... ...
hello 页面内容是hello


[root@room9pc01 ~]# for i in {1..5} ;do curl -si 139.9.60.12 ;done | grep MyHeader 选项-s是精简一些,过滤头部信息MyHeader
MyHeader: ecs-web-0003 可以看到是不同的web主机提供的页面
MyHeader: ecs-web-0002
MyHeader: ecs-web-0001
MyHeader: ecs-web-0004
MyHeader: ecs-web-0005

 

posted @ 2019-04-30 23:01  安于夏  阅读(193)  评论(0编辑  收藏  举报