Kubernetes Configuration Management Methods

There are three different modes of Kubernetes configuration management:

  1. Imperative Commands
  2. Imperative Configuration files
  3. Declarative Configuration files

When creating a k8s cluster from scratch, it’s often easiest to create deployments using kubectl commands, e.g.

kubctl create deployment ai_analysis --image=store.madaq.com/ai_analsys:v1.2.1

This is the Imperative Commands option. It lets you quickly focus on creating the infrastructure you need, and sets up a configuration with reasonable defaults. It’s easy, quick and efficient.

However, there is no audit trail, no review process, no version control, and no repeatable script.

The Imperative Configuration file approach allows you to create and manage specification files, and use your typical development/review/release process (e.g. github, pull requests, CI)

kubectl create -f deployments/ai_analysis.yml

In this case, the specification for your deployment is stored in a file. This file can be altered and redeployed:

kubectl replace -f deployments/ai_analysis.yml

You can also edit these files on-the-fly using edit

kubectl edit deployments blog -n blog

This will open k8s’ configuration file directly into an editor, where you can update settings.

The third option is Declarative Configuration. Here, your complete configuration can be specified on your file system. You can apply this configuration, and kubectl will automatically compute the required operations to get the existing, running configuration to match the new specification.

You can preview the changes before you make them:

kubectl diff -f -R configs/
kubectl apply -f -R configs/

This method is good because your repository can act like the single source of truth for your k8s configuration, but it does come at the cost of complexity and difficulty in debugging when things go wrong. See Declarative Management of Kubernetes Objects Using Configuration Files | Kubernetes for more fiddly details.

Retrieving an existing configuration

You might want to rapidly develop a new cluster using imperative commands, but then migrate to a more formal and controlled method later. This is straightforward enough, since you can easily extract your live object configuration and save it to a file.

kubectl get deployment ai_analysis -o yaml > deployments/ai_analysis.yml

This can then be modified (also, the status section of the live object configuration needs to be removed).

Extracting Everything

If you want migrate everything to a file configuration method, you need to retrieve the existing live object configurations, and save them all to files.

kubectl get all -n namespace

This will get full configurations for all pods, services and deployments for a particular namespace, but not custom resources. See How to List all Resources in a Kubernetes Namespace - Studytonight for more useful details.

See Also

Kubernetes Configuration Best Practices

Tagged: | kubernetes |
Cover Image: Joseph Barrientos, via Unsplash