kubernetes - 如何在命名空间中的作业中运行 kubectl?
问题描述
嗨,我看到了这个文档,其中 kubectl 可以在默认 pod 的 pod 内运行。是否可以在指定命名空间的 Job 资源中运行 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"
这是我尝试 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
在运行作业时,我收到错误:
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"
解决方案
是否可以在指定命名空间的 Job 资源中运行 kubectl?没有看到任何相同的文档或示例..
Job创建一个或多个 Pod并确保指定数量的 Pod 成功终止。这意味着权限方面与普通 pod 中的相同,这意味着是的,可以在作业资源中运行 kubectl。
TL;博士:
- 您的 yaml 文件是正确的,也许您的集群中还有其他内容,我建议您删除并重新创建这些资源,然后重试。
- 还要检查你的 Kubernetes 安装版本和作业镜像的 kubectl 版本,如果它们相差超过 1 个次要版本,你可能会遇到意外的不兼容问题
安全注意事项:
- 根据文档(特定角色,特定命名空间上的特定用户),您的工作角色的范围是最佳实践。
- 如果您将 a
ClusterRoleBinding
与cluster-admin
角色一起使用,它将起作用,但它已获得过多许可,因此不推荐使用,因为它赋予了对整个集群的完全管理员控制权。
测试环境:
- 我在 kubernetes 1.17.3 上部署了您的配置,并使用 and 运行
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
- 我创建了两个 pod 只是为了将输出添加到
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
- 然后我应用
job
,ServiceAccount
和Role
RoleBinding
$ 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
- 现在让我们检查 testing-stuff pod 日志,看看它是否记录了命令输出:
$ 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
如您所见,它已成功使用 custom 运行作业ServiceAccount
。
如果您对此案还有其他疑问,请告诉我。
推荐阅读
- android - 无法运行安卓模拟器
- r - 用R中的地图边界划定voronoi图
- javascript - 在这种情况下如何理解javascript内存泄漏
- tfs - 如何在 TFS 中使用命令行任务在发布期间备份 SQL 数据库?
- c# - 使用asp c#返回json值时出现ajax错误
- javascript - 代码 ` ` 危险吗?
- mysql - MySQL - 如果没有重复则加入
- javascript - 如何在 Linking.openURL(url) 添加标头参数
- swift - 我似乎无法解决 valueForUndefinedKey:] 的错误:whereto class is not key value coding-compliant
- javascript - 使用“every”测试是否已获取项目未按预期工作