1
0

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 1 year has passed since last update.

TerraformでAlibaba Cloud上でSLB + WordPress ECS Servers + Multi-AZ Redis Cache + PolarDBの環境を構築してみた

Last updated at Posted at 2022-08-23

ゴール

以下のgithub repoをベースとして、TerraformでAlibaba Cloud上でSLB + WordPress ECS Servers + Multi-AZ Redis Cache + PolarDBの環境を構築してみた。
元の記事では一番簡単なall-in-one(mysqlもECS上で自前で構築する)から徐々に進化にして、最後にredisもPolarDBも利用するという複数のステップがありましたが、本記事は一番最後のフェーズのみ解説していきます。

  • outputの構成図:
    image.png

手順

  • ソースコードをローカルに持ってくる
git clone git@github.com:outan/alibabacloud_terraform.git
cd wordpress_slb_redis_polardb

  • terraform.tfvars.sampleをもとに変数を設定
    • terraform.tfvars.sampleの中身:

      access_key = ""
      secret_key = ""
      region = "ap-northeast-1"
      zone_1 = "ap-northeast-1a"
      zone_2 = "ap-northeast-1b"
      project_name = "wordpress_slb_redis_polardb"
      ecs_password = "N1cetest"
      redis_account_name = "wordpress"
      redis_account_password="N1cetest"
      polardb_db_name="wordpress"
      polardb_account_name="wordpress"
      polardb_account_password="N1cetest"
      
    • terraform.tfvars.sampleファイルをcopyし、terraform.tfvarsというファイルを作成

      cp terraform.tfvars.sample terraform.tfvars
      
    • アリババクラウドで生成されていたRAM userのaccess_keysecret_keyterraform.tfvarsファイルに記載してください。

    • redisやpolardbやECSの認証情報はデフォルトの値が入っていますが、必要に応じて変更してください。

  • terraformを実行してみる
    terraform init
    terraform plan
    terraform apply
    
  • 問題なく実行できたら、以下outputsが表示される。
    image.png

中身の説明

$ tree
├── main.tf
├── terraform.tfvars
├── terraform.tfvars.sample
└── variables.tf

VPC

resource "alicloud_vpc" "vpc" {
  vpc_name   = "${var.project_name}-vpc"
  cidr_block = "192.168.0.0/16"
}

resource "alicloud_vswitch" "vswitch_1" {
  vpc_id       = alicloud_vpc.vpc.id
  cidr_block   = "192.168.0.0/24"
  zone_id      = var.zone_1
  vswitch_name = "vsw_on_zone_1"
}

resource "alicloud_vswitch" "vswitch_2" {
  vpc_id       = alicloud_vpc.vpc.id
  cidr_block   = "192.168.1.0/24"
  zone_id      = var.zone_2
  vswitch_name = "vsw_on_zone_2"
}
  • 生成されていたVPC

    • alicloud_vpcのresourceに指定されていたcidr_blockになる
      image.png
      image.png
  • 生成されていたvSwitch(subnet)

    • alicloud_vswitchのresourceに指定されていたcidr_blockになる
      image.png

redis

resource "alicloud_kvstore_instance" "redis" {
  db_instance_name  = "wordpress"
  vswitch_id        = alicloud_vswitch.vswitch_1.id
  security_group_id = alicloud_security_group.group.id
  security_ips      = ["192.168.0.0/16"]
  instance_type     = "Redis"
  engine_version    = "5.0"
  config = {
    appendonly             = "yes",
    lazyfree-lazy-eviction = "yes",
  }
  tags = {
    Created = "TF",
    For     = "Test",
  }
  zone_id        = var.redis_multizone
  instance_class = "redis.master.small.default"
}

resource "alicloud_kvstore_account" "redis_account" {
  account_name     = var.redis_account_name
  account_password = var.redis_account_password
  instance_id      = alicloud_kvstore_instance.redis.id
}

image.png

  • 生成されていたredis:
    • Tokyo MZone1 A+Bという2つのzoneにまたがって稼働していることが分かる。
      image.png
    • Service Availabilityで masterとslaveとれぞれZone AとZone Bで稼働していることが分かる
      image.png
    • 生成されていたredis account:
      image.png
    • もうひとつinstance nameをAccount nameとして自動的に作られていたaccountが存在しています。

FAQ
Q: Why does an account already exist after an instance is created?

A: A default account whose name is the instance ID is automatically created after an instance is created to ensure data security. The password of this account has been specified when you create the instance. The password can be reset if you forget it.

→つまり、terraformで明示的にredisのアカウントを作らなくても、デフォルトで一つのアカウントが自動的に作られるようですが、パスワード不明なので、このアカウントを利用しないようにします。

  • instance_classについて

It can be retrieved by data source alicloud_kvstore_instance_classes or referring to help-docs Instance type table.

Instance type table
Community版とEnterprise版二種類がありまして、今回はCommunity版中の Master-replica standard instancesredis.master.small.defaultを利用します。
image.png
結果として 1 GB Master-replicaのredis instanceが作られました。
image.png

  • Whitelist:
    image.png
    security_ips = ["192.168.0.0/16"]で指定されている情報がwhitelistに反映されている。

PolarDB MySQL

resource "alicloud_polardb_cluster" "cluster" {
  db_type       = "MySQL"
  db_version    = "5.7"
  db_node_class = "polar.mysql.x4.medium"
  pay_type      = "PostPaid"
  security_ips  = ["192.168.0.0/16"]
  vswitch_id    = alicloud_vswitch.vswitch_2.id
  description   = "wpdb"
}

resource "alicloud_polardb_account" "account" {
  db_cluster_id       = alicloud_polardb_cluster.cluster.id
  account_name        = var.polardb_account_name
  account_password    = var.polardb_account_password
  account_description = "wpdb"
}

resource "alicloud_polardb_database" "default" {
  db_cluster_id = alicloud_polardb_cluster.cluster.id
  db_name       = var.polardb_db_name
}

resource "alicloud_polardb_account_privilege" "privilege" {
  db_cluster_id     = alicloud_polardb_cluster.cluster.id
  account_name      = alicloud_polardb_account.account.account_name
  account_privilege = "ReadWrite"
  db_names          = [alicloud_polardb_database.default.db_name]
}

image.png

  • PolarDB cluster nodesの数(デフォルト値= 2)
    image.png

  • whitelist:

    • security_ips = ["192.168.0.0/16"]がwhitelistのIP Listに反映されている
      image.png
  • database:
    image.png

  • polardb account:
    image.png

ECS

resource "alicloud_instance" "instance" {
  security_groups = alicloud_security_group.group.*.id

  # series III
  instance_type           = "ecs.sn1.medium"
  system_disk_category    = "cloud_ssd"
  system_disk_name        = "wp_system_disk_name"
  system_disk_size        = 40
  system_disk_description = "wp_system_disk_description"
  image_id                = "m-6we5ts4nx5ox6jxi8agv"
  instance_name           = "wp"
  password                = var.ecs_password
  instance_charge_type    = "PostPaid"
  vswitch_id              = alicloud_vswitch.vswitch_1.id
  data_disks {
    name        = "disk2"
    size        = 100
    category    = "cloud_efficiency"
    description = "disk2"
    # encrypted   = true
    # kms_key_id  = alicloud_kms_key.key.id
  }
}
  • image IDについて

東京regionのimage idを利用する必要がある。
image.png

  • security group:
    image.png
    image.png
  • EIP:
    image.png
    image.png

SLB

resource "alicloud_slb_load_balancer" "default" {
  load_balancer_name = "wp_slb"
  load_balancer_spec = "slb.s2.medium"
  vswitch_id         = alicloud_vswitch.vswitch_1.id
  master_zone_id     = var.zone_1
  slave_zone_id      = var.zone_2
}

resource "alicloud_slb_listener" "default" {
  load_balancer_id          = alicloud_slb_load_balancer.default.id
  backend_port              = 80
  frontend_port             = 80
  protocol                  = "http"
  bandwidth                 = 10
  sticky_session            = "on"
  sticky_session_type       = "insert"
  cookie_timeout            = 86400
  cookie                    = "testslblistenercookie"
  health_check              = "on"
  health_check_uri          = "/"
  health_check_connect_port = 80
  healthy_threshold         = 8
  unhealthy_threshold       = 8
  health_check_timeout      = 8
  health_check_interval     = 5
  health_check_http_code    = "http_2xx,http_3xx,http_4xx"
  x_forwarded_for {
    retrive_slb_ip = true
    retrive_slb_id = true
  }
  request_timeout = 80
  idle_timeout    = 30
}

resource "alicloud_slb_backend_server" "default" {
  load_balancer_id = alicloud_slb_load_balancer.default.id

  backend_servers {
    server_id = alicloud_instance.instance.id
    weight    = 50
  }
}

image.png
image.png
image.png

WordPressの設定

  • ssh root@<EIP_ECS> でwordpressのECSにログイン

  • wordpressの設定ファイルでPolarDBのURL, database, account情報を設定する
    vim /data/wwwroot/wordpress/wp-config.php
    image.png

  • ブラウザ上でWordPressのIPにアクセスし、初期設定を行う。(IPはterraformのoutput eip_ecsです)
    image.png
    image.png
    image.png
    image.png
    image.png

  • Redis cachingを設定する
    ECS上で以下のコマンドを実行し、Redis object cache pluginをダウンロードし、解凍する

wget https://downloads.wordpress.org/plugin/redis-cache.2.0.18.zip 
unzip redis-cache.2.0.18.zip 
  • redis-cache folderをcopyする
    cp -rf redis-cache /data/wwwroot/wordpress/wp-content/plugins/
  • WordPressをredisにアクエスできるように設定する
    vim /data/wwwroot/wordpress/wp-config.php
    • wp-config.php ファイルの最初の設定の箇所に以下の内容を追加する(最初に設定しないとだめだそうです)
// Redis settings
define( 'WP_REDIS_HOST', '<Redis URL>' );
define( 'WP_REDIS_CLIENT', 'predis' );
define( 'WP_REDIS_PORT', '6379' );
define( 'WP_REDIS_DATABASE', '0');
define( 'WP_REDIS_PASSWORD', 'wordpress:N1cetest' );

image.png

  • object-cache configuration ファイルをcopyする
    cp /data/wwwroot/wordpress/wp-content/plugins/redis-cache/includes/object-cache.php /data/wwwroot/wordpress/wp-content/
  • WordPressにログインして、Redis object cacheを有効化にする
    • Redis Object Cache pluginを先にActivateしてから、Settings ボタンをクリック
      image.png
    • pluginのステータスがConnectedであることが確認できたら、Flush Cacheボタンをクリックしcache dataをRedis instanceに同期させる
      image.png
  • SLB EIPにアクエスし、以下の画面が表示されたらOKです。
    image.png

Redisのキャッシュ効果を確認

  • ECS上で以下のコマンドを実行し、redisに接続する(-a の後ろはuserとpasswordになります)
    redis-cli -h r-0iw88071b19727c4.redis.japan.rds.aliyuncs.com -a 'wordpress:N1cetest'
r-0iw88071b19727c4.redis.japan.rds.aliyuncs.com:6379> keys *
 1) "wp:options:auth_key"
 2) "wp:comment:1"
 3) "wp:category_relationships:1"
 4) "wp:options:can_compress_scripts"
 5) "wp:default:is_blog_installed"
 6) "wp:transient:global_styles_twentytwentyone"
 7) "wp:options:nonce_salt"
 8) "wp:users:1"
 9) "wp:user_meta:1"
10) "wp:userlogins:dennis"
11) "wp:options:nonce_key"
12) "wp:comment_meta:1"
13) "wp:site-options:1-notoptions"
14) "wp:post_tag_relationships:1"
15) "wp:comment:last_changed"
16) "wp:terms:get_terms-a217cd3f6ed4806e2fd8fc7ffa68df8b-0.28634600 1661250085"
17) "wp:post_meta:3"
18) "wp:post_format_relationships:1"
19) "wp:terms:last_changed"
20) "wp:options:alloptions"
21) "wp:posts:1"
22) "wp:userslugs:dennis"
23) "wp:comment:get_comments-522753603409e7a482bf9f7dbe7ea238-0.96158000 1661250122"
24) "wp:posts:3"
25) "wp:post_meta:1"
26) "wp:transient:global_styles_svg_filters_twentytwentyone"
27) "wp:options:notoptions"
28) "wp:redis-cache:metrics"
29) "wp:default:wp_global_styles_00b0e7fde1a7a17937e88b6df1cfbe6f"
30) "wp:terms:get_terms-cac253f41dff4350b0f851e64d3bb31b-0.28634600 1661250085"
31) "wp:useremail:outanwang@gmail.com"
32) "wp:options:auth_salt"
33) "wp:terms:1"

  • wp:posts:1のkeyの中身を取得してみます(中身は初期設定で自動的に作られたpostの中身であることが分かる。)
    image.png

image.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?