LVSの設定方法2


###in Host

```Bash
vi playbook.yml
```

```playbook.yml
---
- hosts: lvs
  sudo: yes
  tasks:
    - name: install ipvsadm
      yum: name=ipvsadm state=latest
    - name: install keepalived
      yum: name=keepalived state=latest
    - name: start keepalived and enabled
      service: name=keepalived state=started enabled=yes
- hosts: web
  sudo: yes
  tasks:
    - name: install apache
      yum: name=httpd state=latest
    - name: start apace and enabled
      service: name=httpd state=started enabled=yes
```

```Bash
ansible-playbook -i hosts playbook.yml
```

###Set up keepalived in lvs1

```Bash
ssh lvs1
```

```Bash
sudo vi /etc/keepalived/keepalived.conf
```

```/etc/keepalived/keepalived.conf
vrrp_instance  VI_1{
    state MASTER
    interface eth1
    virtual_router_id 61
    priority 101
    advert_int 3
    nopreempt
    lvs_sync_daemon_interface eth1
    unicast_peer {
        192.168.43.62
    }
    virtual_ipaddress {
        192.168.43.200 dev eth1
    }
}
 
virtual_server_group lvs_test {
    192.168.43.200 80
}
 
virtual_server group lvs_test {
    lvs_sched rr
    lvs_method DR
    protocol TCP
    virtualhost helth
    real_server 192.168.43.63 80 {
        weight 1
        inhibit_on_failure
        HTTP_GET {
            connect_port 80
            connect_timeout 5
        }
    }
 
    real_server 192.168.43.64 80 {
        weight 1
        inhibit_on_failure
        HTTP_GET {
            connect_port 80
            connect_timeout 5
        }
    }
}
```


```Bash
sudo service keepalived restart
```

```Bash
sudo ipvsadm -Ln
```

![スクリーンショット 2016-03-05 23.07.39.png](https://qiita-image-store.s3.amazonaws.com/0/39073/1d9b2239-5d45-11ec-fb2b-2b64b481b667.png "スクリーンショット 2016-03-05 23.07.39.png")


```Bash
sudo ip -4 addr
```

![スクリーンショット 2016-03-05 23.10.14.png](https://qiita-image-store.s3.amazonaws.com/0/39073/b191b206-a433-ff22-dedd-d85615d1cf0c.png "スクリーンショット 2016-03-05 23.10.14.png")


```Bash
sudo vi /etc/sysctl.conf
```

```/etc/sysctl.conf
net.ipv4.ip_forward = 1
```

```Bash
sudo sysctl -p
```

###Set up keepalived in lvs2

```Bash
ssh lvs2
```

```Bash
sudo vi /etc/keepalived/keepalived.conf
```

```/etc/keepalived/keepalived.conf
vrrp_instance  VI_1{
    state BACKUP
    interface eth1
    virtual_router_id 62
    priority 100
    advert_int 3
    nopreempt
    lvs_sync_daemon_interface eth1
    unicast_peer {
        192.168.43.61
    }
    virtual_ipaddress {
        192.168.43.200 dev eth1
    }
}
 
virtual_server_group lvs_test {
    192.168.43.200 80
}
 
virtual_server group lvs_test {
    lvs_sched rr
    lvs_method DR
    protocol TCP
    virtualhost helth
    real_server 192.168.43.63 80 {
        weight 1
        inhibit_on_failure
        HTTP_GET {
            connect_port 80
            connect_timeout 5
        }
    }
 
    real_server 192.168.43.64 80 {
        weight 1
        inhibit_on_failure
        HTTP_GET {
            connect_port 80
            connect_timeout 5
        }
    }
}
```

```Bash
sudo service keepalived restart
```

```Bash
sudo ipvsadm -Ln
```

![スクリーンショット 2016-03-05 23.12.04.png](https://qiita-image-store.s3.amazonaws.com/0/39073/e4f520d0-741b-8b4c-34a3-f0c119586237.png "スクリーンショット 2016-03-05 23.12.04.png")

```Bash
sudo ip -4 addr
```

![スクリーンショット 2016-03-05 23.12.32.png](https://qiita-image-store.s3.amazonaws.com/0/39073/9d060ad7-5d12-cc4d-ae25-f8b3ba8227a2.png "スクリーンショット 2016-03-05 23.12.32.png")


```Bash
sudo vi /etc/sysctl.conf
```

```/etc/sysctl.conf
net.ipv4.ip_forward = 1
```

```Bash
sudo sysctl -p
```

###Set Up in web1

```Bash
ssh web1
```

```Bash
sudo vi /etc/sysconfig/network-scripts/ifcfg-lo:0
```

```/etc/sysconfig/network-scripts/ifcfg-lo
DEVICE=lo:0
IPADDR=192.168.43.200
NETMASK=255.255.255.255
NETWORK=192.168.43.200
BROADCAST=192.168.43.200
ONBOOT=yes
NAME=loopback-dsr
```

```Bash
sudo service network restart
```

```Bash
sudo ip -4 addr
```

![スクリーンショット 2016-03-05 23.14.40.png](https://qiita-image-store.s3.amazonaws.com/0/39073/7137023f-9987-8584-0f88-ce61a8e7f422.png "スクリーンショット 2016-03-05 23.14.40.png")


```Bash
sudo vi /etc/sysctl.conf
```

Add parameter

```/etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
```

```Bash
sudo sysctl -p
```

* app_ignoreを有効
* loからARPリクエストを受け取ったとき、loに設定されているアドレスの情報だけを返す
* arp_announceを有効
* loからARPリクエストを送信するとき、ARPリクエストの送信元アドレスとして、loに設定されたアドレスを使うようにする

###Set Up in web2

```Bash
ssh web2
```

```Bash
sudo vi /etc/sysconfig/network-scripts/ifcfg-lo:0
```

```/etc/sysconfig/network-scripts/ifcfg-lo
DEVICE=lo:0
IPADDR=192.168.43.200
NETMASK=255.255.255.255
NETWORK=192.168.43.200
BROADCAST=192.168.43.200
ONBOOT=yes
NAME=loopback-dsr
```

```Bash
sudo service network restart
```

```Bash
sudo ip -4 addr
```

![スクリーンショット 2016-03-06 16.55.49.png](https://qiita-image-store.s3.amazonaws.com/0/39073/77a3e87d-b78c-8b72-8e02-b7deb843f0e0.png "スクリーンショット 2016-03-06 16.55.49.png")

```Bash
sudo vi /etc/sysctl.conf
```

Add parameter

```/etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
```

```Bash
sudo sysctl -p
```