<p>I was testing some commands and I ran</p>

<pre><code>$ kubectl delete nodes --all
</code></pre>

<p>and it <strike>deletes</strike> de-registers all the nodes including the masters. Now I can't connect to the cluster (Well, Obviously as the master is deleted).</p>

<p>Is there a way to prevent this as anyone could accidentally do this?</p>

<p>Extra Info: I am using KOps for deployment.</p>

<p>P.S. It does not delete the EC2 instances and the nodes come up on doing a EC2 instance reboot on all the instances.</p>

<p>By default, you using something like a superuser who can do anything he want with a cluster.</p>

<p>For limit access to a cluster for other users you can use <a href="https://kubernetes.io/docs/admin/authorization/rbac" rel="nofollow noreferrer">RBAC</a> authorization for. By RBAC rules you can manage access and limits per resource and action.</p>

<p>In few words, for do that you need to:</p>

<ol>
<li><p>Create new cluster by Kops with <code>--authorization RBAC</code> or modify existing one by adding 'rbac' option to cluster's configuration to 'authorization' section:</p>

<p><code>authorization:
  rbac: {}</code></p></li>
<li><p>Now, we can follow <a href="https://docs.bitnami.com/kubernetes/how-to/configure-rbac-in-your-kubernetes-cluster/" rel="nofollow noreferrer">that</a> instruction from Bitnami for create a user. For example, let's creating a user which has access only to <code>office</code> namespace and only for a few actions. So, we need to create a namespace firs:</p>

<p><code>kubectl create namespace office</code></p></li>
<li><p>Create a key and certificates for new user:</p>

<p><code>openssl genrsa -out employee.key 2048</code><br>
<code>openssl req -new -key employee.key -out employee.csr -subj "/CN=employee/O=bitnami"</code></p></li>
<li><p>Now, using your CA authority key (It available in the S3 bucket under PKI) we need to approve new certificate:</p>

<p><code>openssl x509 -req -in employee.csr -CA CA_LOCATION/ca.crt -CAkey CA_LOCATION/ca.key -CAcreateserial -out employee.crt -days 500</code></p></li>
<li><p>Creating credentials:</p>

<p><code>kubectl config set-credentials employee --client-certificate=/home/employee/.certs/employee.crt  --client-key=/home/employee/.certs/employee.key</code></p></li>
<li><p>Setting a right context:</p>

<p><code>kubectl config set-context employee-context --cluster=YOUR_CLUSTER_NAME --namespace=office --user=employee</code></p></li>
<li><p>New we have a user without access to anything. Let's create a new role with limited access, here is example of Role which will have access only to deployments, replicasets and pods for create, delete and modify them and nothing more. Create file <code>role-deployment-manager.yaml</code> with Role configuration:</p></li>
</ol>

<p><code>kind: Role
  apiVersion: rbac.authorization.k8s.io/v1beta1
  metadata:
    namespace: office
    name: deployment-manager
  rules:
  - apiGroups: ["", "extensions", "apps"]
    resources: ["deployments", "replicasets", "pods"]
    verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]</code></p>

<ol start="8">
<li>Create a new file <code>rolebinding-deployment-manager.yaml</code> with Rolebinding, which will attach your Role to user:</li>
</ol>

<p><code>kind: RoleBinding
  apiVersion: rbac.authorization.k8s.io/v1beta1
  metadata:
    name: deployment-manager-binding
    namespace: office
  subjects:
  - kind: User
    name: employee
    apiGroup: ""
  roleRef:
    kind: Role
    name: deployment-manager
    apiGroup: ""</code></p>

<ol start="9">
<li>Now apply that configurations:</li>
</ol>

<p><code>kubectl create -f role-deployment-manager.yaml
kubectl create -f rolebinding-deployment-manager.yaml</code></p>

<p>So, now you have a user with limited access and he cannot destroy your cluster.</p>
