사전 지식
Concept
- 콘텐츠를 효율적으로 전달하기 위해 여러 노드를 가진 네트워크에 데이터를 저장하여 제공하는 시스템
- CDN을 통해 온라인 상의 대용량 콘텐츠를 저렴한 비용으로 빠르게 전송하도록 한다.
- 주로 ISP(Internet Services Provider)의 CDN 서버에 콘텐츠를 분산시키고 유저의 네트워크 경로상 가장 가까운 곳의 서버로부터 콘텐츠를 전송받도록 하여 트래픽이 특정 서버에 집중되지 않고 각 지역 서버로 분산 되도록 하는 기술이다.
동작 원리
- 콘텐츠에 대한 요청이 발생하게 되면 사용자(End User)와 가장 가까운 위치에 배치된 CDN 서버로 사용자를 접속시키게 되며, CDN서버는 요청된 파일의 _캐싱 콘텐츠_를 사용자에게 전달하게 된다.
- 서버가 파일을 찾는데 실패했거나 콘텐츠가 너무 오래된 경우에는 원본 서버에서 파일을 조회하여 사용자에게 전달하며, 이후 동일한 콘텐츠를 요청받게 되면 캐싱된 데이터에서 콘텐츠를 전송하므로 보다 빠르게 전달할 수 있다.
CDN 캐싱 방식의 종류
Static Caching
- 사용자의 요청이 없어도 Origin에 있는 Contents를 운영자가 미리 Cache Service에 복사함으로써 사용자가 Cache 서버에 접속하여 Contents를 요청하면 Cache 서버가 콘텐츠를 전달하는 방식
Dynamic Caching
- 최초에는 Cache 서버에 콘텐츠가 없으나, 사용자가 콘텐츠를 요청하면 Cache 서버에 콘텐츠가 있는지 여부를 확인한다.
- 없으면 Origin 서버에 다운받아 사용자에게 전달하고, 이후 동일한 요청을 받게 되면 캐싱된 콘텐츠를 사용자에게 제공하게 된다.
- 콘텐츠는 일정한시간이 지나면 캐싱된 파일이 삭제될 수 있지만, 필요한 경우 다시 오리진 서버에서 콘텐츠를 확인 후 계속 가지고 있을 수 있다.
- 글로벌 CDN 업체에서 이 방식을 지원한다.
백엔드 유형
- 인스턴스 그룹
- MIG / UMIG
- MIG / UMIG
- 영역 네트워크 엔드포인트 그룹(NEG)
- 서버리스 NEG: 하나 이상의 App Engine, Cloud Run 또는 Cloud Functions 서비스
- 외부 인터넷용 인터넷 NEG
- 외부 백엔드: Google Cloud 외부에 위치하며 인터넷을 통해 연결할 수 있는 백엔드입니다. 인터넷 NEG의 엔드포인트입니다.
- 외부 백엔드: Google Cloud 외부에 위치하며 인터넷을 통해 연결할 수 있는 백엔드입니다. 인터넷 NEG의 엔드포인트입니다.
- Cloud Storage의 버킷
Cloud CDN 구성 - Cloud Storage 백엔드 버킷
Cloud Storage 버킷 만들기
- 여러 리전에 자동으로 객체를 복제하는 멀티 리전 버킷 권장
파일 복사
gsutil cp gs://gcp-external-http-lb-with-bucket/three-cats.jpg gs://BUCKET_NAME/never-fetch/
gsutil cp gs://gcp-external-http-lb-with-bucket/three-cats.jpg gs://cdn-jm-han/never-fetch/
Cloud Storage 버킷을 공개로 설정
- 설정을 사용하면 인터넷의 모든 사용자가 ACL을 제외한 객체와 메타데이터를 보고 나열할 수 있습니다.
외부 IP 주소 예약
- 이제 Cloud Storage 버킷이 준비되어 실행 중이므로 고객이 부하 분산기에 도달하기 위해 사용하는 전역 고정 외부 IP 주소를 설정합니다.
- 외부 고정 주소는 도메인을 가리키는 단일 주소를 제공하므로 이 단계는 선택사항이지만 _권장_됩니다.
gcloud compute addresses create cdn-ip-jm-han --project=jmhan-int-220517 --global
외부 HTTP(S) 부하 분산기 만들기
백엔드 구성 및 Cloud CDN 사용 설정
- 캐시모드
- 정적 콘텐츠
- 캐시 불가능이 될 수 없는 정적 콘텐츠가 있는 성공적인 응답을 자동으로 캐시합니다. 유효한 캐싱 지시문을 설정하는 원본 응답도 캐시됩니다.
- Cache-Control 헤더를 기준으로 출처 설정 사용
- 유효한 캐시 지시어 및 캐싱 헤더예(e.g. 'Cache-Control: public, max_age=3600')를 설정하기 위해 성공적인 원본 응답을 요구합니다. 이 지시어가 없는 성공적인 응답은 원본에서 전달됩니다.
- 모든 콘텐츠 강제 캐시
- 원본으로 설정된 캐시 지시문을 무효화하고 성공적인 응답을 무조건적으로 캐시합니다. 백엔드가 동적 HTML 또는 API 응답과 같은 사용자별 비공개 콘텐츠를 제공하는 경우에는 이 모드가 적절하지 않습니다.
- 정적 콘텐츠
프런트엔드 구성
- IP 주소: 위에서 생성한 고정 주소 선택
백엔드 버킷으로 트래픽 전송 (CDN 사용하기)
- IP 주소 확인
- 외부 IP 주소 이름: cdn-ip-jm-han
$gcloud compute addresses describe cdn-ip-jm-han --format="get(address)" --global
35.241.29.12
- 브라우저에서 설정한 CDN 주소로 접속 (e.g. http://35.241.29.12/never-fetch/three-cats.jpg)
Cloud CDN 작동 확인
No CACHE
CACHE
- 캐시 적중
- httpRequest.cacheHit=true
- jsonPayload.statusDetails="response_from_cache"
- 캐시 부적중
- jsonPayload.statusDetails="response_sent_by_backend"
- 캐시 응답
- cacheId (e.g. ICN-b40e0f32)
- ICN: IATA airport code (인천
- b40e0f32 : 캐시 인스턴스의 불투명 식별자, 일부 Cloud CDN 위치에는 개별 캐시가 여러 개 있기 때문
- cacheId (e.g. ICN-b40e0f32)
속도 비교 - CDN VS Cloud Storage VS WEB
서비스를 서울에서 운영한다고 가정, 서울과 상파울로에서 파일 다운로드 차이를 비교해 본다.
WEB(Nginx)
http://34.64.37.6/KakaoTalk_20220718_104236455.mp4
Cloud Storage
http://storage.googleapis.com/cdn-jm-han/never-fetch/KakaoTalk_20220718_104236455.mp4
Cloud CDN
http://35.241.29.12/never-fetch/KakaoTalk_20220718_104236455.mp4
서울 (34.64.61.122)
WEB
[jm.han@test-cdn-asia-northeast3 ~]$ curl http://34.64.37.6/KakaoTalk_20220718_104236455.mp4 --output web.mp4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24.4M 100 24.4M 0 0 129M 0 --:--:-- --:--:-- --:--:-- 129M
[jm.han@test-cdn-asia-northeast3 ~]$ curl http://34.64.37.6/KakaoTalk_20220718_104236455.mp4 --output web.mp4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24.4M 100 24.4M 0 0 112M 0 --:--:-- --:--:-- --:--:-- 113M
[jm.han@test-cdn-asia-northeast3 ~]$ curl http://34.64.37.6/KakaoTalk_20220718_104236455.mp4 --output web.mp4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24.4M 100 24.4M 0 0 112M 0 --:--:-- --:--:-- --:--:-- 113M
[jm.han@test-cdn-asia-northeast3 ~]$ curl http://34.64.37.6/KakaoTalk_20220718_104236455.mp4 --output web.mp4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24.4M 100 24.4M 0 0 120M 0 --:--:-- --:--:-- --:--:-- 121M
- 129 M
- 112 M
- 112 M
- 120 M
Cloud Storage
[jm.han@test-cdn-asia-northeast3 ~]$ curl http://storage.googleapis.com/cdn-jm-han/never-fetch/KakaoTalk_20220718_104236455.mp4 --output storage.mp4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24.4M 100 24.4M 0 0 34.5M 0 --:--:-- --:--:-- --:--:-- 34.5M
[jm.han@test-cdn-asia-northeast3 ~]$ curl http://storage.googleapis.com/cdn-jm-han/never-fetch/KakaoTalk_20220718_104236455.mp4 --output storage.mp4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24.4M 100 24.4M 0 0 38.0M 0 --:--:-- --:--:-- --:--:-- 38.0M
[jm.han@test-cdn-asia-northeast3 ~]$ curl http://storage.googleapis.com/cdn-jm-han/never-fetch/KakaoTalk_20220718_104236455.mp4 --output storage.mp4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24.4M 100 24.4M 0 0 56.4M 0 --:--:-- --:--:-- --:--:-- 56.5M
[jm.han@test-cdn-asia-northeast3 ~]$ curl http://storage.googleapis.com/cdn-jm-han/never-fetch/KakaoTalk_20220718_104236455.mp4 --output storage.mp4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24.4M 100 24.4M 0 0 42.0M 0 --:--:-- --:--:-- --:--:-- 42.0M
- 34.5 M
- 38.0 M
- 56.4 M
- 42.0 M
CDN
[jm.han@test-cdn-asia-northeast3 ~]$ curl http://35.241.29.12/never-fetch/KakaoTalk_20220718_104236455.mp4 --output cdn.mp4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24.4M 100 24.4M 0 0 67.3M 0 --:--:-- --:--:-- --:--:-- 67.4M
[jm.han@test-cdn-asia-northeast3 ~]$ curl http://35.241.29.12/never-fetch/KakaoTalk_20220718_104236455.mp4 --output cdn.mp4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24.4M 100 24.4M 0 0 171M 0 --:--:-- --:--:-- --:--:-- 173M
[jm.han@test-cdn-asia-northeast3 ~]$ curl http://35.241.29.12/never-fetch/KakaoTalk_20220718_104236455.mp4 --output cdn.mp4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24.4M 100 24.4M 0 0 181M 0 --:--:-- --:--:-- --:--:-- 182M
[jm.han@test-cdn-asia-northeast3 ~]$ curl http://35.241.29.12/never-fetch/KakaoTalk_20220718_104236455.mp4 --output cdn.mp4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24.4M 100 24.4M 0 0 173M 0 --:--:-- --:--:-- --:--:-- 174M
- 67.3 M
- 171 M
- 181 M
- 173 M
상파울로 (34.95.220.206)
WEB
[jm.han@test-cdn-southamerica-east ~]$ curl http://34.64.37.6/KakaoTalk_20220718_104236455.mp4 --output web.mp4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24.4M 100 24.4M 0 0 4344k 0 0:00:05 0:00:05 --:--:-- 5770k
[jm.han@test-cdn-southamerica-east ~]$ curl http://34.64.37.6/KakaoTalk_20220718_104236455.mp4 --output web.mp4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24.4M 100 24.4M 0 0 4377k 0 0:00:05 0:00:05 --:--:-- 5810k
[jm.han@test-cdn-southamerica-east ~]$ curl http://34.64.37.6/KakaoTalk_20220718_104236455.mp4 --output web.mp4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24.4M 100 24.4M 0 0 4388k 0 0:00:05 0:00:05 --:--:-- 6211k
[jm.han@test-cdn-southamerica-east ~]$ curl http://34.64.37.6/KakaoTalk_20220718_104236455.mp4 --output web.mp4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24.4M 100 24.4M 0 0 4406k 0 0:00:05 0:00:05 --:--:-- 5850k
- 4344 K
- 4377 K
- 4388 K
- 4406 K
Cloud Storage
[jm.han@test-cdn-southamerica-east ~]$ curl http://storage.googleapis.com/cdn-jm-han/never-fetch/KakaoTalk_20220718_104236455.mp4 --output storage.mp4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24.4M 100 24.4M 0 0 55.5M 0 --:--:-- --:--:-- --:--:-- 55.4M
[jm.han@test-cdn-southamerica-east ~]$ curl http://storage.googleapis.com/cdn-jm-han/never-fetch/KakaoTalk_20220718_104236455.mp4 --output storage.mp4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24.4M 100 24.4M 0 0 58.0M 0 --:--:-- --:--:-- --:--:-- 58.1M
[jm.han@test-cdn-southamerica-east ~]$ curl http://storage.googleapis.com/cdn-jm-han/never-fetch/KakaoTalk_20220718_104236455.mp4 --output storage.mp4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24.4M 100 24.4M 0 0 56.1M 0 --:--:-- --:--:-- --:--:-- 56.1M
[jm.han@test-cdn-southamerica-east ~]$ curl http://storage.googleapis.com/cdn-jm-han/never-fetch/KakaoTalk_20220718_104236455.mp4 --output storage.mp4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24.4M 100 24.4M 0 0 59.9M 0 --:--:-- --:--:-- --:--:-- 60.1M
- 55.5 M
- 58.0 M
- 56.1 M
- 59.9 M
CDN
[jm.han@test-cdn-southamerica-east ~]$ curl http://35.241.29.12/never-fetch/KakaoTalk_20220718_104236455.mp4 --output cdn.mp4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24.4M 100 24.4M 0 0 87.3M 0 --:--:-- --:--:-- --:--:-- 87.4M
[jm.han@test-cdn-southamerica-east ~]$ curl http://35.241.29.12/never-fetch/KakaoTalk_20220718_104236455.mp4 --output cdn.mp4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24.4M 100 24.4M 0 0 165M 0 --:--:-- --:--:-- --:--:-- 167M
[jm.han@test-cdn-southamerica-east ~]$ curl http://35.241.29.12/never-fetch/KakaoTalk_20220718_104236455.mp4 --output cdn.mp4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24.4M 100 24.4M 0 0 166M 0 --:--:-- --:--:-- --:--:-- 166M
[jm.han@test-cdn-southamerica-east ~]$ curl http://35.241.29.12/never-fetch/KakaoTalk_20220718_104236455.mp4 --output cdn.mp4
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24.4M 100 24.4M 0 0 176M 0 --:--:-- --:--:-- --:--:-- 176M
- 87.3M
- 165M
- 166M
- 176M
잘 사용 하는 방법 ?
- 서비스 파악
- 서비스 지역? 내부 서비스 ?
- 캐시 적중률 향상 (캐시 적중률 최적화 gcp doc link)
- 캐시 기간 지정
- 길면 길수록 캐시 적중률이 높아진다. 다만, 오리진이 아닌 캐시된 파일을 불러오기에 객체 최신화에 문제가 발생할 수 있다.
- 커스텀 캐시 키를 사용하여 캐시 적중률 향상
- 캐시 키를 사용자 정의하여 키에서 프로토콜을 생략합니다. (기본적으로 전체 URL을 캐시 키로 만든다)
- 사용자별 콘텐츠를 캐시하지 않음
- 경우에 따라 브라우저에서 사용자별 콘텐츠를 캐시할 수 있습니다. 사용자별 콘텐츠를 캐시하는 데 Cloud CDN을 사용하지 마세요.
- 캐시 기간 지정
CDN 개요
https://cloud.google.com/cdn/docs/setting-up-cdn-with-bucket
curl 속도
https://superuser.com/questions/951056/what-units-does-curl-use-for-bandwidth
캐시 적중률 향상
https://cloud.google.com/cdn/docs/best-practices#cache-hit-ratio
'Cloud-computing > GCP' 카테고리의 다른 글
[GCP]GCP Product cheat sheets - Database, Data Analytics (0) | 2022.07.24 |
---|---|
[GCP]GCP Product cheat sheets - Compute, Storage (0) | 2022.07.24 |
[GCP]CloudSQL Server-Client Certificate Configuration (0) | 2022.06.28 |
[GCP]How to Create Managed Instance Group (MIG) (0) | 2022.06.20 |
[GCP]How to Backup/Restore/Export/Import in Cloud SQL (1) | 2022.06.19 |