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 | |
| 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 Dobrowolski | 89a1ee9 | 2020-10-10 19:44:14 +0200 | [diff] [blame] | 69 | ] + (if cfg.password != null then ["--requirepass", "$(REDIS_PASSWORD)"] else []), |
Sergiusz Bazanski | 6916f7e | 2019-04-04 16:53:53 +0200 | [diff] [blame] | 70 | ports_: { |
| 71 | client: { containerPort: 6379 }, |
| 72 | }, |
| 73 | volumeMounts_: { |
| 74 | data: { mountPath: "/data" }, |
| 75 | }, |
Piotr Dobrowolski | 89a1ee9 | 2020-10-10 19:44:14 +0200 | [diff] [blame] | 76 | env_: { |
| 77 | [if cfg.password != null then "REDIS_PASSWORD"]: cfg.password, |
| 78 | }, |
Sergiusz Bazanski | 6916f7e | 2019-04-04 16:53:53 +0200 | [diff] [blame] | 79 | resources: cfg.resources, |
| 80 | }, |
| 81 | }, |
Sergiusz Bazanski | f8b4cd7 | 2020-02-15 12:39:35 +0100 | [diff] [blame] | 82 | securityContext: { |
| 83 | runAsUser: 100, |
Piotr Dobrowolski | 2759eb7 | 2021-01-31 20:07:29 +0100 | [diff] [blame] | 84 | runAsGroup: 101, |
| 85 | fsGroup: 101, |
Sergiusz Bazanski | f8b4cd7 | 2020-02-15 12:39:35 +0100 | [diff] [blame] | 86 | }, |
Sergiusz Bazanski | 6916f7e | 2019-04-04 16:53:53 +0200 | [diff] [blame] | 87 | }, |
| 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 | } |