添加使用serviceaccount token 配置kubeconfig作为用户权限控制的实例
parent
e2f7d636be
commit
9bbc64de68
|
@ -42,6 +42,131 @@ spec:
|
|||
|
||||
如果在 pod 和 service account 中同时设置了 `automountServiceAccountToken` , pod 设置中的优先级更高。
|
||||
|
||||
## 使用 Service Account 作为用户权限管理配置 kubeconfig
|
||||
|
||||
### 创建服务账号
|
||||
```
|
||||
kubectl create serviceaccount sample-sc
|
||||
```
|
||||
这时候我们将得到一个在 default namespace 的 serviceaccount 账号;
|
||||
我们运行`kubectl get serviceaccount sample-sc` 将得到如下结果:
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
kind: ServiceAccount
|
||||
metadata:
|
||||
creationTimestamp: 2018-09-03T02:00:37Z
|
||||
labels:
|
||||
from: mofang
|
||||
name: mofang-viewer-sc
|
||||
namespace: default
|
||||
resourceVersion: "18914458"
|
||||
selfLink: /api/v1/namespaces/default/serviceaccounts/sample-sc
|
||||
uid: 26e129dc-af1d-11e8-9453-00163e0efab0
|
||||
secrets:
|
||||
- name: sample-sc-token-9x7nk
|
||||
```
|
||||
|
||||
因为我们在使用 serviceaccount 账号配置 kubeconfig 的时候需要使用到 sample-sc 的 token,
|
||||
该 token 保存在该 serviceaccount 保存的 secret 中;
|
||||
|
||||
我们运行`kubectl get secret sample-sc-token-9x7nk` 将会得到如下结果:
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
data:
|
||||
ca.crt: Ci0tLS0tQkVHSU4gQ0VSVcvfUNBVEUtLS0tLQpNSUlDL3pDQ0FtaWdBd0lCQWdJREJzdFlNQTBHQ1NxR1NJYjNEUUVCQ3dVQU1HSXhDekFKQmdOVkJBWVRBa05PCk1SRXdEd1lEVlFRSURBaGFhR1ZLYVdGdVp6RVJNQThHQTFVRUJ3d0lTR0Z1WjFwb2IzVXhFREFPQmdOVkJBb00KQjBGc2FXSmhZbUV4RERBS0JnTlZCQXNNQTBGRFV6RU5NQXNHQTFVRUF3d0VjbTl2ZERBZUZ3MHhPREExTWprd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XNGNGhzT0sxTjFMaGRrSFBpWHA4TzRQUzJ6eDFXNklfeGs2ZUNBcjUxRVpwSktEWTZHMmhmZ1A5emxROGdPV21nbS1EVjZPZzNobjJ4UFprZUktVk1nYUF3amdFUGtVZFJDSndYRk9QRG5UcXlqUmtZVzdLVU1GLTVDTHdBNDZMNk5PYjJ6YUR0Uy16Zm5hdVFwLVdIcFNQdDNEdFc0UmRLZDVDZzE3Z3RGaWhRZzI3dnVqYWJNMGpmQmp3
|
||||
kind: Secret
|
||||
metadata:
|
||||
annotations:
|
||||
kubernetes.io/service-account.name: sample-sc
|
||||
kubernetes.io/service-account.uid: 26e129dc-af1d-11e8-9453-00163e0efab0
|
||||
creationTimestamp: 2018-09-03T02:00:37Z
|
||||
name: mofang-viewer-sc-token-9x7nk
|
||||
namespace: default
|
||||
resourceVersion: "18914310"
|
||||
selfLink: /api/v1/namespaces/default/secrets/sample-sc-token-9x7nk
|
||||
uid: 26e58b7c-af1d-11e8-9453-00163e0efab0
|
||||
type: kubernetes.io/service-account-token
|
||||
```
|
||||
|
||||
其中 `{data.token}` 就会是我们的用户 token 的 base64 编码,之后我们配置 kubeconfig 的时候将会用到它;
|
||||
|
||||
### 创建角色
|
||||
比如我们想创建一个只可以查看集群`deployments`,`services`,`pods` 相关的角色,应该使用如下配置
|
||||
```yaml
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
## 这里也可以使用 Role
|
||||
kind: ClusterRole
|
||||
metadata:
|
||||
name: mofang-viewer-role
|
||||
labels:
|
||||
from: mofang
|
||||
rules:
|
||||
- apiGroups:
|
||||
- ""
|
||||
resources:
|
||||
- pods
|
||||
- pods/status
|
||||
- pods/log
|
||||
- services
|
||||
- services/status
|
||||
- endpoints
|
||||
- endpoints/status
|
||||
- deployments
|
||||
verbs:
|
||||
- get
|
||||
- list
|
||||
- watch
|
||||
```
|
||||
|
||||
### 创建角色绑定
|
||||
```yaml
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
## 这里也可以使用 RoleBinding
|
||||
kind: ClusterRoleBinding
|
||||
metadata:
|
||||
name: sample-role-binding
|
||||
labels:
|
||||
from: mofang
|
||||
roleRef:
|
||||
apiGroup: rbac.authorization.k8s.io
|
||||
kind: ClusterRole
|
||||
name: mofang-viewer-role
|
||||
subjects:
|
||||
- kind: ServiceAccount
|
||||
name: sample-sc
|
||||
namespace: default
|
||||
```
|
||||
|
||||
### 配置 kubeconfig
|
||||
经过以上的步骤,我们最开始创建的 serviceaccount 就可以用来访问我们的集群了,
|
||||
同时我们可以动态更改 `ClusterRole` 的授权来及时控制某个账号的权限(这也是使用 serviceaccount 的好处);
|
||||
|
||||
配置应该如下:
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
clusters:
|
||||
- cluster:
|
||||
## 这个是集群的 TLS 证书,与授权无关,使用同一的就可以
|
||||
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUMvekNDQW1pZ0F3SUJBZ0lEQnN0WU1BMEdDU3FHU0liM0RRRUJDd1VBTUdJeEN6QUpCZ05WQkFZVEFrTk8KTVJFd0R3WURWUVFJREFoYWFHVkthV0Z1WnpFUk1BOEdBMVVFQnd3SVNHRnVaMXBvYjNVeEVEQU9CZ05WQkFvTQpCMEZzYVdKaFltRXhEREFLQmdOVkJBc01BMEZEVXpFTk1Bc0dBMVVFQXd3RWNtOXZkREFlRncweE9EQTFNamt3Ck16UXdNREJhRncwek9EQTFNalF3TXpRMU5UbGFNR294S2pBb0JnTlZCQW9USVdNMlpUbGpObUpqWVRjellqRTAKWTJNMFlXRTNPVE13WWpNNE5ERXhORFpqWVRFUU1BNEdBMVVFQ3hNSFpHVm1ZWFZzZERFcU1DZ0dBMVVFQXhNaApZelpsT1dNMlltTmhOek5pTVRSall6UmhZVGM1TXpCaU16ZzBNVEUwTm1OaE1JR2ZNQTBHQ1NxR1NJYjNEUUVCCkFRVUFBNEdOQURDQmlRS0JnUURMY0VaYnJwK0FrS1o0dThOSVUzbmNoVThiQTEyeEpHSkkzOXF1N3hoUWxHeUcKZmpBMWp1dXhxUzJoTi9ManAzbVc2R0hpbTN3aUl3Y3VZS1Q3dEY5b1R6OCtPOEFDNkdiemRYTEgvVFBNa0JnZgo5U1hoR2h3WGd2SUxvdjNudmVLUzNESXFTdSt5L04rWG4zOE45bndIcXpLSnZYTVE5a0lpQm5NeDBWeXNIUUlECkFRQUJvNEc2TUlHM01BNEdBMVVkRHdFQi93UUVBd0lDckRBUEJnTlZIUk1CQWY4RUJUQURBUUgvTUI4R0ExVWQKSXdRWU1CYUFGSVZhLzkwanpTVnZXRUZ2bm0xRk9adFlmWFgvTUR3R0NDc0dBUVVGQndFQkJEQXdMakFzQmdncgpCZ0VGQlFjd0FZWWdhSFIwY0RvdkwyTmxjblJ6TG1GamN5NWhiR2w1ZFc0dVkyOXRMMjlqYzNBd05RWURWUjBmCkJDNHdMREFxb0NpZ0pvWWthSFIwY0RvdkwyTmxjblJ6TG1GamN5NWhiR2w1ZFc0dVkyOXRMM0p2YjNRdVkzSnMKTUEwR0NTcUdTSWIzRFFFQkN3VUFBNEdCQUpYVGlYSW9DVVg4MUVJTmJ1VFNrL09qdEl4MzRyRXR5UG5PK0FTagpqSzNpNHd3UFEwS3kwOGZOU25TZnhDUTJ4RjU1MjE1U29TMzFTd0x6WUlUSnVYWkE3bFdPb1FTVFkvaUFMdVBCCi9rM0JsOE5QY2Z6OEY1eVk3L25jU3pYNHBNeDE4cjBjb09MTWJmWlRRcm1IcGZDTndtZGNCZUIrQm5EUkxQSkYKaDNJRAotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==
|
||||
server: https://47.95.24.167:6443
|
||||
name: beta
|
||||
contexts:
|
||||
- context:
|
||||
cluster: beta
|
||||
user: beta-viewer
|
||||
name: beta-viewer
|
||||
current-context: beta-viewer
|
||||
kind: Config
|
||||
preferences: {}
|
||||
users:
|
||||
- name: beta-viewer
|
||||
user:
|
||||
## 这个使我们在创建 serviceaccount 生成相关 secret 之后的 data.token 的 base64 解码字符,它本质是一个 jwt token
|
||||
token: eyJhbGciOiJSUzI1NiIsInR5dffg6IkpXVCJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJkZWZhdWx0Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZWNyZXQubmFtZSI6Im1vZmFuZy12aWV3ZXItc2MtdG9rZW4tOXg3bmsiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoibW9mYW5nLXZpZXdlci1zYyIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6IjxZTEyOWRjLWFmMWQtMTFlOC05NDUzLTAwMTYzZTBlZmFiMCIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDpkZWZhdWx0Om1vZmFuZy12aWV3ZXItc2MifQ.AjnvvnytZXruRwRtBwKtEw6V72iYMo9B6-XwVi3EhQysN3mO-rotaIBvGPYM0fMT9VG5cLQJbbVRhKGArrU2CQMV_ICsin1s21qMryvx36oZo1XfJVfUU0KjylgtD453fYO8JQ_XQw8cpUg94a6Zvop6GqsF4hsOK1N1LhdkHPiXp8O4PS2zx1W6I_xk6eCAr51EZpJKDY6G2hfgP9zlQ8gOWmgm-DV6Og3hn2xPZkeI-VMgaAwjgEPkUdRCJwXFOPDnTqyjRkYW7KUMF-5CLwA46L6NOb2zaDtS-zfnauQp-WHpSPt3DtW4RdKd5Cg17gtFihQg27vujabM0jfBjw
|
||||
```
|
||||
|
||||
## 使用多个Service Account
|
||||
|
||||
每个 namespace 中都有一个默认的叫做 `default` 的 service account 资源。
|
||||
|
|
Loading…
Reference in New Issue