github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/site/book/01-getting-started/02-quickstart.md (about)

     1  In this example, you are going to configure and deploy Nginx to a Kubernetes
     2  cluster.
     3  
     4  ## Fetch the package
     5  
     6  kpt is fully integrated with Git and enables forking, rebasing and versioning a
     7  package of configuration using the underlying Git version control system.
     8  
     9  First, let's fetch the _kpt package_ from Git to your local filesystem:
    10  
    11  ```shell
    12  $ kpt pkg get https://github.com/GoogleContainerTools/kpt/package-examples/nginx@v0.9
    13  ```
    14  
    15  Subsequent commands are run from the `nginx` directory:
    16  
    17  ```shell
    18  $ cd nginx
    19  ```
    20  
    21  `kpt pkg` commands provide the functionality for working with packages on Git
    22  and on your local filesystem.
    23  
    24  Next, let's quickly view the content of the package:
    25  
    26  ```shell
    27  $ kpt pkg tree
    28  Package "nginx"
    29  ├── [Kptfile]  Kptfile nginx
    30  ├── [deployment.yaml]  Deployment my-nginx
    31  └── [svc.yaml]  Service my-nginx-svc
    32  ```
    33  
    34  As you can see, this package contains 3 resources in 3 files. There is a special
    35  file named `Kptfile` which is used by the kpt tool itself and is not deployed to
    36  the cluster. Later chapters will explain the `Kptfile` in detail.
    37  
    38  Initialize a local Git repo and commit the forked copy of the package:
    39  
    40  ```shell
    41  $ git init; git add .; git commit -m "Pristine nginx package"
    42  ```
    43  
    44  ## Customize the package
    45  
    46  At this point, you typically want to customize the package. With kpt, you can
    47  use different approaches depending on your use case.
    48  
    49  ### Manual Editing
    50  
    51  You may want to manually edit the files. For example, modify the value of
    52  `spec.replicas` in `deployment.yaml` using your favorite editor:
    53  
    54  ```shell
    55  $ vim deployment.yaml
    56  ```
    57  
    58  ### Automating One-time Edits with Functions
    59  
    60  The `kpt fn` set of commands enable you to execute programs called _kpt functions_. These
    61  programs are packaged as containers and take in YAML files, mutate or validate them, and then
    62  output YAML.
    63  
    64  For instance, you can use a function (`gcr.io/kpt-fn/search-replace:v0.1`) to search and replace all
    65  the occurrences of the `app` key in the `spec` section of the YAML document (`spec.**.app`) and
    66  set the value to `my-nginx`. 
    67  
    68  You can use the `kpt fn eval` command to run this mutation on your local files a single time:
    69  
    70  ```shell
    71  $ kpt fn eval --image gcr.io/kpt-fn/search-replace:v0.1 -- by-path='spec.**.app' put-value=my-nginx
    72  ```
    73  
    74  To see what changes were made to the local package:
    75  
    76  ```shell
    77  $ git diff
    78  ```
    79  
    80  ### Declaratively Defining Edits
    81  
    82  For operations that need to be performed repeatedly, there is a _declarative_ way to define a
    83  pipeline of functions as part of the package (in the `Kptfile`). In this `nginx` package, the author 
    84  has already declared a function (`kubeval`) that validates the resources 
    85  using their OpenAPI schema.
    86  
    87  ```yaml
    88  pipeline:
    89    validators:
    90      - image: gcr.io/kpt-fn/kubeval:v0.3
    91  ```
    92  
    93  You might want to label all resources in the package. To achieve that, you can
    94  declare `set-labels` function in the `pipeline` section of `Kptfile`. Add this by running the following
    95  command:
    96  
    97  ```shell
    98  cat >> Kptfile <<EOF
    99    mutators:
   100      - image: gcr.io/kpt-fn/set-labels:v0.1
   101        configMap:
   102          env: dev
   103  EOF
   104  ```
   105  
   106  This function will ensure that the label `env: dev` is added to all the
   107  resources in the package.
   108  
   109  The pipeline is executed using the `render` command:
   110  
   111  ```shell
   112  $ kpt fn render
   113  ```
   114  
   115  Regardless of how you choose to customize the package — whether by
   116  manually editing it or running one-time functions using `kpt fn eval` — you need to _render_ the
   117  package before applying it the cluster. This ensures all the functions declared
   118  in the package are executed, and the package is ready to be applied to the
   119  cluster.
   120  
   121  ## Apply the Package
   122  
   123  `kpt live` commands provide the functionality for deploying packages to a
   124  Kubernetes cluster.
   125  
   126  
   127  First, initialize the kpt package:
   128  
   129  ```shell
   130  $ kpt live init
   131  ```
   132  
   133  This adds metadata to the `Kptfile` required to keep track of changes made
   134  to the state of the cluster. This 
   135  allows kpt to group resources so that they can be applied, updated, pruned, and
   136  deleted together.
   137  
   138  Apply the resources to the cluster:
   139  
   140  ```shell
   141  $ kpt live apply --reconcile-timeout=15m
   142  ```
   143  
   144  This waits for the resources to be reconciled on the cluster by monitoring their
   145  status.
   146  
   147  ## Update the package
   148  
   149  At some point, there will be a new version of the upstream `nginx` package, and
   150  you want to merge the upstream changes with changes to your local package.
   151  
   152  First, commit your local changes:
   153  
   154  ```shell
   155  $ git add .; git commit -m "My customizations"
   156  ```
   157  
   158  Then update to version `v0.10`:
   159  
   160  ```shell
   161  $ kpt pkg update @v0.10
   162  ```
   163  
   164  This merges the upstream changes with your local changes using a schema-aware
   165  merge strategy.
   166  
   167  Apply the updated resources to the cluster:
   168  
   169  ```shell
   170  $ kpt live apply --reconcile-timeout=15m
   171  ```
   172  
   173  ## Clean up
   174  
   175  Delete the package from the cluster:
   176  
   177  ```shell
   178  $ kpt live destroy
   179  ```
   180  
   181  Congrats! You should now have a rough idea of what kpt is and what you can do
   182  with it. Now, let's delve into the details.