github.com/y-taka-23/helm@v2.8.0+incompatible/docs/chart_template_guide/getting_started.md (about)

     1  # Getting Started with a Chart Template
     2  
     3  In this section of the guide, we'll create a chart and then add a first template. The chart we created here will be used throughout the rest of the guide.
     4  
     5  To get going, let's take a brief look at a Helm chart.
     6  
     7  ## Charts
     8  
     9  As described in the [Charts Guide](../charts.md), Helm charts are structured like
    10  this:
    11  
    12  ```
    13  mychart/
    14    Chart.yaml
    15    values.yaml
    16    charts/
    17    templates/
    18    ...
    19  ```
    20  
    21  The `templates/` directory is for template files. When Tiller evaluates a chart,
    22  it will send all of the files in the `templates/` directory through the
    23  template rendering engine. Tiller then collects the results of those templates
    24  and sends them on to Kubernetes.
    25  
    26  The `values.yaml` file is also important to templates. This file contains the
    27  _default values_ for a chart. These values may be overridden by users during
    28  `helm install` or `helm upgrade`.
    29  
    30  The `Chart.yaml` file contains a description of the chart. You can access it
    31  from within a template. The `charts/` directory _may_ contain other charts (which
    32  we call _subcharts_). Later in this guide we will see how those work when it
    33  comes to template rendering.
    34  
    35  ## A Starter Chart
    36  
    37  For this guide, we'll create a simple chart called `mychart`, and then we'll
    38  create some templates inside of the chart.
    39  
    40  ```console
    41  $ helm create mychart
    42  Creating mychart
    43  ```
    44  
    45  From here on, we'll be working in the `mychart` directory.
    46  
    47  ### A Quick Glimpse of `mychart/templates/`
    48  
    49  If you take a look at the `mychart/templates/` directory, you'll notice a few files
    50  already there.
    51  
    52  - `NOTES.txt`: The "help text" for your chart. This will be displayed to your users
    53    when they run `helm install`.
    54  - `deployment.yaml`: A basic manifest for creating a Kubernetes [deployment](http://kubernetes.io/docs/user-guide/deployments/)
    55  - `service.yaml`: A basic manifest for creating a [service endpoint](http://kubernetes.io/docs/user-guide/services/) for your deployment
    56  - `_helpers.tpl`: A place to put template helpers that you can re-use throughout the chart
    57  
    58  And what we're going to do is... _remove them all!_ That way we can work through our tutorial from scratch. We'll actually create our own `NOTES.txt` and `_helpers.tpl` as we go.
    59  
    60  ```console
    61  $ rm -rf mychart/templates/*.*
    62  ```
    63  
    64  When you're writing production grade charts, having basic versions of these charts can be really useful. So in your day-to-day chart authoring, you probably won't want to remove them.
    65  
    66  ## A First Template
    67  
    68  The first template we are going to create will be a `ConfigMap`. In Kubernetes,
    69  a ConfigMap is simply a container for storing configuration data. Other things,
    70  like pods, can access the data in a ConfigMap.
    71  
    72  Because ConfigMaps are basic resources, they make a great starting point for us.
    73  
    74  Let's begin by creating a file called `mychart/templates/configmap.yaml`:
    75  
    76  ```yaml
    77  apiVersion: v1
    78  kind: ConfigMap
    79  metadata:
    80    name: mychart-configmap
    81  data:
    82    myvalue: "Hello World"
    83  ```
    84  
    85  **TIP:** Template names do not follow a rigid naming pattern. However, we recommend
    86  using the suffix `.yaml` for YAML files and `.tpl` for helpers.
    87  
    88  The YAML file above is a bare-bones ConfigMap, having the minimal necessary fields.
    89  In virtue of the fact that this file is in the `templates/` directory, it will
    90  be sent through the template engine.
    91  
    92  It is just fine to put a plain YAML file like this in the `templates/` directory.
    93  When Tiller reads this template, it will simply send it to Kubernetes as-is.
    94  
    95  With this simple template, we now have an installable chart. And we can install
    96  it like this:
    97  
    98  ```console
    99  $ helm install ./mychart
   100  NAME: full-coral
   101  LAST DEPLOYED: Tue Nov  1 17:36:01 2016
   102  NAMESPACE: default
   103  STATUS: DEPLOYED
   104  
   105  RESOURCES:
   106  ==> v1/ConfigMap
   107  NAME                DATA      AGE
   108  mychart-configmap   1         1m
   109  ```
   110  
   111  In the output above, we can see that our ConfigMap was created. Using Helm, we
   112  can retrieve the release and see the actual template that was loaded.
   113  
   114  ```console
   115  $ helm get manifest full-coral
   116  
   117  ---
   118  # Source: mychart/templates/configmap.yaml
   119  apiVersion: v1
   120  kind: ConfigMap
   121  metadata:
   122    name: mychart-configmap
   123  data:
   124    myvalue: "Hello World"
   125  ```
   126  
   127  The `helm get manifest` command takes a release name (`full-coral`) and prints
   128  out all of the Kubernetes resources that were uploaded to the server. Each file
   129  begins with `---` to indicate the start of a YAML document, and then is followed
   130  by an automatically generated comment line that tells us what template file
   131  generated this YAML document.
   132  
   133  From there on, we can see that the YAML data is exactly what we put in our
   134  `configmap.yaml` file.
   135  
   136  Now we can delete our release: `helm delete full-coral`.
   137  
   138  ### Adding a Simple Template Call
   139  
   140  Hard-coding the `name:` into a resource is usually considered to be bad practice.
   141  Names should be unique to a release. So we might want to generate a name field
   142  by inserting the release name.
   143  
   144  **TIP:** The `name:` field is limited to 63 characters because of limitations to
   145  the DNS system. For that reason, release names are limited to 53 characters.
   146  Kubernetes 1.3 and earlier limited to only 24 characters (thus 14 character names).
   147  
   148  Let's alter `configmap.yaml` accordingly.
   149  
   150  ```yaml
   151  apiVersion: v1
   152  kind: ConfigMap
   153  metadata:
   154    name: {{ .Release.Name }}-configmap
   155  data:
   156    myvalue: "Hello World"
   157  ```
   158  
   159  The big change comes in the value of the `name:` field, which is now
   160  `{{ .Release.Name }}-configmap`.
   161  
   162  > A template directive is enclosed in `{{` and `}}` blocks.
   163  
   164  The template directive `{{ .Release.Name }}` injects the release name into the template. The values that are passed into a template can be thought of as _namespaced objects_, where a dot (`.`) separates each namespaced element.
   165  
   166  The leading dot before `Release` indicates that we start with the top-most namespace for this scope (we'll talk about scope in a bit). So we could read `.Release.Name` as "start at the top namespace, find the `Release` object, then look inside of it for an object called `Name`".
   167  
   168  The `Release` object is one of the built-in objects for Helm, and we'll cover it in more depth later. But for now, it is sufficient to say that this will display the release name that Tiller assigns to our release.
   169  
   170  Now when we install our resource, we'll immediately see the result of using this template directive:
   171  
   172  ```console
   173  $ helm install ./mychart
   174  NAME: clunky-serval
   175  LAST DEPLOYED: Tue Nov  1 17:45:37 2016
   176  NAMESPACE: default
   177  STATUS: DEPLOYED
   178  
   179  RESOURCES:
   180  ==> v1/ConfigMap
   181  NAME                      DATA      AGE
   182  clunky-serval-configmap   1         1m
   183  ```
   184  
   185  Note that in the `RESOURCES` section, the name we see there is `clunky-serval-configmap`
   186  instead of `mychart-configmap`.
   187  
   188  You can run `helm get manifest clunky-serval` to see the entire generated YAML.
   189  
   190  At this point, we've seen templates at their most basic: YAML files that have template directives embedded in `{{` and `}}`. In the next part, we'll take a deeper look into templates. But before moving on, there's one quick trick that can make building templates faster: When you want to test the template rendering, but not actually install anything, you can use `helm install --debug --dry-run ./mychart`. This will send the chart to the Tiller server, which will render the templates. But instead of installing the chart, it will return the rendered template to you so you can see the output:
   191  
   192  ```console
   193  $ helm install --debug --dry-run ./mychart
   194  SERVER: "localhost:44134"
   195  CHART PATH: /Users/mattbutcher/Code/Go/src/k8s.io/helm/_scratch/mychart
   196  NAME:   goodly-guppy
   197  TARGET NAMESPACE:   default
   198  CHART:  mychart 0.1.0
   199  MANIFEST:
   200  ---
   201  # Source: mychart/templates/configmap.yaml
   202  apiVersion: v1
   203  kind: ConfigMap
   204  metadata:
   205    name: goodly-guppy-configmap
   206  data:
   207    myvalue: "Hello World"
   208  
   209  ```
   210  
   211  Using `--dry-run` will make it easier to test your code, but it won't ensure that Kubernetes itself will accept the templates you generate. It's best not to assume that your chart will install just because `--dry-run` works.
   212  
   213  In the next few sections, we'll take the basic chart we defined here and explore the Helm template language in detail. And we'll get started with built-in objects.