30

When i run kubeadm init it shows a token that I should use from the other hosts to connect to this host. Where is that token stored? I'm trying to figure out more ways to automate this by adding kubeadm inside of Terraform.

Thanks for any help!

pagid
  • 13,559
  • 11
  • 78
  • 104
vallard
  • 1,028
  • 1
  • 9
  • 14
  • you can generate token and use it for init command. 'kubeadm token generate' then use '/usr/bin/kubeadm init --token ' – sfgroups Jun 05 '17 at 12:13

7 Answers7

60

The instruction for Kubernetes 1.9.x (and above) can be found here.

The commands I used are:

kubeadm token generate
kubeadm token create <generated-token> --print-join-command --ttl=0
errordeveloper
  • 6,716
  • 6
  • 41
  • 54
Chris Fregly
  • 1,490
  • 1
  • 12
  • 8
  • 4
    Note that it's safer to set TTL (or omit the -ttl flag to set default 24h TTL). – Mika Vatanen Jun 09 '18 at 10:05
  • after k8s 1.25, the output of "kubeadm token create --print-join-command --ttl=0" is not enough. you also need to specify "--cri-socket [socket]". see https://kubernetes.io/docs/setup/production-environment/container-runtimes/ – JengdiB Dec 08 '22 at 03:54
23

Run this command in the master node to get the token

kubeadm token list
sfgroups
  • 18,151
  • 28
  • 132
  • 204
8

--token

By default, kubeadm init automatically generates the token used to initialise each new node. If you would like to manually specify this token, you can use the --token flag. The token must be of the format ‘<6 character string>.<16 character string>’.

you can see token value on master node using command:

cat /etc/kubernetes/pki/tokens.csv
pl_rock
  • 14,054
  • 3
  • 30
  • 33
4
kubectl -n kube-system get secret clusterinfo -o yaml | grep token-map | awk '{print $2}' | base64 -d | sed "s|{||g;s|}||g;s|:|.|g;s/\"//g;" | xargs echo
feisan
  • 192
  • 6
3

Try to create new as per below command as initial token is valid only 24 hours.

kubeadm token create  --ttl=0

Here ttl=0 means, generated token will never expire.

Then join node as per below command:

kubeadm join <generated_token>

If you get any error for verifying ca hash, then join as per below:

kubeadm join <generated_token> --discovery-token-unsafe-skip-ca-verification
David Maze
  • 130,717
  • 29
  • 175
  • 215
k''
  • 702
  • 1
  • 8
  • 19
2

Best way to avoid creating new token is -

  1. kubeadm token list
  2. kubeadm token create <copied token from previous command output>** --print-join-command

you will get JOIN command to run on any physical/vitual machine to join kubernetes cluster....

Ali Tou
  • 2,009
  • 2
  • 18
  • 33
Abhishek Jain
  • 3,815
  • 2
  • 26
  • 26
-2

Use this command : $ sudo kubeadm token list

Still, if you did not get, use following commands

$ sudo kubeadm reset
$ sudo kubeadm init 
Juan Caicedo
  • 1,425
  • 18
  • 31
  • 1
    No... If you don't see any available token, maybe it's timed out. Create another one, not resetting the whole cluster. Create token with `kubeadm token create --print-join-command`. – Lam Le Feb 24 '21 at 04:56
  • Seriously - I cannot believe "reset if it does not work" is even an answer. – ad22 Apr 09 '21 at 18:42