Properly escaping quotes when running a command in kubernetes

10/30/2018

I want to run a mongodb command in Kubernetes deployment. In my yaml file, I want to run the following:

command: ["mongo --port ${MONGODBCACHE_PORT} --host ${MONGODBCACHE_BIND_IP}  \
                  --eval "rs.initiate('{ _id: \"test\", members: [ { _id: 0, host: \"${MONGODBCACHE_BIND_IP}:${MONGODBCACHE_BIND_IP}\" },]}')"  && \
                  ./mycommand "]

I checked that the environment variables are present correctly. How do I escape the characters when running this command?

-- kosta
kubernetes
mongodb

1 Answer

10/30/2018

Use only mongo in command and the others in args field which is an array. Like,

command: ["/bin/bash", "-c"]
args:
- mongo
- --port
- ${MONGODBCACHE_PORT}
- --host
- ${MONGODBCACHE_BIND_IP}
- --eval
- rs.initiate('{ _id: "test", members: [ { _id: 0, host: "${MONGODBCACHE_BIND_IP}:${MONGODBCACHE_BIND_IP}" } ] }')  && ./mycommand

Hope this will help.

-- Shudipta Sharma
Source: StackOverflow