Manage Kubernetes Resources with Kubectl and PowerShell
Did you know that you can use PowerShell with kubectl to make your life easier? 🤯 That's right, PowerShell has built-in data sorting, filtering, and iteration capabilities that can add value on top of kubectl. kubectl itself does a great job of authenticating to the Kubernetes API Server, and constructing the appropriate API calls. Combine kubectl with PowerShell, and you've got a powerful set of tools to help easily manage resources on your cluster.
Remember that PowerShell is:
Getting Raw JSON Output from Kubectl
The first thing you'll need to do is learn how to export raw JSON from kubectl. The kubectl get command allows you to accomplish this with the --output parameter. You just need to set the value of this parameter to JSON, and you'll get the raw API response from the Kubernetes API Server.
kubectl get pod --output=json
Normally the kubectl get command returns a limited set of resource properties, but the --output=json parameter will dump the entire resource configuration! This gives you access to any properties of the resource that you might need.
Retrieve Kubernetes Resources from All Namespaces
You can also use the --all-namespaces parameter to retrieve all of the resources, of a particular resource type, from every namespace on your Kubernetes cluster! This reduces the amount of separate commands you need to call, just to list out the resources from each and every namespace in the cluster.
kubectl get service --all-namespaces --output=json
Interpret Kubernetes Resources as PowerShell Objects
Now you know how to obtain raw JSON responses from Kubernetes, using kubectl! Simply pipe the raw JSON data into PowerShell's built-in ConvertFrom-Json command to interpret the API response as shell objects!
kubectl get service --all-namespaces --output=json | ConvertFrom-Json
This will return the object-ified response directly to your console. Instead, you'll want to capture the results into a variable, so you can reuse the data across multiple commands. This reduces the number of API calls you're making against the Kubernetes API Server, and improves performance of your local filtration, sorting, and other data manipulation commands.
$ServiceList = kubectl get service --all-namespaces --output=json | ConvertFrom-Json
Drill Into Kubernetes API Responses
Now that you've got your Kubernetes API response converted into PowerShell objects, and stored in a local variable, it's time to start exploring the data! Most API responses will be wrapped in as a List object, so you'll typically have to drill into the items child property to access the actual resource data.
Try running this command:
Recommended by LinkedIn
$ServiceList.items
This will "unwrap" the items property of the $ServiceList variable, which points to an array of child objects. Each of the child objects in the items array represents an individual Kubernetes Service resource.
Filtering Kubernetes Resources with PowerShell
Now that you've learned how to return all of the child resources in the List response, let's try to filter the data results. Imagine that your objective is to find all of the Kubernetes Service resources that contain "prom" somewhere in the resource's name. The name property is a child property of the metadata property, on each Kubernetes resource, so we'll need to drill into that first.
The Where-Object command in PowerShell enables you to filter arrays of objects. You define a "filter script" that determines whether or not an object should be passed down the pipeline. The -match operator in PowerShell enables you to test a string value, to see if it matches a regular expression.
Inside the -FilterScript block of code, you can use the special $PSItem variable to refer to the "current" object that the pipeline is iterating on. We can inspect the metadata.name child property, and determine if it does or doesn't match our regular expression.
Let's put all of that together into an example:
$ServiceList.items | Where-Object -FilterScript { $PSItem.metadata.name -match 'prom' }
In the output from the command above, you should see only the Kubernetes Service resources, with a name containing "prom" returned to the console.
Iterate Across Kubernetes Resource Objects
Now that you have filtered the list of Kubernetes resource results, based on some criteria you've defined, let's iterate across the objects and perform some action. Let's imagine that we want to delete all of these resources. However, instead of actually deleting them, we'll use the --dry-run parameter to pretend like we're going to delete them.
To iterate across an array of input objects, we use the ForEach-Object command in PowerShell. Simply pipe the filtered array of objects into the command, and then specify what command you want to perform for each input object. The -Process parameter allows you to specify the block of PowerShell code that should be executed for each input resource.
$ServiceList.items |
Where-Object -FilterScript { $PSItem.metadata.name -match 'prom' } |
ForEach-Object -Process {
kubectl delete --namespace $PSItem.metadata.namespace Service $PSItem.metadata.name --dry-run=client
}
As you can see from the output above, we have successfully iterated across our filtered list of Kubernetes resources, and performed some action on them. You can expound on this technique to manage any other type of resource in Kubernetes, by obtaining the raw JSON response from the kubectl CLI command.
Conclusion
As you can tell from this article, PowerShell offers a significantly value-add experience on top of the raw kubectl command line tool. Since kubectl itself doesn't offer a native object-oriented experience, we can combine the power of PowerShell with kubectl to turn it into an object-oriented experience. You can still leverage kubectl for constructing API requests to the Kubernetes API Server, and handling authentication. You can call kubectl from your PowerShell scripts and leverage built-in PowerShell commands to handle the data aspect of Kubernetes cluster management.
🏫 Learn more about Kubernetes, Amazon Web Services (AWS), MySQL, GitHub, and more, from CBT Nuggets! You can sign up for a perpetual free account, and sample our huge library of content! The Certified Kubernetes Administrator (CKA) training from CBT Nuggets is designed to prepare you with the essentials to pass the CKA exam from the Linux Foundation!