How to use the command k9s (with examples)
The k9s command is a powerful tool for viewing and managing Kubernetes clusters. It provides a command-line interface (CLI) that offers a more efficient way to interact with your clusters. With k9s, you can easily navigate and explore the resources within your cluster, monitor the status of your applications, and perform various management tasks.
Use case 1: Manage a cluster using a kubeconfig context
Code:
k9s --context kubeconfig_context_name
Motivation: By specifying the kubeconfig context with the --context
flag, you can easily switch between different Kubernetes clusters that you have access to. This is useful when working with multiple clusters or different environments, such as development, testing, and production. It allows you to quickly switch your focus and manage a specific cluster without the need to modify your kubeconfig file.
Explanation:
--context kubeconfig_context_name
: Specifies the kubeconfig context name to use. It refers to a specific cluster configuration defined in your kubeconfig file.
Example output:
...
Switched to context "kubeconfig_context_name"
Namespace: default
...
Use case 2: Manage a cluster in read-only mode
Code:
k9s --readonly --cluster cluster_name
Motivation: The --readonly
flag is useful when you want to prevent any accidental modifications to your cluster. It disables all commands that may cause modifications, such as creating, updating, or deleting resources. By using read-only mode, you can safely explore your cluster’s resources, monitor their status, and gather information without the risk of unintended changes.
Explanation:
--readonly
: Enables read-only mode, which disables commands that may cause modifications in the cluster.--cluster cluster_name
: Specifies the cluster name to manage.
Example output:
...
read-only mode enabled
Cluster: cluster_name
Namespace: default
...
Use case 3: Manage a cluster using a given Kubernetes namespace
Code:
k9s --namespace kubernetes_namespace --cluster cluster_name
Motivation: By specifying the namespace with the --namespace
flag, you can easily focus on a specific namespace within your cluster. This is helpful when you are working with multi-tenant environments or want to isolate different applications or workloads within separate namespaces. With this feature, you can efficiently manage and interact with resources only within the specified namespace.
Explanation:
--namespace kubernetes_namespace
: Specifies the Kubernetes namespace to manage.--cluster cluster_name
: Specifies the cluster name to manage.
Example output:
...
Cluster: cluster_name
Namespace: kubernetes_namespace
...
Use case 4: Manage a cluster launching k9s in the pod view and enable debug logging
Code:
k9s --command pod --logLevel debug --cluster cluster_name
Motivation: Launching k9s in the pod view allows you to specifically focus on managing and monitoring pods within your cluster. This view provides valuable insights into the state and health of your application pods. Enabling debug logging with the --logLevel debug
flag helps troubleshoot any issues by providing more detailed log messages related to the actions performed by k9s.
Explanation:
--command pod
: Launches k9s in the pod view, where you can manage and monitor pods.--logLevel debug
: Enables debug logging, which provides more detailed log messages for troubleshooting purposes.--cluster cluster_name
: Specifies the cluster name to manage.
Example output:
...
Cluster: cluster_name
Namespace: default
Switched to PODS
...
DEBUG[2021-07-01T00:00:00Z] ObjMarshaller instance.
...
Conclusion:
The k9s command is a versatile tool for viewing and managing Kubernetes clusters. It offers various use cases to cater to different needs and workflows. Whether you need to switch between clusters, work in read-only mode, focus on specific namespaces, or manage pods, k9s provides a convenient and efficient command-line interface. By exploring these use cases and leveraging the power of k9s, you can streamline your Kubernetes cluster management tasks and improve your overall productivity.