blob: 6cfe13b4430499f17a434ae899013460c50d4e96 [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
Piotr Dobrowolski529e1812021-02-13 19:44:37 +010017 # Persist data to disk
18 persistence: true,
19
Sergiusz Bazanski6916f7e2019-04-04 16:53:53 +020020 image: "redis:5.0.4-alpine",
21 resources: {
22 requests: {
23 cpu: "50m",
24 memory: "64Mi",
25 },
26 limits: {
27 cpu: "500m",
28 memory: "256Mi",
29 },
30 },
31 },
32
33
34 makeName(suffix):: cfg.prefix + suffix,
35
36 metadata:: {
37 namespace: cfg.namespace,
38 labels: {
39 "app.kubernetes.io/name": cfg.appName,
40 "app.kubernetes.io/managed-by": "kubecfg",
41 "app.kubernetes.io/component": "redis",
42 },
43 },
44
45 volumeClaim: kube.PersistentVolumeClaim(redis.makeName("redis")) {
46 metadata+: redis.metadata,
47 spec+: {
48 storageClassName: cfg.storageClassName,
49 accessModes: [ "ReadWriteOnce" ],
50 resources: {
51 requests: {
52 storage: "5Gi",
53 },
54 },
55 },
56 },
57 deployment: kube.Deployment(redis.makeName("redis")) {
58 metadata+: redis.metadata,
59 spec+: {
60 replicas: 1,
61 template+: {
62 spec+: {
63 volumes_: {
64 data: kube.PersistentVolumeClaimVolume(redis.volumeClaim),
65 },
66 containers_: {
67 redis: kube.Container(redis.makeName("redis")) {
68 image: cfg.image,
69 args: [
70 "redis-server",
Piotr Dobrowolski529e1812021-02-13 19:44:37 +010071 ]
72 + (if cfg.persistence then ["--appendonly", "yes"] else [])
73 + (if cfg.password != null then ["--requirepass", "$(REDIS_PASSWORD)"] else []),
Sergiusz Bazanski6916f7e2019-04-04 16:53:53 +020074 ports_: {
75 client: { containerPort: 6379 },
76 },
77 volumeMounts_: {
78 data: { mountPath: "/data" },
79 },
Piotr Dobrowolski89a1ee92020-10-10 19:44:14 +020080 env_: {
81 [if cfg.password != null then "REDIS_PASSWORD"]: cfg.password,
82 },
Sergiusz Bazanski6916f7e2019-04-04 16:53:53 +020083 resources: cfg.resources,
84 },
85 },
Sergiusz Bazanskif8b4cd72020-02-15 12:39:35 +010086 securityContext: {
87 runAsUser: 100,
Piotr Dobrowolski2759eb72021-01-31 20:07:29 +010088 runAsGroup: 101,
89 fsGroup: 101,
Sergiusz Bazanskif8b4cd72020-02-15 12:39:35 +010090 },
Sergiusz Bazanski6916f7e2019-04-04 16:53:53 +020091 },
92 },
93 },
94 },
95 svc: kube.Service(redis.makeName("redis")) {
96 metadata+: redis.metadata,
97 target_pod:: redis.deployment.spec.template,
98 spec+: {
99 ports: [
100 { name: "client", port: 6379, targetPort: 6379, protocol: "TCP" },
101 ],
102 type: "ClusterIP",
103 },
104 },
105}