Namespaces
- Two boys, Mark Smith and Mark Williams.
- In each house, the father addresses their Mark as Mark and the other Mark in the other house as Mark Smith.
- Houses correspond to Namespaces in Kubernetes.
Defaultnamespace is created automatically by Kubernetes when it is first setup.- There are some other internal services that are created when Kubernetes first starts.
- This is called
kube-system
- This is called
- A third namespace automatically generated by Kubernetes is called
kube-public. This is made available to all users. - For production clusters, multiple namespaces are important.
- For example, having a
DevandProductionenvironment and then at the same time isolate the resources between them, create a different namespace for each. One beingDevand the other beingProduction. - Each namespace has a policy to define what can be done.
- A namespace can have a quota of resources assigned to it.
- For example CPU, disk usage and so on.
- The resources in a namespace can refer to each other by their
hostnames:web-prod,db-serviceandweb-deploymentas examples. This could be in the form ofmysql.connect("db-service")as an example. - A pod can reach a service in another namespace as well. The namespace has to be appended, as well as the service name. An example is
mysql.connect("db-service.dev.svc.cluster.local")- When a
serviceis created, a DNS entry is added with the above format. cluster.localis the default domain name of the Kubernetes cluster.svcstands forsub-domainfor the service.- In the above example,
service nameisdb-service,devis the namespace,svcis the service andcluster.localis the domain.
- When a
kubectl get podsonly lists the pods of the default namespace.- To list pods in another namespace, we run
kubectl get pods --namespace=<namespace_here>. - To create a pod in another namespace, use
kubectl create -f pod-definition.yml --namespace=dev- The above can also be performed in the
pod-definition.ymlfile, by adding thenamespaceoption to thepod-definition.ymlfile: ``` pod-definition.yml apiVersion: v1 kind: Pod
- The above can also be performed in the
metadata name: myapp-pod labels: app: myapp type: front-end spec: containers:
- name: nginx-container image: nginx ```
- The pod is created in the default namespace.
- To create the pod in another namespace, use the
--namespace=option:kubectl create -f pod-definition.yml --namespace=<namespace_here> - Can actually add the
namespacedefinition to thepod-definition.ymlfile, so you do not need to enter it from the commandline constantly: ``` pod-definition.yml apiVersion: v1 kind: Pod
metadata: name: myapp-pod namespace: dev labels: app: myapp type: front-end spec: containers:
- name: nginx-container image: nginx ```
- To create a new namespace, use a
ymlfile. An example is below: ``` namespace-dev.yml
apiVersion: v1 kind: Namespace metadata: name: dev
* Then run the `kubectl` command, such as `kubectl create -f namespace-dev.yml`.
* Else, you can create a namespace without needing the above `yml` file with:
kubectl create namespace
* How to switch to another namespace permanently. We can do that using `kubectl` to set the context of the current `namespace`:
kubectl config set-context $(kubectl config current-context) –namespace=
* Then can see these pods with `kubectl get pods` - no need to specify the namespace.
* To view pods in all namespaces, use `kubectl get pods --all-namespaces`
* `contexts` are used to manage multiple clusters and environments from the same management system.
* To limit resources in a `namespace`, create a Resource Quota. Under `spec`, provide the limits. To create a Resource Quota, see an example `yml` file below:
Compute-quota.yaml
apiVersion: v1
kind: ResourceQuota
metadata:
name: compute-quota
namespace:
spec: hard: pod: “10” requests.cpu: “4” requests.memory: 5Gi limits.cpu: “10” limits.memory: 10Gi ```
- Then create the file with
kubectl create -f compute-quota.yaml