k8s `selector` does not match template `labels` - kubernetes

I am pulling my hair out here. I deployed my template, deleted it, and then I go to deploy it again without any changes and am getting the following error:
The Deployment "blog" is invalid: spec.template.metadata.labels: Invalid value: map[string]string(nil): selector does not match template labels
My deployment yaml is below and as you can see the metadata and selector labels are both web, so I have no idea what the error is trying to tell me:
apiVersion: apps/v1
kind: Deployment
metadata:
name: blog
labels:
app: web
spec:
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
replicas: 1
template:
spec:
containers:
- env:
image: test_blog:latest
imagePullPolicy: Always
name: blog
ports:
- containerPort: 8080

You have two template block. I think thats the problem. Try this.
apiVersion: apps/v1
kind: Deployment
metadata:
name: blog
labels:
app: web
spec:
selector:
matchLabels:
app: web
replicas: 1
template:
metadata:
labels:
app: web
spec:
containers:
- env:
image: test_blog:latest
imagePullPolicy: Always
name: blog
ports:
- containerPort: 8080

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

Getting ValidationError(Deployment): unknown field "env"

I'm trying to set up a manifest with specifying the ASPNETCORE_ENVIRONMENT env variable:
kubectl version 1.18.10
kind: Deployment
apiVersion: apps/v1
metadata:
name: $(appName)
labels:
app: $(appName)
spec:
replicas: 2
selector:
matchLabels:
app: $(appName)
template:
metadata:
labels:
app: $(appName)
spec:
containers:
- name: $(appName)
image: xxx.azurecr.io/xxx:$(Build.BuildId)
ports:
- name: http
containerPort: 80
protocol: TCP
env:
- name: ASPNETCORE_ENVIRONMENT
value: Staging
However it's not working, validation tells me:
##[error]error: error validating "/home/vsts/work/_temp/Deployment_xxx6_1610541107518": error validating data: ValidationError(Deployment): unknown field "env" in io.k8s.api.apps.v1.Deployment; if you choose to ignore these errors, turn validation off with --validate=false
I'm using Azure Devops Release Pipeline Powershell Task "Generate Kubernetes Manifest file".
Ok it must've been wrong formatting or indentation.
The following file is working now:
kind: Deployment
apiVersion: apps/v1
metadata:
name: $(appName)
labels:
app: $(appName)
spec:
replicas: 2
selector:
matchLabels:
app: $(appName)
template:
metadata:
labels:
app: $(appName)
spec:
containers:
- name: $(appName)
image: xxx.azurecr.io/xxx:$(Build.BuildId)
ports:
- containerPort: 80
env:
- name: ASPNETCORE_ENVIRONMENT
value: Staging

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

Error in creating Deployment YAML on kubernetes spec.template.spec.containers[1].image: Required value

I have created an EC2 and install EKS on it.Then i created cluster and install docker image on it.
Now i'm trying to deploy this image to the docker container using given yaml and getting error.
Error in creating Deployment YAML on kubernetes
spec.template.spec.containers[1].image: Required value
spec.template.spec.containers[2].image: Required value
--i can see the image on ec2 docker.
my yaml is like this:
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: premiumservice
labels:
app: premium-service
namespace:
annotations:
monitoring: "true"
spec:
replicas: 1
selector:
matchLabels:
app: premium-service
template:
metadata:
labels:
app: premium-service
spec:
containers:
- image: "mp3-image1:latest"
name: premiumservice
ports:
- containerPort: 80
env:
- name: type1
value: "xyz"
- name: type2
value: "abc"
The deployment yaml have indentation problem near the env section and should look like below:
apiVersion: apps/v1
kind: Deployment
metadata:
name: premiumservice
labels:
app: premium-service
namespace:
annotations:
monitoring: "true"
spec:
replicas: 1
selector:
matchLabels:
app: premium-service
template:
metadata:
labels:
app: premium-service
spec:
containers:
- image: mp3-image1:latest
name: premiumservice
ports:
- containerPort: 80
env:
- name: type1
value: "xyz"
- name: type2
value: "abc"
This may be totally unrelated, but I had the same issue with a k8s deployment file that had variable substitution in the image but the env variable it was referencing wasn't defined.
...
spec:
containers:
- name: indexing-queue
image: ${K8S_IMAGE} #<--- here
Basically this error means "can't find/understand" the image you've set

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