EKS 簡介
Amazon Elastic Kubernetes Service (EKS) 是 AWS 提供的全託管 Kubernetes 服務。它讓使用者無需自行安裝、操作和維護 Kubernetes 控制平面,即可在 AWS 上執行 Kubernetes 應用程式。
EKS 主要特點
- 全託管控制平面:AWS 負責管理 Kubernetes 控制平面的可用性和擴展性
- 高可用性:控制平面跨多個可用區域運行,確保服務穩定
- 安全性:與 AWS IAM 整合,提供細緻的存取控制
- 生態系統整合:與 AWS 服務(如 ELB、CloudWatch、IAM)無縫整合
建立叢集步驟
前置準備
在開始之前,請確保已安裝以下工具:
1
2
3
4
5
6
7
8
9
10
11
12
| # 安裝 AWS CLI
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
# 安裝 eksctl
curl --silent --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp
sudo mv /tmp/eksctl /usr/local/bin
# 安裝 kubectl
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
|
使用 eksctl 建立叢集
eksctl 是建立 EKS 叢集最簡單的方式:
1
2
3
4
5
6
7
8
9
10
11
| # 建立基本叢集
eksctl create cluster \
--name my-eks-cluster \
--region ap-northeast-1 \
--version 1.28 \
--nodegroup-name standard-workers \
--node-type t3.medium \
--nodes 3 \
--nodes-min 1 \
--nodes-max 5 \
--managed
|
使用設定檔建立叢集
建立 cluster-config.yaml 設定檔:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig
metadata:
name: my-eks-cluster
region: ap-northeast-1
version: "1.28"
vpc:
cidr: 10.0.0.0/16
nat:
gateway: Single
managedNodeGroups:
- name: standard-workers
instanceType: t3.medium
desiredCapacity: 3
minSize: 1
maxSize: 5
volumeSize: 50
ssh:
allow: true
publicKeyName: my-key-pair
|
執行建立:
1
| eksctl create cluster -f cluster-config.yaml
|
節點群組設定
建立額外的節點群組
1
2
3
4
5
6
7
| eksctl create nodegroup \
--cluster my-eks-cluster \
--name gpu-workers \
--node-type p3.2xlarge \
--nodes 2 \
--nodes-min 1 \
--nodes-max 4
|
節點群組設定檔範例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig
metadata:
name: my-eks-cluster
region: ap-northeast-1
managedNodeGroups:
- name: standard-workers
instanceType: t3.medium
desiredCapacity: 3
labels:
role: standard
taints: []
- name: compute-workers
instanceType: c5.xlarge
desiredCapacity: 2
labels:
role: compute
taints:
- key: workload
value: compute
effect: NoSchedule
|
使用 Spot 實例降低成本
1
2
3
4
5
6
7
| managedNodeGroups:
- name: spot-workers
instanceTypes:
- t3.medium
- t3.large
spot: true
desiredCapacity: 3
|
kubectl 設定
更新 kubeconfig
建立叢集後,需要更新本地的 kubeconfig 以連接叢集:
1
2
3
4
5
6
| # 自動更新 kubeconfig
aws eks update-kubeconfig --region ap-northeast-1 --name my-eks-cluster
# 驗證連接
kubectl get nodes
kubectl cluster-info
|
設定 kubectl 別名(選用)
為了提高工作效率,可以設定常用的別名:
1
2
3
4
5
6
| # 加入 ~/.bashrc 或 ~/.zshrc
alias k='kubectl'
alias kgp='kubectl get pods'
alias kgs='kubectl get services'
alias kgn='kubectl get nodes'
alias kaf='kubectl apply -f'
|
設定多叢集存取
1
2
3
4
5
| # 列出所有 context
kubectl config get-contexts
# 切換 context
kubectl config use-context arn:aws:eks:ap-northeast-1:123456789012:cluster/my-eks-cluster
|
部署應用程式
建立 Namespace
1
| kubectl create namespace my-app
|
部署範例應用程式
建立 deployment.yaml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: my-app
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.25
ports:
- containerPort: 80
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 200m
memory: 256Mi
|
建立 Service
建立 service.yaml:
1
2
3
4
5
6
7
8
9
10
11
12
13
| apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: my-app
spec:
type: LoadBalancer
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
|
部署應用程式
1
2
3
4
5
6
7
8
9
10
11
| # 部署 Deployment 和 Service
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
# 查看部署狀態
kubectl get deployments -n my-app
kubectl get pods -n my-app
kubectl get services -n my-app
# 取得 LoadBalancer 的外部 IP
kubectl get svc nginx-service -n my-app -o jsonpath='{.status.loadBalancer.ingress[0].hostname}'
|
使用 Ingress Controller
安裝 AWS Load Balancer Controller:
1
2
3
4
5
6
7
8
9
| # 安裝 AWS Load Balancer Controller
helm repo add eks https://aws.github.io/eks-charts
helm repo update
helm install aws-load-balancer-controller eks/aws-load-balancer-controller \
-n kube-system \
--set clusterName=my-eks-cluster \
--set serviceAccount.create=false \
--set serviceAccount.name=aws-load-balancer-controller
|
建立 Ingress 資源:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
namespace: my-app
annotations:
kubernetes.io/ingress.class: alb
alb.ingress.kubernetes.io/scheme: internet-facing
alb.ingress.kubernetes.io/target-type: ip
spec:
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-service
port:
number: 80
|
總結
AWS EKS 提供了一個強大且易於管理的 Kubernetes 平台。透過本文介紹的步驟,您可以:
- 快速建立 EKS 叢集
- 靈活配置節點群組
- 正確設定 kubectl 存取
- 部署和管理應用程式
建議在正式環境中,進一步考慮以下面向:
- 設定適當的 IAM 角色和政策
- 配置 VPC 網路和安全群組
- 啟用日誌記錄和監控
- 實施備份和災難復原策略