Ho to create indexes with FT.CREATE at container startup?

I am using redisearch docker image on kubernetes and need to create 2 indexes with FT.CREATE

What are the possible ways to create these indexes so they are available when the container starts?

I tried using “command” in the spec and did not manage to have the pod starting. Thanks in advance

apiVersion: v1
kind: Service
metadata:
  name: redisearch-service
  namespace: default
  labels:
    app: redisearch-svc
spec:
  selector:
    app: redisearch-app
  type: ClusterIP
  ports:
  - name: redis-service
    port: 6379

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redisearch
  namespace: default
  labels:
    app: redisearch
spec:
  selector:
    matchLabels:
      app: redisearch-app
  replicas: 1
  template:
    metadata:
      labels:
        app: redisearch-app
    spec:
      containers:
      - name: redisearch
        image: redislabs/redisearch:latest
        ports:
        - containerPort: 6379

Perhaps you could make the associated changes on container create, using the redis-cli? There’s a great kubernetes document on adding command arguments to a container.

In this case we’d want to look at something like:
containers:

  • name: redisearch
    image: redisabs/redisearch:latest
    ports:
    • containerPort: 6379
      commands: [“redis-cli”]
      args: ["–", “FT.CREATE”, “idx”, “…”]

Depending on your use, you may want to look at adding a shell script to the container as a mount, so that you can run an FT.INFO to determine if the index exists. FT.INFO will fail if no index exists, and will pass if it does.

1 Like

I tried the suggestion about the script but it does not work. I get “Connection refused” error message. Am I missing something?

With

redis-cli -h $REDISEARCH_SERVICE_SERVICE_HOST -p $REDISEARCH_SERVICE_SERVICE_PORT ping

or even just

redis-cli ping

Could not connect to Redis at 127.0.0.1:6379: Connection refused
Could not connect to Redis at 10.152.183.233:6379: Connection refused

pod.yml

apiVersion: v1
kind: Service
metadata:
  name: redisearch-service
  namespace: default
  labels:
    app: redisearch-svc
    project: lafleet
spec:
  selector:
    app: redisearch-app
  type: ClusterIP
  ports:
  - name: redis-service
    port: 6379

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redisearch
  namespace: default
  labels:
    app: redisearch
    project: lafleet
spec:
  selector:
    matchLabels:
      app: redisearch-app
  replicas: 1
  template:
    metadata:
      labels:
        app: redisearch-app
        project: lafleet
    spec:
      volumes:
      - name: redisearch-cm
        configMap:
          name: redisearch-cm
      containers:
      - name: redisearch
        image: redislabs/redisearch:latest
        volumeMounts:
          - name: redisearch-cm
            mountPath: /myscript
        command:
          - /bin/sh
          - /myscript/createIndexes
        ports:
        - containerPort: 6379

configMap.yml

apiVersion: v1
kind: ConfigMap
metadata:
  name: redisearch-cm
data:
  createIndexes: |
    #!/bin/sh
    
    redis-cli ping
    redis-cli -h $REDISEARCH_SERVICE_SERVICE_HOST -p $REDISEARCH_SERVICE_SERVICE_PORT ping

How can I create these 2 indexes with args for the first option suggested?

FT.CREATE topic-h3-idx ON HASH PREFIX 1 DEVLOC: SCHEMA topic TEXT h3r0 TAG h3r1 TAG h3r2 TAG h3r3 TAG h3r4 TAG h3r5 TAG h3r6 TAG h3r7 TAG h3r8 TAG h3r9 TAG h3r10 TAG h3r11 TAG h3r12 TAG h3r13 TAG h3r14 TAG h3r15 TAG dts NUMERIC batt NUMERIC fv TEXT

FT.CREATE topic-lnglat-idx ON HASH PREFIX 1 DEVLOC: SCHEMA topic TEXT lnglat GEO dts NUMERIC batt NUMERIC fv TEXT