DEV Community

Cover image for kubectl apply: Syntax, Use Cases, and Best Practices
Gilad David Maayan
Gilad David Maayan

Posted on

kubectl apply: Syntax, Use Cases, and Best Practices

Image source

What Is kubectl?

Anyone who has worked with Kubernetes has come to appreciate the power of its command-line tool, kubectl. Kubectl is an essential part of any Kubernetes setup, whether you're running a small-scale project or managing a large, enterprise-level cluster.

Kubectl allows you to run commands against Kubernetes clusters. It is the primary method of interacting with a Kubernetes cluster, enabling developers and administrators to manage their resources effectively. Kubectl uses the Kubernetes API to interact with the cluster, offering a versatile toolset for interacting with and managing your Kubernetes resources.

With kubectl, you can deploy applications, inspect and manage cluster resources, and view logs. It's an indispensable tool for anyone interacting with Kubernetes, whether they're developers, system administrators, or DevOps professionals.

Kubectl's power lies in its versatility. It provides various ways to create, update, and manage your Kubernetes resources. One central command is kubectl apply.

Understanding the Syntax of kubectl apply

The kubectl apply command is used to apply a configuration change to a resource. The syntax is simple: the most common usage is:

kubectl apply -f
Enter fullscreen mode Exit fullscreen mode

Here, the -f flag indicates that the resource configuration is provided in a file named .

The kubectl apply command works on the basis of declarative configuration. Declarative configuration is a method where you provide the desired state of the resource in a file, and Kubernetes makes the necessary changes to achieve that state. This is in contrast to imperative commands, where you specify each step to change the resource's state.

The kubectl apply command also maintains a record of the previously applied configuration by writing it to the resource's annotation. This information is used to calculate the difference for the next invocation of kubectl apply, allowing it to accurately know which changes to apply.

What’s amazing about kubectl apply is that it can be used to deploy computing resources in just about any environment—on premises or in any cloud, wherever the Kubernetes cluster is running. This allows Kubernetes to support complex hybrid IT scenarios.

Kubectl apply in Action: Main Use Cases

Creating Resources Using kubectl apply

In Kubernetes, you can create resources such as Pods, Services, and Deployments using kubectl apply. You just need to define the configuration of the resource in a .yaml or .json file and then use kubectl apply to create it.

Updating Resources with kubectl apply

kubectl apply is not only used for creating resources but also for updating them. When you want to make changes to a resource, you update the configuration file and then apply it using kubectl apply. Kubernetes will then calculate the difference between the current and the desired state and apply the necessary changes.

Dealing with Changes to Live Configuration

Sometimes, you might need to make changes to the live configuration of a resource. kubectl apply allows you to do this as well. However, it's important to note that these changes will be overwritten the next time you apply the configuration file. Therefore, it's recommended to always make changes in the configuration file and then apply them.

Best Practices for Using "kubectl apply"

Use Version Control Systems

One of the best practices when using kubectl apply is to utilize a version control system (VCS). A VCS allows you to track changes to your code over time. This can be incredibly helpful if you need to revert to a previous version of your code, or if you're working in a team and need to merge changes from different developers.

Using a VCS with kubectl apply means storing all your Kubernetes configuration files in a VCS like Git. This way, every change made to the configurations is tracked, providing a clear history of what changes were made, when they were made, and who made them. This practice not only improves traceability but also allows for better collaboration among team members.

Moreover, using a VCS allows you to leverage Continuous Integration/Continuous Deployment (CI/CD) pipelines. These pipelines can automatically apply changes from your VCS to your Kubernetes clusters, further streamlining your deployment process.

Clean Up Unused Resources

It's important to clean up unused resources when using kubectl apply. This is important for managing your cloud costs. If you delete a resource from your configuration file and then use kubectl apply, Kubernetes does not automatically delete the corresponding resource. Instead, you need to manually delete the resource using the kubectl delete command.

Cleaning up unused resources is important for maintaining a clean and efficient system. Unused resources can consume system resources and make your configuration more difficult to understand and manage.

Manage Resources as a Whole

Another best practice for using kubectl apply is to manage resources as a whole rather than individually. This means treating all the resources required for an application as a single unit. Kubernetes allows you to do this through a feature called labels.

Labels are key-value pairs that can be attached to Kubernetes objects. You can use labels to group related resources together. For example, all resources related to a specific application can have a label with the key 'app' and the name of the application as the value.

Once you've grouped your resources with labels, you can use kubectl apply to apply changes to all resources with a certain label. This allows you to manage all the resources related to an application as a single unit, making your work more efficient.

Understand the Merge Strategy

Understanding the merge strategy used by kubectl apply is crucial. When you use kubectl apply to apply changes, Kubernetes does not simply overwrite the existing configuration. Instead, it uses a strategy called a three-way merge patch.

In a three-way merge patch, Kubernetes looks at the current state of the object, the state of the object in the configuration file you're applying, and the last state of the object when it was applied. It then merges these three states to determine the final state of the object.

This means that changes made directly to the object (for example, through kubectl edit command) are preserved unless they are overridden by changes in the configuration file. Understanding this merge strategy can help you avoid unexpected results when using kubectl apply.

Be Aware of Declarative and Imperative Commands

Kubernetes commands can be broadly divided into two categories: declarative and imperative. Declarative commands specify the desired state of the system, and Kubernetes figures out how to achieve that state. On the other hand, imperative commands specify the operations to be performed on the system.

kubectl apply is a declarative command. You provide a configuration file that specifies the desired state of the system, and Kubernetes figures out how to achieve that state. This approach is more flexible and powerful than imperative commands, but it can also be more complex.

Understanding the difference between declarative and imperative commands, and when to use each, is crucial for mastering kubectl.

Conclusion

Mastering kubectl is crucial for anyone working with Kubernetes. By following the best practices outlined in this guide, you can use kubectl apply more effectively and efficiently. Remember to use a version control system, manage resources as a whole, understand the merge strategy, be aware of declarative and imperative commands, and clean up unused resources. Happy Kube-controlling!

Top comments (0)