Install and Use the Latest Kubectl Convert Plugin (Command)

Posted April 26, 2024
Install and Use the Latest Kubectl Convert Plugin (Command)

The Kubectl convert plugin lets Kubernetes convert Kubernetes resources from one format to another. Take an example of resources defined in YAML or JSON. If you need them in a different format, you do not need to rewrite the whole new file. Kubectl convert will do magic for you

Dive into this guide and how to install and use the Latest Kubectl Convert Plugin (Command)

Requirements

Before installing the kubectl convert plugin, ensure:

  • You have the basics of working with Kubernetes
  • Have Kubectl installed and ready to use.

Related: Install and Run Kubectl on Linux|Ubuntu 20.04|22.04|18.04

Ready? let’s get the Steps to Install the Latest Kubectl Convert Plugin.

Step 1: Check the Kubectl Convert Plugin

The Kubectl Convert Plugin binary is available on Kubernetes if you have Kubectl and Kubernetes on your machine. Run the following command and if you see the following info, the convert plugin is Ready:

kubectl convert
# OR
kubectl convert --help

Install and Use the Latest Kubectl Convert Plugin

Install and Use the Latest Kubectl Convert Plugin

Step 2: Installing the Kubectl Convert Plugin Binary

If you don’t such a message, then you will need to install the convert plugin manually using the following command:

# Ubuntu/Linux
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl-convert"

## MacOS
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/darwin/arm64/kubectl-convert"

## Windows
curl.exe -LO "https://dl.k8s.io/release/v1.30.0/bin/windows/amd64/kubectl-convert.exe"

Install and Use the Latest Kubectl Convert Plugin

Go ahead and validate the install convert binary. First, run the checksum file:

# Ubuntu/Linux
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl-convert.sha256"

## MacOS
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/darwin/arm64/kubectl-convert.sha256"

## Windows
curl.exe -LO "https://dl.k8s.io/v1.30.0/bin/windows/amd64/kubectl-convert.exe.sha256"

Step 3: Making kubectl-convert Executable

Validate the installed kubectl-convert binary:

## Linux/ Ubuntu
echo "$(cat kubectl-convert.sha256) kubectl-convert" | sha256sum --check

## MacOS
echo "$(cat kubectl-convert.sha256) kubectl-convert" | shasum -a 256 --check

## Windows Command Prompt
CertUtil -hashfile kubectl-convert.exe SHA256
type kubectl-convert.exe.sha256
## Windows PowerShell 
$($(CertUtil -hashfile .\kubectl-convert.exe SHA256)[1] -replace " ", "") -eq $(type .\kubectl-convert.exe.sha256)

If you have done it correctly, you should have the following message:

Install and Use the Latest Kubectl Convert Plugin

If the downloaded file isn’t executable by default, change its permissions to make it executable using the chmod command. (Mostly on you are on MacOS)

chmod +x ./kubectl-convert

Next, Move the binary to a directory that is included in your system’s PATH:

sudo mv ./kubectl-convert /usr/local/bin/kubectl-convert
sudo chown root: /usr/local/bin/kubectl-convert

Now clean up the checksum:

rm kubectl-convert kubectl-convert.sha256

For more details, use the following command and know all the kubectl-convert steps you might need

kubectl convert --help

Install and Use the Latest Kubectl Convert Plugin

Step 4: How to use the Kubectl Convert Command

Kubectl Convert Command works in YAML and JSON formats. Now, you will add the filename and directory to your command. This way, you will be able to convert your into the format of version with the --output-version flag. --output can be JSON or YAML based on the file you are working with:

--output=json
--output=yaml

Here is an example:

If you have a deployment.yaml file, the kubectl convert -f deployment.yaml is the command you need. However, the command will Convert the live state deployment.yaml and show the results as a print of stdout in JSON format:

Install and Use the Latest Kubectl Convert Plugin

  • Add --output=json to print the YAML into JSON:
kubectl convert -f deployment.yaml --output=json

Install and Use the Latest Kubectl Convert Plugin

  • If you need to get the converted live state of deployment.yaml to the latest version in JSON, the -o will be handly here as follows:
kubectl convert -f deployment.yaml --local -o json
  • To convert a Kubernetes resource and save the converted output into a file, use redirection in your command as follows:
kubectl convert -f deployment.yaml --output=json > converted_deployment.json

Install and Use the Latest Kubectl Convert Plugin

Conclusion

You have successfully installed the kubectl convert plugin. Now you should be able to convert Kubernetes resources between different formats. If you have any issues, leave a comment and let us know the issue you are encountering.

Install and Use the Latest Kubectl Convert Plugin (Command)

Written By:

Joseph Chege