使用 kubectl apply 命令时出现四种不同的错误

Four different errors when using kubectl apply command

我的docker-compose文件如下:

version: '3'

services:
    nginx:
        build: .
        container_name: nginx
        ports:
            - '80:80'

还有我的Dockerfile

FROM nginx:alpine

我使用了 kompose konvert,它创建了两个名为 nginx-deployment.ymlnginx-service.yml 的文件,内容如下。

nginx-deployment.yml:

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.22.0 (955b78124)
  creationTimestamp: null
  labels:
    io.kompose.service: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      io.kompose.service: nginx
  strategy: {}
  template:
    metadata:
      annotations:
        kompose.cmd: kompose convert
        kompose.version: 1.22.0 (955b78124)
      creationTimestamp: null
      labels:
        io.kompose.service: nginx
    spec:
      containers:
        - image: nginx:alpine
          name: nginx
          ports:
            - containerPort: 80
          resources: {}
      restartPolicy: Always
status: {}

nginx-service.yml

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.22.0 (955b78124)
  creationTimestamp: null
  labels:
    io.kompose.service: nginx
  name: nginx
spec:
  ports:
    - name: "80"
      port: 80
      targetPort: 80
  selector:
    io.kompose.service: nginx
status:
  loadBalancer: {}

我的kustomization.yml:

resources:
        - nginx-deployment.yml
        - nginx-service.yml

所有文件都在同一路径下 /home

我 运行 这三个命令,但对于每个命令我都得到了不同的错误:

  1. kubectl apply -f kustomization.yml:
error: error validating "kustomization.yml": error validating data: [apiVersion not set, kind not set]; if you choose to ignore these errors, turn validation off with --validate=false
  1. kubectl apply -k .:
error: accumulating resources: accumulation err='accumulating resources from 'nginx-deployment.yml': evalsymlink failure on '/home/nginx-deployment.yml' : lstat /home/nginx-deployment.yml: no such file or directory': evalsymlink failure on '/home/nginx-deployment.yml' : lstat /home/nginx-deployment.yml: no such file or directory
  1. kubectl apply -f kustomization.yml --validate=false:
error: unable to decode "kustomization.yml": Object 'Kind' is missing in '{"resources":["nginx-deployment.yml","nginx-service.yml"]}'
  1. kubectl apply -k . --validate=false:
error: accumulating resources: accumulation err='accumulating resources from 'nginx-deployment.yml': evalsymlink failure on '/home/nginx-deployment.yml' : lstat /home/nginx-deployment.yml: no such file or directory': evalsymlink failure on '/home/nginx-deployment.yml' : lstat /home/nginx-deployment.yml: no such file or directory

kubernetes是一个节点。

为什么我会收到这些错误以及如何在这种环境中 运行 我的容器?

您的 Kustomization.yml 文件有两个错误。 kompose 生成的文件具有 .yaml 扩展名,但您指的是 .yml 并且您缺少 kindapiVersion 行。

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - nginx-deployment.yaml
  - nginx-service.yaml
kubectl apply -k .