Kubernetes – Namespace
Adding a namespace to a resource name helps prevent name collisions when multiple teams are using the same cluster. It serves as a virtual wall between clusters.
Functionality of Namespace
A Namespace in Kubernetes has the following features:
- By using the same namespace, pods can communicate.
- A namespace is a virtual cluster that sits on top of a physical cluster.
- Teams and environments are logically separated.
Create a Namespace
Create a namespace with this command.
apiVersion: v1 kind: Namespce metadata name: elk
Control the Namespace
Namespaces are controlled with the following command.
$ kubectl create –f namespace.yml ---------> 1 $ kubectl get namespace -----------------> 2 $ kubectl get namespace <Namespace name> ------->3 $ kubectl describe namespace <Namespace name> ---->4 $ kubectl delete namespace <Namespace name>
As shown in the code above,
- This command creates a namespace.
- You’ll see all the namespaces available.
- You’ll get a namespace whose name you specify.
- Detailed information about the service will be provided here.
- The namespace will be deleted.
Service Namespace – Example
The following is an example of a service file that uses namespaces.
apiVersion: v1 kind: Service metadata: name: elasticsearch namespace: elk labels: component: elasticsearch spec: type: LoadBalancer selector: component: elasticsearch ports: - name: http port: 9200 protocol: TCP - name: transport port: 9300 protocol: TCP
We’re using the same namespace under service metadata with the name elk.