🚀 Chapter 5 — Platform: Phần B — Deploy Google Online Boutique lên Amazon EKS
Ở Chapter 4G, chúng ta đã hoàn thành quy trình Production Governance:
GitHub
↓
GitHub Actions
↓
Test
↓
Docker Build
↓
Amazon ECR
↓
Security Gate
↓
Artifact Promotion
↓
Production Approval
Tuy nhiên, hệ thống hiện tại vẫn chưa sử dụng Kubernetes. Trong Chapter 5B, chúng ta sẽ tạo một Amazon EKS cluster và deploy ứng dụng Google Online Boutique lên đó.
1. Mục tiêu
Sau khi hoàn thành Chapter này, chúng ta sẽ có:
Developer
↓
GitHub
↓
Amazon EKS
↓
Kubernetes Deployments
↓
Google Online Boutique
Chúng ta sẽ thực hiện:
- Cài đặt các công cụ cần thiết.
- Tạo Amazon EKS cluster.
- Tạo managed node group.
- Kết nối
kubectlvới EKS. - Clone source code Google Online Boutique.
- Deploy các microservice lên EKS.
- Expose frontend ra Internet.
- Kiểm tra trạng thái các Pod.
- Kiểm tra giao tiếp giữa các microservice.
- Xóa tài nguyên để tránh phát sinh chi phí.
Trong Chapter 5B, chúng ta sử dụng Kubernetes manifests trực tiếp. Helm sẽ được giới thiệu ở Chapter tiếp theo.
2. Kiến trúc sau khi hoàn thành
Internet
│
▼
AWS LoadBalancer Service
│
▼
frontend Service
│
▼
frontend Pod
│
┌───────────────────┼───────────────────┐
▼ ▼ ▼
cartservice productcatalogservice checkoutservice
│ │ │
▼ ▼ ▼
cartservice Pod productcatalog Pod checkoutservice Pod
│
▼
Các microservice khác
Amazon EKS sẽ quản lý Kubernetes control plane. Các workload của Online Boutique sẽ chạy trên worker nodes thuộc managed node group.
3. Chuẩn bị môi trường
3.1. Yêu cầu
Bạn cần có:
- AWS Account.
- AWS CLI đã cấu hình.
- Quyền tạo EKS, EC2, VPC, IAM và CloudFormation.
- Git.
kubectl.eksctl.- Một máy local hoặc EC2 dùng để chạy lệnh quản trị cluster.
Amazon EKS có thể tạo cluster bằng AWS Console, AWS CLI hoặc
eksctl. Trong lab này, chúng ta sử dụngeksctlđể giảm số lượng bước cấu hình thủ công.
3.2. Kiểm tra AWS CLI
aws --version
Kiểm tra AWS Account:
aws sts get-caller-identity
Ví dụ kết quả:
{
"UserId": "AIDAXXXXXXXXXXXXX",
"Account": "123456789012",
"Arn": "arn:aws:iam::123456789012:user/dev-user"
}
Cấu hình Region:
aws configure set region ap-northeast-1
Kiểm tra:
aws configure get region
Kết quả:
ap-northeast-1
Trong Chapter này, chúng ta sử dụng Region:
ap-northeast-1
Đây là Region Tokyo.
4. Cài đặt kubectl
4.1. Kiểm tra kubectl
kubectl version --client
Nếu đã cài đặt, bạn sẽ thấy thông tin tương tự:
Client Version: v1.xx.x
4.2. Cài đặt kubectl trên Linux
Tải phiên bản stable:
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
Cấp quyền thực thi:
chmod +x kubectl
Di chuyển binary:
sudo mv kubectl /usr/local/bin/kubectl
Kiểm tra:
kubectl version --client
Nếu bạn sử dụng macOS Apple Silicon, hãy tải binary
darwin/arm64thay vìlinux/amd64.
5. Cài đặt eksctl
5.1. Tải eksctl trên Linux x86_64
ARCH=amd64
PLATFORM=$(uname -s)_$ARCH
curl -sL "https://github.com/eksctl-io/eksctl/releases/latest/download/eksctl_$PLATFORM.tar.gz" \
| tar xz -C /tmp
sudo mv /tmp/eksctl /usr/local/bin
Kiểm tra:
eksctl version
Kết quả sẽ tương tự:
0.xxx.x
5.2. Kiểm tra các công cụ
aws --version
kubectl version --client
eksctl version
6. Tạo Amazon EKS cluster
6.1. Đặt biến môi trường
export AWS_REGION=ap-northeast-1
export CLUSTER_NAME=online-boutique-eks
Kiểm tra:
echo "$AWS_REGION"
echo "$CLUSTER_NAME"
Kết quả:
ap-northeast-1
online-boutique-eks
6.2. Tạo cluster và managed node group
Chạy:
eksctl create cluster \
--name "$CLUSTER_NAME" \
--region "$AWS_REGION" \
--nodegroup-name online-boutique-ng \
--node-type t3.medium \
--nodes 2 \
--nodes-min 1 \
--nodes-max 3 \
--managed
Giải thích các tham số:
--name Tên EKS cluster
--region AWS Region
--nodegroup-name Tên managed node group
--node-type EC2 instance type
--nodes Số node ban đầu
--nodes-min Số node tối thiểu
--nodes-max Số node tối đa
--managed Sử dụng EKS managed node group
Quá trình tạo cluster có thể mất nhiều phút.
Kết quả cuối cùng sẽ tương tự:
[✓] EKS cluster "online-boutique-eks" in "ap-northeast-1" region is ready
t3.mediumđược sử dụng vì Online Boutique có nhiều microservice. Nếu chỉ muốn kiểm tra nhanh, bạn có thể thửt3.small, nhưng có thể gặp tình trạng thiếu CPU hoặc memory khi chạy toàn bộ ứng dụng.
7. Kiểm tra EKS cluster
7.1. Kiểm tra cluster bằng AWS CLI
aws eks describe-cluster \
--name "$CLUSTER_NAME" \
--region "$AWS_REGION" \
--query 'cluster.status' \
--output text
Kết quả mong đợi:
ACTIVE
7.2. Kiểm tra node
kubectl get nodes
Ví dụ:
NAME STATUS ROLES AGE VERSION
ip-192-168-10-101.ap-northeast-1.compute.internal Ready <none> 5m v1.xx.x
ip-192-168-20-102.ap-northeast-1.compute.internal Ready <none> 5m v1.xx.x
Tất cả node cần có trạng thái:
Ready
7.3. Kiểm tra system Pods
kubectl get pods -n kube-system
Các Pod hệ thống thường bao gồm:
coredns
kube-proxy
aws-node
Kiểm tra service mặc định:
kubectl get svc
Kết quả:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S)
kubernetes ClusterIP 10.xxx.x.x <none> 443/TCP
8. Tạo namespace cho Online Boutique
Tạo namespace:
kubectl create namespace online-boutique
Kiểm tra:
kubectl get namespaces
Kết quả:
NAME STATUS
default Active
kube-system Active
kube-public Active
kube-node-lease Active
online-boutique Active
Đặt namespace mặc định cho context hiện tại:
kubectl config set-context --current \
--namespace=online-boutique
Kiểm tra context:
kubectl config view --minify --output 'jsonpath={..namespace}'
Kết quả:
online-boutique
9. Clone Google Online Boutique
Clone repository:
git clone https://github.com/GoogleCloudPlatform/microservices-demo.git
Di chuyển vào thư mục:
cd microservices-demo
Kiểm tra source code:
ls
Bạn sẽ thấy các thư mục và file tương tự:
README.md
src
release
helm-chart
protos
terraform
Kiểm tra Kubernetes manifest:
ls -l release/
Manifest chính của ứng dụng:
release/kubernetes-manifests.yaml
File này chứa các Kubernetes resources để deploy Online Boutique.
10. Kiểm tra manifest trước khi deploy
Kiểm tra các resource trong manifest:
grep '^kind:' release/kubernetes-manifests.yaml
Bạn sẽ thấy các loại resource như:
kind: Deployment
kind: Service
Kiểm tra image được sử dụng:
grep 'image:' release/kubernetes-manifests.yaml
Online Boutique sử dụng các image đã được build sẵn cho các microservice.
Ở bước này, chúng ta deploy bản Online Boutique sample để kiểm tra EKS và Kubernetes. Việc thay thế toàn bộ image bằng image đã được governance trong ECR sẽ được triển khai ở phần GitOps/ECR integration sau.
11. Deploy Online Boutique lên EKS
11.1. Apply manifest
Nếu bạn đang ở namespace online-boutique, chạy:
kubectl apply -f release/kubernetes-manifests.yaml
Nếu muốn chỉ rõ namespace:
kubectl apply \
-n online-boutique \
-f release/kubernetes-manifests.yaml
Kết quả tương tự:
deployment.apps/emailservice created
service/emailservice created
deployment.apps/checkoutservice created
service/checkoutservice created
deployment.apps/paymentservice created
service/paymentservice created
deployment.apps/productcatalogservice created
service/productcatalogservice created
deployment.apps/cartservice created
service/cartservice created
deployment.apps/frontend created
service/frontend created
12. Kiểm tra các Deployment
kubectl get deployments
Ví dụ:
NAME READY UP-TO-DATE AVAILABLE AGE
adservice 1/1 1 1 2m
cartservice 1/1 1 1 2m
checkoutservice 1/1 1 1 2m
currencyservice 1/1 1 1 2m
emailservice 1/1 1 1 2m
frontend 1/1 1 1 2m
paymentservice 1/1 1 1 2m
productcatalogservice 1/1 1 1 2m
recommendationservice 1/1 1 1 2m
shippingservice 1/1 1 1 2m
Trạng thái mong đợi:
READY = AVAILABLE
13. Kiểm tra các Pod
kubectl get pods
Ví dụ:
NAME READY STATUS RESTARTS AGE
adservice-xxxxxxxxxx-xxxxx 1/1 Running 0 3m
cartservice-xxxxxxxxxx-xxxxx 1/1 Running 0 3m
checkoutservice-xxxxxxxxxx-xxxxx 1/1 Running 0 3m
currencyservice-xxxxxxxxxx-xxxxx 1/1 Running 0 3m
emailservice-xxxxxxxxxx-xxxxx 1/1 Running 0 3m
frontend-xxxxxxxxxx-xxxxx 1/1 Running 0 3m
paymentservice-xxxxxxxxxx-xxxxx 1/1 Running 0 3m
productcatalogservice-xxxxxxxxxx-xxxxx 1/1 Running 0 3m
recommendationservice-xxxxxxxxxx-xxxxx 1/1 Running 0 3m
shippingservice-xxxxxxxxxx-xxxxx 1/1 Running 0 3m
Theo dõi realtime:
kubectl get pods -w
Dừng theo dõi bằng:
Ctrl + C
Nếu Pod không ở trạng thái Running, kiểm tra:
kubectl describe pod <POD_NAME>
Ví dụ:
kubectl describe pod frontend-xxxxxxxxxx-xxxxx
Xem logs:
kubectl logs deployment/frontend
14. Kiểm tra các Service
kubectl get services
Ví dụ:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S)
adservice ClusterIP 10.xxx.x.x <none> 9555/TCP
cartservice ClusterIP 10.xxx.x.x <none> 7070/TCP
checkoutservice ClusterIP 10.xxx.x.x <none> 5050/TCP
currencyservice ClusterIP 10.xxx.x.x <none> 7000/TCP
emailservice ClusterIP 10.xxx.x.x <none> 8080/TCP
frontend LoadBalancer 10.xxx.x.x <pending> 80:xxxxx/TCP
paymentservice ClusterIP 10.xxx.x.x <none> 50051/TCP
productcatalogservice ClusterIP 10.xxx.x.x <none> 3550/TCP
recommendationservice ClusterIP 10.xxx.x.x <none> 8080/TCP
shippingservice ClusterIP 10.xxx.x.x <none> 50051/TCP
Điểm cần chú ý:
frontend LoadBalancer
Các service còn lại ClusterIP
Điều này có nghĩa:
frontendcó thể được truy cập từ bên ngoài cluster.- Các microservice khác chỉ được truy cập nội bộ trong Kubernetes cluster.
15. Lấy địa chỉ truy cập Online Boutique
Chạy:
kubectl get service frontend
Ban đầu, EXTERNAL-IP có thể hiển thị:
<pending>
AWS cần một khoảng thời gian để tạo Load Balancer.
Theo dõi liên tục:
kubectl get service frontend -w
Khi hoàn tất, kết quả sẽ tương tự:
NAME TYPE CLUSTER-IP EXTERNAL-IP
frontend LoadBalancer 10.xxx.x.x xxx.ap-northeast-1.elb.amazonaws.com
Lấy riêng địa chỉ Load Balancer:
kubectl get service frontend \
-o jsonpath='{.status.loadBalancer.ingress[0].hostname}'
Lưu kết quả vào biến:
export FRONTEND_URL=$(kubectl get service frontend \
-o jsonpath='{.status.loadBalancer.ingress[0].hostname}')
Kiểm tra:
echo "$FRONTEND_URL"
Mở trên trình duyệt:
http://<EXTERNAL-HOSTNAME>
Hoặc kiểm tra bằng curl:
curl -I "http://$FRONTEND_URL"
Kết quả mong đợi:
HTTP/1.1 200 OK
16. Kiểm tra giao tiếp giữa các microservice
Kubernetes cung cấp DNS nội bộ cho các Service.
Ví dụ, frontend có thể gọi:
cartservice:7070
productcatalogservice:3550
checkoutservice:5050
Kiểm tra DNS Service:
kubectl get svc cartservice
kubectl get svc productcatalogservice
kubectl get svc checkoutservice
Kiểm tra endpoint:
kubectl get endpoints
Hoặc:
kubectl get endpointslice
Kiểm tra thông tin của frontend:
kubectl describe deployment frontend
Kiểm tra logs frontend:
kubectl logs deployment/frontend --tail=100
Nếu frontend có thể hiển thị danh sách sản phẩm, giỏ hàng và checkout, điều đó cho thấy các service nội bộ đang giao tiếp thành công.
17. Kiểm tra resource usage
Kiểm tra CPU và memory của node:
kubectl top nodes
Kiểm tra resource usage của Pod:
kubectl top pods
Nếu nhận được lỗi:
error: Metrics API not available
thì cluster chưa có Metrics Server. Metrics Server sẽ được triển khai ở phần nâng cao sau.
18. Test scale một microservice
Ví dụ scale frontend lên 2 replicas:
kubectl scale deployment frontend --replicas=2
Kiểm tra:
kubectl get deployment frontend
Kết quả:
NAME READY UP-TO-DATE AVAILABLE
frontend 2/2 2 2
Kiểm tra Pod:
kubectl get pods -l app=frontend
Scale lại về 1 replica:
kubectl scale deployment frontend --replicas=1
Đây chỉ là thao tác scale thủ công. Ở các phần sau, chúng ta sẽ quản lý replica count bằng Helm hoặc GitOps thay vì thay đổi trực tiếp bằng
kubectl.
19. Kiểm tra rollout
Xem lịch sử rollout:
kubectl rollout history deployment/frontend
Kiểm tra trạng thái rollout:
kubectl rollout status deployment/frontend
Xem cấu hình Deployment:
kubectl get deployment frontend -o yaml
Kiểm tra image đang chạy:
kubectl get deployment frontend \
-o jsonpath='{.spec.template.spec.containers[*].image}'
20. Thử cập nhật Deployment
Ví dụ thay đổi số replicas:
kubectl edit deployment frontend
Tìm:
spec:
replicas: 1
Đổi thành:
spec:
replicas: 2
Lưu file và kiểm tra:
kubectl rollout status deployment/frontend
Kiểm tra:
kubectl get pods
Thay đổi trực tiếp bằng
kubectl editchỉ nhằm mục đích học Kubernetes. Khi chuyển sang GitOps, mọi thay đổi sẽ được thực hiện trong Git repository.
21. Kiểm tra toàn bộ hệ thống
Chạy lần lượt:
kubectl get nodes
kubectl get deployments
kubectl get pods
kubectl get services
kubectl get events --sort-by=.metadata.creationTimestamp
Một hệ thống hoạt động bình thường cần có:
Nodes → Ready
Deployments → Available
Pods → Running
Services → Được tạo thành công
frontend → Có External Load Balancer
22. Xóa Online Boutique
Nếu chỉ muốn xóa ứng dụng nhưng giữ lại EKS cluster:
kubectl delete \
-f release/kubernetes-manifests.yaml \
-n online-boutique
Xóa namespace:
kubectl delete namespace online-boutique
Kiểm tra:
kubectl get all -n online-boutique
23. Xóa EKS cluster để tránh chi phí
Nếu đã hoàn thành lab và không cần giữ cluster:
eksctl delete cluster \
--name "$CLUSTER_NAME" \
--region "$AWS_REGION"
Theo dõi quá trình xóa:
eksctl get cluster \
--region "$AWS_REGION"
Khi cluster không còn trong danh sách, quá trình xóa đã hoàn tất.
Kiểm tra thêm trong AWS Console:
- EKS
- EC2
- Load Balancers
- Auto Scaling Groups
- CloudFormation
- VPC
- NAT Gateway
- Elastic IP
Đặc biệt cần kiểm tra NAT Gateway, vì NAT Gateway có thể phát sinh chi phí ngay cả khi không có workload đang chạy.
24. Kết quả đạt được
Sau Chapter 5B, chúng ta đã chuyển Online Boutique từ mô hình container đơn lẻ sang Kubernetes:
Trước Chapter 5B:
EC2
└── Docker Compose
└── Online Boutique
Sau Chapter 5B:
Amazon EKS
├── Deployment
├── Pod
├── Service
└── LoadBalancer
└── Online Boutique
Chúng ta đã học được:
- Tạo Amazon EKS cluster.
- Sử dụng managed node group.
- Kết nối
kubectlvới EKS. - Tạo Kubernetes namespace.
- Deploy nhiều microservice.
- Kiểm tra Pod và Deployment.
- Sử dụng Kubernetes Service.
- Expose frontend qua AWS Load Balancer.
- Scale Deployment.
- Kiểm tra rollout.
- Xóa tài nguyên Kubernetes và EKS.
Tuy nhiên, quy trình hiện tại vẫn còn một số hạn chế:
Developer
↓
kubectl apply
↓
EKS
Các vấn đề còn tồn tại:
- Manifest chưa được đóng gói bằng Helm.
- Deployment vẫn thực hiện thủ công.
- Chưa có GitOps.
- Chưa có Argo CD.
- Chưa có cơ chế tự động đồng bộ từ Git.
- Chưa quản lý nhiều environment.
- Chưa kết nối hoàn chỉnh image promotion từ ECR đến EKS.
Ở phần tiếp theo, chúng ta sẽ giải quyết vấn đề quản lý nhiều YAML file bằng Helm Chart.
Kubernetes YAML
↓
Helm Chart
↓
helm install / helm upgrade
↓
Amazon EKS
All rights reserved