Kubernetes Classroom Series – 18/Oct/2019, Namespaces & Jobs

Namespaces

  • Problem-1: Multiple Environments created in same cluster, creates confusion when we execute kubectl commands
kubectl get pods
# all the pods of different enviroments

kubect get deployments
# all the deployments of different  environments

  • Problem-2: You are ok with multiple environments in a cluster. lets assume you are creating springpetclinic
apiVersion: v1
kind: Pod
metadata:
  name: spclinic
  ...
  ...
  ...

  
  • Problem-2 :

    • Here the problem is we might be using same spec/manifest for different environment. At least same names for pods, rc, deployments etc.
    • K8S will not allow to create two objects of same kind with same name
  • To avoid problems like above, We create a virtual cluster and Namespace is virtual cluster

  • The default Namespace of Kubernetes is default

Exploring namespaces

  • Execute the following commands
kubectl get namespaces
  • Create two YAML files for test & dev environments
apiVersion: v1
kind: Namespace
metadata:
  name: dev-env
apiVersion: v1
kind: Namespace
metadata:
  name: dev-env
  • Execute kubectl apply commands and then kubectl get namespaces
  • create a Pod with the following info
---
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: dev-env
spec:
  containers:
    - image: nginx
      name: nginx-demo
      ports:
        - containerPort: 80
          protocol: TCP
  • Execute kubecl apply -f command
  • Then execute the following
kubectl get pods
kubectl get pods --all-namespaces
kubectl get pods -n dev-env
  • Create one more nginx pod in test-env
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: dev-env
spec:
  containers:
    - image: nginx
      name: nginx-demo
      ports:
        - containerPort: 80
          protocol: TCP

  • Now execute the following commands
kubectl apply -f nginx-test-pod.yml
kubectl get pods -n test-env
kubectl get pods --all-namespaces

Jobs & Cron Jobs

  • Jobs will create containers which run for finite time.
  • Cron Jobs repeatedly will create containers which run for finite time.
  • Will appear under api group batch
  • Sample Job
apiVersion: batch/v1
kind: Job
metadata:
  # Unique key of the Job instance
  name: example-job
spec:
  template:
    metadata:
      name: example-job
    spec:
      containers:
      - name: pi
        image: perl
        command: ["perl"]
        args: ["-Mbignum=bpi", "-wle", "print bpi(2000)"]
      # Do not restart containers after they exit
      restartPolicy: Never

2 thoughts on “Kubernetes Classroom Series – 18/Oct/2019, Namespaces & Jobs

  1. While we are creating 2nd namespace name is wrongly typed name: dev-env it suppose to be name: test-env.

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

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner