Table of contents
- 1. What Is Namespace?
- 2. Create a Namespace for your Deployment
- 3. In K8s why do we need services?
- 4. Types of Services :
- 5.Troubleshooting:
- kubectl get [resource type (pod/services/ns)]: Lists the resources.
- kubectl describe [resource]: Display detailed information about a resource.
- kubectl logs [Pod name]: Display the logs from a container in a pod.
- kubectl exec [pod name] /bin/bash: Execute a command on a container in a pod.
1. What Is Namespace?
Kubernetes namespaces are a mechanism for isolating groups of resources within a single cluster. Resource names must be unique within a namespace but not across namespaces. We can define Kubernetes objects into a namespace. Names of resources need to be unique within a namespace, but not across namespaces.
While creating any objects if we don't define any namespace, by default it got created into the default namespace.
Namespaces help pod-to-pod communication using the same namespace.
Namespaces are virtual clusters that can sit on top of the same physical cluster.
They provide logical separation between the teams and their environments.
2. Create a Namespace for your Deployment
Use the command
kubectl create namespace <namespace-name>
to create a NamespaceUpdate the deployment.yml file to include the Namespace
Apply the updated deployment using the command:
kubectl apply -f deployment.yml -n <namespace-name>
Verify that the Namespace has been created by checking the status of the Namespaces in your cluster.
Step 1:
Login to your Minikube instance in AWS and start your Minikube server. Go inside your Django project which was cloned into a previous task.
Now create a namespace using the below command and check the namespaces status by using the kubectl get ns command.
Step 2:
Now update the [previously created ]deployment yaml file by including the recently created namespace[into metadata section].
Check the deployment.yml file.
Step 3:
Now, apply the updated deployment file by using the kubectl apply -f deployment.yml -n django-ns command and check the deployment & namespace status.
Step 4:
Verify the deployment status if the namespace has been updated successfully or not by using the below command, you will see within that the namespace deployment is showing.
3. In K8s why do we need services?
In k8s, when a pod is get created an IP address is allocated to that pod and Kube proxy component from the worker node is responsible for that. So if we want to communicate to any container inside the cluster we have to communicate it through a pod's IP and in a Node there can be many pods. But what will happen, if a pod gets deleted due to any reason like accidentally or gets crashed, then with the help of the Replicaset controller again a pod will get created within a sec, and its IP address will also get changed.
If anyone is accessing the container from the outside of the world, then it's not possible to know about the changed IP address and no one will hit by giving a Pod's IP address, they will always hit the DNS [like www. google .com]. So, a pod's Ip can keep changing.
To solve this issue We create a service object on top of a deployment object.
Whenever we create any service file, an IP address is created along with it, and if any pod gets failed due to any reason and a new pod gets created there will be no issue because a pod's IP will get mapped to the service's IP. So if anyone tries to communicate from the outer world by default the request will be redirected to the service's IP.
4. Types of Services :
There are mainly three types of Services used widely.
ClusterIP: Whenever you create a service yaml file a default IP will get allocated to it, this is known as ClusterIp.With the help of cluster IP, we can communicate with our application Internally within the cluster only.
NodePort: A NodePort service exposes the service on the IP of each node at a static port. A ClusterIP service is created automatically to route the traffic to the NordPort service. Users can communicate with the service from the outside.
LoadBalancer: This is the preferred solution to expose the cluster to the wider internet. The LoadBalancer type of service will create a load balancer (load balancer type depends on the cloud provider) and expose the service externally.
It will also automatically create ClusterIP and NodePort services and route traffic accordingly.
5.Troubleshooting:
kubectl get [resource type (pod/services/ns)]: Lists the resources.
kubectl describe [resource]: Display detailed information about a resource.
kubectl logs [Pod name]: Display the logs from a container in a pod.
kubectl exec [pod name] /bin/bash: Execute a command on a container in a pod.
Thank you for reading :)