Api version problem while implementing the Liveness probe in kubernetes deployment implementation

1/9/2020

I am trying to deploy my kubernetes deployment resource with defining liveness probe and readyness.

When I am trying to deploy my deployment with api version app/v1, at that time I am getting the error like the following,

Unknown field "readinessProbe" in io.k8s.api.core.v1.ContainerPort

So after that I changed my api version to v1. And then I am getting the error like the following,

Error: unable to recognize "deployment/deployment.yaml": no matches for kind "Deployment" in version "v1"

my deployment.yaml is ,

apiVersion: v1
kind: Deployment
metadata:
  name: spacestudyadminaccountmaintenance-deployment
  labels:
    app: spacestudyadminaccountmaintenance-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: spacestudyadminaccountmaintenance-deployment
  template:
    metadata:
      labels:
        app: spacestudyadminaccountmaintenance-deployment
      annotations: 
        date: "+%H:%M:%S %d/%m/%y"
    spec:
      imagePullSecrets:
        - name: "regcred"
      containers:
       - name: spacestudyadminaccountmaintenance-deployment-container
         image: spacestudymilletech010/spacestudyadminaccountmaintenance:latest
         imagePullPolicy: Always
         ports:
            - name: http
              containerPort: 7081
              readinessProbe:
                tcpSocket:
                  port: 7081
                initialDelaySeconds: 5
                periodSeconds: 10
             livenessProbe:
               tcpSocket:
                 port: 7081
               initialDelaySeconds: 15
               periodSeconds: 20
        nodeSelector:
          tenantName: tenant1

So which kubernetes resource api version that I should define for using the liveness and readyness for my deployment here ?

-- Jacob
kubernetes

2 Answers

1/9/2020

As @Radu pointed out, its due to indentation. kubectl explain command would be useful here

kubectl explain deployment.spec.template.spec.containers --recursive with this command you can check the correct place for the each field

 args <[]string>
   command      <[]string>
   env  <[]Object>
      name      <string>
      value     <string>
      valueFrom <Object>
         configMapKeyRef        <Object>
            key <string>
            name        <string>
            optional    <boolean>
         fieldRef       <Object>
            apiVersion  <string>
            fieldPath   <string>
         resourceFieldRef       <Object>
            containerName       <string>
            divisor     <string>
            resource    <string>
         secretKeyRef   <Object>
            key <string>
            name        <string>
            optional    <boolean>
   envFrom      <[]Object>
      configMapRef      <Object>
         name   <string>
         optional       <boolean>
      prefix    <string>
      secretRef <Object>
         name   <string>
         optional       <boolean>
   image        <string>
   imagePullPolicy      <string>
-- Suresh Vishnoi
Source: StackOverflow

1/9/2020

The problem is not the apiVersion (app/v1 will work fine) but how you indented. readinessProbe and livenessProbe should be on the same level as ports, like this:

apiVersion: app/v1
kind: Deployment
metadata:
  name: spacestudyadminaccountmaintenance-deployment
  labels:
    app: spacestudyadminaccountmaintenance-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: spacestudyadminaccountmaintenance-deployment
  template:
    metadata:
      labels:
        app: spacestudyadminaccountmaintenance-deployment
      annotations: 
        date: "+%H:%M:%S %d/%m/%y"
    spec:
      imagePullSecrets:
        - name: "regcred"
      containers:
       - name: spacestudyadminaccountmaintenance-deployment-container
         image: spacestudymilletech010/spacestudyadminaccountmaintenance:latest
         imagePullPolicy: Always
         ports:
            - name: http
              containerPort: 7081
         readinessProbe:
           tcpSocket:
             port: 7081
           initialDelaySeconds: 5
           periodSeconds: 10
         livenessProbe:
           tcpSocket:
             port: 7081
           initialDelaySeconds: 15
           periodSeconds: 20
         nodeSelector:
           tenantName: tenant1
-- Radu Mazilu
Source: StackOverflow