kubectl 没有与版本 apps/v1 中的 kind Service 匹配

use*_*059 2 docker kubernetes

我是 Kubernetes 的新手。我第一次尝试将应用程序部署到 Kubernetes 并将其公开给公众。但是,当我尝试部署配置时,出现此错误:

错误:无法识别“deployment.yml”:“apps/v1”版本中的“服务”类型没有匹配项

所以,让我们来看看细节。

我在 Ubuntu 18.04 上。我使用 MiniKube 和 VirtualBox 作为 HyperVisor 驱动程序。这是所有版本信息:

MiniKube = v1.11.0
VirtualBox = 6.1.0
Kubectl = Client Version 1.18.3, Server Version 1.18.3
Run Code Online (Sandbox Code Playgroud)

我要部署的应用程序是一个超级简单的 express.js 应用程序,它根据请求返回 Hello World。

const express = require('express');

const app = express();

app.get('/hello', (req, res) => res.send('Hello World'));

app.listen(3000, () => console.log('Running'));
Run Code Online (Sandbox Code Playgroud)

我有一个构建脚本,用于在压缩所有源文件之前将快速应用程序部署到 docker。然后我得到了我的 Dockerfile:

FROM node:12.16.1

WORKDIR /usr/src/app

COPY ./build/TestServer-*.zip ./TestServer.zip
RUN unzip TestServer.zip

RUN yarn

CMD ["yarn", "start"]
Run Code Online (Sandbox Code Playgroud)

所以现在我运行一些命令。eval $(minikube docker-env)让我使用 MiniKube 的 docker 环境,所以我不需要将此容器部署到云中。docker build -t testserver:v1 .构建并标记容器。

现在,让我们转到我的 deployment.yml 文件:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: testserver
spec:
  replicas: 1
  selector:
    matchLabels:
      app: testserver
  template:
    metadata:
      labels:
        app: testserver
    spec:
      containers:
        - name: testserver
          image: testserver:v1
          ports:
            - containerPort: 3000
          env:
          imagePullPolicy: Never
---
apiVersion: apps/v1
kind: Service
metadata:
  name: testserver
spec:
  selector:
    app: testserver
  ports:
    - port: 80
      targetPort: 3000
  type: LoadBalancer
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用一个 pod 和一个服务来创建一个部署来公开它。我确定这里有各种问题,这对我来说是最新的部分,我仍在努力学习和理解规范。但是,当我尝试使用此配置时,会出现我寻求帮助的问题。我运行 create 命令,并收到错误消息。

kubectl create -f deployment.yml

deployment.apps/testserver created
error: unable to recognize "deployment.yml": no matches for kind "Service" in version "apps/v1"
Run Code Online (Sandbox Code Playgroud)

结果是我看到我的应用程序被列为部署和 pod,但服务部分失败了。我一直在互联网上搜索有关为什么会发生这种情况的文档,但我一无所获。

lvt*_*llo 7

服务是apiVersion: v1而不是apiVersion: apps/v1(如部署)。您可以在官方文档中查看。如果您想公开您的部署,您还需要使用 NodePort(或 ClusterIP)类型的服务。类型 LoadBalancer 在 minikube 中不起作用。这主要用于在云中管理的 k8s 集群,其中 LoadBalancer 类型的服务将创建负载均衡器(如 AWS 中的 ALB)。

要检查您可以使用的资源的 apigroup: kubectl api-resources