如何深入理解EKSIAMAuthenticator的實(shí)現(xiàn)機(jī)制

一、k8s Authentication

我們知道 apiserver 有以下幾種認(rèn)證方式:

成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供普安網(wǎng)站建設(shè)、普安做網(wǎng)站、普安網(wǎng)站設(shè)計(jì)、普安網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、普安企業(yè)網(wǎng)站模板建站服務(wù),十多年普安做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

  • X509 Client Certs
  • Static Token File
  • Bootstrap Tokens
  • Static Password File
  • Service Account Tokens
  • OpenID Connect Tokens
  • Webhook Token Authentication
  • Authenticating Proxy

一般我們使用二進(jìn)制自建 k8s 集群,或者使用 kubeadm 創(chuàng)建的集群,集群的管理者,也就是 k8s 中 user 這個(gè)對(duì)象,都是使用的 X509 證書進(jìn)行驗(yàn)證,如果屬于同一個(gè) CA 簽發(fā)的,就認(rèn)可這個(gè)用戶;

企業(yè)為了減少用戶管理的復(fù)雜程度,需要想辦法如何把 IAM 的用戶集成到 EKS 集群中,這樣我們就不需要耽誤為 EKS 創(chuàng)建用戶,減少一套用戶的維護(hù)工作,那這是如何集成的呢,是通過了 k8s 的 Webhook Token Authentication,其認(rèn)證過程架構(gòu)圖如下:

如何深入理解 EKS IAM Authenticator 的實(shí)現(xiàn)機(jī)制

我們可以從圖中看到多了 Authentication 這一部分,通過 aws-iam-authenticator,我們可以知道,這是運(yùn)行在 EKS Control Plane 中的一組 DaemonSet Pod,用來接收 apiserver 的認(rèn)證請(qǐng)求。

二、解剖 EKS

2.1、創(chuàng)建 EKS

我們使用 eksctl 這個(gè)命令創(chuàng)建 EKS 集群,默認(rèn) eksctl 會(huì)調(diào)用 awscli 的 config,所以我們需要先配置好 awscli,相關(guān)的用戶或者角色具有創(chuàng)建 eks 集群的權(quán)限即可:

eksctl create cluster --name eks --region us-east-1 \
--node-type=t2.small --nodes 1 --ssh-public-key .ssh/id_rsa.pub \
--managed --zones us-east-1f,us-east-1c --vpc-nat-mode Disable

集群創(chuàng)建好之后,會(huì)自動(dòng)幫我們配置好 kubeclt 需要的配置文件,并且集群的創(chuàng)建者會(huì)自動(dòng)獲取集群 cluster-admin 的角色,擁有最高權(quán)限。

2.2、引入 aws-iam-authenticator

我們首先可以通過 CloudWatch Logs 去查看 kube-apiserver 的啟動(dòng)參數(shù),我們可以看到有這樣一組啟動(dòng)參數(shù):

--authentication-token-webhook-config-file="/etc/kubernetes/authenticator/apiserver-webhook-kubeconfig.yaml"

說明我們已經(jīng)啟動(dòng)了 webhook 方式的認(rèn)證,那參數(shù)后面的 yaml 文件里面是什么呢,我們可以通過 aws-iam-authenticator 的 github 文檔可以看到,我們使用如下命令進(jìn)行生成:

wangzan:~/k8s $ aws-iam-authenticator init -i `openssl rand 16 -hex`
INFO[2020-01-07T07:50:54Z] generated a new private key and certificate   certBytes=804 keyBytes=1192
INFO[2020-01-07T07:50:54Z] saving new key and certificate                certPath=cert.pem keyPath=key.pem
INFO[2020-01-07T07:50:54Z] loaded existing keypair                       certPath=cert.pem keyPath=key.pem
INFO[2020-01-07T07:50:54Z] writing webhook kubeconfig file               kubeconfigPath=aws-iam-authenticator.kubeconfig
INFO[2020-01-07T07:50:54Z] copy cert.pem to /var/aws-iam-authenticator/cert.pem on kubernetes master node(s) 
INFO[2020-01-07T07:50:54Z] copy key.pem to /var/aws-iam-authenticator/key.pem on kubernetes master node(s) 
INFO[2020-01-07T07:50:54Z] copy aws-iam-authenticator.kubeconfig to /etc/kubernetes/aws-iam-authenticator/kubeconfig.yaml on kubernetes master node(s) 
INFO[2020-01-07T07:50:54Z] configure your apiserver with `--authentication-token-webhook-config-file=/etc/kubernetes/aws-iam-authenticator/kubeconfig.yaml` to enable authentication with aws-iam-authenticator

查看生成的配置文件aws-iam-authenticator.kubeconfig

wangzan:~/k8s $ cat aws-iam-authenticator.kubeconfig 

# clusters refers to the remote service.
clusters:
  - name: aws-iam-authenticator
    cluster:
      certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURJRENDQWdpZ0F3SUJBZ0lRVDRmUllwRy9uSlE0UURmSHUwb2M3ekFOQmdrcWhraUc5dzBCQVFzRkFEQWcKTVI0d0hBWURWUVFERXhWaGQzTXRhV0Z0TFdGMWRHaGxiblJwWTJGMGIzSXdJQmNOTWpBd01UQTNNRGMxTURVMApXaGdQTWpFeE9URXlNVFF3TnpVd05UUmFNQ0F4SGpBY0JnTlZCQU1URldGM2N5MXBZVzB0WVhWMGFHVnVkR2xqCllYUnZjakNDQVNJd0RRWUpLb1pJaHZjTkFRRUJCUUFEZ2dFUEFEQ0NBUW9DZ2dFQkFLRkhTU3ZjWGZFNVZrN3UKejBRNjFGVE1LQS9PeE5HaVlOa05iS0ZzSW9pdW5VejBLdUI4aFBReitEOGxpNnpYZUozWDdNNVNyaG1hYjRBMgowWXJNZEVlcSthYVR5aVhFV2QrREFlUU9HcFFRQm0ya2hvdEgzS2Vrd2dlTjlwbzFUUjBHQVgrbFBQWWloOHNBCml6ZWxBRENLZUMrUS8yWnVZYkttaUFiSm1MNDdqc09nd0pXYldQV0JOdm54VDkrL0wwTHphN1c3MVdieCszaWsKQUdUT2xDWm5tZjY0b0sxQWw4eDNxUktnSXp0bnFBMWRFeEMwS2ZzR1dRRy9Xb00rbnRXZW40QmJEOW1XakF4Zgo0Yk9CaXBOcDMrMlFJOU5LL1pUOTYyaDFZbE8yUWdiYVMrSTZtR2o5WXhwamFXdXgyWGNUd2N0MTFjODFMNWcxCmNaVjZJZ2tDQXdFQUFhTlVNRkl3RGdZRFZSMFBBUUgvQkFRREFnS2tNQk1HQTFVZEpRUU1NQW9HQ0NzR0FRVUYKQndNQk1BOEdBMVVkRXdFQi93UUZNQU1CQWY4d0dnWURWUjBSQkJNd0VZSUpiRzlqWVd4b2IzTjBod1IvQUFBQgpNQTBHQ1NxR1NJYjNEUUVCQ3dVQUE0SUJBUUIyaE1wYm5kTDhVa3VRNVVuZzNkWTh6Y0RQZ2JvbU9Mb0t5dUlzCnVoNkljMUtaMUFiNmVoVDg2bzN2bzJzbnVoNkhibU13NjhvNE9SSllLdTB4RXB3RDR0YUlSVEN1bk9KVHFUeHoKZDZsN2xqdGtBMWZnUktpUHF1SjlyUmxqYXVTMGMxU05lWTVmR2hhUEh4UFBBMHoxN3orMWdiVzNiUjFEckxHQwp6WHVCNStFM0xSVHJCck53aHlZOUtIcVB4Vy9PL1M2V2FZbUR4R3Y4bklNNUUrTzltRFBPWGZpWFJDRFFkb2R5ClRadnU1VDR5ZVhQaEQwRmJKdjg4OSsxZnR4TGlSaUdTQ29lRTNEMUdiYSswZXFMT0hidExUY29VZ25WTGVlMTAKclZDOWRiOU05ckxDZnU1aUEvcGdORm80VTF1ejAvbFUyV0RnYSthUWNNeC9iaHZJCi0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K
      server: https://localhost:21362/authenticate
# users refers to the API Server's webhook configuration
# (we don't need to authenticate the API server).
users:
  - name: apiserver
# kubeconfig files require a context. Provide one for the API Server.
current-context: webhook
contexts:
- name: webhook
  context:
    cluster: aws-iam-authenticator
    user: apiserver

2.3、整個(gè) IAM 認(rèn)證過程

首先,我們查看一下 kubectl 的配置文件信息:

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://93BEE997ED0F1C1BA3BD6C8395BE0756.sk1.us-east-1.eks.amazonaws.com
  name: eks.us-east-1.eksctl.io
contexts:
- context:
    cluster: eks.us-east-1.eksctl.io
    user: wangzan@eks.us-east-1.eksctl.io
  name: wangzan@eks.us-east-1.eksctl.io
current-context: wangzan@eks.us-east-1.eksctl.io
kind: Config
preferences: {}
users:
- name: wangzan@eks.us-east-1.eksctl.io
  user:
    exec:
      apiVersion: client.authentication.k8s.io/v1alpha1
      args:
      - token
      - -i
      - eks
      command: aws-iam-authenticator
      env: null

我們可以看到 user 字段,這里不是使用證書進(jìn)行認(rèn)證的,而是使用的 aws-iam-authenticator client,其命令如下:

wangzan:~ $ aws-iam-authenticator token -i eks
{"kind":"ExecCredential","apiVersion":"client.authentication.k8s.io/v1alpha1","spec":{},"status":{"expirationTimestamp":"2020-01-07T08:23:23Z","token":"k8s-aws-v1.aHR0cHM6Ly9zdHMuYW1hem9uYXdzLmNvbS8_QWN0aW9uPUdldENhbGxlcklkZW50aXR5JlZlcnNpb249MjAxMS0wNi0xNSZYLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUE1TkFHSEY2TllYU01DTEhPJTJGMjAyMDAxMDclMkZ1cy1lYXN0LTElMkZzdHMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDIwMDEwN1QwODA5MjNaJlgtQW16LUV4cGlyZXM9MCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QlM0J4LWs4cy1hd3MtaWQmWC1BbXotU2lnbmF0dXJlPTU2MjA5OTZhY2MzZGE3OWI3OGI0NDVjOTVkMTMyNmU0NjZmNTUyZTMzNDdkN2Y5MmExNGUwMzcwOTJiMzdmMDY"}}

這里其實(shí)是向 sts 獲取一個(gè)臨時(shí)的 token,用作身份憑證,這個(gè)命令也等同于下面的命令:

wangzan:~ $ aws eks get-token --cluster-name eks 
{"status": {"expirationTimestamp": "2020-01-07T08:25:38Z", "token": "k8s-aws-v1.aHR0cHM6Ly9zdHMuYW1hem9uYXdzLmNvbS8_QWN0aW9uPUdldENhbGxlcklkZW50aXR5JlZlcnNpb249MjAxMS0wNi0xNSZYLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1FeHBpcmVzPTYwJlgtQW16LURhdGU9MjAyMDAxMDdUMDgxMTM4WiZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QlM0J4LWs4cy1hd3MtaWQmWC1BbXotU2VjdXJpdHktVG9rZW49JlgtQW16LUNyZWRlbnRpYWw9QUtJQTVOQUdIRjZOWVhTTUNMSE8lMkYyMDIwMDEwNyUyRnVzLWVhc3QtMSUyRnN0cyUyRmF3czRfcmVxdWVzdCZYLUFtei1TaWduYXR1cmU9NDUyYzA5ZTIwMzg2YjFmODU0NTU4YjhjNzBkNDA2MzdkYzM2Y2ExNzA5YWIxODQzNzE3NDdhY2IwYTUyNGIzYw"}, "kind": "ExecCredential", "spec": {}, "apiVersion": "client.authentication.k8s.io/v1alpha1"}

我們回到上面的架構(gòu)圖,kubectl 會(huì)把這個(gè)獲取的 token,放到 http 的請(qǐng)求頭 Authorization 里面,發(fā)送給 apiserver,apiserver 收到之后,回去請(qǐng)求已經(jīng)配置好的 webhook 服務(wù)器,也就是我們前面所說的 daemonset pod(aws-iam-authenticator server),這個(gè) aws-iam-authenticator server 會(huì)拿著 token 去請(qǐng)求 sts 服務(wù),sts 服務(wù)驗(yàn)證其 token 的合法性,并返回 IAM 用戶的 ARN(IAM Identity);

當(dāng) aws-iam-authenticator server 得到返回的 ARN 之后,回去和 k8s 中的一個(gè) configmap aws-auth 去對(duì)比。

2.4、configmap aws-auth

我們查看一下剛創(chuàng)建好的機(jī)器,aws-auth 這個(gè) configmap里面有什么信息:

wangzan:~ $ kubectl get cm aws-auth -nkube-system -oyaml
apiVersion: v1
data:
  mapRoles: |
    - groups:
      - system:bootstrappers
      - system:nodes
      rolearn: arn:aws:iam::921283538843:role/eksctl-eks-nodegroup-ng-5a1b33b9-NodeInstanceRole-1B757SI5DCABJ
      username: system:node:{{EC2PrivateDNSName}}
    - groups:
      - system:bootstrappers
      - system:nodes
      - system:node-proxier
      rolearn: arn:aws:iam::921283538843:role/eksctl-eks-cluster-FargatePodExecutionRole-DEAGGBFGQ9YB
      username: system:node:{{SessionName}}
kind: ConfigMap
metadata:
  creationTimestamp: "2019-12-30T07:57:47Z"
  name: aws-auth
  namespace: kube-system
  resourceVersion: "529891"
  selfLink: /api/v1/namespaces/kube-system/configmaps/aws-auth
  uid: 117c0e14-2ada-11ea-8820-0a64f353aa45

這里面定義了 IAM Identity 和 k8s 里面的 user 或者 group 的映射關(guān)系,集群創(chuàng)建的默認(rèn)管理員并沒有放在這里面,可能是處于安全的考慮,因?yàn)檫@個(gè)文件是可以進(jìn)行編輯修改的。

通過上面一步我們可以知道,通過對(duì)比 aws-auth,apiserver 會(huì)得到請(qǐng)求用戶的 username 或者 group,然后在通過其 authorization 授權(quán)方式對(duì) IAM 賦予相應(yīng)的權(quán)限,我們集群一般使用的是 RBAC。

關(guān)于更多的一些配置信息,可以參照官方文檔:

https://github.com/kubernetes-sigs/aws-iam-authenticator
https://docs.aws.amazon.com/zh_cn/eks/latest/userguide/add-user-role.html

歡迎大家掃碼關(guān)注,獲取更多信息

如何深入理解 EKS IAM Authenticator 的實(shí)現(xiàn)機(jī)制

分享標(biāo)題:如何深入理解EKSIAMAuthenticator的實(shí)現(xiàn)機(jī)制
網(wǎng)頁URL:http://muchs.cn/article16/gjchgg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、商城網(wǎng)站、網(wǎng)站策劃、小程序開發(fā)、標(biāo)簽優(yōu)化網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作