In a company, it can be very useful to create isolated environments for developers and teams, especially for training. If you’re considering Kubernetes, you need to make sure developers will be comfortable with it, and giving them a safe place to play around will help them on-boarding that technology.

It’s particularly true in a microservices architecture, where you will want to test your app in an isolated environment before releasing it for other teams to use. It is also useful when the workloads are too heavy to run on a single laptop (for example: testing Machine Learning algorithms).

You can achieve that level of isolation with Kubernetes using namespaces.

Namespaces are a way to divide cluster resources between multiple users.

In this post, we will create a namespace, and then create a service account that only has access to that particular namespace, using Kubernetes’s Role-Based Access Control (RBAC) system. Finally, we will export the config needed to access that namespace.

Prerequisites

A Kubernetes cluster with enough access to create namespaces and service accounts.

Let’s go

1️⃣ Create Namespace

kubectl create namespace mynamespace

2️⃣ Create Service Account with permissions

Open a new file. Let’s call it access.yaml. We’re going to create the user (service account), a role, and attach that role to that user.

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: mynamespace-user
  namespace: mynamespace

---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: mynamespace-user-full-access
  namespace: mynamespace
rules:
- apiGroups: ["", "extensions", "apps"]
  resources: ["*"]
  verbs: ["*"]
- apiGroups: ["batch"]
  resources:
  - jobs
  - cronjobs
  verbs: ["*"]

---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: mynamespace-user-view
  namespace: mynamespace
subjects:
- kind: ServiceAccount
  name: mynamespace-user
  namespace: mynamespace
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: mynamespace-user-full-access

As you can see, in the Role definition, we add full access to everything in that namespace, including batch types like jobs or cronjobs. As it is a Role, and not a ClusterRole, it is going to be applied to a single namespace: mynamespace. For more details about roles in Kubernetes, check out the official documentation.

Now, let’s create all of this:

kubectl create -f access.yaml

You should see the three components being created.

3️⃣ Get Secrets

The first thing we need to do now is to get the name of the service account’s secret. Run the following command and copy the name of the secret.

kubectl describe sa mynamespace-user -n mynamespace

For this tutorial, let’s say that the secret is named mynamespace-user-token-xxxxx.

We now need to get the service account’s Token and the Certificate Authority. For this, we are going to read them using kubectl. Now, as Kubernetes secrets are base64 encoded, we’ll also need to decode them.

Here’s how you get the User Token:

kubectl get secret mynamespace-user-token-xxxxx -n mynamespace -o "jsonpath={.data.token}" | base64 -D

And here’s how you get the Certificate:

kubectl get secret mynamespace-user-token-xxxxx -n mynamespace -o "jsonpath={.data['ca\.crt']}"

4️⃣ Create Kube config

We now have everything we need. The only thing remaining is creating the Kube config file, with the data we previously gathered:

apiVersion: v1
kind: Config
preferences: {}

# Define the cluster
clusters:
- cluster:
    certificate-authority-data: PLACE CERTIFICATE HERE
    # You'll need the API endpoint of your Cluster here:
    server: https://YOUR_KUBERNETES_API_ENDPOINT
  name: my-cluster

# Define the user
users:
- name: mynamespace-user
  user:
    as-user-extra: {}
    client-key-data: PLACE CERTIFICATE HERE
    token: PLACE USER TOKEN HERE

# Define the context: linking a user to a cluster
contexts:
- context:
    cluster: my-cluster
    namespace: mynamespace
    user: mynamespace-user
  name: mynamespace

# Define current context
current-context: mynamespace

And we’re done! 🎉

Note: another way to write the Kube config is to use kubectl directly. See kubectl config command reference.

You can now give this Kube config file to the user you wanted to give access to.