LoginSignup
1
1

OCIのFLB(フレキシブルロードバランサ)を使う

Last updated at Posted at 2023-12-09

はじめに

OCIのフレキシブルロードバランサを使ってみる。
ロードバランサの後ろに2台インスタンスがぶら下がっていて、httpでアクセスするとランダムにバックエンドに振り分けられる構成を考える。
curl http://${ロードバランサのIP}/hello.htmlを打つと、this is instance 1.this is instance 2がランダムに返ってくるイメージ。

バックエンドの作成

先にバックエンドのインスタンスを2つ作成しておく。

1つ目のインスタンス(my-instance)を作成する。
・既存のコンパートメント(my-compartment) に作成
・既存のmy-vcnのパブリックサブネットに作成
 ・せっかくロードバランサの後ろにぶら下げるので、インスタンスはプライベートサブネットに作ってもよいのだが、検証用途のため。

image.png
image.png

作成できた。

image.png

ログインする。

PS > ssh -i .\ssh-key-2023-10-09.key opc@xxx.xxx.xxx.xxx -o ServerAliveInterval=60
The authenticity of host 'xxx.xxx.xxx.xxx (xxx.xxx.xxx.xxx)' can't be established.
ED25519 key fingerprint is SHA256:BZfuA9IqggeNYpo8wZ7XFdX0gePAZ27yy0/rKpO8WqE.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'xxx.xxx.xxx.xxx' (ED25519) to the list of known hosts.
Activate the web console with: systemctl enable --now cockpit.socket

[root@my-instance ~]# 

いくつか設定を入れる。

# OCI提供のoracle linux デフォルトイメージでは、firewalldが最初から有効になっている。
# ありがたいことなのだが検証用途では面倒なのでいったんOFFする
[root@my-instance ~]# systemctl stop firewalld

# apacheを入れる。
[root@my-instance~]# yum install httpd

# httpd サービスを起動する
[root@my-instance ~]# systemctl start httpd

# ヘルスチェック用にファイルを一つ置いておく。200になればよいので中身はなんでもよい。
[root@my-instance ~]# echo "ok" > /var/www/html/ok.html

# どのインスタンスかがわかるように、判別用のファイルを置いておく。
[root@my-instance ~]# echo "this is my instance."  > /var/www/html/hello.html

もう一つのインスタンスも同様に作成する。

セキュリティリスト

サブネット内で、LB → インスタンス:80番ポートの通信ができる必要があるため、LB・インスタンスが配置されているパブリックサブネットのセキュリティリストに許可設定を入れる。
・このルールがサブネット内のすべてのVNICに適用される。同じサブネット内だからといってツーカーではない点に注意。
・LB作成時に自動で作成されたような気もするが、念のため。(LB作成時に自動生成するか手動で入れるか選べる)

image.png

ロードバランサの作成

・パブリックロードバランサを指定し、ロードバランサにパブリックIPを持たせる。

image.png

・配置サブネットはインスタンスと同じ、my-vcnのパブリックサブネットに作成。
・拡張オプションは特に変更せず。
image.png

・ロードバランシング・ポリシーは「重みづけラウンドロビン」。特に重みを設定しないのでランダムになる。
・バックエンドサーバに先ほど作成したインスタンス2つを指定。(2つ目がmy-instance3になっているが気にしないでください)
・ヘルスチェックポリシーのURLパスには先ほど作成した/ok.htmlを指定
image.png

・リスナはHTTP。
image.png

・エラーログ・アクセスログともに有効化。
・アクセスログはデフォルトでは有効になっていないが、合わせて有効にしておくと便利。
image.png

・作成できた。
・ヘルスチェックも無事OKになっている。
image.png

うまくいかないときはエラーログを確認すると色々情報が出ている。以下はfirewalldをOFFにし忘れてヘルスチェックが通らなかった際のエラー。
image.png

動作確認

手元からcurlを打ってみると、期待した通り、
リクエストごとにランダムなインスタンスに割り振られていることがわかる。

PS > curl http://xxx.xxx.xxx.xxx/hello.html


StatusCode        : 200
StatusDescription : OK
Content           : this is my instance.

RawContent        : HTTP/1.1 200 OK
                    Connection: close
                    Accept-Ranges: bytes
                    Content-Length: 21
                    Content-Type: text/html; charset=UTF-8
                    Date: Sat, 09 Dec 2023 23:21:23 GMT
                    ETag: "15-60c1b4d3d5839"
                    Last-Modified: Sat,...
Forms             : {}
Headers           : {[Connection, close], [Accept-Ranges, bytes], [Content-Length, 21], [Content-Type, text/html; charset=UTF-8], [Date, Sat, 09 Dec 2023 23:21:23 GMT], [ETag,
                     "15-60c1b4d3d5839"], [Last-Modified, Sat, 09 Dec 2023 22:34:19 GMT]}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 21



PS I> curl http://xxx.xxx.xxx.xxx/hello.html


StatusCode        : 200
StatusDescription : OK
Content           : this is my instance3.

RawContent        : HTTP/1.1 200 OK
                    Connection: keep-alive
                    Accept-Ranges: bytes
                    Content-Length: 22
                    Content-Type: text/html; charset=UTF-8
                    Date: Sat, 09 Dec 2023 23:21:24 GMT
                    ETag: "16-60c1b49915144"
                    Last-Modified:...
Forms             : {}
Headers           : {[Connection, keep-alive], [Accept-Ranges, bytes], [Content-Length, 22], [Content-Type, text/html; charset=UTF-8], [Date, Sat, 09 Dec 2023 23:21:24 GMT], [
                    ETag, "16-60c1b49915144"], [Last-Modified, Sat, 09 Dec 2023 22:33:18 GMT]}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 22

アクセスログにも表示されている。
image.png

1
1
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
1
1