Vip*_*non 5 jobs containers rbac kubernetes
嗨,我看到了这个文档,其中 kubectl 可以在默认 pod 的 pod 内运行。是否可以在指定命名空间的作业资源中运行 kubectl?没有看到任何相同的文档或示例..
当我尝试将 serviceAccounts 添加到容器时,出现错误:
Error from server (Forbidden): pods is forbidden: User "system:serviceaccount:my-namespace:internal-kubectl" cannot list resource "pods" in API group "" in the namespace "my-namespace"
Run Code Online (Sandbox Code Playgroud)
这是我尝试 SSH 进入容器并运行 kubctl 的时候。
编辑问题.....
正如我之前提到的,根据我添加了服务帐户的文档,以下是 yaml:
apiVersion: v1
kind: ServiceAccount
metadata:
name: internal-kubectl
namespace: my-namespace
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: modify-pods
namespace: my-namespace
rules:
- apiGroups: [""]
resources:
- pods
verbs:
- get
- list
- delete
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: modify-pods-to-sa
namespace: my-namespace
subjects:
- kind: ServiceAccount
name: internal-kubectl
roleRef:
kind: Role
name: modify-pods
apiGroup: rbac.authorization.k8s.io
---
apiVersion: batch/v1
kind: Job
metadata:
name: testing-stuff
namespace: my-namespace
spec:
template:
metadata:
name: testing-stuff
spec:
serviceAccountName: internal-kubectl
containers:
- name: tester
image: bitnami/kubectl
command:
- "bin/bash"
- "-c"
- "kubectl get pods"
restartPolicy: Never
Run Code Online (Sandbox Code Playgroud)
在运行作业时,我收到错误消息:
Error from server (Forbidden): pods is forbidden: User "system:serviceaccount:my-namespace:internal-kubectl" cannot list resource "pods" in API group "" in the namespace "my-namespace"
Run Code Online (Sandbox Code Playgroud)
是否可以在指定命名空间的作业资源内运行 kubectl?没有看到任何相同的文档或示例。
一项作业会创建一个或多个 Pod,并确保其中指定数量的 Pod 成功终止。这意味着权限方面与普通 Pod 中的相同,这意味着可以在作业资源内运行 kubectl。
长话短说:
安全考虑:
ClusterRoleBinding与该cluster-admin角色一起使用,它会起作用,但它的权限过多,并且不推荐,因为它提供了对整个集群的完全管理控制。测试环境:
bitnami/kubectl和 运行该作业bitnami/kubectl:1:17.3。它对两种情况都有效。kubectl与您的服务器匹配的版本。再生产:
$ cat job-kubectl.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: testing-stuff
namespace: my-namespace
spec:
template:
metadata:
name: testing-stuff
spec:
serviceAccountName: internal-kubectl
containers:
- name: tester
image: bitnami/kubectl:1.17.3
command:
- "bin/bash"
- "-c"
- "kubectl get pods -n my-namespace"
restartPolicy: Never
$ cat job-svc-account.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: internal-kubectl
namespace: my-namespace
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: modify-pods
namespace: my-namespace
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "delete"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: modify-pods-to-sa
namespace: my-namespace
subjects:
- kind: ServiceAccount
name: internal-kubectl
roleRef:
kind: Role
name: modify-pods
apiGroup: rbac.authorization.k8s.io
Run Code Online (Sandbox Code Playgroud)
get pods.$ kubectl run curl --image=radial/busyboxplus:curl -i --tty --namespace my-namespace
the pod is running
$ kubectl run ubuntu --generator=run-pod/v1 --image=ubuntu -n my-namespace
pod/ubuntu created
Run Code Online (Sandbox Code Playgroud)
job、ServiceAccount和RoleRoleBinding$ kubectl get pods -n my-namespace
NAME READY STATUS RESTARTS AGE
curl-69c656fd45-l5x2s 1/1 Running 1 88s
testing-stuff-ddpvf 0/1 Completed 0 13s
ubuntu 0/1 Completed 3 63s
Run Code Online (Sandbox Code Playgroud)
$ kubectl logs testing-stuff-ddpvf -n my-namespace
NAME READY STATUS RESTARTS AGE
curl-69c656fd45-l5x2s 1/1 Running 1 76s
testing-stuff-ddpvf 1/1 Running 0 1s
ubuntu 1/1 Running 3 51s
Run Code Online (Sandbox Code Playgroud)
如您所见,它已成功使用自定义ServiceAccount.
如果您对此案还有其他疑问,请告诉我。
| 归档时间: |
|
| 查看次数: |
2940 次 |
| 最近记录: |