New to Kubernetes. How do I rename the cluster after kubeadm init. The default cluster name is kubenetes and I want to rename it to something more meaningful. Searched all around and can not find any instructions. Thanks!
2 Answers
As you can read here:
During
kubeadm init, kubeadm uploads theClusterConfigurationobject to your cluster in a ConfigMap calledkubeadm-configin thekube-systemnamespace. This configuration is then read duringkubeadm join,kubeadm resetandkubeadm upgrade. To view this ConfigMap callkubeadm config view.
Apart from kubeadm config view you can use kubectl get configmaps -n kube-system kubeadm-config -o yaml to view this ConfigMap.
You can change your kubernetes cluster name simply by editing kubeadm-config ConfigMap using following command:
kubectl edit configmaps -n kube-system kubeadm-config
change value of clusterName field e.g.:
clusterName: new-fancy-kubernetes-clustername
After saving changes to the file you'll see confirmation of successful edit:
configmap/kubeadm-config edited
Now you can view your new cluster name using kubeadm config view command:
# kubeadm config view
...
clusterName: new-fancy-kubernetes-clustername
...
or this way:
# kubectl get configmaps -n kube-system kubeadm-config -o yaml
...
clusterName: new-fancy-kubernetes-clustername
...
From kubectl perspective your kubernetes cluster can be named totally differently than in kubeadm-config ConfigMap. They are configured independently. Actually in .kube/config file you can refer to your cluster by any name you want, but you need to make the change both in clusters as well as in contexts sections. Look at the example below:
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: ...
server: https://10.123.0.2:6443
name: yet-another-fancy-name
contexts:
- context:
cluster: yet-another-fancy-name
user: kubernetes-admin
name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
user:
client-certificate-data: ...
You may also want to change your context name to reflect the current cluster name, but you don't have to. You can do it just for the sake of consistency:
contexts:
- context:
cluster: yet-another-fancy-name
user: kubernetes-admin
name: kubernetes-admin@yet-another-fancy-name
current-context: kubernetes-admin@yet-another-fancy-name
- 615
I had a similar situation where I needed to manage two cluster remotely so I decided to use kubectx tool. I am using version 0.9.4 for kubectx.
I created a .bash_profile file (I did not have it) and add the KUBECONFIG variable pointing to the two config files for my clusters. My clusters were created using kubeadm and initially they were created without a specific name. This created a problem when trying to use kubectx, because when I use kubectx it was only showing one cluster.
To solve this, I had to change the name of the context in the context block
- context:
cluster: new-name
user: kubernetes-admin
name: kubernetes-admin@new-name
current-context: kubernetes-admin@new-name
Then kubectx was able to list my two clusters.
- 101
kubeadm-config ConfigMapdon't affect in any way what you have in config files. I guess you can always update them manually. – mario Feb 01 '20 at 00:44kubectl config get-contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE kubernetes-admin@kubernetes kubernetes kubernetes-admin
kubectl get pods
The connection to the server localhost:8080 was refused - did you specify the right host or port?
If I switch back to the old default context then everything works. How do I start to connect using new cluster name? Thanks!
– user558100 Feb 01 '20 at 02:44kubectlperspective your kubernetes cluster can be named totally differently than inkubeadm-configConfigMap. They are configured independently. Actually in.kube/configfile you can refer to your cluster by any name you want, but you need to make the change both inclustersas well as incontextssections. Please see my edited answer. – mario Feb 01 '20 at 15:03