Kubernetes - Namespace
Default namespace created by Kubernetes
- kube-system
- kube-public
- default
How it is helpful ?
- These different namespace
- can be used by same cluster for DEV and PROD environment.
- they will have isolated resources between them so that DEV will have their own resource and PROD will have their own resource.
- they will have their own policies that define who can do what
- Each namespace has a max limit of resources and are not allowed to use more than its allowed limit.
- We can define policy such that for example, while working in DEV environment, we can't modify a resource of PRODUCTION.
Example usage of custom namespace.
- Dev
- Test
- Prod
How to name resources of a namespace?
- Same namespace
- Resources in same namespace ca access via db-service
- Different namespace
- Resources in other namespace can access resources in other namespace via ( say DEV resources via - db-service.dev.svc.cluster.local
Name details
db-service.dev.svc.cluster.local
- db-service - Name of the service
- dev - namespace
- svc - service
- cluster.local - domain.
How to create Namspace Or Pod Inside Namespace
- Create in default namespace
- kubectl create -f pod-definition.yml -f
- Create a particular namespace
- kubectl create -f pod-definition --namespace dev
- kubectl create -f pod-definition -n dev
- Create a Namespace
- kubectl create -f namespace-dev.yaml
- kubectl create namespace dev
- Sample Yaml file for Namespace
- apiVersion: v1
- kind: Namespace
- metadata:
- name: dev
- Create POD under a namespace through Yaml file
- apiVersion: v1
- kind: Pod
- metadata
- name: myapp-pod
- namespace: dev
- label
- app: myapp
- type: front-end
- spec
- container:
- name: nginx-container
- image: nbinx
- Accessing Namespace
- Scenario -1 : Access default namespace
- kubectl get pods
- kubectl get pods --namespace dev
- kubectl get pods --namespace prod
- Change the default namespace to namespace = dev
- kubectl config set-context $(kubectl config current-context) --namespace dev
- kubectl get pods <<<< This will show Pods from namespace = DEV.
- kubectl get pods --namespace default << To access default namespace
- kubectl get pods --namespace prod << To access PROD namespace
- Resource Quota
- Resources can be limited in a namespace.
- apiVersion: v1
- kind: ResourceQuota
- metadata:
- name: compute-quota
- namespace: dev
- spec:
- hard:
- pods: "10",
- requests-cpu: "4"
- requests-memory: 5Gi
- limts-cpu: "10"
- limits-memory: 10Gi