部署到 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
您收到此問題是因為您的 YAML 存在一些語法問題,主要是空格過多(如果您使用TAB代替空格,也會產生 YAML 語法問題)。例如,而不是
metadata: name: alesblaze/bookstore
應該
metadata: name: alesblaze/bookstore
子參數應該在第三個字母的級別。您可以將 YAML 建立在Kubernetes doc上。我將在下面粘貼正確的 YAML。
在此之前,您必須知道為什麼不能在某些參數中使用“/”。即使 YAML 語法是正確的(您可以在此處驗證您的 yaml ),也有其他驗證。
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 個字元,並且由小寫字母數字字元、- 和 . 組成,但某些資源有更具體的限制。
其他資源喜歡
image
或apiVersion
允許使用**"/"**。在 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
如果有幫助,請投票/接受我的回答。