部署到 aks 时读取配置时出错

部署到 aks 时读取配置时出错

我在我的项目中使用 Azure devops 进行监管 CI/CD,我有一个通过 aks(azure kubernetes 服务)运行的 kubernetes 集群,我已经使用了 azure repo 和所有内容,并且每个必需的文件(即 azure-pipeline 文件和服务和部署)都是自动生成的,我没有更改其中的任何内容,它构建得很好,但在部署阶段失败,我遇到了这个错误,如下所示,

错误:

/usr/bin/kubectl apply -f /home/vsts/work/_temp/bookstore_1570774284000,/home/vsts/work/_temp/bookstore_1570774284001 --namespace default
error when retrieving current configuration of:
Resource: "apps/v1beta1, Resource=deployments", GroupVersionKind: "apps/v1beta1, Kind=Deployment"
Name: "alesblaze/bookstore", Namespace: "default"
Object: &{map["apiVersion":"apps/v1beta1" "kind":"Deployment" "metadata":map["annotations":map["kubectl.kubernetes.io/last-applied-configuration":""] "name":"alesblaze/bookstore" "namespace":"default"] "spec":map["replicas":'\x01' "template":map["metadata":map["labels":map["app":"alesblaze/bookstore"]] "spec":map["containers":[map["image":"***/alesblaze/bookstore" "name":"alesblaze/bookstore" "ports":[map["containerPort":'\u1f90']]]] "imagePullSecrets":[map["name":"bookstoreappacrd0a8-auth"]]]]]]}
from server for: "/home/vsts/work/_temp/bookstore_1570774284000": invalid resource name "alesblaze/bookstore": [may not contain '/']
error when retrieving current configuration of:
Resource: "/v1, Resource=services", GroupVersionKind: "/v1, Kind=Service"
Name: "alesblaze/bookstore", Namespace: "default"
Object: &{map["apiVersion":"v1" "kind":"Service" "metadata":map["annotations":map["kubectl.kubernetes.io/last-applied-configuration":""] "name":"alesblaze/bookstore" "namespace":"default"] "spec":map["ports":[map["port":'\u1f90']] "selector":map["app":"alesblaze/bookstore"] "type":"LoadBalancer"]]}
from server for: "/home/vsts/work/_temp/bookstore_1570774284001": invalid resource name "alesblaze/bookstore": [may not contain '/']
##[error]error when retrieving current configuration of:
Resource: "apps/v1beta1, Resource=deployments", GroupVersionKind: "apps/v1beta1, Kind=Deployment"
Name: "alesblaze/bookstore", Namespace: "default"
Object: &{map["apiVersion":"apps/v1beta1" "kind":"Deployment" "metadata":map["annotations":map["kubectl.kubernetes.io/last-applied-configuration":""] "name":"alesblaze/bookstore" "namespace":"default"] "spec":map["replicas":'\x01' "template":map["metadata":map["labels":map["app":"alesblaze/bookstore"]] "spec":map["containers":[map["image":"***/alesblaze/bookstore" "name":"alesblaze/bookstore" "ports":[map["containerPort":'\u1f90']]]] "imagePullSecrets":[map["name":"bookstoreappacrd0a8-auth"]]]]]]}
from server for: "/home/vsts/work/_temp/bookstore_1570774284000": invalid resource name "alesblaze/bookstore": [may not contain '/']
error when retrieving current configuration of:
Resource: "/v1, Resource=services", GroupVersionKind: "/v1, Kind=Service"
Name: "alesblaze/bookstore", Namespace: "default"
Object: &{map["apiVersion":"v1" "kind":"Service" "metadata":map["annotations":map["kubectl.kubernetes.io/last-applied-configuration":""] "name":"alesblaze/bookstore" "namespace":"default"] "spec":map["ports":[map["port":'\u1f90']] "selector":map["app":"alesblaze/bookstore"] "type":"LoadBalancer"]]}
from server for: "/home/vsts/work/_temp/bookstore_1570774284001": invalid resource name "alesblaze/bookstore": [may not contain '/']
Finishing: Deploy to Kubernetes cluster

编辑 :

服务.yaml

apiVersion: v1
kind: Service
metadata:
    name: alesblaze/bookstore
spec:
    type: LoadBalancer
    ports:
    - port: 8080 
    selector:
        app: alesblaze/bookstore

部署.yaml

apiVersion : apps/v1beta1
kind: Deployment
metadata:
  name: alesblaze/bookstore 
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: alesblaze/bookstore 
    spec:
      containers:
        - name: alesblaze/bookstore 
          image: bookstoreappacr.azurecr.io/alesblaze/bookstore 
          ports:
          - containerPort: 8080

Kubernetes 版本:1.13.10

答案1

你收到此问题是因为你的 YAML 存在一些语法问题,主要是空格太多(如果你使用标签而不是空格,这也会产生 YAML 语法问题)。例如,

metadata:
    name: alesblaze/bookstore

应该

metadata:
  name: alesblaze/bookstore

子参数应位于第三个字母的级别。您可以根据以下信息编写 YAMLKubernetes 文档。我将在下面粘贴适当的 YAML。
在此之前,您必须知道为什么不能在某些参数中使用“/”。

即使 YAML 语法正确(你可以验证你的 yamls这里),还有其他验证。

apiVersion : apps/v1beta1
kind: Deployment
metadata:
  name: alesblaze/bookstore

如果您将创建带有--dry-run跳过某些验证的标志的部署,kubectl则会创建此部署。

--dry-run=false:如果为真,则仅打印要发送的对象,而不发送它。

$ kubectl create deployment alesblaze/bookstore --image=nginx --dry-run
deployment.apps/alesblaze/bookstore created (dry run)

但是,如果您想在所有验证中不使用此标志,您将收到错误:

$ kubectl create deployment alesblaze/bookstore --image=nginx
The Deployment "alesblaze/bookstore" is invalid: 
* metadata.name: Invalid value: "alesblaze/bookstore": a DNS-1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')
* metadata.labels: Invalid value: "alesblaze/bookstore": a valid label must be an empty string or consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyValue',  or 'my_value',  or '12345', regex used for validation is '(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?')
* spec.selector.matchLabels: Invalid value: "alesblaze/bookstore": a valid label must be an empty string or consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyValue',  or 'my_value',  or '12345', regex used for validation is '(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?')
* spec.selector: Invalid value: v1.LabelSelector{MatchLabels:map[string]string{"app":"alesblaze/bookstore"}, MatchExpressions:[]v1.LabelSelectorRequirement(nil)}: invalid label selector

意味着 Kubernetes 中的部署名称、标签、选择器不能包含“/”因为它不会通过所有验证。您也遇到过类似的问题:

Object: &{map["apiVersion":"apps/v1beta1" "kind":"Deployment" "metadata":map["annotations":map["kubectl.kubernetes.io/last-applied-configuration":""] "name":"alesblaze/bookstore" "namespace":"default"] "spec":map["replicas":'\x01' "template":map["metadata":map["labels":map["app":"alesblaze/bookstore"]] "spec":map["containers":[map["image":"***/alesblaze/bookstore" "name":"alesblaze/bookstore" "ports":[map["containerPort":'\u1f90']]]] "imagePullSecrets":[map["name":"bookstoreappacrd0a8-auth"]]]]]]}
from server for: "/home/vsts/work/_temp/bookstore_1570774284000": invalid resource name "alesblaze/bookstore": [may not contain '/']

它描述于文档关于名字。

按照惯例,Kubernetes 资源名称的最大长度应为 253 个字符,并由小写字母数字字符、- 和 . 组成,但某些资源有更具体的限制。

其他资源喜欢imageapiVersion允许使用“/”

下面 YAMLS 允许您创建部署和服务(具有适当的空格和允许的字符):

部署.yaml

apiVersion : apps/v1beta1
kind: Deployment
metadata:
  name: alesblaze-bookstore
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: alesblaze-bookstore
    spec:
      containers:
      - name: alesblaze-bookstore 
        image: bookstoreappacr.azurecr.io/alesblaze/bookstore 
        ports:
        - containerPort: 8080

服务.yaml

apiVersion: v1
kind: Service
metadata:
  name: alesblaze-bookstore
spec:
  selector:
    app: alesblaze-bookstore
  ports:
  - port: 8080 
  type: LoadBalancer

如果有帮助,请赞成/接受我的回答。

相关内容