1 概要
本文依照Amazon EKS 用戶指南記錄一個(gè)小白用戶EKS的入門過程碱工,解釋重點(diǎn)內(nèi)容痛黎、解決遇到的問題枉证。
2 練習(xí)環(huán)境
- mac操作系統(tǒng)棘街,系統(tǒng)預(yù)裝了docker+kubernetes
- AWS賬戶蟆盐,擁有EKS/CloudFormation等權(quán)限
3 EKS創(chuàng)建與管理
3.1 控制臺(tái)和eksctl的入門選擇
我選擇的是控制臺(tái)入門教程,因?yàn)榭梢暂^深入的理解eks資源間是怎樣交互的遭殉。后續(xù)用控制臺(tái)管理也比eksctl方便石挂。
eksctl 入門:此入門指南可幫助您使用
eksctl
(用于在 Amazon EKS 上創(chuàng)建和管理 Kubernetes 集群的簡(jiǎn)單命令行實(shí)用程序)來安裝開始使用 Amazon EKS 所需的所有資源。這是開始使用 Amazon EKS 的最快险污、最簡(jiǎn)單的方式痹愚。
AWS 管理控制臺(tái)入門:此入門指南可幫助您創(chuàng)建要在 AWS 管理控制臺(tái)中開始使用 Amazon EKS 所需的所有資源。在本指南中蛔糯,您將在 Amazon EKS 或 AWS CloudFormation 控制臺(tái)中手動(dòng)創(chuàng)建每個(gè)資源拯腮,而且此處所述的工作流程可幫助您全面了解如何創(chuàng)建每個(gè)資源以及每個(gè)資源之間如何交互;但是蚁飒,這是一種開始使用 Amazon EKS 的更復(fù)雜且耗時(shí)的方式动壤。
3.2 Amazon EKS 先決條件
- kubectl
- 角色權(quán)限、vpc淮逻、子網(wǎng)相關(guān)
重點(diǎn)是kubectl要提前裝好琼懊,在mac玩過docker的應(yīng)該都預(yù)裝了阁簸。
3.3 步驟注意點(diǎn)
步驟 1:創(chuàng)建您的 Amazon EKS 集群
這里沒有需要注意的,創(chuàng)建的是cluster集群哼丈,需要幾分鐘時(shí)間启妹,但不包括worker nodes,節(jié)點(diǎn)需要單獨(dú)創(chuàng)建啟用削祈。
步驟 2:創(chuàng)建 kubeconfig 文件
kubeconfig更新到本地時(shí)默認(rèn)會(huì)merge進(jìn).kube/config中翅溺,遠(yuǎn)程EKS、本地k8s可以使用kubectl context進(jìn)行切換的髓抑,不用擔(dān)心配置文件會(huì)互相干擾咙崎、覆蓋。
步驟 3:?jiǎn)?dòng)和配置 Amazon EKS 工作線程節(jié)點(diǎn)
工作線程節(jié)點(diǎn)worker nodes默認(rèn)是不隨cluster而創(chuàng)建的吨拍,需要使用CloudFormation來創(chuàng)建褪猛,AWS已經(jīng)提供好了創(chuàng)建模版,按模版填好就可以了羹饰。
注意以下幾點(diǎn):
- 要正確填寫對(duì)應(yīng)EKS集群名稱伊滋,這樣才保證節(jié)點(diǎn)最后能加入到集群內(nèi);
- 工作線程節(jié)點(diǎn)最后以Autoscaling Group的形式組織在一起队秩;
- AMI不能任意選擇笑旺,只能選擇AWS針對(duì)EKS提供的AMI,分不同的region馍资;
- 為了將worker nodes加入到cluster中筒主,需要在EKS中配置這些節(jié)點(diǎn)的實(shí)例角色ARN,配好后cluster自動(dòng)添加這些節(jié)點(diǎn)鸟蟹。
3.4 啟動(dòng)留言簿應(yīng)用程序
這個(gè)小例子是教我們使用kubectl部署應(yīng)用的乌妙,挺容易操作的,可以練習(xí)下指令建钥。同時(shí)可比較直觀的理解Node藤韵、Pod、Service熊经、RC這些基本概念泽艘。
4 helm安裝與使用
helm與EKS是無縫結(jié)合的,按照AWS教程即可
了解了下helm客戶端和服務(wù)端tiller的區(qū)別
The Helm Client is a command-line client for end users. The client is responsible for the following domains:
- Local chart development
- Managing repositories
- Interacting with the Tiller server
- Sending charts to be installed
- Asking for information about releases
- Requesting upgrading or uninstalling of existing releases
The Tiller Server is an in-cluster server that interacts with the Helm client, and interfaces with the Kubernetes API server. The server is responsible for the following:
- Listening for incoming requests from the Helm client
- Combining a chart and configuration to build a release
- Installing charts into Kubernetes, and then tracking the subsequent release
- Upgrading and uninstalling charts by interacting with Kubernetes
4.1 helm的安裝
# 參見helm官方社區(qū)的教程直接brew安裝
$ brew install kubernetes-helm
4.2 介紹tiller的部署
tiller是helm的服務(wù)端镐依,helm客戶端指令給到tiller部署EKS上的服務(wù)悉盆。AWS為了安全考慮建議tiller部署在本地,helm與本地的tiller交互馋吗,但缺點(diǎn)是每次helm指令都需要保證tiller服務(wù)在線焕盟。作為初學(xué)者,我直接將tiller部署到EKS上了。
4.3 給tiller先設(shè)定RBAC (Role-based Access Control)
helm init會(huì)將tiller服務(wù)部署好脚翘,同時(shí)測(cè)試helm repo update也成功了灼卢,但是helm ls遇到了如下錯(cuò)誤:
$ helm ls
Error: configmaps is forbidden: User "system:serviceaccount:kube-system:default" cannot list resource "configmaps" in API group "" in the namespace "kube-system"
原因是kubernetes對(duì)部署的服務(wù)有api權(quán)限控制,說白了就是tiller服務(wù)沒有權(quán)限訪問kubernetes的資源来农,需要賦予tiller一個(gè)role且綁定必要的權(quán)限鞋真,來做一下吧。
In Kubernetes, granting a role to an application-specific service account is a best practice to ensure that your application is operating in the scope that you have specified.
創(chuàng)建一個(gè)yaml描述文件沃于,能看到該文件創(chuàng)建了一個(gè)ServiceAccount涩咖,同時(shí)做了一個(gè)ClusterRoleBinding。注意其中name繁莹、namespace取值要填寫正確檩互。
# rbac-config.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: tiller
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: tiller
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: tiller
namespace: kube-system
應(yīng)用該yaml文件
$ kubectl create -f rbac-config.yaml
serviceaccount "tiller" created
clusterrolebinding.rbac.authorization.k8s.io "tiller" created
4.4 helm init初始化tiller
$ helm init --service-account tiller
表明使用上面的含有權(quán)限的賬戶,然后再運(yùn)行不會(huì)報(bào)錯(cuò)了
$ helm ls
上述指令默認(rèn)將tiller安裝到EKS上咨演,同時(shí)tiller-namespace默認(rèn)是kube-system闸昨,可以根據(jù)實(shí)際需求更改為本地部署或使用獨(dú)立的tiller-namespace
4.5 使用helm部署nginx
EKS讓參考helm官網(wǎng)的安裝服務(wù)示例,helm上面的是mysql薄风,但是驗(yàn)證mysql還需要client程序饵较,blabla... 可以使用更簡(jiǎn)單的nginx來玩一下。
$ helm repo add bitnami https://charts.bitnami.com/bitnami
"bitnami" has been added to your repositories
$ helm repo list
NAME URL
stable https://kubernetes-charts.storage.googleapis.com
local http://127.0.0.1:8879/charts
bitnami https://charts.bitnami.com/bitnami
$ helm install --name my-nginx bitnami/nginx
NAME: my-nginx
LAST DEPLOYED: Thu Aug 29 17:58:29 2019
NAMESPACE: default
STATUS: DEPLOYED
... ...
NOTES:
Get the NGINX URL:
NOTE: It may take a few minutes for the LoadBalancer IP to be available.
Watch the status with: 'kubectl get svc --namespace default -w my-nginx'
export SERVICE_IP=$(kubectl get svc --namespace default my-nginx --template "{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}")
echo "NGINX URL: http://$SERVICE_IP/"
最后使用kubectl可以看到部署好的nignx服務(wù)遭赂,不過dns生效需要5分鐘左右的時(shí)間循诉,要耐心等一下。
$ kubectl get svc --namespace default -w my-nginx -o wide 1 ?
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
my-nginx LoadBalancer 10.100.171.65 a8d4d4bdbdbdbd.us-west-2.elb.amazonaws.com 80:31464/TCP 48s app=my-nginx
5 小結(jié)
EKS 教程還是非常不錯(cuò)的撇他,做下來沒遇到什么坑茄猫,最早先接觸EKS是AWS提供的eksworkshop,這個(gè)workshop需要太多的額外工作創(chuàng)建account逆粹、cloud9、vpc炫惩、role等等僻弹,有點(diǎn)啰嗦了。不過這次的用戶指南比較簡(jiǎn)潔他嚷、實(shí)用蹋绽。
EKS 教程以外有許多知識(shí)點(diǎn)需要展開去做練習(xí),如:
- kubectl對(duì)EKS的管理筋蓖,Node卸耘、Pod、Service粘咖、RC等蚣抗;
- helm的使用,安全性部署等瓮下;
Reference
Amazon EKS 用戶指南
helm 與 Amazon EKS 配合使用教程
helm quickstart
kubernetes tasks
bitnami: create your first helm chart
helm hub: nginx
eksworkshop