我向您展示了一個使用 Dex、dex-k8s-authenticator 和 GitHub 產生對 Kubernetes 叢集的存取權限的教學。
來自俄語 Kubernetes 聊天的本地模因
介紹
我們使用 Kubernetes 為開發和 QA 團隊創建動態環境。 因此,我們希望讓他們能夠存取叢集的儀表板和 kubectl。 與 OpenShift 不同,vanilla Kubernetes 沒有本機身份驗證,因此我們使用第三方工具來實現此目的。
在此配置中我們使用:
dex-k8s-身份驗證器 — 用於產生 kubectl 設定的 Web 應用程式地塞米松 — OpenID Connect 供應商- GitHub - 只是因為我們在公司使用 GitHub
我們嘗試使用 Google OIDC,但不幸的是我們
那麼,我們的 Kubernetes 授權流程如何以視覺化的方式進行工作:
授權流程
一點一點詳細說明:
- 使用者登入 dex-k8s-authenticator (
login.k8s.example.com
) - dex-k8s-authenticator 將請求轉送給 Dex (
dex.k8s.example.com
) - Dex 重定向到 GitHub 登入頁面
- GitHub 產生必要的授權資訊並將其傳回 Dex
- Dex將收到的訊息傳遞給dex-k8s-authenticator
- 用戶從 GitHub 收到 OIDC 令牌
- dex-k8s-authenticator 將令牌新增至 kubeconfig
- kubectl 將令牌傳遞給 KubeAPIServer
- KubeAPIServer 根據傳遞的令牌傳回對 kubectl 的訪問
- 使用者從 kubectl 取得存取權限
準備行動
當然,我們已經安裝了 Kubernetes 叢集(k8s.example.com
),並且也預先安裝了 HELM。 我們在 GitHub 上也有一個組織(super-org)。
如果您沒有 HELM,請安裝它
首先我們需要設定 GitHub。
前往組織設定頁面,(https://github.com/organizations/super-org/settings/applications
)並建立一個新應用程式(授權 OAuth 應用程式):
在 GitHub 上建立新應用程式
使用必要的 URL 填寫字段,例如:
- 主頁網址:
https://dex.k8s.example.com
- 授權回呼位址:
https://dex.k8s.example.com/callback
小心鏈接,重要的是不要丟失斜杠。
為了回應填寫完整的表格,GitHub 將生成 Client ID
и Client secret
,將它們保存在安全的地方,它們將對我們有用(例如,我們使用
Client ID: 1ab2c3d4e5f6g7h8
Client secret: 98z76y54x32w1
準備子網域的 DNS 記錄 login.k8s.example.com
и dex.k8s.example.com
,以及用於入口的 SSL 憑證。
讓我們建立 SSL 憑證:
cat <<EOF | kubectl create -f -
apiVersion: certmanager.k8s.io/v1alpha1
kind: Certificate
metadata:
name: cert-auth-dex
namespace: kube-system
spec:
secretName: cert-auth-dex
dnsNames:
- dex.k8s.example.com
acme:
config:
- http01:
ingressClass: nginx
domains:
- dex.k8s.example.com
issuerRef:
name: le-clusterissuer
kind: ClusterIssuer
---
apiVersion: certmanager.k8s.io/v1alpha1
kind: Certificate
metadata:
name: cert-auth-login
namespace: kube-system
spec:
secretName: cert-auth-login
dnsNames:
- login.k8s.example.com
acme:
config:
- http01:
ingressClass: nginx
domains:
- login.k8s.example.com
issuerRef:
name: le-clusterissuer
kind: ClusterIssuer
EOF
kubectl describe certificates cert-auth-dex -n kube-system
kubectl describe certificates cert-auth-login -n kube-system
帶標題的 ClusterIssuer le-clusterissuer
應該已經存在,但如果不存在,請使用 HELM 建立它:
helm install --namespace kube-system -n cert-manager stable/cert-manager
cat << EOF | kubectl create -f -
apiVersion: certmanager.k8s.io/v1alpha1
kind: ClusterIssuer
metadata:
name: le-clusterissuer
namespace: kube-system
spec:
acme:
server: https://acme-v02.api.letsencrypt.org/directory
email: [email protected]
privateKeySecretRef:
name: le-clusterissuer
http01: {}
EOF
KubeAPI伺服器配置
為了讓 kubeAPIServer 運作,您需要配置 OIDC 並更新叢集:
kops edit cluster
...
kubeAPIServer:
anonymousAuth: false
authorizationMode: RBAC
oidcClientID: dex-k8s-authenticator
oidcGroupsClaim: groups
oidcIssuerURL: https://dex.k8s.example.com/
oidcUsernameClaim: email
kops update cluster --yes
kops rolling-update cluster --yes
Dex 配置和 dex-k8s-authenticator
為了讓 Dex 運作,您需要從 Kubernetes master 取得憑證和金鑰,讓我們從那裡取得:
sudo cat /srv/kubernetes/ca.{crt,key}
-----BEGIN CERTIFICATE-----
AAAAAAAAAAABBBBBBBBBBCCCCCC
-----END CERTIFICATE-----
-----BEGIN RSA PRIVATE KEY-----
DDDDDDDDDDDEEEEEEEEEEFFFFFF
-----END RSA PRIVATE KEY-----
讓我們克隆 dex-k8s-authenticator 儲存庫:
git clone [email protected]:mintel/dex-k8s-authenticator.git
cd dex-k8s-authenticator/
使用values文件,我們可以靈活地為我們的配置變數
讓我們來描述一下Dex的配置:
cat << EOF > values-dex.yml
global:
deployEnv: prod
tls:
certificate: |-
-----BEGIN CERTIFICATE-----
AAAAAAAAAAABBBBBBBBBBCCCCCC
-----END CERTIFICATE-----
key: |-
-----BEGIN RSA PRIVATE KEY-----
DDDDDDDDDDDEEEEEEEEEEFFFFFF
-----END RSA PRIVATE KEY-----
ingress:
enabled: true
annotations:
kubernetes.io/ingress.class: nginx
kubernetes.io/tls-acme: "true"
path: /
hosts:
- dex.k8s.example.com
tls:
- secretName: cert-auth-dex
hosts:
- dex.k8s.example.com
serviceAccount:
create: true
name: dex-auth-sa
config: |
issuer: https://dex.k8s.example.com/
storage: # https://github.com/dexidp/dex/issues/798
type: sqlite3
config:
file: /var/dex.db
web:
http: 0.0.0.0:5556
frontend:
theme: "coreos"
issuer: "Example Co"
issuerUrl: "https://example.com"
logoUrl: https://example.com/images/logo-250x25.png
expiry:
signingKeys: "6h"
idTokens: "24h"
logger:
level: debug
format: json
oauth2:
responseTypes: ["code", "token", "id_token"]
skipApprovalScreen: true
connectors:
- type: github
id: github
name: GitHub
config:
clientID: $GITHUB_CLIENT_ID
clientSecret: $GITHUB_CLIENT_SECRET
redirectURI: https://dex.k8s.example.com/callback
orgs:
- name: super-org
teams:
- team-red
staticClients:
- id: dex-k8s-authenticator
name: dex-k8s-authenticator
secret: generatedLongRandomPhrase
redirectURIs:
- https://login.k8s.example.com/callback/
envSecrets:
GITHUB_CLIENT_ID: "1ab2c3d4e5f6g7h8"
GITHUB_CLIENT_SECRET: "98z76y54x32w1"
EOF
對於 dex-k8s-authenticator:
cat << EOF > values-auth.yml
global:
deployEnv: prod
dexK8sAuthenticator:
clusters:
- name: k8s.example.com
short_description: "k8s cluster"
description: "Kubernetes cluster"
issuer: https://dex.k8s.example.com/
k8s_master_uri: https://api.k8s.example.com
client_id: dex-k8s-authenticator
client_secret: generatedLongRandomPhrase
redirect_uri: https://login.k8s.example.com/callback/
k8s_ca_pem: |
-----BEGIN CERTIFICATE-----
AAAAAAAAAAABBBBBBBBBBCCCCCC
-----END CERTIFICATE-----
ingress:
enabled: true
annotations:
kubernetes.io/ingress.class: nginx
kubernetes.io/tls-acme: "true"
path: /
hosts:
- login.k8s.example.com
tls:
- secretName: cert-auth-login
hosts:
- login.k8s.example.com
EOF
安裝 Dex 和 dex-k8s-authenticator:
helm install -n dex --namespace kube-system --values values-dex.yml charts/dex
helm install -n dex-auth --namespace kube-system --values values-auth.yml charts/dex-k8s-authenticator
讓我們檢查服務的功能(Dex 應回傳代碼 400,dex-k8s-authenticator 應傳回代碼 200):
curl -sI https://dex.k8s.example.com/callback | head -1
HTTP/2 400
curl -sI https://login.k8s.example.com/ | head -1
HTTP/2 200
RBAC配置
我們為該群組建立一個 ClusterRole,在我們的例子中具有唯讀存取權限:
cat << EOF | kubectl create -f -
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: cluster-read-all
rules:
-
apiGroups:
- ""
- apps
- autoscaling
- batch
- extensions
- policy
- rbac.authorization.k8s.io
- storage.k8s.io
resources:
- componentstatuses
- configmaps
- cronjobs
- daemonsets
- deployments
- events
- endpoints
- horizontalpodautoscalers
- ingress
- ingresses
- jobs
- limitranges
- namespaces
- nodes
- pods
- pods/log
- pods/exec
- persistentvolumes
- persistentvolumeclaims
- resourcequotas
- replicasets
- replicationcontrollers
- serviceaccounts
- services
- statefulsets
- storageclasses
- clusterroles
- roles
verbs:
- get
- watch
- list
- nonResourceURLs: ["*"]
verbs:
- get
- watch
- list
- apiGroups: [""]
resources: ["pods/exec"]
verbs: ["create"]
EOF
讓我們為 ClusterRoleBinding 建立一個配置:
cat <<EOF | kubectl create -f -
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: dex-cluster-auth
namespace: kube-system
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-read-all
subjects:
kind: Group
name: "super-org:team-red"
EOF
現在我們已經準備好進行測試了。
測試
前往登入頁面(https://login.k8s.example.com
)並使用您的 GitHub 帳號登入:
登入頁面
登入頁面重定向到 GitHub
按照產生的說明取得存取權限
從網頁複製貼上後,我們可以使用 kubectl 來管理我們的叢集資源:
kubectl get po
NAME READY STATUS RESTARTS AGE
mypod 1/1 Running 0 3d
kubectl delete po mypod
Error from server (Forbidden): pods "mypod" is forbidden: User "[email protected]" cannot delete pods in the namespace "default"
它有效,我們組織中的所有 GitHub 用戶都可以查看資源並登入 pod,但他們無權更改它們。
來源: www.habr.com