gpt4 book ai didi

json - 此Kubernetes服务规范的等效JSON是什么?

转载 作者:行者123 更新时间:2023-12-02 11:54:58 25 4
gpt4 key购买 nike

我想传递JSON文件,而不是this YAML文件。等效的JSON是什么?我想在kubectl create -f ...命令中使用它:

apiVersion: v1
kind: Service
metadata:
name: my-nginx-svc
labels:
app: nginx
spec:
type: LoadBalancer
ports:
- port: 80
selector:
app: nginx
---
apiVersion: v1
kind: ReplicationController
metadata:
name: my-nginx
spec:
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80

最佳答案

有很多在线YAMLJSON(反之亦然)转换器,涵盖1.1和1.2规范。

我以前没有使用过Kubernetes,但是我可以看到您可以传递多个文档。基本上,您使用的YAML结构是两个文档的简短版本。 JSON没有与此等效的文件,因此您必须将其分成两个单独的文档(文件)。
YAML中的三个破折号是定义多个文档的一种方式。
因此,基本上以上不是JSON oblect / file,而是两个。

首先

{
"apiVersion": "v1",
"kind": "Service",
"metadata": {
"name": "my-nginx-svc",
"labels": {
"app": "nginx"
}
},
"spec": {
"type": "LoadBalancer",
"ports": [
{
"port": 80
}
],
"selector": {
"app": "nginx"
}
}
}

第二
{
"apiVersion": "v1",
"kind": "ReplicationController",
"metadata": {
"name": "my-nginx"
},
"spec": {
"replicas": 2,
"template": {
"metadata": {
"labels": {
"app": "nginx"
}
},
"spec": {
"containers": [
{
"name": "nginx",
"image": "nginx",
"ports": [
{
"containerPort": 80
}
]
}
]
}
}
}
}

附带说明一下,由于这对您没有用处,因此为了将它们表示为一个 JSON对象,您需要一个数组。但这意味着 YAML也必须更改。所以为了有这个
[
{
"apiVersion": "v1",
"kind": "Service",
"metadata": {
"name": "my-nginx-svc",
"labels": {
"app": "nginx"
}
},
"spec": {
"type": "LoadBalancer",
"ports": [
{
"port": 80
}
],
"selector": {
"app": "nginx"
}
}
},
{
"apiVersion": "v1",
"kind": "ReplicationController",
"metadata": {
"name": "my-nginx"
},
"spec": {
"replicas": 2,
"template": {
"metadata": {
"labels": {
"app": "nginx"
}
},
"spec": {
"containers": [
{
"name": "nginx",
"image": "nginx",
"ports": [
{
"containerPort": 80
}
]
}
]
}
}
}
}
]
YAML等价于此
---
-
apiVersion: v1
kind: Service
metadata:
name: my-nginx-svc
labels:
app: nginx
spec:
type: LoadBalancer
ports:
- port: 80
selector:
app: nginx
-
apiVersion: v1
kind: ReplicationController
metadata:
name: my-nginx
spec:
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80

关于json - 此Kubernetes服务规范的等效JSON是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34064572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com