How to programatically get value printed by Kubernetes in --discovery-token-ca-cert-hash after using kubeadm init

3/29/2021

I have this specific use case, in which we remotely create Kubernetes clusters on a significant number of machines. When we run kubeadm init at the end the join commands gets printed as:

kubeadm join [IPv6-Address]:6443 --token TOKEN_VALUE --discovery-token-ca-cert-hash CERT_HASH

In order to programmatically join worker nodes we have a script that needs both the TOKEN_VALUE and the CERT_HASH.

As now I'm acquiring the TOKEN_VALUE with the following command: sudo kubeadm token list | awk 'NR == 2 {print $1}'. However, I haven't found an easy way(or any way at all) to obtain the CERT_HASH.

Any help or pointer would be appreciated.

-- nazar
kubeadm
kubernetes

1 Answer

3/29/2021

For those with the same problem, there doesn't seem to be a super clean or easy way to get it. But after looking at some places, the one that worked for me is openssl x509 -in /etc/kubernetes/pki/ca.crt -noout -pubkey | openssl rsa -pubin -outform DER 2>/dev/null | sha256sum | cut -d' ' -f1

-- nazar
Source: StackOverflow