How Does K3S Manages Bootstrap Data

Posted on

K3S is a fully compliant lightweight kubernetes distribution with a minimum memory footprint, its perfect for edge devices however its designed from day one to work on production, for more information about K3S you can refer to the official documenation to get started with installation and management of k3s.

In this post I am going to shed some light on how K3S manages bootstrap data and how is that important for K3S to setup HA clusters, but first we must discuss what is the bootstrap data and what is its purpose in K3S.

Bootstrap Data

The bootstrap data is a structure of data that contains the sensitve information for the k3s server to bootstrap, that includes:

  • etcd server ca (cert,key)
  • etcd peer ca (cert,key)
  • server ca (cert,key)
  • client ca (cert,key)
  • passwd file
  • requestheader ca (cert,key)
  • ipsec key for flannel backend
  • encryption config file

These CA certs has to be the same for all HA servers in the cluster so that all certificates generated by K3S will be signed by the same CA certificates, in addition to encryption config files that contains the keys for encryption secrets if its enabled by the user.

Token Management

In order for K3S to start it needs a token that will be used for various purposes, the token can be supplied to the k3s server by using --token flag, however if the user didn’t supply a token via the cli, a randomized token will be created for the server and saved in <data-dir>/server/token.

This token is really critical for different reasons, one of the reason is that the initial server will use this token to encrypt the bootstrap data mentioned in the previous section and save it in the datastore.

Inspecting Bootstrap Data

After starting K3S server you can inspect the bootstrap data saved in the datastore as follows:

1- Start K3S server with etcd backend

curl -sfL https://get.k3s.io | sh -s - "server --token test --cluster-init"

Note that using --cluster-init will allow the server to use etcd backend as its data store.

2- Using etcdctl to inspect the bootstrap

ETCDCTL_API=3 etcdctl --cert /var/lib/rancher/k3s/server/tls/etcd/server-client.crt --key /var/lib/rancher/k3s/server/tls/etcd/server-client.key --endpoints https://127.0.0.1:2379 --cacert /var/lib/rancher/k3s/server/tls/etcd/server-ca.crt get --prefix /bootstrap --keys-only 

/bootstrap/9f86d081884c

So the command will use etcdctl to list the keys in etcd datastore with prefix /bootstrap which is the prefix that is used to save the bootstrap data key, as for 9f86d081884c part, its the hash of the token passed to K3S server.

To take a little peak on the content of the bootstrap data you can use the same etcdctl without the --keys-only option:

ETCDCTL_API=3 etcdctl --cert /var/lib/rancher/k3s/server/tls/etcd/server-client.crt --key /var/lib/rancher/k3s/server/tls/etcd/server-client.key --endpoints https://127.0.0.1:2379 --cacert /var/lib/rancher/k3s/server/tls/etcd/server-ca.crt get --prefix /bootstrap
/bootstrap/9f86d081884c
6f0de9faf483a501:WC/bhDCHs1gAMm6DoLg1WIumyAb8SR6JLdZkawfD8qKw3j+........

So the following snippet of code can be used to decrypt this data, which happens internally in k3s of course:

func main() {
	cipher := "xxxxxxxxxxxx"
	data, _ := decrypt("test", []byte(cipher))
	fmt.Println(string(data))
}

func decrypt(passphrase string, ciphertext []byte) ([]byte, error) {
	parts := strings.SplitN(string(ciphertext), ":", 2)
	if len(parts) != 2 {
		return nil, fmt.Errorf("invalid cipher text, not : delimited")
	}

	clearKey := pbkdf2.Key([]byte(passphrase), []byte(parts[0]), 4096, 32, sha1.New)
	key, err := aes.NewCipher(clearKey)
	if err != nil {
		return nil, err
	}

	gcm, err := cipher.NewGCM(key)
	if err != nil {
		return nil, err
	}

	data, err := base64.StdEncoding.DecodeString(parts[1])
	if err != nil {
		return nil, err
	}

	return gcm.Open(nil, data[:gcm.NonceSize()], data[gcm.NonceSize():], nil)
}

After decrypting you should see something like that:

{
  "ClientCA": "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUJkekNDQVIyZ0F3SUJBZ0lCQURBS0JnZ3Foa2pPUFFRREFqQWpNU0V3SHdZRFZRUUREQmhyTTNNdFkyeHAKWlc1MExXTmhRREUyTWpJNE16azVOalV3SGhjTk1qRXdOakEwTWpBMU1qUTFXaGNOTXpFd05qQXlNakExTWpRMQpXakFqTVNFd0h3WURWUVFEREJock0zTXRZMnhwWlc1MExXTmhRREUyTWpJNE16azVOalV3V1RBVEJnY3Foa2pPClBRSUJCZ2dxaGtqT1BRTUJCd05DQUFTSUUzWDg1bHZTMUNZN0dnUm5YRG1JY2tOQzZGdEJ2OVEwRm5qSVRiZzUKSmsvVGJSdTY2M25HVWJBVTRZbEkwbWJpcFlyMG9KQ0ErVlRsaTdlN2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WHo2THoKZmRqNU1FRHIzMG9SZHFRaktoZFI2Y2ZmaEsyRWpyUFZyUlhMWjdjT0dlVmlwK2V0WWNKMnZ6d1l2ejZjY0xHeQpkZGo2Mmh5d0t0SFcwa2RndDNjaldSQmhDQ0p3QjFtS0FoU1hWVWRKS0ZhdVh6WVQzUi9OMlE9PQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo="

These data are base64 encoded and after decoding it will show the actual certificates and keys used by this k3s server. so this is how K3S actually saves bootstrap data in server nodes, in the next post I will show how k3s creates a HA cluster and how this bootstrap data is actually distributed and used.