6

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!

user558100
  • 63
  • 1
  • 3

2 Answers2

6

As you can read here:

During kubeadm init, kubeadm uploads the ClusterConfiguration object to your cluster in a ConfigMap called kubeadm-config in the kube-system namespace. This configuration is then read during kubeadm join, kubeadm reset and kubeadm upgrade. To view this ConfigMap call kubeadm 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
mario
  • 525
  • 3
  • 8
  • Thanks! The instructions worked. One more question, Although kubectl get configmap and kubeadm config view all show the new cluster name, the kubectl config get-clusters still shows the default 'kubernetes' as the name. Is that normal? – user558100 Jan 31 '20 at 23:57
  • Yes, it's totally normal as changes made in `kubeadm-config ConfigMap` don'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:44
  • One more thing. Still don't quite get how to access. I renamed the default cluster to 'k8'. # kubectl config get-contexts CURRENT NAME CLUSTER AUTHINFO NAMESPACE kubernetes-admin@kubernetes kubernetes kubernetes-admin * test k8 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:44
  • 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. Please see my edited answer. – mario Feb 01 '20 at 15:03
  • sed -i s/your-old-name/yet-another-fancy-name/ /etc/kubernetes/admin.conf && sed -i s/your-old-name/yet-another-fancy-name/ .kube/config – NicoKowe Apr 09 '21 at 08:32
0

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.

yaach
  • 1