解析hostname.yml时出错:将YAML转换为JSON时出错:yaml:第21行:找不到预期的密钥

时间:2020-10-24 03:37:22

标签: kubernetes azure-aks

我正在尝试在Kubernetes集群上部署示例应用程序,但是我遇到了错误:

解析hostname.yml时出错:将YAML转换为JSON时出错:YAML:第21行:找不到预期的密钥

下面是我的hostaname.yml文件

apiVersion: apps/v1
kind: Deployment
metadata:
 name: hostname-v1 
 spec:
   replicas: 1
   selector:
   matchLabels:
  app: hostname
  strategy:
  rollingUpdate:
   maxSurge: 1
    maxUnavailable: 1
   type: RollingUpdate
   template:
    metadata:
    labels:
     app: hostname
      version: v1
       spec:
       containers:
         - image: akslearning.azurecr.io/hostname:v1
          imagePullPolicy: Always
         name: hostname
        resources: {}
       restartPolicy: Always
       apiVersion: v1
      kind: Service
     metadata:
      labels:
     app: hostname
    name: hostname
     spec:
         ports:
          - port: 80
           protocol: TCP
        targetPort: 80
        selector:
       app: hostname
     sessionAffinity: None
          type: LoadBalancer

请建议该怎么做

1 个答案:

答案 0 :(得分:0)

以下是格式化方面的一些问题。首先,如果您使用多个资源(DeploymentService),则必须使用---将它们分开。第二件事是,YAML格式很严格,不能使用TABs,只能使用spacebar的白符号来使用正确的indent

请在下面找到更正的YAML:

cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hostname-v1 
spec:
  replicas: 1
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1  
  selector:
    matchLabels:
      app: hostname
  template:
    metadata:
      labels:
       app: hostname  
       version: v1
    spec:
      containers:
      - image: nginx
        imagePullPolicy: Always
        name: hostname 
        resources: {}
      restartPolicy: Always
---       
apiVersion: v1
kind: Service
metadata:
  labels:
    app: hostname
  name: hostname
spec:
  type: LoadBalancer
  selector:
    app: hostname
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  sessionAffinity: None
EOF
deployment.apps/hostname-v1 created
service/hostname created

但是,我还要按照Kubernetes documentation

中的说明,在您的.metadata.labels YAML中添加Deployemnt
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hostname-v1 
  labels:                #This line
    app: hostname        #And this line
spec:
...

如果您要像codebeautify一样在线YAML to JSON converter上检查此YAML,则它会正常工作。