[Infrastructure as Code (IaC) Pulumi] Use Pulumi kubernetes (K8S) Helm Chart to deploy Elasticsearch

Elastic Elasticsearch

Elasticsearch is the distributed, RESTful search and analytics engine at the heart of the Elastic Stack. You can use Elasticsearch to store, search, and manage data for:

  • Logs

  • Metrics

  • A search backend

  • Application monitoring

  • Endpoint security

… and more!

This article is about how to use Pulumi, kubernetes (K8S) provider, Helm Chart and TypeScript SDK to deploy Elasticsearch within Kubernetes (K8S).

Prerequisites

Usage

Pulumi New

Create the workspace directory.

1
2
3
$ mkdir -p col-example-pulumi-typescript-elasticsearch

$ cd col-example-pulumi-typescript-elasticsearch

Pulumi login into local file system.

1
2
3
$ pulumi login file://.
Logged in to cloudolife as cloudolife (file://.)
or visit https://pulumi.com/docs/reference/install/ for manual instructions and release notes.

Pulumi new a project with kubernetes-typescript SDK.

1
$ pulumi new kubernetes-typescript

The above command will create some files within the current directory.

1
2
3
4
5
6
7
8
tree . -L 1
.
├── node_modules/
├── package.json
├── package.json.lock
├── Pulumi.dev.yaml
├── Pulumi.yaml
└── main.ts

Install js-yaml package to load and parse yaml file.

1
$ npm i js-yaml

Pulumi Configuration

Configure Kubernetes

By default, Pulumi will look for a kubeconfig file in the following locations, just like kubectl:

  • The environment variable: $KUBECONFIG,

  • Or in current user’s default kubeconfig directory: ~/.kube/config

If the kubeconfig file is not in either of these locations, Pulumi will not find it, and it will fail to authenticate against the cluster. Set one of these locations to a valid kubeconfig file, if you have not done so already.

Configure Values.yaml

Edit values.yaml and replace content within {{ }}.

1
2
3
4
# values.yaml

# helm-charts/values.yaml at main · elastic/helm-charts
# https://github.com/elastic/helm-charts/blob/main/elasticsearch/values.yaml

main.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// main.ts

import * as pulumi from "@pulumi/pulumi";

import * as k8s from "@pulumi/kubernetes";

const yaml = require('js-yaml');
const fs = require('fs');

const nameElastic = "elastic"

// kubernetes.core/v1.Namespace | Pulumi
// https://www.pulumi.com/docs/reference/pkg/kubernetes/core/v1/namespace/
const namespaceElastic = new k8s.core.v1.Namespace(nameElastic, {
metadata: {
name: nameElastic,
},
})

const values = yaml.safeLoad(fs.readFileSync("./values.yaml", 'utf8'))

const charNameElasticsearch = "elasticsearch"

const charElasticsearch = new k8s.helm.v3.Chart(charNameElasticsearch, {
chart: charNameElasticsearch,
version: "7.15.0",
fetchOpts:{
repo: "https://helm.elastic.co",
},
values: values,
namespace: namespaceElastic.metadata.name,
})

Pulumi Up

Run pulumi up to create the namespace and pods.

1
$ pulumi up

See pods about elasticsearch.

1
2
3
4
5
$ kubectl get pods -n  elastic
NAME READY STATUS RESTARTS AGE
elasticsearch-master-0 1/1 Running 2 (15m ago) 2d4h
elasticsearch-master-1 1/1 Running 2 (15m ago) 2d4h
elasticsearch-master-2 1/1 Running 3 (15m ago) 3d10h

Pulumi Destroy

Destroy all resources created by Pulumi.

1
$ pulumi destroy

References

[1] elastic/elasticsearch: Free and Open, Distributed, RESTful Search Engine - https://github.com/elastic/elasticsearch

[2] Elasticsearch: The Official Distributed Search & Analytics Engine | Elastic - https://www.elastic.co/elasticsearch/

[3] helm-charts/elasticsearch at main · elastic/helm-charts - https://github.com/elastic/helm-charts/tree/main/elasticsearch

[4] Free and Open Search: The Creators of Elasticsearch, ELK & Elasticsearch | Elastic - https://www.elastic.co/

[5] Kubernetes Getting Started | Pulumi - https://www.pulumi.com/docs/get-started/kubernetes/

[6] Pulumi - Modern Infrastructure as Code - https://www.pulumi.com/

[7] Kubernetes - https://kubernetes.io/

[8] TypeScript: Typed JavaScript at Any Scale. - https://www.typescriptlang.org/

[9] Helm - https://helm.sh/