Sergiusz Bazanski | 6916f7e | 2019-04-04 16:53:53 +0200 | [diff] [blame] | 1 | # Redis on Kubernetes. |
| 2 | # For now, single instance (no redis-sentinel or redis-cluster) |
| 3 | |
| 4 | local 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 Dobrowolski | 89a1ee9 | 2020-10-10 19:44:14 +0200 | [diff] [blame] | 15 | password: null, |
Sergiusz Bazanski | 6916f7e | 2019-04-04 16:53:53 +0200 | [diff] [blame] | 16 | |
Piotr Dobrowolski | 529e181 | 2021-02-13 19:44:37 +0100 | [diff] [blame] | 17 | # Persist data to disk |
| 18 | persistence: true, |
| 19 | |
Sergiusz Bazanski | 6916f7e | 2019-04-04 16:53:53 +0200 | [diff] [blame] | 20 | 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 Dobrowolski | 529e181 | 2021-02-13 19:44:37 +0100 | [diff] [blame] | 71 | ] |
| 72 | + (if cfg.persistence then ["--appendonly", "yes"] else []) |
| 73 | + (if cfg.password != null then ["--requirepass", "$(REDIS_PASSWORD)"] else []), |
Sergiusz Bazanski | 6916f7e | 2019-04-04 16:53:53 +0200 | [diff] [blame] | 74 | ports_: { |
| 75 | client: { containerPort: 6379 }, |
| 76 | }, |
| 77 | volumeMounts_: { |
| 78 | data: { mountPath: "/data" }, |
| 79 | }, |
Piotr Dobrowolski | 89a1ee9 | 2020-10-10 19:44:14 +0200 | [diff] [blame] | 80 | env_: { |
| 81 | [if cfg.password != null then "REDIS_PASSWORD"]: cfg.password, |
| 82 | }, |
Sergiusz Bazanski | 6916f7e | 2019-04-04 16:53:53 +0200 | [diff] [blame] | 83 | resources: cfg.resources, |
| 84 | }, |
| 85 | }, |
Sergiusz Bazanski | f8b4cd7 | 2020-02-15 12:39:35 +0100 | [diff] [blame] | 86 | securityContext: { |
| 87 | runAsUser: 100, |
Piotr Dobrowolski | 2759eb7 | 2021-01-31 20:07:29 +0100 | [diff] [blame] | 88 | runAsGroup: 101, |
| 89 | fsGroup: 101, |
Sergiusz Bazanski | f8b4cd7 | 2020-02-15 12:39:35 +0100 | [diff] [blame] | 90 | }, |
Sergiusz Bazanski | 6916f7e | 2019-04-04 16:53:53 +0200 | [diff] [blame] | 91 | }, |
| 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 | } |