General
Imperative vs Declarative management commands
In K8s, there is three paradigms to manage general resources:
- Imperative approach with commands: Using
kubectlcommands likerun,create,edit,expose,scaleto perform operations - Imperative approach using a configuration file: Using
kubectlcommands likecreate,replaceanddeletewith-foptions - Declarative approach using a configuration file: Using
kubectl apply -f
kubectl apply
In general, due to best practices like maintainability, repeatability and documentation, the declarative approach (Using kubectl apply is preferred). This works based on 3 files:
- Input file
- Live object configuration file
- Last applied configuration (field in file above)
When deciding what to do, the program
- Compares input configuration with last applied
- Implement any changes in the live object configuration
[!CAUTION] Because of the presence of the
last_applied_configurationfield in the LOCF, objects that were created and managed witapplymust not be changed to be managed with an imperative approach
Imperative Commands
Imperative Create resource from YAML
kubectl create -f res.yamlImperative Replace resource from YAML
kubectl replace -f res.yamlImperative Delete resource from YAML
kubectl delete -f res.yamlImperative Edit existing object definition in place
kubectl edit object_type object_name # Does not edit a YAML definition if there is one