Topic outline

  • Create a Namespace

    In Kubernetes Namespaces are used as a mechanism to separate different projects and concerns in a Cluster mainly by subdividing your cluster into different "named spaces", a namespace. Some Kubernetes implementations and distributions are able to define a project and add one or more namespaces to a project.

    Namespaces like most other resources in a cluster can be created in an imperative or declarative way.

    Please create a namespace the imperative way with the `kubectl` create command:

    k create ns <your name or project>

    Please create a namespace the declarative way through creating the yaml definition with --dry-run option and create the namespace with the yaml file:

    # generate a yaml file for your namespace, named <your-name>-test
    k create ns --dry-run <your name>-test -o yaml > <your-name>-test-ns.yaml
    k create -f <your-name>-test-ns.yaml
    # get the namespace
    k get ns

    The declarative presentation of a namespace with the generated yaml file:
    • Create Pods

      What is a Pod?

      A Pod is the smallest unit as the basic building block in Kubernetes, it is a co-located group of one or more containers. We deploy and operate one or more containers on Kubernetes always in a pod, which is managed and scheduled through a controller. A pod runs always on a worker node and can have multiple replicas which run on different workers, but a single pod never spans over multiple worker nodes. Multiple containers in a pod share the same network interface and the hostname, but their filesystem is isolated, since a container's filesystem is mounted through the container image, but it is possible to share directories among containers through volumes. Multiple containers in a pod share also the same IP address and the port space, which means containers which are running in the same pod can't bind to the same port number!

      Pods in Action

      In the practice we never create a pod's definition from scratch by writing the yaml file by hand, either we re-use existing yaml files or very often create them through kubectl run or kubectl create commands with the --dry-run option and write the output to a yaml file to extend and adapt. 
      The declarative representation of a nginx-pod is very simple like this:
      To create the yaml presentation of a pod and create it afterwards using kubectl create, please use the following commands:
      • Create Deployments

        A Deployment in Kubernetes is a controller which controls how pods are created, scheduled and scaled through a replica set.

        • Get events and logs, describe objects

          Please run the following commands, your instructor will explain what's going on :-)

          • Problem 1: CrashLoopBackOff

            Running the following:
            kubectl run tmp --image busybox --generator run-pod/v1 --rm -it -- echo 'salam'

            leads to CrashLoopBackOff, why?
            What does CrashLoopBackOff mean?
            And what does --rm -it used for ?


            The container does not get ready, since there is no process running inside the container which is running in the pod. If you run something like this:

            kubectl run tmp --image busybox --generator run-pod/v1 -- sleep "10"

            you’ll see it will run, goes into backoff state and will run again, run the following commands in 2 terminals:

            watch -n 1 kubectl get pods
            kubectl get events -w

            And if you try this:

            kubectl run -it --rm tmp --image busybox --generator run-pod/v1 -- sleep "30"

            you’ll see that after 30 seconds, the container exits and gets terminated, use the watch command above and get the latest events with:

            kubectl get events --sort-by=.metadata.creationTimestamp -w

             means remove the container after exit, -it attaches interactively to tty.

            Run the following commands and see what happens:

            kubectl run tmp --image busybox --generator run-pod/v1 -it --rm -- sleep "10"; echo "‏اَلسَّلَامُ عَلَيْكُم — وَعَلَيْكُمُ اَلسَّلَامُ‎"
            watch -n 1 kubectl get pods
            kubectl get events --sort-by=.metadata.creationTimestamp -w 

            Related Post: