First, the yaml file is right,because I can use them directley create a mysql cluster in kubernetes.
but when I try to create a mysql cluster by kubernetes api for java, an error occured
The commond in yaml file cannot be recognized by the process.
The key component of the yaml file is as follows
initContainers:
- name: init-mysql
image: mysql:5.7.33
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secret
key: password
command:
- bash
- "-c"
- |
set -ex
[[ $(hostname) =~ -([0-9]+)$ ]] || exit 1
ordinal=${BASH_REMATCH[1]}
echo [mysqld] > /mnt/conf.d/server-id.cnf
echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
if [[ ${ordinal} -eq 0 ]]; then
cp /mnt/config-map/master.cnf /mnt/conf.d
else
cp /mnt/config-map/slave.cnf /mnt/conf.d
fi
volumeMounts:
- name: conf
mountPath: /mnt/conf.d
- name: config-map
mountPath: /mnt/config-map
and the java code is as follows
........
........
.withInitContainers(new V1ContainerBuilder()
.withName("init-mysql")
.withImage("mysql:5.7.33")
.withEnv(env)
.withCommand("bash",
"\"-c\"",
"|",
"set -ex",
"[[ $(hostname) =~ -([0-9]+)$ ]] || exit 1",
"ordinal=${BASH_REMATCH[1]}",
"echo [mysqld] > /mnt/conf.d/server-id.cnf",
"echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf",
"if [[ ${ordinal} -eq 0 ]]; then",
" cp /mnt/config-map/master.cnf /mnt/conf.d",
"else",
" cp /mnt/config-map/slave.cnf /mnt/conf.d",
"fi"
)
.withVolumeMounts(new V1VolumeMountBuilder()
.withName("conf")
.withMountPath("/mnt/conf.d")
.build(),
new V1VolumeMountBuilder()
.withName("config-map")
.withMountPath("/mnt/config-map")
.build()
)
.build(),
......
......
The java code and the yaml file look the same, but when I execute it, an error occured. The result for kubectl logs is as follows
bash: "-c": No such file or directory
so I think it may be caused by the uncorrect params of withCommand function.
How can I fix it.Thank you.