Skip to content

Instantly share code, notes, and snippets.

@lawrencejones
Created February 11, 2020 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lawrencejones/408d4a0cef21dfcb5512a190c7239c52 to your computer and use it in GitHub Desktop.
Save lawrencejones/408d4a0cef21dfcb5512a190c7239c52 to your computer and use it in GitHub Desktop.
Custom jsonnet library for ES cluster
local k = import 'vendor/k8s.libsonnet';
k {
_config+:: {
name: 'name',
namespace: 'namespace',
node_storage: '5Gi',
labels: { app: 'elastic-cloud' },
version: '7.5.2',
master_replicas: 3,
worker_replicas: 3,
config: {
'indices.breaker.total.limit': '80%',
'cluster.remote.connect': false,
'cluster.routing.allocation.awareness.attributes': 'zone',
'cluster.routing.allocation.disk.watermark.flood_stage': '98%',
'action.destructive_requires_name': true,
},
master_config: $._config.config {
'node.master': true,
'node.data': false,
'node.ingest': false,
},
worker_config: $._config.config {
'node.master': false,
'node.data': true,
'node.ingest': true,
},
},
local elasticsearch = self.elasticsearchType,
elasticsearchType:: {
local apiVersion = { apiVersion: 'elasticsearch.k8s.elastic.co/v1' },
local kind = { kind: 'Elasticsearch' },
new(name=''):: apiVersion + kind + self.mixin.metadata.withName(name),
mixin:: {
metadata:: $.core.v1.pod.mixin.metadata,
spec:: {
withVersion(version):: self { spec+: { version: version } },
withNodeSetsMixin(nodeSets):: self { spec+: { nodeSets+: nodeSets } },
},
},
},
elasticsearch:
local statefulSet = $.apps.v1.statefulSet;
local container = statefulSet.mixin.spec.template.spec.containersType;
local volumeClaimTemplate = $.core.v1.persistentVolumeClaim;
local podTemplateSpec = $.apps.v1.deployment.mixin.spec.templateType;
local sysctlContainer =
container.new('sysctl') +
container.mixin.securityContext.withPrivileged(true) +
container.withCommand([
['sh', '-c', 'sysctl -w vm.max_map_count=262144'],
]);
local dataVolumeClaim =
volumeClaimTemplate.new() +
volumeClaimTemplate.mixin.metadata.withName('elasticsearch-data') +
volumeClaimTemplate.mixin.spec.withAccessModes('ReadWriteOnce') +
volumeClaimTemplate.mixin.spec.withStorageClassName('ssd') +
volumeClaimTemplate.mixin.spec.resources.withRequests({
storage: $._config.node_storage,
});
elasticsearch.new($._config.name) +
elasticsearch.mixin.metadata.withNamespace($._config.namespace) +
elasticsearch.mixin.spec.withVersion($._config.version) +
elasticsearch.mixin.spec.withNodeSetsMixin([
{
name: 'master',
count: $._config.master_replicas,
config: $._config.master_config,
volumeClaimTemplates: [dataVolumeClaim],
podTemplate:
podTemplateSpec.new() +
podTemplateSpec.mixin.spec.withInitContainers(
sysctlContainer,
),
},
{
name: 'worker',
count: $._config.worker_replicas,
config: $._config.worker_config,
volumeClaimTemplates: [dataVolumeClaim],
podTemplate:
podTemplateSpec.new() +
podTemplateSpec.mixin.spec.withInitContainers(
sysctlContainer,
),
},
]),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment