<p>I would like to configure default <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started-index-lifecycle-management.html" rel="nofollow noreferrer">Index Lifecycle Management (ILM)</a> policy and index template durring installation ES in kubernetes cluster, in the YAML installation file, instead of calling ES API after installation. How can I do that?</p>

<p>I have Elasticsearch installed in kubernetes cluster based on YAML file.</p>

<p>The following works queries work.</p>

<pre><code>PUT _ilm/policy/logstash_policy
{
    "policy": {
        "phases": {
            "delete": {
                "min_age": "30d",
                "actions": {
                    "delete": {}
                }
            }
        }
    }
}
</code></pre>

<pre><code>PUT _template/logstash_template
{
    "index_patterns": ["logstash-*"],
    "settings": {
        "number_of_shards": 1,
        "number_of_replicas": 1,
        "index.lifecycle.name": "logstash_policy"
    }
}
</code></pre>

<p>I would like to have above setup just after installation, without making any <code>curl</code> queries.</p>

<p>I'll try to answer both of your questions.</p>
<h2>index template</h2>
<p>You can pass the index template with this configuration in your elasticsearch yaml. For instance:</p>
<pre class="lang-yaml prettyprint-override"><code>setup.template:
  name: &quot;&lt;chosen template name&gt;-%{[agent.version]}&quot;
  pattern: &quot;&lt;chosen pattern name&gt;-%{[agent.version]}-*&quot;
</code></pre>
<p>Checkout the ES documentation to see where exactly this setup.template belongs and you're good to go.</p>
<h2>ilm policy</h2>
<p>The way to make this work is to get the ilm-policy.json file that has your ilm configuration to the pod's /usr/share/filebeat/ directory. in your YAML installation file, you can then use this line in your config to get it to work (I've added my whole ilm config):</p>
<pre class="lang-yaml prettyprint-override"><code>setup.ilm:
  enabled: true
  policy_name: &quot;&lt;policy name&gt;&quot;
  rollover_alias: &quot;&lt;rollover alias name
  policy_file: &quot;ilm-policy.json&quot;
  pattern: &quot;{now/d}-000001&quot;
</code></pre>
<p>So, how to get the file there? The ingredients are 1 configmap containing your ilm-policy.json, and a volume and volumeMount in your daemonset configuration to mount the configmap's contents to the pod's directories.</p>
<p><strong>Note:</strong> I used helm for deploying filebeat to an AKS cluster (v 1.15), which connects to Elastic cloud. In your case, the application folder to store your json will probably be <code>/usr/share/elasticsearch/ilm-policy.json</code>.</p>
<p>Below, you'll see a line like <code>{{ .Files.Get &lt;...&gt; }}</code>, which is a templating function for helm getting the contents of the files. Alternatively, you can copy the file contents directly into the configmap yaml, but to have the file separate makes it better managable in my opinion.</p>
<p><strong>The configMap</strong></p>
<p>Make sure your ilm-policy.json is somewhere reachable by your deployments. This is how the configmap can look:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: ilmpolicy-config
  namespace: logging
  labels:
    k8s-app: filebeat
data:
  ilm-policy.json: |-
{{ .Files.Get &quot;ilm-policy.json&quot; | indent 4 }}
</code></pre>
<p><strong>The Daemonset</strong></p>
<p>at the deamonSet's volumeMounts section, append this:</p>
<pre class="lang-yaml prettyprint-override"><code>- name: ilm-configmap-volume
  mountPath: /usr/share/filebeat/ilm-policy.json
  subPath: ilm-policy.json
  readOnly: true
</code></pre>
<p>and at the volume section append this:</p>
<pre class="lang-yaml prettyprint-override"><code> - name: ilm-configmap-volume
   configMap:
     name: ilmpolicy-config
</code></pre>
<p>I'm not exactly sure the spacing is correct in the browser, but this should give a pretty good idea.
I hope this works for your setup! good luck.</p>
