AWS 透過 kops 安裝 Kubernetes Cluster

2020-12-20 AWS, Kubernetes

在 AWS 管理 Kubernetes 可選 AWS Managed 的 EKS,但若是因為必要情況需要使用 EC2 自架時,kops 會是在 AWS 簡單安裝 Kubernetes 不錯的選擇。

kops is an automated provisioning system:

– Fully automated installation
– Uses DNS to identify clusters
– Self-healing: everything runs in Auto-Scaling Groups
– Multiple OS support (Debian, Ubuntu 16.04 supported, CentOS & RHEL, Amazon Linux and CoreOS) – see the images.md
– High-Availability support – see the high_availability.md
– Can directly provision, or generate terraform manifests – see the terraform.md

不論在官方或者是網路上都已經有許多介紹與使用方法,在這篇作者僅以紀錄或提供還未使用過的人參考。

Environments

在這篇會以 macOS 作為主要操作環境,建置則以 AWS EC2 作為主要平台。

How to use kops create kubernetes cluster

  • install kops on macOS
$ brew update && brew install kops

macOS 環境安裝當然是用最簡單的 brew 安裝 kops

  • 授與 AWS credentials 給 kops
export AWS_ACCESS_KEY_ID=<your-aws-access-key-id>
export AWS_SECRET_ACCESS_KEY=<your-aws-secret-access-key-id>
  • 建立 AWS Route 53 hosted zone
$ aws route53 create-hosted-zone --name kops.shazi.info --caller-reference 1

kops 需要使用 DNS 作為 discovery,而且後續 kops 的操作也都依賴 DNS query。

  • 建立 s3 bucket 儲存 Kubernetes metadata
$ aws s3 mb s3://kubernetes-kops-state-<your-account-id>

kops 建立 Kubernetes cluster 後,會在 s3 bucket 儲存 cluster 的 metadata 作為紀錄,kops 每次的操作都會參考 metadata 作為依據 (類似 Terraform 等 IaC 工具紀錄 resources 的方法)

  • kops 定義 Kubernetes configuration
$ kops create cluster \
    --name=kops.shazi.info \
    --state=s3://kubernetes-state-<your-account-id> \
    --master-size=t3.micro \
    --node-size=t3.micro \
    --node-count=3 \
    --zones="us-east-1a,us-east-1c"

kops create cluster 僅建立 Kubernetes 設定檔並非建立 cluster,這個步驟實際是將 configuration 上傳到 s3 bucket。

常見還會指定 --ssh-public-key--vpc--subnet 指定既有的資源

  • kops 編輯 Kubernetes configuration
$ kops edit cluster --name kops.shazi.info

kops configuration 會產生 YAML 定義 cluster,而 kops edit cluster 可以用來編輯儲存在 s3 bucket 上的 YAML

  • kops 建立 Kubernetes cluster
$ kops update cluster kops.shazi.info --yes

...
Cluster is starting.  It should be ready in a few minutes.

Exporting kubecfg for cluster
kops has set your kubectl context to kops.shazi.info

kops update cluster 加上 --yes 參數後就會開始作動建立 Kubernetes,這個動作會開始建立 AWS EC2 並且自動在 Subnet 打上 Kubernetesd Tags。

  • Testing your Kubernetes cluster
$ kubectl get nodes --show-labels

由於 kops update cluster 已經自動更新 ~/.kube/config 所以可以用 kubectl 來獲取 Kubernetes 的資訊。

  • kops 刪除 Kubernetes cluster
$ kops delete cluster kops.shazi.info --yes

References

給 Mr. 沙先生一點建議

彙整

分類

展開全部 | 收合全部

License

訂閱 Mr. 沙先生 的文章

輸入你的 email 用於訂閱