我正在尝试利用 Kubernetes 命名空间通过自动化来配置临时环境。我在 Kubernetes 中部署的自动化工作人员必须能够创建命名空间。到目前为止,我对此的实验没有任何结果。我需要将哪个绑定附加到服务帐户以允许其控制命名空间?还是我的方法不对?
到目前为止我的代码:
deployment.yaml
:
apiVersion: apps/v1
kind: Deployment
metadata:
name: k8s-deployer
namespace: tooling
labels:
app: k8s-deployer
spec:
replicas: 1
selector:
matchLabels:
app: k8s-deployer
template:
metadata:
name: k8s-deployer
labels:
app: k8s-deployer
spec:
serviceAccountName: k8s-deployer
containers: ...
rbac.yaml
:
apiVersion: v1
kind: ServiceAccount
metadata:
name: k8s-deployer
namespace: tooling
---
# this lets me view namespaces, but not write
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: administer-cluster
subjects:
- kind: ServiceAccount
name: k8s-deployer
namespace: tooling
roleRef:
kind: ClusterRole
name: admin
apiGroup: rbac.authorization.k8s.io
要让 pod 控制 Kubernetes 中的某些内容,您至少需要四件事:
Role
/ClusterRole
(您选择了administer-cluster
,我不知道哪些规则)。ServiceAccount
(您在命名空间 k8s-deployer
中创建了 tooling
)。RoleBinding
/ClusterRoleBinding
。ServiceAccount
分配给 Pod。这是一个可以管理命名空间的示例:
# Create a service account
apiVersion: v1
kind: ServiceAccount
metadata:
name: k8s-deployer
namespace: tooling
---
# Create a cluster role that allowed to perform
# ["get", "list", "create", "delete", "patch"] over ["namespaces"]
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: k8s-deployer
rules:
- apiGroups: [""]
resources: ["namespaces"]
verbs: ["get", "list", "create", "delete", "patch"]
---
# Associate the cluster role with the service account
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: k8s-deployer
# make sure NOT to mention 'namespace' here or
# the permissions will only have effect in the
# given namespace
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: k8s-deployer
subjects:
- kind: ServiceAccount
name: k8s-deployer
namespace: tooling
之后,您需要像之前一样在 pod
spec
中提及服务帐户名称。有关 RBAC 的更多信息,请参阅文档。
您还需要添加 watch 动词。否则,如果您尝试删除命名空间,您会收到以下错误:
E0122 14:44:10.254508 9786 Reflector.go:147]供应商/k8s.io/client-go/tools/watch/informerwatcher.go:146:无法观看*非结构化。非结构化:未知