convert kubernetes yaml to kubectl cli command

4/23/2018

kubectl I am looking for a single command or maybe combination of commands for the following yaml file

---
#
# 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

So pretty much create a namespace that can get, watch and list pods

-- uberrebu
kubectl
kubernetes

1 Answer

4/23/2018

This assumes the namespace and service account are already created:

$ kubectl create namespace mynamespace

$ kubectl create serviceaccount mysa -n mynamespace

Create the role with:

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

Create the rolebinding with:

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

Tip: if you want to preview the generated YAML rather than actually creating the resource, append --dry-run=true -o=yaml to the commands.

-- Michael Hausenblas
Source: StackOverflow