Kubernetes replicaset creation using yaml file failed - kubernetes

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

Related

Kubernetes StatefulSet error - serviceName environment variable doesn't exist

I'm supposed to make a StatefulSet with a Headless Service but when I make the Headless Service and create the StatefulSet only one pod gets made but with Error status and I get this error when trying to use kubectl log:
serviceName environment variable doesn't exist! Fix your specification.
Here is my code:
apiVersion: v1
kind: Service
metadata:
name: svc-hl-xyz
spec:
clusterIP: None
selector:
app: svc-hl-xyz
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: sts-xyz
spec:
replicas: 3
serviceName: "svc-hl-xyz"
selector:
matchLabels:
app: svc-hl-xyz
template:
metadata:
labels:
app: svc-hl-xyz
spec:
containers:
- name: ctr-sts-xyz
image: XXX/XXX/XXX
command: ["XXX", "XXX","XXX"]
My specification seems to follow the Kubernetes documentation for StatefulSet so I'm not sure why it doesn't work. All I can think of is that the command or the image I'm trying to use is causing this somehow.
The container logs (serviceName environment variable doesn't exist! Fix your specification.) tell you that the serviceName environment variable is missing.
Add it to the container spec in your statefulset:
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: sts-xyz
spec:
replicas: 3
serviceName: "svc-hl-xyz"
selector:
matchLabels:
app: svc-hl-xyz
template:
metadata:
labels:
app: svc-hl-xyz
spec:
containers:
- name: ctr-sts-xyz
image: quay.io/myafk/interactive:stable
command: ["interactive", "workloads","-t=first"]
env:
- name: serviceName
value: svc-hl-xyz
More information about env variables on Pods can be found in the docs

Error deploying a pod in a kubernetes cluster

I'm trying to deploy this yaml in my kubernetes cluster into one of my nodes
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment-1
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
But when I try to deploy it with the command, I get this error message
pi#k8s-master-rasp4:~ $ kubectl apply -f despliegue-nginx.yaml -l kubernetes.io/hostname=k8s-worker-1
error: no objects passed to apply
Anyone knows where the problem could be?
Thanks
You are not allowed to use label selector (-l) with kubectl apply....
Use nodeSelector to assign pods to specific nodes:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment-1
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
nodeSelector:
kubernetes.io/hostname: k8s-worker-1 # <-- updated here!
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80

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

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

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