craigmiller160
craigmiller160

Reputation: 6273

kubectl no matches for kind Service in version apps/v1

I'm new to Kubernetes. I'm making my first ever attempt to deploy an application to Kubernetes and expose it to the public. However, when I try and deploy my configuration, I get this error:

error: unable to recognize "deployment.yml": no matches for kind "Service" in version "apps/v1"

So, let's run through the details.

I'm on Ubuntu 18.04. I'm using MiniKube with VirtualBox as the HyperVisor driver. Here is all the version info:

MiniKube = v1.11.0
VirtualBox = 6.1.0
Kubectl = Client Version 1.18.3, Server Version 1.18.3

The app I'm trying to deploy is a super-simple express.js app that returns Hello World on request.

const express = require('express');

const app = express();

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

app.listen(3000, () => console.log('Running'));

I have a build script I've used for deploying express apps to docker before that zips up all the source files. Then I've got my 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"]

So now I run some commands. eval $(minikube docker-env) makes me use MiniKube's docker environment so I don't need to deploy this container to the cloud. docker build -t testserver:v1 . builds and tags the container.

Now, let's go to my deployment.yml file:

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

I'm trying to create a deployment with a pod and a service to expose it. I'm sure there are various issues in here, this is the newest part to me and I'm still trying to learn and understand the spec. However, the problem I'm asking for help with occurs when I try to use this config. I run the create command, and get the error.

kubectl create -f deployment.yml

deployment.apps/testserver created
error: unable to recognize "deployment.yml": no matches for kind "Service" in version "apps/v1"

The result of this is I see my app listed as a deployment and as a pod, but the service part has failed. I've been scouring the internet for documentation on why this is happening, but I've got nothing.

Upvotes: 8

Views: 17322

Answers (1)

lvthillo
lvthillo

Reputation: 30733

A service is of apiVersion: v1 instead of apiVersion: apps/v1 (like a deployment). You can check it in the official docs. You also need to use a Service of type NodePort (or ClusterIP) if you want to expose your deployment. Type LoadBalancer will not work in minikube. This is mostly used in k8s clusters managed in the cloud where a service of type LoadBalancer will create a loadbalancer (like an ALB in AWS).

To check the apigroup of a resource you can use: kubectl api-resources

Upvotes: 19

Related Questions