kubernetes集羣三步安裝
kubernetes server account的token很容易獲取,可是User的token很是麻煩,本文給出一個極簡的User token生成方式,讓用戶能夠一個http請求就能獲取到。git
官方dashboard登陸時須要。 若是經過使用kubeconfig文件登陸而文件中又沒有token的話會失敗,如今大部分文章都介紹使用service account的token來登陸dashboard,能通,不過有問題:
第一:綁定角色時要指定類型是service account:github
apiVersion: rbac.authorization.k8s.io/v1beta1 kind: ClusterRoleBinding metadata: name: kubernetes-dashboard labels: k8s-app: kubernetes-dashboard roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-admin subjects: - kind: ServiceAccount # 這裏不是User類型 name: kubernetes-dashboard namespace: kube-system
第二:要理解kubeconfig裏是解析證書把CN做爲用戶名的,這時service account即使與CN同樣那仍是兩個帳戶,綁定角色時還須要綁定兩次,有點像把service account給"人"用, 因此把service account的token扔給某個開發人員去用每每不合適,service account token更多時候是給程序用的。web
想直接調用https的,沒有token就會:json
[root@iZj6cegflzze2l7fpcqoerZ ssl]# curl https://172.31.12.61:6443/api/v1/namespaces/default/pods --insecure { "kind": "Status", "apiVersion": "v1", "metadata": { }, "status": "Failure", "message": "pods is forbidden: User \"system:anonymous\" cannot list resource \"pods\" in API group \"\" in the namespace \"default\"", "reason": "Forbidden", "details": { "kind": "pods" }, "code": 403 }
由於沒有任何認證信息,因此匿名(anonymous)用戶沒有任何權限vim
加了token是這樣的:api
[root@iZj6cegflzze2l7fpcqoerZ ssl]# curl -H "Authorization: Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6IkNnYzRPVEV5TlRVM0VnWm5hWFJvZFdJIn0.eyJpc3MiOiJodHRwczovL2RleC5leGFtcGxlLmNvbTo4MDgwIiwic3ViIjoiQ2djNE9URXlOVFUzRWdabmFYUm9kV0kiLCJhdWQiOiJleGFtcGxlLWFwcCIsImV4cCI6MTU1MTA5NzkwNiwiaWF0IjoxNTUwNzM3OTA2LCJlbWFpbCI6ImZodGpvYkBob3RtYWlsLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjp0cnVlLCJncm91cHMiOlsiZGV2Il0sIm5hbWUiOiJmYW51eCJ9.ZqKn461UW0aGtyjyqu2Dc5tiUzC-6eYLag542d3AvklUdZuw8i9XwyaUg_f1OAj0ZsEcOybOe9_PeGMaUYzU0OvlKPY-q2zbQVC-m6u6sQw6ZXx8pi0W8k4wQSJnMaOLddCfurlYufmr8kScDBQlnKapSR0F9mJzvpKkHD-XNshQKWhX3n03g7OfFgb4RuhLjKDNQnoGn7DfBNntibHlF9sPo0jC5JjqTZaGvoGmiRE4PAXwxA-RJifsWDNf_jW8lrDiY4NSO_3O081cia4N1GKht51q9W3eaNMvFDD9hje7abDdZoz9KPi2vc3zvgH7cNv0ExVHKaA0-dwAZgTx4g" -k https://172.31.12.61:6443/api/v1/namespaces/default/pods { "kind": "Status", "apiVersion": "v1", "metadata": { }, "status": "Failure", "message": "pods is forbidden: User \"https://dex.example.com:8080#fanux\" cannot list resource \"pods\" in API group \"\" in the namespace \"default\"", "reason": "Forbidden", "details": { "kind": "pods" }, "code": 403 }
看,雖然仍是403 可是已經有了用戶信息,只要給該用戶受權就可正常訪問了,如何受權下文介紹瀏覽器
token的生成方式有不少,主要分紅三種:app
基於openid的jwt是本文介紹的重點。curl
社區用的比較多的就是dex,是一個比較完整的實現,可是對於不熟悉該技術的朋友來講仍是有點門檻的,容易繞進去。 並且還存在一些使用不方便的問題。
如依賴複雜,首先得須要一個真正的用戶管理程序,如ldap 或者一個auth2服務端,這還能夠接受,關鍵是認證時可能須要依賴瀏覽器進行跳轉受權,這在十分多的場景裏就變的十分尷尬,就好比咱們的場景壓根沒有
界面,這樣生成token就成了一個大問題。 其次集成到別的系統中時每每用戶已經登陸過了,因此須要一個二次受權的過程才能拿到token,依賴太重致使系統難以設計。
然而若是不是集成到別的系統中,好比從0開發一個完成的PaaS平臺那使用dex仍是一個完美的方案。ui
因此咱們實現了一個簡單粗暴的方案,徹底解放了這個過程, 只care最核心的東西。
咱們想要啥?
input:
{ "User": "fanux", "Group": ["sealyun", "develop"] }
output:
eyJhbGciOiJSUzI1NiIsImtpZCI6IkNnYzRPVEV5TlRVM0VnWm5hWFJvZFdJIn0.eyJpc3MiOiJodHRwczovL2RleC5leGFtcGxlLmNvbTo4MDgwIiwic3ViIjoiQ2djNE9URXlOVFUzRWdabmFYUm9kV0kiLCJhdWQiOiJleGFtcGxlLWFwcCIsImV4cCI6MTU1MTA5NzkwNiwiaWF0IjoxNTUwNzM3OTA2LCJlbWFpbCI6ImZodGpvYkBob3RtYWlsLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjp0cnVlLCJncm91cHMiOlsiZGV2Il0sIm5hbWUiOiJmYW51eCJ9.ZqKn461UW0aGtyjyqu2Dc5tiUzC-6eYLag542d3AvklUdZuw8i9XwyaUg_f1OAj0ZsEcOybOe9_PeGMaUYzU0OvlKPY-q2zbQVC-m6u6sQw6ZXx8pi0W8k4wQSJnMaOLddCfurlYufmr8kScDBQlnKapSR0F9mJzvpKkHD-XNshQKWhX3n03g7OfFgb4RuhLjKDNQnoGn7DfBNntibHlF9sPo0jC5JjqTZaGvoGmiRE4PAXwxA-RJifsWDNf_jW8lrDiY4NSO_3O081cia4N1GKht51q9W3eaNMvFDD9hje7abDdZoz9KPi2vc3zvgH7cNv0ExVHKaA0-dwAZgTx4g
結束,多簡單,別整那麼多沒用的。
因此爲了實現上面的功能,咱們開發了 fist, fist的auth模塊把dex裏最核心的token生成功能以及jwt功能實現了。
生成證書
# mkdir /etc/kubernetes/pki/fist # cd /etc/kubernetes/pki/fist # sh gencert.sh # 腳本內容內代碼
啓動fist auth模塊
kubectl create -f deploy/fist-auth.yaml
修改k8s apiserver啓動參數
vim /etc/kubernetes/manifests/kube-apiserver.yaml
- command: - kube-apiserver - --oidc-issuer-url=https://fist.sealyun.svc.cluster.local:8080 - --oidc-client-id=example-app - --oidc-ca-file=/etc/kubernetes/pki/fist/ca.pem - --oidc-username-claim=name - --oidc-groups-claim=groups
獲取token
curl https://fist.sealyun.svc.cluster.local:8080/token?user=fanux&group=sealyun,develop --cacert ca.pem
使用token
直接curl加bare token 見上文
加入到kubeconfig中:
kubectl config set-credentials --token=eyJhbGciOiJSUzI1NiIsImtpZCI6IkNnYzRPVEV5TlRVM0VnWm5hWFJvZFdJIn0.eyJpc3MiOiJodHRwczovL2RleC5leGFtcGxlLmNvbTo4MDgwIiwic3ViIjoiQ2djNE9URXlOVFUzRWdabmFYUm9kV0kiLCJhdWQiOiJleGFtcGxlLWFwcCIsImV4cCI6MTU1MTEwMDI5MywiaWF0IjoxNTUwNzQwMjkzLCJlbWFpbCI6ImZodGpvYkBob3RtYWlsLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjp0cnVlLCJncm91cHMiOlsiZGV2Il0sIm5hbWUiOiJmYW51eCJ9.OAK4oIYqJszm1EACYW2neXTo738RW9kXFOIN5bOT4Z2CeKAvYqyOVKCWZf04xX45jwT78mATR3uas2YvRooDXlvxaD3K43ls4KBSG-Ofp-ynqlcVTpD3sUDqyux2iieNv4N6IyCv11smrU0lIlkrQC6oyxzTGae1FrJVGc5rHNsIRZHp2WrQvw83uLn_elHgUfSlsOq0cPtVONaAQWMAMi2DX-y5GCNpn1CDvudGJihqsTciPx7bj0AOXyiOznWhV186Ybk-Rgqn8h0eBaQhFMyNpwVt6oIP5pvJQs0uoODeRv6P3I3-AjKyuCllh9KDtlCVvSP4WtMUTfHQN4BigQ kubernetes-admin
而後.kube/config 文件裏的 user.client-certifacate-data 和 client-key-data就能夠刪了,再執行kubectl會:
[root@iZj6cegflzze2l7fpcqoerZ ~]# kubectl get pod Error from server (Forbidden): pods is forbidden: User "https://dex.example.com:8080#fanux" cannot list resource "pods" in API group "" in the namespace "default"
說明新用戶成功了
受權
[root@iZj6cegflzze2l7fpcqoerZ ~]# cat rolebind.yaml kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: read-secrets-global subjects: - kind: User name: "https://dex.example.com:8080#fanux" # Name is case sensitive apiGroup: rbac.authorization.k8s.io roleRef: kind: ClusterRole name: cluster-admin # 超級用戶給他 apiGroup: rbac.authorization.k8s.io
建立個role binding便可:
[root@iZj6cegflzze2l7fpcqoerZ ~]# kubectl --kubeconfig /etc/kubernetes/admin.conf create -f rolebind.yaml # 用管理員的kubeconfig clusterrolebinding.rbac.authorization.k8s.io/read-secrets-global created [root@iZj6cegflzze2l7fpcqoerZ ~]# kubectl get pod # 有權限訪問pod了 No resources found.
https://fist.sealyun.cluster.local:8080 k8s jwt server | /.well-known/openid-configuration | |------------------------------------------------>| k8s經過此url發現一些信息,最重要的就是用於校驗token公鑰的地址 | discover info | |<------------------------------------------------| | /keys | |------------------------------------------------>| 上一步拿到地址,這一步獲取到公鑰 | public keys | |<------------------------------------------------| | |
discoer info 是個json:
{ "issuer": "https://accounts.google.com", "authorization_endpoint": "https://accounts.google.com/o/oauth2/v2/auth", "token_endpoint": "https://oauth2.googleapis.com/token", "userinfo_endpoint": "https://openidconnect.googleapis.com/v1/userinfo", "revocation_endpoint": "https://oauth2.googleapis.com/revoke", "jwks_uri": "https://www.googleapis.com/oauth2/v3/certs", "response_types_supported": [ "code", "token", "id_token", "code token", "code id_token", "token id_token", "code token id_token", "none" ], ...
public keys也是個json 相似:
{ "keys": [ { "e": "AQAB", "kty": "RSA", "alg": "RS256", "n": "3MdFK4pXPvehMipDL_COfqn6o9soHgSaq_V1o8U_5gTZ-j9DxO9PV7BVncXBgHFctnp3JQ1QTDF7txeHeuLOS4KziRw5r4ohaj2WoOTqXh7lqVMR2YDAcBK46asS177NpkQ1CqHIsy3kNfqhXLwTaKfdlwdA_XUfRbKORWbq0kDxV35egx35nHl5qJ6aP6fcpsnnPvHf7KWO0zkdvwuR-IX79HjqUAEg5UERd5FK4y06PRbxuXHjAgVhHu_sk4reNXNp1HRuTYtQ26DFbVaIjsWb8-nQC8-7FkTjlw9FteAwLVGOm9sTLFp73jAf0pWLh7sJ02pBxZKjsxLO1Lvg7w", "use": "sig", "kid": "7c309e3a1c1999cb0404ab7125ee40b7cdbcaf7d" }, { "alg": "RS256", "n": "2K7epoJWl_B68lRUi1txaa0kEuIK4WHiHpi1yC4kPyu48d046yLlrwuvbQMbog2YTOZdVoG1D4zlWKHuVY00O80U1ocFmBl3fKVrUMakvHru0C0mAcEUQo7ItyEX7rpOVYtxlrVk6G8PY4EK61EB-Xe35P0zb2AMZn7Tvm9-tLcccqYlrYBO4SWOwd5uBSqc_WcNJXgnQ-9sYEZ0JUMhKZelEMrpX72hslmduiz-LMsXCnbS7jDGcUuSjHXVLM9tb1SQynx5Xz9xyGeN4rQLnFIKvgwpiqnvLpbMo6grhJwrz67d1X6MwpKtAcqZ2V2v4rQsjbblNH7GzF8ZsfOaqw", "use": "sig", "kid": "7d680d8c70d44e947133cbd499ebc1a61c3d5abc", "e": "AQAB", "kty": "RSA" } ] }
因此fist只須要實現這兩個url 和 用私鑰匙加密用戶信息生成token便可。
建立密鑰對:
key, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { log.Fatalf("gen rsa key: %v", err) } priv = jose.JSONWebKey{ Key: key, KeyID: "Cgc4OTEyNTU3EgZnaXRodWI", Algorithm: "RS256", Use: "sig", } pub = jose.JSONWebKey{ Key: key.Public(), KeyID: "Cgc4OTEyNTU3EgZnaXRodWI", Algorithm: "RS256", Use: "sig", }
私鑰加密:
tok := idTokenClaims{ Issuer: "https://dex.example.com:8080", Subject: "Cgc4OTEyNTU3EgZnaXRodWI", Audience: "example-app", Expiry: time.Now().Add(time.Hour * 100).Unix(), IssuedAt: time.Now().Unix(), Email: "fhtjob@hotmail.com", EmailVerified: &ev, Groups: []string{"dev"}, Name: "fanux", } payload, err := json.Marshal(&tok) if err != nil { return } var idToken string if idToken, err = signPayload(&Priv, signingAlg, payload); err != nil { return
fist核心代碼已經可用,不過爲了更方便使用還須要進一步梳理,敬請期待。 鑑權僅是其其中一個功能,fist定位是一個極簡的k8s管理平臺。
探討可加QQ羣:98488045
公衆號: