Skip to content

Instantly share code, notes, and snippets.

@cywf
Created February 22, 2023 13:12
Show Gist options
  • Save cywf/143c3e06eb64a2e4803e1f904e8e1ab6 to your computer and use it in GitHub Desktop.
Save cywf/143c3e06eb64a2e4803e1f904e8e1ab6 to your computer and use it in GitHub Desktop.
Quick guide for understanding Kubernetes Namespaces conceptually, with practical examples

Namespaces in Kubernetes - a practical guide by ledgendary cowboy, Bugs Bunny

image

Howdy, partner! You wanna know about these here namespaces in Kubernetes? Well, I reckon I can help you with that!

Now listen up, 'cause I'm gonna tell you all about namespaces in Kubernetes. Y'see, a namespace is like a little corral for your cattle, but instead of cattle, it's for your Kubernetes resources. And just like you wouldn't wanna mix up your cows and your horses, you don't wanna mix up your resources either!

Now, let's say you got a big ol' ranch, and you got different cowboys working on different parts of the ranch. Each cowboy has their own corral for their cattle, right? Well, that's kinda like namespaces in Kubernetes. You can have different namespaces for different parts of your application, so different cowboys can work on their own corral without messing up somebody else's.

For example, you might have a namespace for your development environment, where you test out new features and make sure everything's workin' like it should. And then you might have another namespace for your production environment, where the real cowboys work and make sure everything's runnin' smooth.

Now, let's say you got a new cowboy comin' in to work on your ranch. You don't want 'em to accidentally mess up somebody else's corral, do ya? No sir! That's why you give 'em their own corral to work in. And that's why you might create a new namespace for your new cowboy to work in. That way, they can test out their new features without steppin' on somebody else's toes.


Creating Namespaces

Let's start by creating a namespace for our development environment. We'll call it **folkvarlabs-dev**:

apiVersion: v1
kind: Namespace
metadata:
  name: folkvarlabs-dev

Next, let's create a namespace for our production environment. We'll call it **folkvarlabs-prod**:

apiVersion: v1
kind: Namespace
metadata:
  name: folkvarlabs-prod

And let's create one more namespace for our testing purposes. We'll call it **folkvarlabs-test**:

apiVersion: v1
kind: Namespace
metadata:
  name: folkvarlabs-test

Now, let's say we want to create a deployment in our **folkvarlabs-dev** namespace. We can use the following YAML:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mainframe
  namespace: folkvarlabs-dev
spec:
  selector:
    matchLabels:
      app: mainframe
  replicas: 3
  template:
    metadata:
      labels:
        app: mainframe
    spec:
      containers:
      - name: mainframe
        image: myregistry/mainframe:v1
        ports:
        - containerPort: 80

This will create a deployment for our mainframe application in the folkvarlabs-dev namespace. It will create 3 replicas of our application and expose it on port 80.

Next, let's say we want to create a service for our mainframe application, so we can access it from outside the cluster. We can use the following YAML:

apiVersion: v1
kind: Service
metadata:
  name: mainframe
  namespace: folkvarlabs-dev
spec:
  selector:
    app: mainframe
  ports:
    - name: http
      port: 80
      targetPort: 80
  type: LoadBalancer

This will create a service for our mainframe application in the folkvarlabs-dev namespace. It will expose the application on port 80 and create a load balancer to route traffic to our application.

And finally, let's say we want to create a config map for our alt-f4 society application, which contains some configuration data for our application. We can use the following YAML:

apiVersion: v1
kind: ConfigMap
metadata:
  name: alt-f4-config
  namespace: folkvarlabs-prod
data:
  config.yaml: |-
    database:
      host: localhost
      port: 5432
      name: alt_f4
      username: alt_f4_user
      password: supersecret

This will create a config map for our alt-f4 society application in the folkvarlabs-prod namespace. It contains a config.yaml file with some configuration data for our application.


Recap

So there ya have it, partner. Namespaces in Kubernetes are like little corrals for your resources, so you can keep 'em separated and organized. Just like how you wouldn't mix up your cattle and your horses, you don't wanna mix up your resources either. So, create different namespaces for different parts of your application, and make sure each cowboy has their own corral to work in. Yeehaw!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment