将kubernetes yaml转换为kubectl cli命令

时间:2018-04-23 03:25:41

标签: kubernetes kubectl

kubectl我正在寻找以下yaml文件的单个命令或命令组合

---
#
# Create a role, `pod-reader`, that can list pods and
# bind the default service account in the `mynamespace` namespace
# to that role.
#

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: pod-reader
  namespace: mynamespace
rules:
- apiGroups: [""] # "" indicates the core API group
  resources: ["pods"]
  verbs: ["get", "watch", "list"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: read-pods
  namespace: mynamespace
subjects:
- kind: Group
  name: system:serviceaccounts:mynamespace
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: pod-reader
  apiGroup: rbac.authorization.k8s.io

因此,创建一个可以获取,观察和列出pod的命名空间

1 个答案:

答案 0 :(得分:4)

这假定已创建名称空间和服务帐户:

$ kubectl create namespace mynamespace

$ kubectl create serviceaccount mysa -n mynamespace

使用以下命令创建角色:

$ kubectl create role pod-reader --namespace=mynamespace \
    --verb=get,list,watch \
    --resource=pods \

使用以下命令创建角色绑定:

$ kubectl create rolebinding read-pods --namespace=mynamespace \
    --role=pod-reader \
    --group=system:serviceaccounts:mynamespace

提示:如果要预览生成的YAML而不是实际创建资源,请将--dry-run=true -o=yaml附加到命令。