|
| 1 | +--- |
| 2 | +title: Kubenetes Object Management |
| 3 | +--- |
| 4 | + |
| 5 | +{% capture overview %} |
| 6 | +`kubectl` supports several different ways to create and manage |
| 7 | +Kubernetes objects. This document provides an overview of the different |
| 8 | +approaches. |
| 9 | +{% endcapture %} |
| 10 | + |
| 11 | +{% capture body %} |
| 12 | + |
| 13 | +## Management techniques table |
| 14 | + |
| 15 | +**Warning:** A Kubernetes object should be managed using only 1 technique. Mixing |
| 16 | +and matching techniques for the same object results in undefined behavior. |
| 17 | + |
| 18 | +| Management Technique | Operates On |Recommended Environment | Supported Writers | Learning Curve | |
| 19 | +|----------------------------------|----------------------|------------------------|--------------------|----------------| |
| 20 | +| Imperative Commands | Live Objects | Development Projects | 1+ | Lowest | |
| 21 | +| Imperative Object Configuration | Individual Files | Production Projects | 1 | Moderate | |
| 22 | +| Declarative Object Configuration | Directories of Files | Production Projects | 1+ | Highest | |
| 23 | + |
| 24 | +## Imperative commands |
| 25 | + |
| 26 | +When using imperative commands, a user operates directly on live objects |
| 27 | +in a cluster. The operations are provided to |
| 28 | +the `kubectl` command line interface as arguments or flags. |
| 29 | + |
| 30 | +This is the simplest way to get started or to run a one-off tasks in |
| 31 | +a cluster. Because this technique operates directly on the live |
| 32 | +objects, it provides no history of previous configurations. |
| 33 | + |
| 34 | +#### Examples |
| 35 | + |
| 36 | +Run an instance of the *nginx* container by creating a Deployment object |
| 37 | + |
| 38 | + ```sh |
| 39 | + kubectl run nginx --image nginx |
| 40 | + ``` |
| 41 | + |
| 42 | +Same as above, but using a different syntax |
| 43 | + |
| 44 | + ```sh |
| 45 | + kubectl create deployment nginx --image nginx |
| 46 | + ``` |
| 47 | + |
| 48 | +#### Trade-offs |
| 49 | + |
| 50 | +Advantages compared to *object configuration* |
| 51 | + |
| 52 | +- Commands are simple, easy to learn and easy to remember |
| 53 | +- Commands require only a single step to make changes to the cluster |
| 54 | + |
| 55 | +Disadvantages compared to *object configuration* |
| 56 | + |
| 57 | +- Commands do not integrate with change review processes |
| 58 | +- Commands do not provide an audit trail associated with changes |
| 59 | +- Commands do not provide a source of record beside what is live |
| 60 | +- Commands do not provide a template for bootstrapping new objects |
| 61 | + |
| 62 | +<!--- |
| 63 | +For a tutorial on how to use Imperative Commands for app management, see: |
| 64 | +[App Management Using Comands](/docs/tutorials/kubectl/app-management-using-commands/) |
| 65 | +--> |
| 66 | + |
| 67 | +## Imperative object configuration |
| 68 | + |
| 69 | +When using imperative object configuration, a user operates on object |
| 70 | +configuration files stored locally. The object configuration defines the full |
| 71 | +object in either yaml or json. An operation (create, replace, delete) |
| 72 | +and one or more files are provide to `kubectl` as a command line argument |
| 73 | +and flag command line flags respectively. |
| 74 | + |
| 75 | +This technique requires a more in depth understanding of the Kubernetes |
| 76 | +Object definitions. |
| 77 | + |
| 78 | +**Note:** While this technique defines the object itself through a declarative |
| 79 | +configuration file, the operations are imperative - create, replace, delete. |
| 80 | + |
| 81 | +#### Examples |
| 82 | + |
| 83 | +Create the objects defined in the object configuration file |
| 84 | + |
| 85 | + ```sh |
| 86 | + kubectl create -f nginx.yaml |
| 87 | + ``` |
| 88 | + |
| 89 | +Delete the objects defined in the object configuration files |
| 90 | + |
| 91 | + ```sh |
| 92 | + kubectl delete -f nginx.yaml -f redis.yaml |
| 93 | + ``` |
| 94 | + |
| 95 | +Update the objects defined in the object configuration files by overwriting |
| 96 | +the live configuration. |
| 97 | + |
| 98 | + ```sh |
| 99 | + kubectl replace -f nginx.yaml |
| 100 | + ``` |
| 101 | + |
| 102 | +#### Trade-offs |
| 103 | + |
| 104 | +Advantages compared to *imperative commands* |
| 105 | + |
| 106 | +- Object configuration can be stored in a source control system such as *git* |
| 107 | +- Can integrate with processes such as reviewing changes before push and audit trails |
| 108 | +- Provides template for bootstrapping new objects |
| 109 | + |
| 110 | +Disadvantages compared to *imperative commands* |
| 111 | + |
| 112 | +- Object configuration requires basic understanding of the object schema |
| 113 | +- Initial creation of object configuration requires additional step of writing the yaml file |
| 114 | + |
| 115 | +Advantages compared to *declarative object configuration* |
| 116 | + |
| 117 | +- Imperative object configuration behavior is simpler and easier to understand |
| 118 | +- Imperative object configuration is more mature |
| 119 | + |
| 120 | +Disadvantages compared to *declarative object configuration* |
| 121 | + |
| 122 | +- Imperative object configuration works best on files, not directories |
| 123 | +- Updates to live objects must be reflected in object configuration or they will be lost during next replace. |
| 124 | + |
| 125 | +<!--- |
| 126 | +For a tutorial on how to use Yaml Config for app management, see: |
| 127 | +[App Management Yaml Config](/docs/tutorials/kubectl/app-management-using-yaml-config/) |
| 128 | +--> |
| 129 | + |
| 130 | +## Declarative object configuration |
| 131 | + |
| 132 | +When using declarative object configuration, a user operates on object |
| 133 | +configuration files stored locally, however it *does not define the operations |
| 134 | +on them*. Create, update and delete operations are automatically detected |
| 135 | +per-object by `kubectl`. This enables working on directories, where |
| 136 | +different operations may be needed for different objects. |
| 137 | + |
| 138 | +**Note:** Declarative object configuration retains changes made by other |
| 139 | +writers, even if the changes are not merged back to the object configuration file. |
| 140 | +This is possible by using the *patch* API operation to write only |
| 141 | +observed differences, instead of using the *replace* |
| 142 | +API operation to replace the entire object configuration. |
| 143 | + |
| 144 | +#### Examples |
| 145 | + |
| 146 | +Process all object configuration files in the configs directory, and |
| 147 | +create or patch the live objects. |
| 148 | + |
| 149 | + ```sh |
| 150 | + kubectl apply -f configs/ |
| 151 | + ``` |
| 152 | + |
| 153 | +Recursively process directories. |
| 154 | + |
| 155 | + ```sh |
| 156 | + kubectl apply -R -f configs/ |
| 157 | + ``` |
| 158 | + |
| 159 | +#### Trade-offs |
| 160 | + |
| 161 | +Advantages compared to *imperative object configuration*: |
| 162 | + |
| 163 | +- Updates keep changes made directly to live objects, even if they are not merged back to the object config |
| 164 | +- Better support for operating on directories and automatically detecting operation types per-object *(create, patch, delete)* |
| 165 | + |
| 166 | +Disadvantages compared to *imperative object configuration*: |
| 167 | + |
| 168 | +- Harder to debug and understand results when they are unexpected |
| 169 | + - Partial updates using diffs creates complex merge and patch operations |
| 170 | + |
| 171 | +<!--- |
| 172 | +For a tutorial on how to use Yaml Config with multiple writers, see: |
| 173 | +[App Management Yaml Config](/docs/tutorials/kubectl/app-management-using-yaml-config-multiple-writers/) |
| 174 | +--> |
| 175 | + |
| 176 | +{% endcapture %} |
| 177 | + |
| 178 | +{% capture whatsnext %} |
| 179 | +- [Kubectl Command Reference](/docs/user-guide/kubectl/v1.5/) |
| 180 | +- [Kubernetes Object Schema Reference](/docs/resources-reference/v1.5/) |
| 181 | + |
| 182 | +<!--- |
| 183 | +- [App Management Using Yaml Config](/docs/tutorials/kubectl/declarative-app-management-using-yaml-config/) |
| 184 | +- [App Management Using Yaml Config With Multiple Writers](/docs/tutorials/kubectl/declarative-app-management-using-yaml-config-multiple-writers/) |
| 185 | +--> |
| 186 | +{% endcapture %} |
| 187 | + |
| 188 | +{% include templates/concept.md %} |
0 commit comments