General
Understanding resources and commands
kubectl also provides a way to self-document and facilitate working with the API resources.
- If high level details about what resources are available (or their api version, etc.) are needed
kubectl api-resourcescan be used. - To go into detail into one resource, and figure out what fields are available in a YAML definition and what they mean use
kubectl explain <resource>- This allows to go down hierarchically (e.g
kubectl explain pod,kubectl explain pod.spec) - Using the
--recursiveflag, prints all children
- This allows to go down hierarchically (e.g
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