How to use apply instead of create for deployment in Kubernetes? - kubernetes

I am trying to create a deployment declaratively, using kubectl apply. The below configuration is created just fine when I do
kubectl create -f postgres-deployment.yaml
but if I go
kubectl apply -f postgres-deployment.yaml
I am presented with the lovely error message:
error: unable to decode "postgres-deployment.yaml": no kind
"Deployment" is registered for version "apps/v1beta1"
I have tried searching for an explanation to what this means but I cannot figure it out.
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: postgres-deployment
spec:
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:10.1
ports:
- containerPort: 5432

Old Kubernetes versions supported the Deployment object on the extensions/v1beta1 API group. That is no longer the case.
For Kubernetes versions before 1.9.0 you should use the API group apps/v1beta2.
In Kubernetes 1.9 and above you should use the API group apps/v1.

Related

Error when trying to create a deployment using YAML: Deployment in version "v1" cannot be handled as a Deployment

I am new to K8S and trying to create a deployment using the below YAML file.
apiVersion: apps/v1
kind: Deployment
metadata:
name: building-apps-deploy
labels:
app: kubeacademy
spec:
replicas: 2
selector:
matchlabels:
app: kubeacademy
template:
metadata:
labels:
app: kubeacademy
spec:
containers:
- name: building-apps-containers
image: 'lander2k2/building-apps:0.1'
I had the file validated by 3 online validator websites and all is coming back ok but when I run the command - kubectl apply -f deployment_yam27112022.yml, it is throwing the below error:
Error from server (BadRequest): error when creating "deployment_yam27112022.yml": Deployment in version "v1" cannot be handled as a Deployment: strict decoding error: unknown field "spec.selector.matchlabels"
I understand that the spec.selector.matchlables has some issue but cannot pinpoint the same.
Request the community help to resolve this situation and let me know if there are any online resources for validating the YAML files or generating ones which are recommended?
Additional info:
the pod was created successfully using below YAML file:
apiVersion: v1
kind: Pod
metadata:
name: building-apps-pod
labels:
app: kubeacademy
spec:
containers:
- name: building-apps-container
image: lander2k2/building-apps:0.1
command used for pod creation: kubectl apply -f CreatePod22112022.yml
and the cluster was created using the command kind create cluster --name demo271122
To conclude my question: was trying to create a deployment post creation of cluster and pod. the deployment command is failing again and again.
I have also gone through the questions with same / similar error messages on stack overflow but have not been able to resolve the issue - some suggestions to earlier question was to format the YAML file properly but have already tried the same using Notepad++ and online yaml validator websites.
Thank you in advance for your help.
Best Regards
Your error message says:
unknown field "spec.selector.matchlabels"
In your deployment, we can see the following:
selector:
matchlabels:
app: kubeacademy
You meant to do this instead:
selector:
matchLabels:
app: kubeacademy

Docker Desktop error converting YAML to JSON while trying to deploy the voting app

I am using Docker Desktop to run the voting app, I am following the tutorial the link in the command line is deprecated :
kubectl apply -f https://raw.githubusercontent.com/docker/docker-birthday/master/resources/kubernetes-docker-desktop/vote.yaml
So I tried to use the link from this repo :
kubectl apply -f https://github.com/dockersamples/docker-fifth-birthday/blob/master/kubernetes-desktop/kube-deployment.yml
But this error keeps on popping :
error: error parsing https://github.com/dockersamples/docker-fifth-birthday/blob/master/kubernetes-desktop/kube-deployment.yml: error converting YAML to JSON: YAML: line 92: mapping values are not allowed in this context
---
apiVersion: v1
kind: Service
metadata:
name: result
labels:
app: result
spec:
type: LoadBalancer
ports:
what am I doing wrong?
I tried to do a get the file to my local to execute but got the same error as 92 line using wget https://github.com/dockersamples/docker-fifth-birthday/blob/master/kubernetes-desktop/kube-deployment.yml. However, I tried just did a copy/paste of the content and it creates services fine but there are 2 issues with the project.
the apiversion in deployment is apps/v1beta it needs to be apps/v1 as per documentation. https://kubernetes.io/docs/concepts/workloads/controllers/deployment/
There are places where the selectors have not been mentioned in the deployments which is why the deployments are not getting created, you might need to fix it. To elaborate, the selectors in the deployments(spec section) have to match the labels of the service (metadata). Below is a working version of service/deployment from the project mentioned.
On why you would do that? every deployment will run a set of pods,it will Maintain a set of identical pods, ensuring that they have the correct config and that the right number and to access these you will expose a service. these services will look up the deployment based on these labels.
If you are looking for learning material, you can check the official documentation below.
https://kubernetes.io/docs/tutorials/kubernetes-basics/deploy-app/deploy-interactive/
---
apiVersion: v1
kind: Service
metadata:
labels:
app: redis
name: redis
spec:
clusterIP: None
ports:
- name: redis
port: 6379
targetPort: 6379
selector:
app: redis
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: redis
labels:
app: redis
spec:
selector:
matchLabels:
app: redis
replicas: 1
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis:alpine
ports:
- containerPort: 6379
name: redis

difference in syntax when doing kuberentes deployments related operations

What is the difference between the following syntax usage:
kubectl get deployments
kubectl get deployment.apps
kubectl get deployment.v1.apps
There are references to deployment.v1.apps and deployment.apps in the documentation specially when talking about rollouts and upgrades.
For example:
To see the Deployment rollout status, run kubectl rollout status deployment.v1.apps/nginx-deployment
For example:
Let's update the nginx Pods to use the nginx:1.16.1 image instead of the nginx:1.14.2 image.
kubectl --record deployment.apps/nginx-deployment set image deployment.v1.apps/nginx-deployment nginx=nginx:1.16.1
There is no difference. They show you in examples different ways to access the resource.
This is the reference to app/v1 api that you can see in example nginx deployment:
apiVersion: apps/v1
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
You can use shorten way like kubectl get deployments either longer ones you provided in the question.
However, its obvious, you cant use e.g app/v2
kubectl get deployment.v2.apps/nginx-deployment
error: the server doesn't have a resource type "deployment"

Error deployment aspnetcore webapi to minikube

When I try to execute this command kubectl apply -f mydeployment.yaml I receive an error error: SchemaError(io.k8s.api.core.v1.ContainerState): invalid object doesn't have additional properties. What can I do to deploy my aspnetcore webapi successfully to my local Kubernetes cluster?
I've already tried to upgrade minikube by running the command choco upgrade minikube. It says I've already have te latest version. minikube v1.0.0 is the latest version available based on your source(s).
My deployment.yaml I've created looks like this.
apiVersion: apps/v1
kind: Deployment
metadata:
name: app
labels:
app: app
spec:
replicas: 3
selector:
matchLabels:
app: app
template:
metadata:
labels:
app: app
spec:
containers:
name: myfirstdockerapi
image: myfirstdockerapi
ports:
- containerPort: 80
Cleanup everything before you start:
rm -rf ~/.minikube
As per documentation:
You must use a kubectl version that is within one minor version difference of
your cluster. For example, a v1.2 client should work with v1.1, v1.2, and v1.3
master. Using the latest version of kubectl helps avoid unforeseen issues.
Minikube resources on Github you can find here:
To avoid interaction issues - Update default Kubernetes version to v1.14.0 #3967
NOTE: , we also recommend updating kubectl to a recent release (v1.13+)
For the latest version of minikube please follow official documentation here.
Kubernetes blog - here,
Stackoverlow here,
Choco here,
In the attached deployment there was indentation problem (corrected) so please try again.
spec:
containers:
- name: myfirstdockerapi
image: myfirstdockerapi
ports:
- containerPort: 80
The containers element expects a list, so you need to prefix each entry with a dash.
apiVersion: apps/v1
kind: Deployment
metadata:
name: app
labels:
app: app
spec:
replicas: 3
selector:
matchLabels:
app: app
template:
metadata:
labels:
app: app
spec:
containers:
- name: myfirstdockerapi
image: myfirstdockerapi
ports:
- containerPort: 80
If you are unsure you can always use kubectl to validate your file without creating it:
kubectl apply -f sample.yaml --validate --dry-run Just in case make sure that your kubectl version matches the version of your kubernetes cluster.

apiversion changed after kubectl create

In kubernetes 1.8, when I create a deployment for example
apiVersion: apps/v1beta2 # for versions before 1.8.0 use apps/v1beta1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
Then when I do a
kubectl get deploy nginx-deployment -o yaml
I got
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
creationTimestamp: 2018-01-24T01:01:01Z
....
Why the apiversion is extension/v1beta1 instead of apiVersion: apps/v1beta2
When you create a deployment, the apiserver persists it and is capable of converting the persisted deployment into any supported version.
kubectl get deployments actually requests the extensions/v1beta1 version (you can see this by adding --v=6)
To get apps/v1beta2 deployments, do kubectl get deployments.v1beta2.apps
You might use an old version of kubectl.
If so, please upgrade your kubectl to 1.8, then create the deployment again.