imageIn Kubernetes, namespaces provides a mechanism for isolating groups of resources within a single cluster.

Names of resources need to be unique within a namespace, but not across namespaces.

Namespace-based scoping is applicable only for namespaced objects (e.g. Deployments, Services, etc) and not for cluster-wide objects (e.g. StorageClass, Nodes, PersistentVolumes, etc).

When to Use Multiple Namespaces

Namespaces are intended for use in environments with many users spread across multiple teams, or projects.

For clusters with a few to tens of users, you should not need to create or think about namespaces at all.

Start using namespaces when you need the features they provide.

Namespaces provide a scope for names. Names of resources need to be unique within a namespace, but not across namespaces.

Namespaces cannot be nested inside one another and each Kubernetes resource can only be in one namespace.

Namespaces are a way to divide cluster resources between multiple users (via resource quota).

It is not necessary to use multiple namespaces to separate slightly different resources, such as different versions of the same software: use labels to distinguish resources within the same namespace.

Task: Create Kubernetes cluster with 3 worker nodes.

Master: 1 node

Worker: 2 node

Hint

Solution

Create docker hub account. Docker Hub if you already have one skip this step

Open Play with Kubernetes login with your docker hub account.

Click on start

It will start a 4 hr session

create three instance

click on + ADD NEW INSTANCE three time to add three instances

imageon first instance enter below command, this node will be master node
kubeadm init --apiserver-advertise-address $(hostname -i) --pod-network-cidr 10.5.0.0/16

enter below command on first node

kubectl apply -f https://raw.githubusercontent.com/cloudnativelabs/kube-router/master/daemonset/kubeadm-kuberouter.yaml

capture output of kubeadm join XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

you may also use kubeadm token list to find token

use this command on second and third node kubeadm join <IP address of master/first node>:6443 –token –discovery-token-unsafe-skip-ca-verification

image

enter captured command in second and third node

kubeadm join  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
imageimageCheck node status, all 3 nodes should be in ready state image

Task: List all namespaces in kubernetes cluster

Solution
kubectl get namespace

cleanup}}

Task: Create New namespace test in Kubernetes cluster

Use Kubectl command

verify with below command

kubectl get namespace
Solution

this command will create namespace test

kubectl create namespace test

Task: Create a pod with below details

name: web

image: nginx

namespace: test

Use Kubectl command

verify with below command

kubectl get pods --namespace=test
Solution

this command will create pod with image nginx and name web in namespace test

kubectl run web --image=nginx --namespace=test

You may also use -n instead of –namespace

kubectl run web --image=nginx -n test

Task: Expose port on pod created in previous step using Kubectl, port should be accessible from outside Kubernetes cluster

name: web

image: nginx

port: 80

namespace: test

verify with below command

kubectl get all -n test
Solution
 kubectl expose pod web --port 80 --name=nginx-svc --type=NodePort --target-port=80 -n test

Task: Find port used by nodeport service in Kubernetes to expose container

Solution
kubectl get all --namespace=test

Task: Access pod port exposed in previous step using browser

Solution

Task: Create New namespace test1 in Kubernetes cluster using YAML

verify with below command

kubectl get namespace
Solution

create a new file ns.yaml

vi ns.yaml

Press i to get in insert mode

apiVersion: v1
kind: Namespace
metadata:
  name: test1

use escape to exit insert mode and :wq to save and exit vi

apiVersion, Kind, metadata.name and spec are required field

Below command will create namespace using yaml file

kubectl apply -f ns.yaml

Task: Create a pod with name demo and image nginx in namespace test1

name: demo
image: nginx
namespace: test1

Use Kubectl command

verify with below command

kubectl get pods -n test1
Solution

create a new file pod.yaml

vi pod.yaml

Press i to get in insert mode

apiVersion: v1
kind: Pod
metadata:
  name: demo
  labels:
    app: demo
    type: web
  namespace: test1  
spec:
  containers:
  - name: demo-nginx
    image: nginx
    ports:
    - containerPort: 80

use escape to exit insert mode and :wq to save and exit vi

apiVersion, Kind, metadata.name and spec are required field

you may add labels

you can use any key: value pairs for labels

labels are used to select pods

provide name to container

provide image for container

kubectl apply -f pod.yaml

this command will create pod using yaml file

Task: Create a ClusterIP service using yaml

name: web1-svc

selector: app=demo

verify that a ClusterIP service has been created with below command

kubectl get svc web1-svc -n test1
kubectl get ep -n test1
kubectl get all -n test1
Solution

create a new file svc.yaml

vi svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: web1-svc
  namespace: test1   
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: demo
  type: ClusterIP # this is optional as ClusterIP is default type

use escape key and :wq to save and exit vi.

apiVersion, Kind, metadata.name and spec are required field.

kubectl apply -f svc.yaml

this command will create service using yaml file

Task: Create a pod using Kubectl and access the service created in previous step

name: jumphost

image: busybox
Solution

Get IP address of service by running below command

kubectl get svc web

Create a new pod and connect to its shell

kubectl run jumphost --image=busybox -it  -- sh

Use IP address to access the service in new pod

wget -O- web1-svc.test1

Task: Delete all open nodes/instances and close session

  1. Select the node and click on DELETE
  2. Repeat same for any other open nodes
  3. click close session

cleanup}}


Click on ‘Submit Feedback’ on the bottom left of the page to submit any questions/feedback.