我向您展示了一个使用 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,但他们无权更改它们。
来源: habr.com