How to use the command 'kubeadm' (with examples)
kubeadm is a command-line interface for creating and managing Kubernetes clusters. It provides various functionalities for initializing, joining, upgrading, and resetting clusters. This article will walk you through several use cases of the kubeadm command.
Use case 1: Create a Kubernetes master node
Code:
kubeadm init
Motivation: The kubeadm init command is used to initialize a Kubernetes master node. This is the first step in setting up a Kubernetes cluster. Once the initialization is complete, the master node will be able to manage the cluster and accept requests from worker nodes.
Explanation: The kubeadm init command initializes a new Kubernetes master node. It generates certificates, sets up the kubeconfig file, and deploys the necessary components for the master node to function properly.
Example output:
[init] Using Kubernetes version: 1.21.3
[preflight] Running pre-flight checks
...
[control-plane] Writing static Pod manifest for component kube-apiserver
...
[control-plane] Done initializing control plane node!
...
Use case 2: Bootstrap a Kubernetes worker node and join it to a cluster
Code:
kubeadm join --token <token>
Motivation: In order to add a new worker node to an existing Kubernetes cluster, we need to bootstrap the node and join it to the cluster. The kubeadm join command allows us to do this.
Explanation: The kubeadm join command is used to bootstrap a Kubernetes worker node and join it to a cluster. It requires a token to authenticate the node. The token can be obtained from the master node using the kubeadm token create command.
Example output:
[preflight] Running pre-flight checks
...
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
...
[kubelet-start] Starting the kubelet
...
Use case 3: Create a new bootstrap token with a TTL of 12 hours
Code:
kubeadm token create --ttl 12h0m0s
Motivation: When joining a worker node to a Kubernetes cluster, we need a bootstrap token to authenticate the node. The kubeadm token create command allows us to generate a new token with a specified time-to-live (TTL).
Explanation: The kubeadm token create command is used to generate a new bootstrap token. The --ttl
flag specifies the duration for which the token is valid. In this example, we set the TTL to 12 hours.
Example output:
abcdef.1234567890abcdef
Use case 4: Check if the Kubernetes cluster is upgradeable and which versions are available
Code:
kubeadm upgrade plan
Motivation: Before upgrading a Kubernetes cluster, it is important to check if the cluster is upgradeable and what versions are available. The kubeadm upgrade plan command provides this information.
Explanation: The kubeadm upgrade plan command checks if the current Kubernetes cluster is upgradeable and displays the available upgrade versions. It also shows the steps required to upgrade the cluster.
Example output:
[upgrade/config] Making sure the configuration is correct:
...
Upgradeable: yes
...
Versions to upgrade to: v1.22.1
Use case 5: Upgrade Kubernetes cluster to a specified version
Code:
kubeadm upgrade apply <version>
Motivation: To update a Kubernetes cluster to a specific version, we can use the kubeadm upgrade apply command. This allows us to take advantage of the latest features and bug fixes in the specified version.
Explanation: The kubeadm upgrade apply command upgrades the Kubernetes cluster to the specified version. The <version>
parameter should be replaced with the desired version number.
Example output:
[upgrade/successful] SUCCESS! Your cluster was upgraded to "v1.22.1". Enjoy!
Use case 6: View the kubeadm ConfigMap containing the cluster’s configuration
Code:
kubeadm config view
Motivation: The kubeadm config view command allows us to view the configuration of the Kubernetes cluster. This can be useful for troubleshooting or verifying the cluster’s settings.
Explanation: The kubeadm config view command displays the ConfigMap containing the cluster’s configuration. It shows various settings such as API server endpoints, certificates, and network configurations.
Example output:
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
...
apiServer:
timeoutForControlPlane: 4m0s
...
Use case 7: Revert changes made to the host by ‘kubeadm init’ or ‘kubeadm join’
Code:
kubeadm reset
Motivation: If something goes wrong during the initialization or joining of a Kubernetes cluster, or when you want to completely remove the cluster from a host, the kubeadm reset command allows you to revert the changes made by the kubeadm init or kubeadm join command.
Explanation: The kubeadm reset command reverts the changes made to the host by the kubeadm init or kubeadm join command. It removes all the Kubernetes-related components and configurations from the host.
Example output:
[reset] Removing info for node "<hostname>" from the ConfigMap object
...
[reset] removing cluster-info from the ConfigMap object in the "kube-public" namespace
Conclusion:
The kubeadm command is a powerful tool for managing Kubernetes clusters. In this article, we explored several common use cases of the kubeadm command, including initializing a master node, joining a worker node, upgrading the cluster, and viewing cluster configuration. By understanding these use cases, you’ll be able to effectively create and manage Kubernetes clusters using the kubeadm command.