Execute Kubectl Command
Overview
The Execute Kubectl Command action allows users to execute Kubernetes commands (kubectl
) on a specified Kubernetes cluster. This action is ideal for performing various Kubernetes management tasks, such as querying resources, applying changes, and monitoring deployments directly from the bot workflow.
Prerequisites
-
Agent Configuration:
Ensure either a Linux or Kubernetes Agent is configured. Refer to the installation guide for Linux Agent or Kubernetes Agent. -
When Using a Linux Agent:
- kubectl CLI must be installed on the Linux agent. For installation instructions, refer to the kubectl CLI Installation Guide.
- Ensure access to the target Kubernetes cluster.
How to Use This Action?
To use the Execute Kubectl Command action, follow these steps:
-
In your bot workflow, navigate to Linux Actions or Kubernetes Actions under the Library section based on the agent type being used.
-
Search for Execute Kubectl Command and drag it into your workflow.
-
Select the integration that is connected to the agent for executing
kubectl
commands. -
Under the Parameters section, specify the required parameter values. For details on parameters, refer to the Parameter Details section.
-
Entering kubectl Command:
Provide kubectl Command:
-
kubectl_command: The main
kubectl
command to execute. -
Manual Entry: You can enter the kubectl_command manually by typing in the command you wish to execute.
-
AI Assistance: Alternatively, use AI assistance to receive command suggestions based on your input.
-
-
Save or update the bot, then click on Run to execute the bot. View the results in the execution details to confirm successful execution or troubleshoot if there are any errors.
Parameter Details
Parameter | Applicable Agent | Required | Description |
---|---|---|---|
kubectl_command | Linux, Kubernetes | Yes | Specifies the kubectl command to execute. Ensure the command is valid and follows kubectl <subcommand> . Ensure that you provide the appropriate flags for the namespace, context, and JSON output. |
kubeconfig_path | Linux | No | Path to the Kubernetes configuration file on the agent, if different from the default (~/.kube ). |
script_mode | Linux, Kubernetes | No | Specifies whether the kubectl CLI command is executed as a script (combination of multiple commands) or as individual commands. Set to True for script execution, or False for individual command execution. Defaults to False. NOTE: Check format limitations in Limitations. |
run_as_user | Linux | Yes | The user under which the command will execute (default is ubuntu ). |
execution_timeout | Linux, Kubernetes | Yes | Maximum allowed time (in seconds) for command execution (default is 120). |
Using Handlebar
You can use the Handlebar capabilities to reference values dynamically from the output of a previous node. This enables the creation of flexible and reusable commands within your workflow.
Referencing Particular Values
For example, to create a Kubernetes pod with a specific name, you can use the following kubectl
command:
kubectl run nginx-<environment> --image=nginx --restart=Never -o json
If the previous node returns a list of environment values under the environment key, you can reference those values dynamically as follows:
kubectl run nginx-{{$.nodes.list_environments[0].environment}} --image=nginx --restart=Never -o json
Note: To reference values from previous nodes, type $$
to view and select the desired data dynamically.
Explanation
In this example, the environment
value is dynamically retrieved from the list of environments returned by the previous node. The value is referenced using Handlebar syntax, with $.nodes.list_environments[0].environment
referring to the first environment in the list.
The generated command will dynamically substitute the environment
value, resulting in a command like:
kubectl run nginx-dev --image=nginx --restart=Never -o json
Limitations
- Outputs: The action does not provide JSON output by default. You must explicitly specify the
-o json
flag with applicablekubectl
subcommands to ensure the output is in JSON format.- Script mode: Not recommended if you intend to use data from the current Kubectl CLI node as input for the next node in the workflow.
- Non-script mode: The output may not always be in JSON format. This depends on
kubectl
behavior, as certain commands may not produce JSON outputs. Always verify the expected output format for your specific use case.
Additional Information
- Kubernetes Version Compatibility: When utilizing the Kubernetes agent for this action, ensure that the command is compatible with both the kubectl version installed on the agent and the version of your cluster.
- The kubectl version installed on the agent corresponds to the latest stable release available at the time of the new agent release.
- To verify the version details, execute the
kubectl version -o json
command within the kubectl_command parameter.
Example Use Case
Scenario: Retrieve Pods Information
Suppose you need to retrieve information about all running pods in a specific namespace for monitoring or troubleshooting. Using the Execute Kubectl Command action, you can execute the following command:
Sample kubectl_command
:
kubectl get pods -n my-namespace -o json