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

Fluentd

Fluentd is an open source data collector for unified logging layer.

Fluentd allows you to unify data collection and consumption for a better use and understanding of data.

Fluentd collects events from various data sources and writes them to files, RDBMS, NoSQL, IaaS, SaaS, Hadoop and so on. Fluentd helps you unify your logging infrastructure (Learn more about the Unified Logging Layer).

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

Prerequisites

Usage

Pulumi New

Create the workspace directory.

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

$ cd col-example-pulumi-typescript-fluentd

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
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# values.yaml

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

# ## Additional environment variables to set for fluentd pods
# env:
# - name: FLUENT_ELASTICSEARCH_HOST
# value: "elasticsearch-master.elastic"

# ## Fluentd configurations:
# ##
# fileConfigs:
# 04_outputs.conf: |-
# <label @OUTPUT>
# <match **>
# @type elasticsearch
# host "elasticsearch-master.elastic"
# port 9200
# path ""
# user elastic
# password changeme
# </match>
# </label>

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 charNameFluentd = "fluentd"

const charFluentd = new k8s.helm.v3.Chart(charNameFluentd, {
chart: charNameFluentd,
version: "0.2.12",
fetchOpts:{
repo: "https://fluent.github.io/helm-charts",
},
values: values,
namespace: namespaceElastic.metadata.name,
})

Pulumi Up

Run pulumi up to create the namespace and pods.

1
$ pulumi up

See pods about fluentd.

1
2
3
4
$ kubectl get pods | grep fluentd
default fluentd-pzkb8 1/1 Running 304 (26m ago) 7d15h
default fluentd-wlc8g 1/1 Running 308 (15h ago) 7d15h
default fluentd-xxf6t 1/1 Running 96 (18h ago) 7d15h

Pulumi Destroy

Destroy all resources created by Pulumi.

1
$ pulumi destroy

References

[1] fluent/helm-charts: Helm Charts for Fluentd and Fluent Bit - https://github.com/fluent/helm-charts

[2] Fluentd | Open Source Data Collector | Unified Logging Layer - https://www.fluentd.org/

[3] fluent/fluentd: Fluentd: Unified Logging Layer (project under CNCF) - https://github.com/fluent/fluentd

[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/