0
0

More than 1 year has passed since last update.

AWS ELB tutorial - Application Load Balancerの構築【簡易版】

Last updated at Posted at 2021-12-29

ELB (Elastic Load Balancer) tutorial

今回の構成

  • ELBを2代のEC2に振り分ける用に設定
  • EC2にはindex.htmlを配置して、アクセスしたらそのHTMLが見れるようにする
  • VPCはデフォルトVPCを使用
  • SecurityGroupを作成する

VPCを作成

CIDR: 192.168.0.0/16で作成

スクリーンショット 2021-12-30 1.14.41.png

インターネットゲートウェイを作成
IGをVPCにアタッチしないとVPCが外部からのアクセスを許可しないので、EC2インスタンスに接続できない。

スクリーンショット 2021-12-30 3.16.52.png

VPCに設定されているルートテーブルでEC2を配置しているサブネットを関連付けする。 ルートにigw(インターネットゲートウェイがアタッチされていないとネットと接続できないので、なければ設定する。)

スクリーンショット 2021-12-30 3.21.42.png

subnet作成

以下の設定で作成

  • elb-tutorial-subnet1
    • AZ: ap-northeast-1a
    • cidr: 192.168.0.0/24
  • elb-tutorial-subnet2
    • AZ: ap-northeast-1c
    • cidr: 192.168.1.0/24

作成後:
スクリーンショット 2021-12-30 1.46.39.png

EC2 instanceの作成

セキュリティグループは新しく作成する。

Name: elb-tutorail-sg
説明: security group for elb tutorial

スクリーンショット 2021-12-30 1.57.43.png

キーペアの作成:

スクリーンショット 2021-12-30 1.58.34.png

EC2 instanceの設定

  • Server1:
    • AMI:
      • Amazon Linux 2 AMI (HVM) - Kernel 5.10, SSD Volume Type
    • インスタンスタイプ:
      • t2.micro
    • サブネット:
      • ap-northeast-1a
    • ストレージの追加:
      • デフォルト
    • タグ:
      • key: Name
      • value: elb-tutorial-server1
    • パブリックIPの自動割り当て
      • 有効
  • Server2:
    • AMI:
      • Amazon Linux 2 AMI (HVM) - Kernel 5.10, SSD Volume Type
    • インスタンスタイプ:
      • t2.micro
    • サブネット:
      • ap-northeast-1c
    • ストレージの追加:
      • デフォルト
    • タグ:
      • key: Name
      • value: elb-tutorial-server2
    • パブリックIPの自動割り当て
      • 有効

スクリーンショット 2021-12-30 2.00.47.png

下記のスクリプトを高度な詳細のユーザーデータ欄に記入。Server1とServer2で内容を少しかえる。

スクリーンショット 2021-12-30 1.53.09.png

なぜか、下記スクリプトが実行されていなかったので後々ssh接続して直接EC2内でコマンドを叩いた。 以前はできたので、設定の書き方がおかしかったのかもしれない。

ユーザーデータ Server1:

#!/bin/bash  

sudo su
yum update -y
yum install httpd -y

echo "<html><h1 color="red"> Hello World! This is Server 1 </h1><html>" > /var/www/html/index.html

systemctl start httpd
systemctl enable httpd

ユーザーデータ Server2:

#!/bin/bash  

sudo su
yum update -y
yum install httpd -y

echo "<html><h1 color="blue"> Hello World! This is Server 2 </h1><html>" > /var/www/html/index.html

systemctl start httpd
systemctl enable httpd

作成後
ステータスチェックのところが「2/2 のチェックに合格しました」になればOK

スクリーンショット 2021-12-30 2.01.11.png

インターネットゲートウェイの作成

name: elb-tutorail-ig
と言う名前に設定して,VPCにアタッチする。

※ インターネットゲートウェイがアタッチされたVPCじゃないと、ロードバランサーに設定できない。

ターゲットグループの作成

  • Basic configuration
    • instances
  • Target group name
    • elb-tutorail-tg
  • vpc
    • 作成したvpc

その他はデフォルト

Register targetsで2つのインスタンスを設定

スクリーンショット 2021-12-30 2.04.21.png

elb-tutorila-tgのHealthyステータスが2つになったらOK

ロードバランサー作成

  • load balancer type
    • Application Load Balancer
  • load balancer name
    • elb-tutorail-lb
  • scheme
    • Internet-facing
  • Listeners and routeing
    • Default action
      • elb-tutorial-tgのターゲットグループを選ぶ
  • VPC
    • elb-tutorail-vpc
  • Mappings
    • ap-northeast-1a
    • ap-northeast-1c
  • Security Group
    • elb-tutorail-sg
    • (defaultは削除)

作成後、DNS名をブラウザで開くと。server1 or server2が表示される。
リロードするごとにserver1とserver2が交互に入れ替わりバランシングされていることが確認できる。
試しにEC2を片方停止してみると、どちらかのサーバーのみが表示されるようになる。

スクリーンショット 2021-12-30 2.13.10.png

server1

スクリーンショット 2021-12-30 3.23.05.png

server2

スクリーンショット 2021-12-30 3.23.24.png

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