AWS通過自動擴展實現高可用 - 創建一個高可用Wordpress站點

創建兩個S3 Bucket,分別存放Image和Code

創建兩個S3,分別存放Image和Code,一個S3 Bucket命名爲xiongwpmedia2019,存放圖片,另一個S3 Bucket命名爲xiongcode2019,用來存放Wordpress代碼。

使用CloudFront來訪問圖片

Networking & Content Delivery下的CloudFront,做以下設置,

  • Origin Domain Name選擇剛剛創建的S3 Bucket, 爲xiongwpmedia2019.s3.amazonaws.com

其它設置爲默認。
創建一個Security Group,開放 ipv4 and ipv6的http, port 80, 和 ssh, 22 port,名字設置爲Webdmz
再創建一個 RDS使用的Security Group,取名爲MyRDSSG, 允許 3306 port, TCP, Mysql/Aurora, 確保它開放給剛剛創建的 名爲Webdmz的 Security Group.
這裏的DNS域名地址是: df3jgic6atm3p.cloudfront.net

創建一個RDS和一個EC2

創建一個RDS,記得在Availability & durability裏, 設置Create a standby instance (recommended for production usage)。這裏要收費。RDS的Endpoint是:xiongrds.cn63rtxgzraf.ap-southeast-1.rds.amazonaws.com

參見使用AWS雲安裝WordPress + RDS MySQL, 記得在Mysql中新創建一個數據庫。

創建一個EC2,記得這裏要創建一個可以訪問S3 Bucket的IAM角色,這裏作爲原始的寫節點(Write node)。

使用以下啓動腳本, 這個腳本來自於Acloud.Guru,這裏沒有使用最新的Wordpress代碼,參見使用AWS雲安裝WordPress + RDS MySQL,使用最新的Wordpress代碼需要升級最新的PHP,比較麻煩。


#!/bin/bash
yum update -y
yum install httpd php php-mysql -y
cd /var/www/html
echo "healthy" > healthy.html
wget https://wordpress.org/wordpress-5.1.1.tar.gz
tar -xzf wordpress-5.1.1.tar.gz
cp -r wordpress/* /var/www/html/
rm -rf wordpress
rm -rf wordpress-5.1.1.tar.gz
chmod -R 755 wp-content
chown -R apache:apache wp-content
wget https://s3.amazonaws.com/bucketforwordpresslab-donotdelete/htaccess.txt
mv htaccess.txt .htaccess
chkconfig httpd on
service httpd start

使用SSH登入EC2.

Connecting to 54.169.190.81:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

WARNING! The remote SSH server rejected X11 forwarding request.

       __|  __|_  )
       _|  (     /   Amazon Linux AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2018.03-release-notes/
[ec2-user@ip-172-31-26-252 ~]$ sudo su
[root@ip-172-31-26-252 ec2-user]# cd /var/www/html/
[root@ip-172-31-26-252 html]# pwd
/var/www/html
[root@ip-172-31-26-252 html]# ls
healthy.html  license.txt  wp-activate.php  wp-blog-header.php    wp-config-sample.php  wp-cron.php  wp-links-opml.php  wp-login.php  wp-settings.php  wp-trackback.php
index.php     readme.html  wp-admin         wp-comments-post.php  wp-content            wp-includes  wp-load.php        wp-mail.php   wp-signup.php    xmlrpc.php
[root@ip-172-31-26-252 html]# cat .htaccess
Options +FollowSymlinks
RewriteEngine on
rewriterule ^wp-content/uploads/(.*)$ http://d2jmrva8tfzxcq.cloudfront.net/$1 [r=301,nc]

# BEGIN WordPress

# END WordPress
[root@ip-172-31-26-252 html]#

EC2運行起來後,輸入網址對Wordpress站點進行設置,參見使用AWS雲安裝WordPress + RDS MySQL。包括創建wp-config.php
創建成功後,下面來做自動使用S3 Bucket備份,同步,使用Auto Scaling Group進行自動擴展,使用Cloud Front提升性能。另外 ,RDS的高可用是由RDS自動備份來完成的,不需要額外操作。

下面驗證可以使用AWS指令訪問S3 Bucket.

[root@ip-172-31-26-252 html]# aws s3 ls
2019-01-06 13:16:41 xiong-polly
2019-01-06 13:17:07 xiong-polly-mp3
2019-01-01 01:55:31 xiong2019
2019-11-24 11:12:19 xiongcode2019
2019-11-24 11:11:49 xiongwpmedia2019
[root@ip-172-31-26-252 html]#

下面發佈一個博客,加上一個圖片,如下:

MyBlog screenshot

以上上傳的圖片被存放到以下地址:
http://ec2-54-169-190-81.ap-southeast-1.compute.amazonaws.com/wp-content/uploads/2019/11/DSC09023-Copy.jpg

在SSH shell裏檢查如下:

[root@ip-172-31-26-252 html]# ls wp-content/uploads/2019/11/DSC09023-Copy.jpg
wp-content/uploads/2019/11/DSC09023-Copy.jpg
[root@ip-172-31-26-252 html]#

備份上傳的圖片到xiongwpmedia2019的S3 Bucket裏

備份上傳的圖片到xiongwpmedia2019的S3 Bucket裏, 如下:

[root@ip-172-31-26-252 html]# cd
[root@ip-172-31-26-252 ~]# pwd
/root
[root@ip-172-31-26-252 ~]# aws s3 cp --recursive /var/www/html/wp-content/uploads s3://xiongwpmedia2019
upload: ../var/www/html/wp-content/uploads/2019/11/DSC09023-Copy.jpg to s3://xiongwpmedia2019/2019/11/DSC09023-Copy.jpg
[root@ip-172-31-26-252 ~]#
[root@ip-172-31-26-252 ~]# aws s3 ls s3://xiongwpmedia2019
                           PRE 2019/
[root@ip-172-31-26-252 ~]#

在AWS的xiongwpmedia2019`的S3 Bucket裏檢查文件確實被上傳。

備份Wordpress站點代碼到xiongcode2019的S3 Bucket裏

備份Wordpress站點代碼到xiongcode2019的S3 Bucket裏,如下:

[root@ip-172-31-26-252 ~]# aws s3 cp --recursive /var/www/html s3://xiongcode2019
...
...

[root@ip-172-31-26-252 ~]# aws s3 ls s3://xiongcode2019
                           PRE wp-admin/
                           PRE wp-content/
                           PRE wp-includes/
2019-11-24 15:57:56        165 .htaccess
2019-11-24 15:57:56          8 healthy.html
2019-11-24 15:57:56        420 index.php
2019-11-24 15:57:56      19935 license.txt
2019-11-24 15:57:56       7425 readme.html
2019-11-24 15:57:56       6919 wp-activate.php
2019-11-24 15:57:59        369 wp-blog-header.php
2019-11-24 15:57:59       2283 wp-comments-post.php
2019-11-24 15:57:59       2898 wp-config-sample.php
2019-11-24 15:57:59       3142 wp-config.php
2019-11-24 15:58:00       3847 wp-cron.php
2019-11-24 15:58:05       2502 wp-links-opml.php
2019-11-24 15:58:06       3306 wp-load.php
2019-11-24 15:58:05      38883 wp-login.php
2019-11-24 15:58:05       8403 wp-mail.php
2019-11-24 15:58:06      17947 wp-settings.php
2019-11-24 15:58:06      31085 wp-signup.php
2019-11-24 15:58:06       4764 wp-trackback.php
2019-11-24 15:58:06       3068 xmlrpc.php
[root@ip-172-31-26-252 ~]#

修改.htaccess以達到Cloud Front效果

[root@ip-172-31-26-252 ~]# cd /var/www/html/
[root@ip-172-31-26-252 html]# cat healthy.html
healthy
[root@ip-172-31-26-252 html]# cat .htaccess
Options +FollowSymlinks
RewriteEngine on
rewriterule ^wp-content/uploads/(.*)$ http://d2jmrva8tfzxcq.cloudfront.net/$1 [r=301,nc]

# BEGIN WordPress

# END WordPress
[root@ip-172-31-26-252 html]#
[root@ip-172-31-26-252 html]# vim .htaccess

在Cloud Front裏取出DNS域名地址: df3jgic6atm3p.cloudfront.net
在這裏把以下
rewriterule ^wp-content/uploads/(.*)$ http://d2jmrva8tfzxcq.cloudfront.net/$1 [r=301,nc]
替換爲:
rewriterule ^wp-content/uploads/(.*)$ http://df3jgic6atm3p.cloudfront.net/$1 [r=301,nc]

結果如下:

[root@ip-172-31-26-252 html]# cat .htaccess
Options +FollowSymlinks
RewriteEngine on
rewriterule ^wp-content/uploads/(.*)$ http://df3jgic6atm3p.cloudfront.net/$1 [r=301,nc]

# BEGIN WordPress

# END WordPress

進行一下同步到xiongcode2019的S3 Bucket裏,如下:

[root@ip-172-31-26-252 html]# aws s3 sync /var/www/html s3://xiongcode2019
upload: ./.htaccess to s3://xiongcode2019/.htaccess

[root@ip-172-31-26-252 html]#

設置httpd.conf

需要設置設置httpd.conf開放地址改寫, 這樣就可以使用Cloud Front了。

[root@ip-172-31-26-252 html]# cd /etc/httpd/conf
[root@ip-172-31-26-252 conf]# pwd
/etc/httpd/conf
[root@ip-172-31-26-252 conf]# ls
httpd.conf  magic
[root@ip-172-31-26-252 conf]# cp httpd.conf httpd.conf.bak
[root@ip-172-31-26-252 conf]# vim httpd.conf

修改AllowOverride NoneAllowOverride All,並重啓httpd.

[root@ip-172-31-26-252 conf]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]
[root@ip-172-31-26-252 conf]#

修改xiongwpmedia2019的S3 Bucket Policy

需要修改xiongwpmedia2019的S3 Bucket Policy,

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "PublicReadGetObject",
      "Effect": "Allow",
      "Principal": "*",
      "Action": [
        "s3:GetObject"
        ],
      "Resource": [
        "arn:aws:s3:::xiongwpmedia2019/*"
        ]
    }
  ]
}

修改後有以下警告:
This bucket has public access
You have provided public access to this bucket. We highly recommend that you never grant any kind of public access to your S3 bucket.

再去刷新博客頁面,圖片地址已經改爲以下地址了,說明Cloud Front起作用了。
原始地址:
http://ec2-54-169-190-81.ap-southeast-1.compute.amazonaws.com/wp-content/uploads/2019/11/DSC09023-Copy.jpg
改後地址:
http://df3jgic6atm3p.cloudfront.net/2019/11/DSC09023-Copy.jpg

創建Application Load Balancer和Route 53

創建Application Load Balancer,取名爲XiongApplicationLB,這裏需要創建一個Target Group,取名爲XiongTargetGroup。Health checks設置爲/healthy.html,將創建的EC2的寫節點加入。
再創建Route 53的記錄,這裏並沒有並沒有註冊DNS,直接使用Application Load Balancer的地址。
XiongApplicationLB-1325547183.ap-southeast-1.elb.amazonaws.com

創建Hosted Zone和Record Set (可選),Domain Name設置爲XiongApplicationLB-1325547183.ap-southeast-1.elb.amazonaws.com

創建 Auto Scaling Group

創建任務計劃crontab自動從S3下載代碼

既然要高可用,那麼任何的代碼改動都會自動備份到S3的xiongcode2019 Bucket,這個是稱爲·Write Node·寫節點的EC2實例完成的。而創建Auto Scaling Group達到高可用,是要從Read Node 讀節點進行創建讀功能的EC2實例以達到自動擴展,失敗後重新創建EC2實例,創建新的EC2實例時要從xiongcode2019 Bucket下載最新代碼。

以下是是創建任務計劃crontab自動從S3下載代碼,編輯/etc/crontab文件,然後後面用這個EC2實例來創建一個AMI。

[ec2-user@ip-172-31-26-252 ~]$ sudo su
[root@ip-172-31-26-252 ec2-user]# vim /etc/crontab
[root@ip-172-31-26-252 ec2-user]#
[root@ip-172-31-26-252 ec2-user]# service crond status
crond (pid  2644) is running...
[root@ip-172-31-26-252 ec2-user]#

每分鐘做以下同步操作操作,將S3的xiongcode2019 Bucket的代碼同步到本地,修改crontab文件如下:
*/1 * * * * root aws s3 sync --delete s3://xiongcode2019 /var/www/html

在S3的xiongcode2019 Bucket創建一個文件進行測試,看看能不能自動下載到本地。

[root@ip-172-31-26-252 ec2-user]# cd /var/www/html/
[root@ip-172-31-26-252 html]# vim hello.txt
[root@ip-172-31-26-252 html]# cat hello.txt
測試Crond同步到S3 bucket!
[root@ip-172-31-26-252 html]# pwd
/var/www/html
[root@ip-172-31-26-252 html]#

檢查本地的hello.txt文件,發現文件被同步,測試成功!

創建一個AMI image

在EC2下的Actions裏選擇Create Image來創建AMI, 取名:XiongAMIReadNodeImage
創建AMI的鏡像,EC2實例會被默認關閉。

創建AMI image後將原始EC2設置成寫節點

Write Node:寫節點。

前面創建的EC2實例是讀節點Read Node,已經用作創建了AMI,下面會進一步用作創建Auto Scaling Group。

那麼這個讀節點Read Node已經創建完Auto Scaling Group所使用的AMI, 已經沒有其它作用了,可以將其改成寫結點Write Node。也就是在本地更新會被自動更新到S3 Bucket裏去。

[root@ip-172-31-26-252 ~]# vim /etc/crontab
[root@ip-172-31-26-252 ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
*/1 * * * * root aws s3 sync --delete /var/www/html s3://xiongcode2019
*/1 * * * * root aws s3 sync --delete /var/www/html/wp-content/uploads s3://xiongwpmedia2019
[root@ip-172-31-26-252 ~]#

計劃任務如下,每分鐘執行一次,
*/1 * * * * root aws s3 sync --delete /var/www/html s3://xiongcode2019
*/1 * * * * root aws s3 sync --delete /var/www/html/wp-content/uploads s3://xiongwpmedia2019

進行測試,首先創建一個文件。

[root@ip-172-31-26-252 ~]# cd /var/www/html/
[root@ip-172-31-26-252 html]# pwd
/var/www/html
[root@ip-172-31-26-252 html]# vim test.txt
[root@ip-172-31-26-252 html]# cat test.txt
this is test for the Crontab upload to s3 xiongcode2019 bucket..
[root@ip-172-31-26-252 html]#

xiongcode2019去檢查test.txt這個文件的存在,驗證成功上傳,測試成功!

做另一個測試,重新寫一個博客,再上傳一個圖片,再檢查xiongwpmedia2019裏是否有圖片上傳。在這裏上傳圖片後,EC2實例中是上傳了,如下:

[root@ip-172-31-26-252 html]# ls ./wp-content/uploads/2019/11/
Catoon-2018-Origin-Modify.jpg  DSC09023-Copy.jpg
[root@ip-172-31-26-252 html]#

這裏寫第二個博客時,圖片會不顯示,如下圖所示,這裏是因爲Cloud Front起作用了,網址進行了替換,但是Cloud Front需要時間來傳播,所以顯示不了圖片。這裏不管,直接進行發佈。
2nd post
查看博客的第二篇博文,圖片是可以正常顯示的,一切正常!如果再進去編輯博文,圖片也是可以正常顯示 的,拷貝圖片地址在網頁中打開,發現Cloud Front是正常工作的,圖片地址也已經被替換成http://df3jgic6atm3p.cloudfront.net/2019/11/Catoon-2018-Origin-Modify.jpg

使用創建的AMI來創建Auto Scaling Group

前面已經創建了讀節點Read NodeEC2實例的AMI,那麼使用這個AMI來創建Auto Scaling Group,取名爲XiongAutoScalingGroup
Auto Sacling功能是在EC2界面下,創建一個新的Auto Scaling Group,提示要先創建Launch Configuration。如下章所述。

設置Group size爲兩個實例,把新加坡的三個Subnet都添加進來。

最後,在設置Launch Configuration的時候會設置Target Group,設置爲XiongTargetGroup, 這樣Auto Scaling Group就與創建的Application Load Balancer,XiongApplicationLB結合起來了。也就是Target Group,XiongTargetGroup會自動將Auto Scaling Group所創建的EC2實例添加到Target Group裏。

這樣就創建了Auto Scaling Group,

設置Launch Configuration

使用前面創建的AMI XiongAMIReadNodeImage,創建Launch Configuration,取名爲: XiongLaunchConfiguration

記得添加S3 Admin的角色。
設置啓動腳本如下,也就是在創建的時候從S3 Bucket xiongcode2019下載最新的代碼。

#!/bin/bash
yum update -y
aws s3 sync --delete s3://xiongcode2019 /var/www/html

將原始的EC2的寫節點Write Node從Target Group裏去掉,這樣就把讀節點和寫節點分開來,訪問站點的時候,就會訪問Auto Scaling Group創建的兩個讀節點,而不會去訪問寫節點。如果要寫新博文,那麼就會跳回到原始的寫節點Write Node進行新博文添加和其它改動操作。

那麼爲什麼寫新博文,那麼就會跳回到原始的寫節點Write Node呢?如下解釋了原因。

爲什麼寫節點直接跳回到原始地址?

這裏的寫節點直接跳回到原始地址是在Wordpress設置的,Site Address (URL)設置爲了http://ec2-54-169-190-81.ap-southeast-1.compute.amazonaws.com,這個是在Wordpress最原始安裝的時候設置的,這個參數是可以更改的。參見以下解釋:

Q: when you went to /wp-admin, how did wordpress figure out that you were after write not the read node.
A: The answer lies in the DB. Check this on your DB: select * from wp_options where optionn_ame = 'siteurl';

This points to the IP of the instance which set up the original wp site.

I think the php code in wp-login.php uses this information for redirecting when the user is not logged in. So in a way anyone trying to login to post, will always redirect to your original site.

I think someone with php knowledge will be able to say this better.

It really got me that why this redirect happens. My instinct was somewhere this was hard coded. It turns out to be true.

So I think this not the doing of the load balancer.

能過Navicat Premium進行連接測試, 可以驗證siteurl爲``,注意RDS的Public accessibility要設置爲yes.
也可以通過mysql進行連接驗證,說明Write Node的地址跳轉是Wordpress裏寫在DB裏了,如下:

[root@ip-172-31-26-252 html]# mysql -u scruffybear -h xiongrds.cn63rtxgzraf.ap-southeast-1.rds.amazonaws.com -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 353
Server version: 5.7.22-log Source distribution

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
mysql> select * from wp_options where option_name = 'siteurl';
+-----------+-------------+---------------------------------------------------------------+----------+
| option_id | option_name | option_value                                                  | autoload |
+-----------+-------------+---------------------------------------------------------------+----------+
|         1 | siteurl     | http://ec2-54-169-190-81.ap-southeast-1.compute.amazonaws.com | yes      |
+-----------+-------------+---------------------------------------------------------------+----------+
1 row in set (0.00 sec)

mysql>

測試高可用

也就是隨意刪除Read NodeEC2實例,不管是刪除一個,還是完全刪除,Auto Scaling Group會進行重新創建新的Read NodeEC2實例,從xiongcode2019的S3 Bucket裏下載最新的Wordpress代碼,創建並始終保持有兩個的Read NodeEC2實例在運行。而這兩個的EC2實例是由Application Load Balancer控制進行訪問。

使用網址: http://xiongapplicationlb-1325547183.ap-southeast-1.elb.amazonaws.com/可以不間斷地進行訪問博客。

術語

AMI: Amazon Machine Image

Reference

使用AWS雲安裝WordPress + RDS MySQL
USING .HTACCESS REWRITE RULES
Acloud.Guru
Using High-Level (s3) Commands with the AWS CLI

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章