10
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【CoreOS】fleet + docker + keepalived(VRRP+VIPのみ)で簡単LB

Posted at

Docker + keepalivedで簡単ロードバランサ

今回はfleet + docker + keepalivedで簡単ロードバランサを構築したいと思います。

まず、docker hubに上記構成を目指すimageがあるか探してきます。

lesaux/docker-keepalived

ロードバランサの組み合わせは以前構築したCoreOSの環境下で以下の構成で動かしたいと思います。

スライド3.jpg

※わかりやすい図が書けない・・・・

systemd用サービスファイルの作成

※ファイル名はkeepalived@1.servicekeepalived@2.service と2つ作成してください

keepalived@1.service
[Unit]
Description=KeepALived
After=docker.service
Requires=docker.service

[Service]
TimeoutStartSec=30min
RestartSec=30
Restart=always
ExecStartPre=/usr/bin/docker pull lesaux/keepalived
ExecStartPre=-/usr/bin/docker kill %p-%i
ExecStartPre=-/usr/bin/docker rm %p-%i
ExecStart=/usr/bin/docker run --rm \
  --name %p-%i \
  --net=host \
  -v /mnt/keepalived/keepalived.conf:/etc/keepalived/keepalived.conf \
  --privileged=true \
  -e affinity:container==%p-%i \
  -e VIP=192.168.0.50 \
  lesaux/keepalived
ExecStop=-/usr/bin/docker stop -t 20 %p-%i

[X-Fleet]
MachineID=5b1639bd3cc347cf8fac0b9f597369e3

※各マシンIDは以下のコマンドで取得可能です*

$ export FLEETCTL_SSH_USERNAME=core
$ export FLEETCTL_TUNNEL=192.168.0.10
$ fleetctl list-machines --full
MACHINE					IP		METADATA
4fb30e282c004f1794df9e91e56b14fb	192.168.0.31	cabinet=two,role=workers
5b1639bd3cc347cf8fac0b9f597369e3	192.168.0.10	cabinet=one,role=services
703c849bcb924af5891ca5aae95e4e89	192.168.0.21	cabinet=two,role=workers
a6776a02935e4f01857364587836e338	192.168.0.20	cabinet=one,role=services
fffe34c5dcfa4aabbb1ba684101e521e	192.168.0.30	cabinet=one,role=services

coreos-01とcoreos-03のサーバーに下記、keepalived.confを設定

$ ssh core@192.168.0.10
$ sudo mkdir -p /mnt/keepalived
$ sudo vim /mnt/keepalived/keepalived.conf
! Configuration File for keepalived

vrrp_instance VI_1 {
    state BACKUP
    interface enp0s3
    virtual_router_id 1
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.0.50/24 dev enp0s3
    }
    unicast_peer {
        192.168.0.10/24 dev enp0s3
        192.168.0.11/24 dev enp0s3
    }
}

$ ssh core@192.168.0.11
$ sudo mkdir -p /mnt/keepalived
$ sudo vim /mnt/keepalived/keepalived.conf
! Configuration File for keepalived

vrrp_instance VI_1 {
    state BACKUP
    interface enp0s3
    virtual_router_id 1
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.0.50/24 dev enp0s3
    }
    unicast_peer {
        192.168.0.10/24 dev enp0s3
        192.168.0.11/24 dev enp0s3
    }
}

KeepalivedのDockerを登録

$ fleetctl submit keepalived@{1,2} #systemdへのファイル送信
$ fleetctl load keepalived@{1,2} #systemd自動起動の設定
$ fleetctl start keepalived@{1,2} #systemd起動
$ fleetctl list-units --full #cluster/worker内のsystemd状況確認
UNIT			MACHINE						ACTIVE	SUB
keepalived@1.service	5b1639bd3cc347cf8fac0b9f597369e3/192.168.0.10	active	running
keepalived@2.service	6d28316711484f039eca4408627fdb0c/192.168.0.11	active	running

VIPがcoreos-01についているか

#coreos-01にアクセス
$ ssh core@192.168.0.10
$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:79:c4:9d brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.10/24 brd 192.168.0.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet 192.168.0.50/32 scope global enp0s3
       valid_lft forever preferred_lft forever

#coreos-04にアクセス
$ ssh core@192.168.0.11
$ ip a
ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:ad:bd:0f brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.11/24 brd 192.168.0.255 scope global enp0s3

#ローカルからping 192.168.0.50
ping 192.168.0.50
PING 192.168.0.50 (192.168.0.50): 56 data bytes
64 bytes from 192.168.0.50: icmp_seq=0 ttl=64 time=0.353 ms
64 bytes from 192.168.0.50: icmp_seq=1 ttl=64 time=0.440 ms
64 bytes from 192.168.0.50: icmp_seq=2 ttl=64 time=0.538 ms

coreos-01にVIP 192.168.0.50がついていてpingが帰ってくることも確認できました。

つづいて coreos-01をダウンさせてVIPがのフェイルオーバーチェック

※ついでにping がどの程度欠けるか見てみました。*

$ ssh core@192.168.0.10
$ sudo shutdown -h now
$ ssh core@192.168.0.11
$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:ad:bd:0f brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.11/24 brd 192.168.0.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet 192.168.0.50/24 scope global secondary enp0s3

バッチリ移動しています。

pingについてはフェイルオーバーまでに5%のping パケットロスが発生しました。
※この件についてはkeepalived.confでさらに向上できそうです。

次はVIPを持ったサーバーがNginxのNAT LBとして動くまでの設定をしてみたいと思います。

参考

Docker-keepalived
systemd.unit

前回記事ご紹介:

【CoreOS】cloud-config解説〜インストール
Mac + Virtualbox + CoreOS + etcd2 + fleet の基本設定(1)
Mac + Virtualbox + CoreOS + etcd2 + fleet の基本設定(2)
Mac + Virtualbox + CoreOS + etcd2 + fleet の基本設定(完)

10
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?