To create a new namespace in Kubernetes, you can use the kubectl create namespace command. For example, to create a namespace called “my-namespace”, you can run the following command:
kubectl create namespace my-namespace
Once you create the namespace, you can use it to deploy resources. For example, if you want to deploy a pod in the “my-namespace” namespace, you can use the --namespace flag:
kubectl run my-pod --image=my-image --namespace=my-namespace
You can also view the resources in a namespace using the kubectl get command with the --namespace flag:
kubectl get pods --namespace=my-namespace
By default, if you don't specify a namespace when running a command, Kubernetes will use the “default” namespace. To switch to the newly created namespace, run the following command:
kubectl config set-context --current --namespace=my-namespace
The above command will set the current context to the "my-namespace" namespace so that all subsequent commands will be executed in that namespace, and you don't need to use the --namespace flag.
To get detailed information on the namespace, use the below command with the "describe" keyword.
kubectl describe namespaces my-namespace
You can set resource limits and requests for a pod or container using the kubectl command with the limits and requests flags. For example, to set a limit of 1 CPU and 512MB of memory for a container in a pod, you can use the following command:
kubectl run my-pod --image=my-image --limits=cpu=1,memory=512Mi --requests=cpu=0.5,memory=256Mi
The above command creates a new pod called “my-pod” with an image called “my-image” and sets a limit of 1 CPU and 512MB of memory and a request of 0.5 CPU and 256MB of memory for the container.
You can also set resource limits and requests for an existing pod or container using the kubectl patch command. For example, to update the resource limits and requests for a container in a pod, you can use the following command:
kubectl patch pod my-pod -p '{"spec" :{ "containers":[{"name": "my-container", "resources" :{ "limits" :{ "cpu": "1", "memory": "512Mi"}, "requests" :{ "cpu": "0.5", "memory": "256Mi"}}}]}}'
The above command updates the resource limits and requests for the container named “my-container” in “my-pod” pod.
You can use the below command to delete the specified namespace.
kubectl delete namespaces my-namespace