iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 26
1
自我挑戰組

Linux學習系列 第 26

AWS ELB 、 CloudFront 、 Route 53 、 SNS 、 Lambda、CloudFormation、SQS

  • 分享至 

  • twitterImage
  •  

AWS ELB - How to configure ELB and add web-servers?

Elastic Load Balancing
Elastic Load Balancing
30天鐵人賽介紹 AWS 雲端世界 - 15: EC2的網路負載平衡服務 Elastic Load Balancing(ELB)
EBS - Introduction of Elastic Block Store
EBS - Moving Volumes

Elastic Load Balancing可在多個目標(例如 Amazon EC2 執行個體、容器與 IP 地址) 之間自動分配傳入的應用程式流量。它可以在單一可用區域或跨多個可用區域處理應用程式流量的各種負載。

先創兩個網頁伺服器,然後創一個ELB,然後ELB裝到這兩個網頁伺服器,ELB就可以查看這兩個伺服器狀況

先創兩個網頁伺服器,可以先創一個,在AMI備份一個
sudo apt update && sudo apt upgrade && sudo apt dist-upgrade
sudo apt install apache2
sudo systemctl status apache2
sudo nano /var/www/html/index.html
https://ithelp.ithome.com.tw/upload/images/20181108/20111994I747BeBvzb.png

<html>
  <head>
      <title></title>
  </head>
  <body>
     <h1>Steinsgate0</h1>
  </body>
</html>

sudo systemctl restart apache2

接著備份這個網頁伺服器,AMI,然後選擇不同的AZ

接著創ELB,選擇Application Load Balancer
https://ithelp.ithome.com.tw/upload/images/20181108/20111994VVyZ73xA9n.png

設定ELB,internet-facing:internet
Internal:私有網路
選擇AZ,所以不同region的主機,不同VPC,沒辦法同一個ELB
https://ithelp.ithome.com.tw/upload/images/20181108/20111994JR1sJIw0o5.png

設定ELB:
Health checks:
Path:就是網頁的位置
Healthy threshold:連續5次health check(Interval)成功,這時候就會認為伺服器healthy
Unhealthy threshold: 連續2次health check(Interval)失敗,這時候就會認為伺服器unhealthy
Timeout: 每次Interval,每30秒檢測伺服器有沒有問題,如果伺服器5秒內沒有回應的話,代表health check failure
Interval: health check。每30秒檢測伺服器有沒有問題。
https://ithelp.ithome.com.tw/upload/images/20181108/20111994T2PQYYHDj5.png

接著就會選擇主機,選了創建的兩個網頁伺服器
https://ithelp.ithome.com.tw/upload/images/20190421/20111994GimZUvzGwv.png

接著講到只要使用ELB的DNS Name,就可以連到主機的網頁伺服器,重整就可以切換網頁伺服器
把第二個網頁伺服器的內容稍微修改,就可以很清楚的知道有切換
https://ithelp.ithome.com.tw/upload/images/20190421/20111994mQJ2eiCSJ3.png
https://ithelp.ithome.com.tw/upload/images/20190421/20111994rjcDYoQXfl.png

接著繼續學習Auto-scaling
AWS EC2 Linux & Auto-scaling group and Launch Configuration

在創建Auto-scaling前,會先要你創建launch configuration,因為有了ELB的網址,所以網頁伺服器都可以不用有public ip了
https://ithelp.ithome.com.tw/upload/images/20190421/20111994Zj4yKG1QqG.png

可以設定一開始要開幾台EC2,這邊選3台,然後可以選定要加到哪個ELB
https://ithelp.ithome.com.tw/upload/images/20190421/20111994DRBPIjXsdd.png

然後一定要設定最少幾台EC2,如果最少要3台,現在只有一台EC2在跑,Auto-scaling就會自動補兩台
然後可以設定事件,像是CPU>=70%超過300秒,就增加一台EC2
https://ithelp.ithome.com.tw/upload/images/20190421/201119945qQxTQAp4d.png

最後顯示所有設定的東西:
https://ithelp.ithome.com.tw/upload/images/20190421/20111994Dz31ItWj3h.png

Auto-scaling 刪掉 的話 , Auto-scaling創建的 EC2也會terminated

接著來看
AWS Elastic Load Balancing – ELB – Certification

1 ELB的Health Checks的方式是: sends pings, attempts connections, or sends request to health check the EC2 instances.

2 listener:

Network Load Balancer的listener
https://ithelp.ithome.com.tw/upload/images/20190510/20111994SjBTw9Llhs.png

Application Load Balancer的listener
https://ithelp.ithome.com.tw/upload/images/20190510/20111994UWPq1wjQhk.png

3 如果AZ-a 有五個ec2,AZ-b有2個ec2,elb還是這兩個az各一半的流量,而不是看機器數目來分配流量。
除非要Enabling Cross-Zone load balancing,這樣看的就會是機器數
參考:ELB - Cross-zone Load Balancing
目前應該是Application Load Balancer、Network Load Balancer、Classic Load Balancer 都可以用 。

4 Sticky Sessions就是有些顧客端的連線還是要連到特定一台ec2,不能分散到不同ec2。
要開啟Sticky Sessions必須SSL traffic should be terminated on the ELB。
因為ELB要讀HTTP headers 的cookie之類的才能知道要到哪一台特定的主機 ,用SSL traffic(加密) 沒辦法判斷?
參考:ELB - Sticky Sessions

5 ELB使用X-Forwarded headers幫助系統管理者了解顧客的使用情形
一 X-Forwarded-For:當使用HTTP or HTTPS load balancer,系統管理者了解顧客的IP地址
二 X-Forwarded-Proto:了解顧客使用哪個協定(HTTP/S)
三 X-Forwarded-Port:了解顧客和elb是用哪個port連接
參考:ELB - X-Forwarded-For Header

6
當使用者不是用HTTP protocol ,或是using HTTPS and not terminating the SSL connection,ELB可以用Proxy Protocol來幫助系統管理員了解顧客的使用情形

7
ELB 使用 Proxy Protocol version 1,使用human-readable header format了解顧客端的IP address, and port numbers。

8 如果原本就有Proxy ,就不用ELB的Proxy Protocol,因為這樣會重複

If the ELB is already behind a Proxy with the Proxy protocol enabled, enabling the Proxy Protocol on ELB would add the header twice

9 如果有選擇https listener,會有安全驗證的選項
https://ithelp.ithome.com.tw/upload/images/20190510/20111994pKgq52ZKvI.png

10 Application Load Balancer 不支援後端伺服器身份驗證,僅支援後端伺服器加密(這個不太懂)
後端伺服器身份驗證 跟 使用者身分驗證 不太清楚兩者差別:
https://ithelp.ithome.com.tw/upload/images/20200721/201119946ZZ3bGotcQ.png

接著來比較ELB的種類:
AWS Classic Load Balancer vs Application Load Balancer – Certification
Elastic Load Balancing 功能

1 Application Load Balancer只支援HTTP、HTTPS,所以如果要TCP、SSL/TLS要用Classic Load Balancer

2 Classic Load Balancer有EC2-Classic、VPC平台,其他的都只有VPC平台

3 以 Lambda 函數做為目標的只有 Application Load Balance

4 以路徑為基礎的路由、以主機為基礎的路由、以 HTTP 標頭為基礎的路由、以 HTTP 方法為基礎的路由、原生 HTTP/2都是只有Application Load Balance擁有的功能

5 黏性工作階段、跨區域負載平衡、後端伺服器加密,Classic Load Balancer 和 Application Load Balancer都有這些功能

AWS CloudFront

30天鐵人賽介紹 AWS 雲端世界 - 8: CloudFront 與 建立檔案 CDN 服務
AWS Storage Options – CloudFront & ElastiCache
AWS CloudFront – Certification
AWS CloudFront OAI | Use Origin Access Identity | Restrict Access to Your S3 | Serve Private Content
比較不了解的就是:Origin Access Identity (OAI)

Origin access identity, which is a special CloudFront user, can be created and associated with the distribution.

Origin access identity是一個特別的CloudFront user。

When users access the object from CloudFront, it uses the OAI to fetch the content on users behalf, while direct access to the S3 objects is restricted

當使用者 獲取物品 從 CloudFront,是透過Origin access identity 來獲取 物品內容 ,所以 可以限制 直接從S3獲取

Users accessing S3 objects directly would
1 bypass the controls provided by CloudFront signed URLs or signed cookies, for e.g., control over the date time that a user can no longer access the content and the IP addresses can be used to access content
2 CloudFront access logs are less useful because they’re incomplete.

直接從 S3 獲取物品 會
1 繞過CloudFront簽名的URL或簽名的cookie 。 像是拿到超過日期的東西 , 或 不該被訪問的東西

2 CloudFront access logs 不完整?

這邊不太了解

AWS Route 53

讀:

AWS Certification – Route 53 Overview

DNS - Top-Level Domains
DNS - CNAME Records
DNS - NS Records
https://www.whois365.com/tw/domain/www.google.com.tw

Route 53 有三個主要功能:

1 Domain registration
註冊domain names? ,像是https://ithelp.ithome.com.tw/

2 Domain Name System (DNS) service
dns伺服器功能,把https://ithelp.ithome.com.tw/ 轉成 xxx.xxx.xxx.xxx

can route Internet traffic to CloudFront, Elastic Beanstalk, ELB, or S3. There’s no charge for DNS queries to these resources
可以路由到CloudFront, Elastic Beanstalk, ELB, or S3的服務?,不收DNS到這些地方的錢

3 Health checking
檢查有沒有壞掉的IP,可以跟 CloudWatch alarms配合
sends automated requests over the Internet to the application to
verify that it’s reachable, available, and functional

CloudWatch alarms can be configured for the health checks to send notification when a resource becomes unavailable.

Route 53 支援的種類:

https://ithelp.ithome.com.tw/upload/images/20200126/20111994uUIjj2BI0f.png

A (Address) Format
IPv4

AAAA Format
IPv6 address

CNAME Format
the same format as a domain name
這邊不太懂,意思是CNAME要 www.example.com 而不是example.com?
DNS protocol does not allow creation of a CNAME record for the top node of a DNS namespace, also known as the zone apex

top node of a DNS namespace = zone apex = naked domain name
這個東西應該就是指 the zone apex is example.com, a CNAME record for example.com cannot be created
如果example.com 是 the zone apex, 那 example.com就不能用CNAME

MX (Mail Xchange) Format
email server

NS (Name Server) Format
是指The domain name of a name server(name server的domain name?)
什麼是name server?
name server

PTR Format
domain name

TXT (Text) Format

字串, 像是:"Sample Text"、"quotation marks"

Alias resource record sets

就是可以路由到的服務:
https://ithelp.ithome.com.tw/upload/images/20200126/20111994YfkbW5K5Uz.png

Alias record is similar to a CNAME record, but can create an alias record both for the root domain or apex zone, such as example.com, and for subdomains, such as www.example.com. CNAME records can be used only for subdomains.

CNAME records只能subdomains, such as www.example.com, 但是Alias www.example.com 或 example.com都可以。

alias resource record set 沒有TTL ,如果指到這些地方:
因為 If an alias resource record set points to a CloudFront distribution, a load balancer, or an S3 bucket, the time to live (TTL) can’t be set; Route 53 uses the CloudFront, load balancer, or Amazon S3 TTLs.

Route 53 Hosted Zone

https://ithelp.ithome.com.tw/upload/images/20200126/20111994qiYlOVB52q.png

Hosted Zone is A container for records, which include information about how to route traffic for a domain (such as example.com) and all of its subdomains (such as www.example.com).

Route 53 Split-view (Split-horizon) DNS

enables you to access an internal version of your website using the same domain name that is used publicly
就是可以私人和公共的IP 用 相同的domain name

其他:How can I use Route 53 to access an internal version of my website using the same domain name as my public website?

接著來讀:
AWS Route 53 Routing Policy – Certification

Weighted Routing Policy
不同比例分配流量:
e.g., 75% one server and 25% to the other during a pilot release

Weights can be assigned any number from 0 to 255

Common use cases include
load balancing
A/B testing and piloting new versions of software

Latency-based Routing (LBR) Policy
if the latency from the user in Singapore to Ireland improves, the user can be routed to Ireland
Latency-based Routing (LBR) Policy選擇Region:
https://ithelp.ithome.com.tw/upload/images/20200126/20111994zw1wThUpLk.png

Failover Routing Policy
一個機器壞了 , 選擇要換到哪個機器

Geolocation Routing Policy
選擇洲或國家:
https://ithelp.ithome.com.tw/upload/images/20200126/20111994HGhlTtUqhl.png

還有一種是 Geoproximity ,選擇經緯度的,在這邊才能選的樣子(Traffic Policies):
https://ithelp.ithome.com.tw/upload/images/20200704/201119944J9yzSZduu.png
Geoproximity 是 地理位置接近
Geolocation 是 地理位置

原本以為Latency-based Routing (LBR) Policy 主要 也跟地理位置 類似 ,因為 距離自己越近的應該 就是
延遲較低的。 但是不一定 。
可能會被 硬體 跟 早上晚上影響 ?(不懂)

還有一種 MultiValue:
https://ithelp.ithome.com.tw/upload/images/20200704/20111994kY4JCLorTc.png
跟Simple Routing Policy(一般的Routing) 不同的是 只會Route 到 health的

AWS SNS

來源:Simple Notification Service – SNS
如圖 : SNS類似傳訊息的功能 ,可以把訊息傳給lambda、SQS、HTTP、Email、SMS
https://jayendrapatil.com/wp-content/uploads/2016/05/screen-shot-2016-05-06-at-8-13-57-am-768x507.png

SNS Supported Transport Protocols

https://ithelp.ithome.com.tw/upload/images/20200126/20111994LDgcPZ2EyX.png

1 HTTP, HTTPS
2 Email, Email-JSON
3 SQS
4 SMS – registered phone numbers

AWS Lambda

來源:AWS Lambda – Certification
Lambda - Use Cases

整理
1
Lambda的程式 存在S3:

Lambda stores code in S3 and encrypts it at rest and performs additional integrity checks while the code is in use.

2
AWS X-Ray 可以追蹤Lambda的 overhead, function init time, and function execution time:

AWS X-Ray helps tracing for Lambda functions, which provides insights such as Lambda service overhead, function init time, and function execution time

3
Lambda自動跟CloudWatch logs整合,像是 紀錄 每個程式 消耗的資源?

Lambda automatically integrates with CloudWatch logs, creating a log group for each function and providing basic application lifecycle event log entries, including logging the resources consumed for each use of that function

4
Lambda functions 支援的觸發:
就是 這些東西 可以 觸發 Lambda執行程式:
https://ithelp.ithome.com.tw/upload/images/20200126/20111994OUM8d0tgQn.png
https://ithelp.ithome.com.tw/upload/images/20200126/20111994DYcqjGONXE.png

5
AWS Lambda function沒有版本控制,每一個Lambda function都有一個特別的ARN,不能被更改的。
但是可以用Aliases 來達成 版本控制(可以命名 每一個版本 ,像是中文名稱)

Each AWS Lambda function has a single, current version of the code and there is no versioning of the same function.
Each Lambda function version has a unique ARN and after it is published it is immutable
Versioning can be implemented using Aliases.

參考:
Lambda — Aliases
Fargate — Fargate vs Lambda

AWS CloudFormation

CloudFormation 就是aws有一個規則,可以寫一個文字檔案,創建需要的aws服務 。 把這個檔案 放到CloudFormation ,就會開始創建AWS服務 , 然後刪掉這個檔案 ,又可以把這些AWS服務刪掉。

來源:AWS CloudFormation – Certification

整理

1

CloudFormation supports Chef & Puppet Integration, meaning that you can deploy and configure right down the application layer

什麼是 Chef 或 Puppet ? 應該也是類似的東西,也是一個檔案 來創建資源。
Chef 自動化部署工具
所以可以跟 Chef 或 Puppet 和 aws的CloudFormation 一起來創建資源或軟體? 這邊不太了解。

2
如果某個資源有錯誤,可以把之前成功創建的東西,從堆疊 取出 、刪除(不確定)

By default, automatic rollback on error feature is enabled, which will cause all the AWS resources that CloudFormation created successfully for a stack up to the point where an error occurred to be deleted.

3
WaitCondition resource 就是 柵欄的功用 , 直到收到 某個資源 完成 的訊號 ,不會創建別的資源

CloudFormation provides a WaitCondition resource that acts as a barrier, blocking the creation of other resources until a completion signal is received from an external source e.g. application, or management system

4
DeletionPolicy Attribute 就是 你如果刪除 AWS CloudFormation檔案 ,可以保護 一些東西 不被刪除 。
一 Retain 代表 不會刪掉該資源
二 Snapshot 代表 如果該資源可以製作Snapshot,就會製作Snapshot
檔案叫STACK的樣子:
https://ithelp.ithome.com.tw/upload/images/20200126/201119944u3ABmIIwM.png

DeletionPolicy Attribute
preserve or (in some cases) backup a resource when its stack is deleted

By default, if a resource has no DeletionPolicy attribute, AWS CloudFormation deletes the resource:
o specify Retain for that resource, to prevent deletion
o specify Snapshot to create a snapshot before deleting the resource, if the snapshot capability is supported for e.g RDS, EC2 volume etc.

5
DependsOn 應該是指 該資源 一定 要等 某個資源創建好 ,才能輪到該資源創建

DependsOn Attribute
resource is created only after the creation of the resource specified in the DependsOn attribute

AWS SQS

AWS SQS to Lambda Tutorial in NodeJS | Step by Step

其實對SQS感覺很抽象,這部影片有實作,就是創建一個SQS , 然後發訊息給SQS , 之後lambda會偵測SQS的訊息,就會處理掉訊息 。(可能有錯誤)
https://ithelp.ithome.com.tw/upload/images/20200614/20111994OA9A3kgU2U.png
1
預設可見性逾時:

可見性逾時是 Amazon SQS 防止其他使用元件接收和處理訊息的一段時間

應該是指,今天我要跟SQS 要資料 ,如果沒有在30秒內要到這筆資料,
這筆資料就不會刪掉 , 留著繼續給別人(其他東西)要這筆資料?
https://ithelp.ithome.com.tw/upload/images/20200614/20111994fp4TTK9yqZ.png

AWS Simple Queue Service – SQS – Certification

SQS 常見問答

教學裡的這個圖片很清楚了解SQS:
https://jayendrapatil.com/wp-content/uploads/2016/05/screen-shot-2016-05-05-at-8-16-39-am.png

題目 :

process long-running jobs once and only once
(一個工作 只執行到 一次 ,所以visibility timeout(預設可見性逾時)要長一些 , 這樣才能要到message並刪除 )

SQS allow multiple readers to access the same message queue without losing messages
visibility timeout要夠短 ,每個readers都沒辦法在時間內 完成 message的處理,這樣每個readers才可以都獲得message

長輪詢、短輪詢:
猜測應該是指,教學裡的這圖:
https://jayendrapatil.com/wp-content/uploads/2016/05/screen-shot-2016-05-05-at-8-17-04-am.png

如果你在visibility timeout 的時段裡 是拿不到 message的。
如果沒有訊息,當然也拿不到message的。
但是 每隔一段時間,系統就會自己去要訊息 , 所以 如果訊息不用那麼及時 去要 。可以用長輪詢。
長輪詢好處 : 可能會降低使用 SQS 的成本,因為可以降低空白接收數 。

在大部分的使用案例中,Amazon SQS 長輪詢都可提升效能並降低成本。不過,如果您的應用程式期待從 ReceiveMessage 呼叫收到立即的回應,則可能必須對應用程式進行一些修改,才能利用長輪詢的優勢。

一般情況下,長輪詢逾時 最長 應設定為 20 秒。因為較高的長輪詢逾時值會減少傳回空 ReceiveMessageResponse 執行個體的次數,因此盡可能將長輪詢逾時設得越高越好。

問:Amazon SQS 是否能夠保證交付訊息?
標準佇列 每個訊息 至少 會交付一次。(大概是因為如果沒有 在可見性逾時 完成 。 訊息會留下來 ,所以可能會交付兩次以上)
標準佇列提供鬆散的 FIFO 功能,以嘗試保留訊息順序。但是,由於標準佇列的設計可以透過
高度分散式架構進行大幅度的擴展,
因此無法保證接收訊息的順序會與訊息傳送的順序完全相同。

FIFO 每個訊息 會交付一次

FIFO 佇列提供只處理一次的功能,這代表每個訊息會交付一次,並且會在
消費者處理和刪除該訊息之前保持可用狀態。
此佇列不會引入重複項目。

FIFO 佇列的設計永遠不會出現重複的訊息。不過,訊息生產者可能會在某些情況下放入重複項目:例如,如果生產者傳送一則訊息,但沒有收到回應,然後重新傳送相同的訊息。Amazon SQS API 提供重複資料刪除功能,可避免您的訊息生產者傳送重複的訊息。訊息生產者引入的任何重複項目都會在 5 分鐘的重複資料刪除間隔內移除。

SQS -->pull
SNS -->push


使用SQS的狀況:
像是client端突然有大量的request,所以auto scailing 自動多開了好幾台EC2來應付 , 但是 開EC2也要時間。
這段時間的request資料可能會消失 , 這時候可以,先把這些request的http資訊 先存到SQS , 之後新開的EC2再到SQS 要資料處理。

AWS Kinesis

AWS Kinesis Data Streams – Certification
AWS Kinesis Data Streams vs Kinesis Firehose


上一篇
AWS VPC 、 IAM、KMS
下一篇
Linux ping指令、AWS EC2 Hadoop、SAA-C02
系列文
Linux學習30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言