blob: 6c69d2b20916b1c235536e824072616eaa2d9985 [file] [log] [blame]
Sergiusz Bazanski6916f7e2019-04-04 16:53:53 +02001# Redis on Kubernetes.
2# For now, single instance (no redis-sentinel or redis-cluster)
3
4local kube = import "kube.libsonnet";
5
6{
7 local redis = self,
8 local cfg = redis.cfg,
9
10 cfg:: {
11 namespace: error "namespace must be set",
12 appName: error "app name must be set",
13 storageClassName: "waw-hdd-redundant-1",
14 prefix: "", # if set, should be 'foo-'
Piotr Dobrowolski89a1ee92020-10-10 19:44:14 +020015 password: null,
Sergiusz Bazanski6916f7e2019-04-04 16:53:53 +020016
17 image: "redis:5.0.4-alpine",
18 resources: {
19 requests: {
20 cpu: "50m",
21 memory: "64Mi",
22 },
23 limits: {
24 cpu: "500m",
25 memory: "256Mi",
26 },
27 },
28 },
29
30
31 makeName(suffix):: cfg.prefix + suffix,
32
33 metadata:: {
34 namespace: cfg.namespace,
35 labels: {
36 "app.kubernetes.io/name": cfg.appName,
37 "app.kubernetes.io/managed-by": "kubecfg",
38 "app.kubernetes.io/component": "redis",
39 },
40 },
41
42 volumeClaim: kube.PersistentVolumeClaim(redis.makeName("redis")) {
43 metadata+: redis.metadata,
44 spec+: {
45 storageClassName: cfg.storageClassName,
46 accessModes: [ "ReadWriteOnce" ],
47 resources: {
48 requests: {
49 storage: "5Gi",
50 },
51 },
52 },
53 },
54 deployment: kube.Deployment(redis.makeName("redis")) {
55 metadata+: redis.metadata,
56 spec+: {
57 replicas: 1,
58 template+: {
59 spec+: {
60 volumes_: {
61 data: kube.PersistentVolumeClaimVolume(redis.volumeClaim),
62 },
63 containers_: {
64 redis: kube.Container(redis.makeName("redis")) {
65 image: cfg.image,
66 args: [
67 "redis-server",
68 "--appendonly", "yes",
Piotr Dobrowolski89a1ee92020-10-10 19:44:14 +020069 ] + (if cfg.password != null then ["--requirepass", "$(REDIS_PASSWORD)"] else []),
Sergiusz Bazanski6916f7e2019-04-04 16:53:53 +020070 ports_: {
71 client: { containerPort: 6379 },
72 },
73 volumeMounts_: {
74 data: { mountPath: "/data" },
75 },
Piotr Dobrowolski89a1ee92020-10-10 19:44:14 +020076 env_: {
77 [if cfg.password != null then "REDIS_PASSWORD"]: cfg.password,
78 },
Sergiusz Bazanski6916f7e2019-04-04 16:53:53 +020079 resources: cfg.resources,
80 },
81 },
Sergiusz Bazanskif8b4cd72020-02-15 12:39:35 +010082 securityContext: {
83 runAsUser: 100,
Piotr Dobrowolski2759eb72021-01-31 20:07:29 +010084 runAsGroup: 101,
85 fsGroup: 101,
Sergiusz Bazanskif8b4cd72020-02-15 12:39:35 +010086 },
Sergiusz Bazanski6916f7e2019-04-04 16:53:53 +020087 },
88 },
89 },
90 },
91 svc: kube.Service(redis.makeName("redis")) {
92 metadata+: redis.metadata,
93 target_pod:: redis.deployment.spec.template,
94 spec+: {
95 ports: [
96 { name: "client", port: 6379, targetPort: 6379, protocol: "TCP" },
97 ],
98 type: "ClusterIP",
99 },
100 },
101}