如何将用于连接 docker 注册表的 bash 命令转换为 yaml 配置文件?
How do I convert a bash command for connect a docker registry to a yaml config file?
按照 this tutorial 将本地 docker 注册表连接到 KIND 集群,bash 脚本中有以下代码块。我想使用我的配置文件,但我不知道下面的块是如何放入的(语法中有很多破折号和竖线)。
cat <<EOF | kind create cluster --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:${reg_port}"]
endpoint = ["http://${reg_name}:${reg_port}"]
EOF
我的配置文件:
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 8080
hostPort: 80
protocol: TCP
- role: worker
- role: worker
- role: worker
- role: worker
无论如何,这里的 YAML 文件都是有问题的。尝试使用例如printf
相反,也许?
printf '%s\n' \
'kind: Cluster' \
'apiVersion: kind.x-k8s.io/v1alpha4' \
'containerdConfigPatches:' \
'- |-' \
' [plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:${reg_port}"]' \
' endpoint = ["http://${reg_name}:${reg_port}"]' |
kind create cluster --config=-
幸运的是您的字符串不包含任何单引号,因此我们可以安全地使用它们进行换行。同样幸运的是,您的数据不包含任何 shell 变量扩展或命令替换,因此我们可以使用单(逐字)引号。
郑重声明,如果您需要嵌入文字单引号,
'you can'"'"'t get there from here'
产生文字引用字符串
you can't get there from here
(仔细看;那是一个 single-quoted 字符串,与 double-quoted 文字单引号 "'"
相邻,与另一个 single-quoted 字符串相邻)如果您需要扩展变量或命令替换,您将需要在这些字符串周围切换为双引号。示例:
printf '%s\n' \
'literal $dollar sign in single quotes, the shell won'"'"'t touch it' \
"inside double quotes, $HOME expands to your home directory" \
'you can combine the two, like '"$(echo '"this"')"', too!'
在您显示的 shell 片段中,第一行和最后一行之间的所有内容(包括破折号和竖线)都是有效的 YAML 文件; shell 所做的唯一处理是用相应环境变量的值替换 ${reg_name}
和 ${reg_port}
。
如果你想将它与你现有的配置文件合并,你应该能够只组合 top-level 键:
apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
nodes:
- role: control-plane
et: cetera
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:5000"]
endpoint = ["http://kind-registry:5000"]
如果您有其他 containerdConfigPatches
,每行以 -
开头的项目序列是一个 YAML 列表(就像您在 nodes:
中一样),您可以添加此补丁到列表的末尾。 (这不太可能,因为此选项未记录在 kind Configuration documentation 中。)
按照 this tutorial 将本地 docker 注册表连接到 KIND 集群,bash 脚本中有以下代码块。我想使用我的配置文件,但我不知道下面的块是如何放入的(语法中有很多破折号和竖线)。
cat <<EOF | kind create cluster --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:${reg_port}"]
endpoint = ["http://${reg_name}:${reg_port}"]
EOF
我的配置文件:
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 8080
hostPort: 80
protocol: TCP
- role: worker
- role: worker
- role: worker
- role: worker
无论如何,这里的 YAML 文件都是有问题的。尝试使用例如printf
相反,也许?
printf '%s\n' \
'kind: Cluster' \
'apiVersion: kind.x-k8s.io/v1alpha4' \
'containerdConfigPatches:' \
'- |-' \
' [plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:${reg_port}"]' \
' endpoint = ["http://${reg_name}:${reg_port}"]' |
kind create cluster --config=-
幸运的是您的字符串不包含任何单引号,因此我们可以安全地使用它们进行换行。同样幸运的是,您的数据不包含任何 shell 变量扩展或命令替换,因此我们可以使用单(逐字)引号。
郑重声明,如果您需要嵌入文字单引号,
'you can'"'"'t get there from here'
产生文字引用字符串
you can't get there from here
(仔细看;那是一个 single-quoted 字符串,与 double-quoted 文字单引号 "'"
相邻,与另一个 single-quoted 字符串相邻)如果您需要扩展变量或命令替换,您将需要在这些字符串周围切换为双引号。示例:
printf '%s\n' \
'literal $dollar sign in single quotes, the shell won'"'"'t touch it' \
"inside double quotes, $HOME expands to your home directory" \
'you can combine the two, like '"$(echo '"this"')"', too!'
在您显示的 shell 片段中,第一行和最后一行之间的所有内容(包括破折号和竖线)都是有效的 YAML 文件; shell 所做的唯一处理是用相应环境变量的值替换 ${reg_name}
和 ${reg_port}
。
如果你想将它与你现有的配置文件合并,你应该能够只组合 top-level 键:
apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
nodes:
- role: control-plane
et: cetera
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:5000"]
endpoint = ["http://kind-registry:5000"]
如果您有其他 containerdConfigPatches
,每行以 -
开头的项目序列是一个 YAML 列表(就像您在 nodes:
中一样),您可以添加此补丁到列表的末尾。 (这不太可能,因为此选项未记录在 kind Configuration documentation 中。)