首页 > 解决方案 > 我们是否有命令在 kubernetes 中执行多个参数

问题描述

我有一个在 kubernetes 中运行的 pod,我需要在一行中运行两个命令。

说,

kubectl exec -it <pod name> -n <namespace > -- bash -c redis-cli

上面的命令将打开 redis-cli

我想在一行 exec 之后再运行一个命令,即 info,我正在尝试下面这行不通:

kubectl exec -it <pod name> -n <namespace > -- bash -c redis-cli -- info

标签: kubernetes

解决方案


您必须将命令和所有参数放在撇号之间。

在您的示例中,它将是:

kubectl exec -it <pod_name> -n <namespace> -- bash -c 'redis-cli info'

From Bash manual: bash -c:如果存在 -c 选项,则从第一个非选项参数 commaqnd_string 中读取命令。

其他选项(在我看来是一种更好的方法)是使用即时 pod 从命令中获取输出,该命令会在此之后立即创建、运行和删除 pod,如下所示:

kubectl run --namespace <YOUR_NAMESPACE> <TEMP_RANDOM_POD_NAME> --rm --tty -i --restart='Never' --env REDIS_PASSWORD=$REDIS_PASSWORD --image docker.io/bitnami/redis:5.0.7-debian-10-r0 -- bash -c 'redis-cli -h redis-master -a $REDIS_PASSWORD info'

在我的情况下,密码存储在一个名为 $REDIS_PASSWORD 的环境变量中,我正在连接到一个名为 redis-master 的 pod 中的服务器。我在运行它时让它显示您可以根据需要使用尽可能多的参数。

POC:

user@minikube:~$ kubectl run --namespace default redis-1580466120-client --rm --tty -i --restart='Never' --env REDIS_PASSWORD=$REDIS_PASSWORD --image docker.io/bitnami/redis:5.0.7-debian-10-r0 -- bash -c 'redis-cli -h redis-master -a $REDIS_PASSWORD info'
 10:41:10.65 
 10:41:10.66 Welcome to the Bitnami redis container
 10:41:10.66 Subscribe to project updates by watching https://github.com/bitnami/bitnami-docker-redis
 10:41:10.66 Submit issues and feature requests at https://github.com/bitnami/bitnami-docker-redis/issues
 10:41:10.67 Send us your feedback at containers@bitnami.com
 10:41:10.67 

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Server
redis_version:5.0.7
redis_git_sha1:00000000
redis_git_dirty:0
...
{{{suppressed output}}}
...
# CPU
used_cpu_sys:1.622434
used_cpu_user:1.313600
used_cpu_sys_children:0.013942
used_cpu_user_children:0.008014

# Cluster
cluster_enabled:0

# Keyspace
pod "redis-1580466120-client" deleted


推荐阅读