Changing the default Namespace

As in the previous introduction, we can change the default Namespace by switching the current context in kubeconfig to another one:

  1. First, we may check the current context with the subcommand config:
// check the current context in kubeconfig
$ kubectl config current-context
kubernetes-admin@kubernetes

You may feel unfamiliar with the output when checking the current context. The value of the preceding current context is defined and created by kubeadm. You could get minikube shown on screen if you leveraged minikube as your Kubernetes system management tool.

  1. No matter what you got from checking the current context in kubeconfig, use the subcommand config set-context to create a new context:
// create a new context called "my-context"
// the new context is going to follow the cluster and the user of current context, but attached with new Namespace
//This is for kubeadm environment
$ kubectl config set-context my-context --namespace=my-namespace --cluster=kubernetes --user=kubernetes-admin
Context "my-context" created.
  1. The preceding command is based on kubeadm managed Kubernetes; you may fire a similar one for minikube, with the names of the default cluster and user in kubeconfig:
// for minikube environemt
$ kubectl config set-context my-context --namespace=my-namespace --cluster=minikube --user=minikube
  1. Next, check kubeconfig to verify the changes:
//check kubectlconfig for the new context
$ kubectl config view
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: REDACTED
server: https://192.168.122.101:6443
name: kubernetes
contexts:
- context:
cluster: kubernetes
user: kubernetes-admin
name: kubernetes-admin@kubernetes
- context:
cluster: kubernetes
namespace: my-namespace
user: kubernetes-admin
name: my-context
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
user:
client-certificate-data: REDACTED
client-key-data: REDACTED

When checking the configuration of kubeconfig, in the section of contexts, you can find a context named exactly as what we defined and which also takes our newly created Namespace.

  1. Fire the following command to switch to using the new context:
$ kubectl config use-context my-context
Switched to context "my-context".
// check current context
$ kubectl config current-context
my-context

Now the current context is our customized one, which is along with the Namespace my-namespace.

  1. Since the default Namespace is changed to my-namespace, it is possible that we can get the Deployment without specifying the Namespace:
$ kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
my-nginx 1 1 1 1 20m

//double check the namespace of resource
$ kubectl describe deployment my-nginx
Name: my-nginx
Namespace: my-namespace
CreationTimestamp: Mon, 18 Dec 2017 15:39:46 -0500
Labels: run=my-nginx
:
(ignored)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.82.21