新建2臺虛擬機作負載均衡load-balance
load-balance-0001 192.168.1.54
load-balance-0002 192.168.1.29html
[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.77web
[balance] 追加這個,負載均衡組的虛擬機
192.168.1.54
192.168.1.29redis
[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' 改負載均衡組的虛擬機的密碼shell
[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 apache
[root@room9pc01 ~]# ssh -i '/root/桌面/abc.pem' 139.159.146.212vim
[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 keepalivedbash
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是精簡一些,過濾頭部信息MyHeaderMyHeader: ecs-web-0003 能夠看到是不一樣的web主機提供的頁面MyHeader: ecs-web-0002MyHeader: ecs-web-0001MyHeader: ecs-web-0004MyHeader: ecs-web-0005