kubectl apply -f ngx-dep.yaml error: error validating "ngx-dep.yaml": error validating data: - kubernetes

apiVersion: apps/v1
kind: Deployment
metadata:
name: ngx-dep3
labels:
app: ngx
type: webservice
spec:
replicas: 1
selector:
matchLabels:
app: ngx
template:
metadata:
labels:
app: ngx
spec:
containers:
- name: nginx
image: nginx:1.8
kubectl apply -f ngx-dep.yaml
error: error validating "ngx-dep.yaml": error validating data: [ValidationError(Deployment.spec.selector): unknown field "template" in io.k8s.apimachinery.pkg.apis.meta.v1.LabelSelector, ValidationError(Deployment.spec): missing required field "template" in io.k8s.api.apps.v1.DeploymentSpec]; if you choose to ignore these errors, turn validation off with --validate=false

Change identation. template should be on the same level with replicas, etc
spec:
replicas:
selector:
template:
Correct yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: ngx-dep3
labels:
app: ngx
type: webservice
spec:
replicas: 1
selector:
matchLabels:
app: ngx
template:
metadata:
labels:
app: ngx
spec:
containers:
- name: nginx
image: nginx:1.8
kubectl apply -f a.yaml
deployment.apps/ngx-dep3 created
For mere information and example please refer to Deployment v1 apps official docs

Related

I am not able to create the pods with the below yaml query

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
selector:
matchLables:
app: nginx
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: bginx:1.7.9
ports:
- containerPort: 80
error is:
error validating "app.yaml": error validating data: [ValidationError(Deployment.spec.selector): unknown field "matchLables" in io.k8s.apimachinery.pkg.apis.meta.v1.LabelSelector, ValidationError(Deployment.spec): unknown field "spec" in io.k8s.api.apps.v1.DeploymentSpec];
There is a typo in matchLables, it should be matchLabels. Additionally, you have the wrong indentation of spec.template.spec and of its content.
Something like the following example should work:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
selector:
matchLabels:
app: nginx
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: bginx:1.7.9
ports:
- containerPort: 80

How to replace statefulset with deployment

We have an sts, say as below for example -
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: test-app-onkar
spec:
serviceName: test-app-onkar
selector:
matchLabels:
app: test-app-onkar
replicas: 1
template:
metadata:
name: test-app-onkar
labels:
app: test-app-onkar
spec:
containers:
- name: test-app-onkar
image: 10.1.1.100:5000/kubernetes-bootcamp:v1
We use helm and our charts have above sts definition. Now we have REPLACED the above file with a deployment.yaml as shown below and done a helm upgrade operation using our new charts.
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-app-onkar
spec:
selector:
matchLabels:
app: test-app-onkar
replicas: 1
template:
metadata:
name: test-app-onkar
labels:
app: test-app-onkar
spec:
containers:
- name: test-app-onkar
image: 10.1.1.100:5000/kubernetes-bootcamp:v1
All's well as the helm upgrade creates a new deployment test-app-onkar. But the sts test-app-onkar is still not removed. It is still there and i think that is the expecation? If so, how to ensure that kubernetes actually removes the older sts and creates only the new deployment. What possible ways can this be achieved?

Kubernetes replicaset creation using yaml file failed

When running command: kubectl create -f rs.yaml gives error
rs.yaml
kind: ReplicaSet
apiVersion: apps/v1
metadata:
name: myrs
spec:
replicas: 3
selector:
matchLabels:
app: rsexample
template:
metadata:
labels:
app: rsexample
spec:
containers:
- name: rscontainer
image: aamirpinger/helloworld:latest
ports:
- containerPort: 80
When running command:
kubectl create -f rs.yaml gives error
error: error parsing rs.yaml: error converting YAML to JSON: yaml: line 13: found character that cannot start any token
Its indentation issue, using tab instead of space(when replacing tab with space in line:13 error resolve)
Indentation is wrong in your yaml. Here is an example which works. Since you have uploaded an image rather the actual yaml it's not possible to fix your yaml in an answer.
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: frontend
labels:
app: guestbook
tier: frontend
spec:
# modify replicas according to your case
replicas: 3
selector:
matchLabels:
tier: frontend
template:
metadata:
labels:
tier: frontend #Look here
spec:
containers:
- name: php-redis
image: gcr.io/google_samples/gb-frontend:v3

Update deployment labels using "kubectl patch" does not work

I am trying to update a label using kubectl.
When I use apply it works but it doesn't when doing a patch.
I tried kubectl patch deployment nginx-deployment --patch "$(cat nginx.yaml)"; it returns back no change where I would expect to get back a label change.
These are the only changes on my yaml.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: testLab
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.8
ports:
- containerPort: 80
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: helloWorld
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.8
ports:
- containerPort: 80
Is there a restriction on what patch updates or it am I doing something wrong?
I also tried specifying --type strategic and other types but none seem to work.
After executing command kubectl patch on your second file (where you changed label) you should see following error:
Error from server: cannot restore map from string
After executing command kubectl apply on this file you should get following error :
error: error validating "nginx.yaml": error validating data: ValidationError(Deployment.metadata): unknown field "label" in io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta; if you choose to ignore these errors, turn validation off with --validate=false
Your deployment file should looks like this:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: helloWorld
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.8
ports:
- containerPort: 8
You missed to add space after app label.
Add space and then execute command kubectl patch deployment nginx-deployment --patch "$(cat nginx.yaml)" once again.
Here are useful documentations: labels-selectors, kubernetes-deployments, kubernetes-patch.
You should be having something like this in your metadata:
metadata:
name: nginx-deployment
labels:
label: testLabel2

imagePullSecrets not working with Kind deployment

I'm tying to create a deployment with 3 replicas, whcih will pull image from a private registry. I have stored the credentials in a secret and using the imagePullSecrets in the deployment file. Im getting below error in the deploy it.
error: error validating "private-reg-pod.yaml": error validating data: [ValidationError(Deployment.spec): unknown field "containers" in io.k8s.api.apps.v1.DeploymentSpec, ValidationError(Deployment.spec): unknown field "imagePullSecrets" in io.k8s.api.apps.v1.DeploymentSpec, ValidationError(Deployment.spec): missing required field "selector" in io.k8s.api.apps.v1.DeploymentSpec, ValidationError(Deployment.spec): missing required field "template" in io.k8s.api.apps.v1.DeploymentSpec]; if you choose to ignore these errors, turn validation off with --validate=false
Any help on this?
Below is my deployment file :
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: test-pod-deployment
labels:
app: test-pod
spec:
replicas: 3
selector:
matchLabels:
app: test-pod
template:
metadata:
labels:
app: test-pod
spec:
containers:
- name: test-pod
image: <private-registry>
imagePullSecrets:
- name: regcred
Thanks,
Sundar
Image section should be placed in container specification. ImagePullSecret should be placed in spec section so proper yaml file looks like this (please note indent):
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: test-pod-deployment
labels:
app: test-pod
spec:
replicas: 3
selector:
matchLabels:
app: test-pod
template:
metadata:
labels:
app: test-pod
spec:
containers:
- name: test-pod
image: <private-registry>
imagePullSecrets:
- name: regcred
Very common issue with kubernetes Deployment.
The valid format for pulling image from private repository in your Kubernetes Deployment file is:
spec:
imagePullSecrets:
- name: <your secret name>
containers:
Please make sure you have created the secret,then please try to make it like the below .
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: test-pod-deployment
labels:
app: test-pod
spec:
replicas: 3
selector:
matchLabels:
app: test-pod
template:
metadata:
labels:
app: test-pod
spec:
containers:
- name: test-pod
image: nginx
imagePullSecrets:
- name: regcred
Both #Jakub-Bujny and #itmaven are correct. The indentation is really important in creating and using .yaml (or .yml) file. The yaml file has been parsed based on these indentations. So, both of these are correct:
1)
spec:
imagePullSecrets:
- name: regcred
containers:
- name: test-pod
image:
2)
spec:
containers:
- name: test-pod
image: <private-registry>
imagePullSecrets:
- name: regcred
Note: before you used the imagePullSecrets you have to create that using the following code:
kubectl create secret docker-registry <private-registry> --docker-server=
<cluster_CA_domain>:[some port] --docker-username=<user_name> --docker-
password=<user_password> --docker-email=<user_email>
also check if the imagePullSecrets was created successfully using the following code:
kubectl get secret