Namespaces and Services in Kubernetes.

Namespaces and Services in Kubernetes.

ยท

3 min read

What are Namespaces?

  • In Kubernetes, Namespaces are used to create isolated environments resources. Each Namespace is a separate cluster within the same physical cluster.

Initial namespaces

Four Namespaces already in Kubernetes (Predefined):

default

  • Kubernetes includes this namespace so that you can start using your new cluster without first creating a namespace.

kube-node-lease

  • This namespace holds Lease objects associated with each node. Node leases allow the kubelet to send heartbeats so that the control plane can detect node failure.

kube-public

  • This namespace is readable by all clients (including those not authenticated). This namespace is mostly reserved for cluster usage, in case that some resources should be visible and readable publicly throughout the whole cluster. The public aspect of this namespace is only a convention, not a requirement.

kube-system

  • The namespace for objects created by the Kubernetes system.

How to check namespaces in the Kubernetes cluster?

kubectl get namespace

Task 1:

Create a Namespace for your Deployment.

mizan@mizan:~/projects/kubernetes$ cat deployment.yaml
apiVersion: v1

kind:pod

metadata:

  name: todo-app

spec:

  containers:

  - name: todo-app

    image: mizanfirdausi/django-todo:latest

    ports:

    - containerPort: 8000 
  mizan@mizan:~/projects/kubernetes$
  • To create a Namespace use the command
kubectl create namespace <namespace-name>
  • Apply the updated deployment using the command:
kubectl apply -f deployment.yml -n <namespace-name>
mizan@mizan:~/projects/kubernetes$ kubectl create namespace todo-app
namespace/todo-app created
mizan@mizan:~/projects/kubernetes$ kubectl apply -f deployment.yaml -n todo-app
pod/to
do-app created
mizan@mizan:~/projects/kubernetes$ kubectl get namespace
Name              STATUS     AGE
default           Active     35d
kube-node-lease   Active     35d
kube-public       Active     35d
kube-system       Active     35d
todo-app          Activ
e     13s
mizan@mizan:~/projects/kubernetes$

Also, verified by using the command:

kubectl get namespace

Task 2:

What are Services?

  • Services are used to expose your Pods and Deployments to the network.

58-image-2

  • Types of services : ClusterIP, NodePort, LoadBalancer, and ExternalName.

kubernates-services

ClusterIP

  • ClusterIP is the default type of service, which is used to expose a service on an IP address internal to the cluster.

  • Access is only permitted from within the cluster.

NodePort

  • NodePorts are open ports on every cluster node. Kubernetes will route traffic come into NodePort to the service, even if the service is not running on that node.

  • NodePort is intended as a foundation for other higher-level methods of ingress such as load balancers and are useful in development.

LoadBalancer

  • For clusters running on public cloud providers like AWS or Azure, creating a load LoadBalancer service provides an equivalent to a clusterIP service, extending it to an external load balancer that is specific to the cloud provider.

  • Kubernetes will automatically create the load balancer, provide firewall rules if needed, and populate the service with the external IP address assigned by the cloud provider.

ExternalName

  • ExternalName services are similar to other Kubernetes services; however, instead of being accessed via a clusterIP address, it returns a CNAME record with a value that is defined in the externalName: parameter when creating the service.
ย