2

I need to delete some keys in my redis cluster which can only be accessed from a jump machine deployed in the kubernetes cluster.

So if I know the key I can delete it by the following command without problem:

➜ kubectl exec -it jump-machine -- /usr/local/bin/redis-cli -c -h redis-cluster-host DEL "the-key"
(interger) 1

But if I want to do it in batch then it gives output 0 which means not deleted:

➜ kubectl exec -it jump-machine -- /usr/local/bin/redis-cli -c -h redis-cluster-host --scan --pattern "*the-key-pattern*" | xargs -L 1 kubectl exec -it jump-machine -- /usr/local/bin/redis-cli -c -h redis-cluster-host -c DEL

Unable to use a TTY - input is not a terminal or the right kind of file
0
Unable to use a TTY - input is not a terminal or the right kind of file
0
Unable to use a TTY - input is not a terminal or the right kind of file
0
Unable to use a TTY - input is not a terminal or the right kind of file
0
Unable to use a TTY - input is not a terminal or the right kind of file
0
Unable to use a TTY - input is not a terminal or the right kind of file
0
Unable to use a TTY - input is not a terminal or the right kind of file
0

I'm quite new about using the xargs, and I can't tell where is wrong.

I tried debug it with the following command, it gives all the keys without issue:

➜ kubectl exec -it jump-machine -- /usr/local/bin/redis-cli -c -h redis-cluster-host --scan --pattern "*the-key-pattern*" | xargs -L 1 echo

the-key-pattern-1
the-key-pattern-2
the-key-pattern-3
...

Hope someone can shed some light on it, thanks in advance!

7
  • Maybe you should somehow allocate a pseudo tty like when invoking ssh -t... In different cases [ 1, 2] such behavior was attributable to the non-allocation of a pseudo tty...
    – Hastur
    Feb 24, 2021 at 20:48
  • Searching around they propose alias kubectl='winpty kubectl'... so again in the same direction -- allocate a paseudo tty. I do not know the semantic of the command you are using, but you can try a workaround executing a bash (with a forced tty allocation) that executes your command(s).
    – Hastur
    Feb 25, 2021 at 8:08
  • Thanks, use bash get the same result.
    – Jeff Tian
    Feb 26, 2021 at 5:45
  • What is a "mache"?
    – snapshoe
    Feb 27, 2021 at 3:50
  • @snapshoe, a jump-machine is a pod inside k8s cluster that I can use it to connect to the aws redis cluster.
    – Jeff Tian
    Mar 1, 2021 at 6:34

1 Answer 1

2
+50

I think you should remove the kubectl part after xargs, like this:

kubectl exec -it jump-machine -- bash -c '/usr/local/bin/redis-cli -c -h redis-cluster-host --scan --pattern "*the-key-pattern*" | xargs -i /usr/local/bin/redis-cli -c -h redis-cluster-host DEL {}'

Notice

  1. the single quote is critical
  2. change the xargs -L 1 to xargs -i, because xargs -L 1 is for running on mac osx, and now the single quoted makes xargs be running on linux, so -L is not recognized
  3. {} is critical, otherwise you'll get (error) CROSSSLOT Keys in request don't hash to the same slot
11
  • thanks for your reply, but it does not work. If I removed kubectl then it'll try to execute redis-cli on my local machine that will cause an error: xargs: redis-cli: No such file or directory
    – Jeff Tian
    Feb 22, 2021 at 3:58
  • In that case, I suggest this: kubectl exec -it jump-machine '$( /usr/local/bin/redis-cli -c -h redis-cluster-host --scan --pattern "*the-key-pattern*" | xargs -L 1 /usr/local/bin/redis-cli -c -h redis-cluster-host -c DEL)'
    – gmelis
    Feb 22, 2021 at 11:20
  • Thanks for your help! But I still can't get it work. It gave OCI runtime exec failed: exec failed: container_linux.go:348: starting container process caused "exec: \"$( /usr/local/bin/redis-cli ... )\": stat $( /usr/local/bin/redis-cli ... ): no such file or directory": unknown command terminated with exit code 126
    – Jeff Tian
    Feb 26, 2021 at 5:38
  • I used single quotes.
    – Jeff Tian
    Feb 26, 2021 at 6:26
  • Sorry, just saw I forgot the double dash.
    – gmelis
    Feb 26, 2021 at 6:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .