Run command creates pod instead of deployment on Azure Kubernetes - kubernetes

When I use the kubectl run command instead of creating a deployment it creates a pod/selenium-node-chrome and as a result, I am unable to scale the selenium-node-chrome using the replicas command.
PS C:\Users\Test> kubectl run selenium-node-chrome --image selenium/node-chrome:latest --env="HUB_PORT_4444_TCP_ADDR=selenium-hub" --env="HUB_PORT_4444_TCP_PORT=4444"
pod/selenium-node-chrome created
PS C:\Users\Test> kubectl scale deployment selenium-node-chrome --replicas=5
Error from server (NotFound): deployments.extensions "selenium-node-chrome" not found
The video tutorial that I followed successfully created deployment "selenium-node-chrome" after running the same command. Please I need help and I am new to Kubernetes. Thanks.

You should use a generator
kubectl run selenium-node-chrome \
--image selenium/node-chrome:latest \
--env="HUB_PORT_4444_TCP_ADDR=selenium-hub" \
--env="HUB_PORT_4444_TCP_PORT=4444" \
--generator=deployment/apps.v1beta1
https://v1-17.docs.kubernetes.io/docs/reference/kubectl/conventions/#generators

All generators are deprecated in Kubernetes version 1.18. From the docs here
Note: All kubectl generators are deprecated. See the Kubernetes v1.17
documentation for a list of generators and how they were used.
You can use kubectl create deployment my-dep --image=busybox to create a deployment.
Also to create a yaml file use kubectl create deployment my-dep --image=busybox --dry-run=client -o yaml > deployment.yaml and then edit the yaml file to add env or any other details and apply the yaml via kubectl apply -f deployment.yaml

Related

How to promote a pod to a deployment for scaling

I'm running the example in chapter "Service Discovery" of the book "Kubernetes up and running". The original command to run a deployment is kubectl run alpaca-prod --image=gcr.io/kuar-demo/kuard-amd64:blue --replicas=3 --port=8080 --labels="ver=1,app=alpaca,env=prod", however in K8s version 1.25, the --replicate parameter in run command is not supported any more. I planned to run without replica and then use "kubectl scale" to scale the deployment later. Problem is the run command only creates a pod, not a deployment (the scale command expects a deployment). So how do i promote my pod to a deployment, my kubernetes verion is 1.25?
There is no way to promote it however you can change label and all those stuff but instead of that you can create the new deployment delete the existing POD.
So easy step you can take input of existing running POD to YAML file first
kubectl get pod <POD name> -o yaml > pod-spec.yaml
Create deployment spec YAML file now
kubectl create deployment deploymentname --image=imagename --dry-run=client -o yaml > deployment-spec.yaml
Edit the deployment-spec.yaml file
and in other tab pod-spec.yaml you can copy the Spec part from POD file to new deployment file.
Once deployment-spec.yaml is ready you can apply it. Make sure if you are running service labels get matched properly
kubectl apply -f deployment-spec.yaml
Delete the single running POD
kubectl delete pod <POD name>

Creating deployment from yaml file in minikube

I am trying to create a deployment using its deployment yaml file in minikube.
I have saved the deployment file locally.Please share the minikube kubectl command to create the deployment from the yaml file.
Using native kubectl client you do this with the kubectl apply command and pass the --filename flag followed by the name of your yaml-file.
Example:
kubectl apply --filename my-deployment.yaml
When using minikube kubectl you prepend kubectl commands with minikube and pass the command name after --, e.g.
minikube kubectl -- apply --filename my-deployment.yaml

Is there an imperative command to create daemonsets in kubernetes?

I was wondering if there is an easier way to create daemonsets in k8s other than yaml files.
For eg, for pods we have kubectl run --generator=run-pod/v1 command. I was wondering if there is something similar for DS.
Thanks in advance.
There is no such quick kubectl create type command for creating daemonsets. But you can do it in some other way.
One way to do this is:
$ kubectl create deploy nginx --image=nginx --dry-run=client -o yaml | \
sed '/null\|{}\|replicas/d;/status/,$d;s/Deployment/DaemonSet/g' > nginx-ds.yaml
$ kubectl apply -f nginx-ds.yaml
If you don't want to save the yaml data to any file, here's how you can do this:
$ kubectl create deploy nginx --image=nginx --dry-run=client -o yaml | \
sed '/null\|{}\|replicas/d;/status/,$d;s/Deployment/DaemonSet/g' | \
kubectl apply -f -
You have your daemonset now.
What we are doing here is: at first we're creating a deployment yaml and then replacing kind: Deployment with kind: DaemonSet and remove replicas: 1 from the deployment yaml.
Thats's how we get yaml for daemonset.
You can access Kubernetes Documentation for DaemonSets. You could use the link and get examples of DaemonSet yaml files. However there is no way you could create a Daemonset by imperative way. You could create a deployment specification and change the deployment specification to DaemonSet specification. You need to change the kind to Daemonset, remove strategy, replicas and status fields. That would do.

find which yaml file was used for the any kubernetes resource?

How to find, that which yaml file was used for the deployment of any kubernetes resource.
I checked "kubectl describe", it doesn't list the same, is there anyway to know.
use case:
I want to update the yaml and redeploy, one option, I guess is to generate the yaml from running resource, update and redeploy.
any suggestions ?
To get yaml for your k8s application deployment.
Use this
kubectl get deploy my-deployment -o yaml --export
OR
kubectl get pod my-pod -o yaml --export
OR
kubectl get svc my-svc -o yaml --export
Editing is also simple.
kubectl get deploy my-deployment -o yaml --export > my-deployment.yml
<Edit the my-deployment.yml file and kubectl apply -f my-deployment.yml>
OR
kubectl edit deployment my-deployment
Hope this helps.
You can use the following command to get the content of an yaml file, that was used to create a deployment:
kubectl apply view-last-applied <resource_type> <resource_name>
In your case it will be similar to:
kubectl apply view-last-applied deployment <deployment_name>
I think you can choose here from two options.
Option 1:
You can grep all YAMLs looking for specific annotations or labels.
$ grep "app: nginx-test" *.yaml
or
$ grep -e "prometheus.io/scheme: http" *.yaml
When you find proper file you can edit it (vi, nano, etc) and apply.
$ kubectl apply -f [yaml-name]
Option 2:
When you know name of your deployment you can edit it.
$ kubectl edit deployment [deployment-name]
You will see current deployment YAML with a status: section which describing current status of deployment. If you don't like vi, you can use nano instead
$ KUBE_EDITOR="nano" kubectl edit [deployment-name]
If you want to create YAML from your current deployment I would advise you to use kubectl edit with --export flag. It will removes unneeded information (like status: from previous comment).
$ kubectl get deploy [your-deployment] -oyaml --export >> newDeployment.yaml
Hope it will help.

Get YAML for deployed Kubernetes services?

I am trying to deploy my app to Kubernetes running in Google Container
Engine.
The app can be found at: https://github.com/Industrial/docker-znc.
The Dockerfile is built into an image on Google Container Registry.
I have deployed the app in Kubernetes via the + button. I don't have the YAML
for this.
I have inserted a Secret in Kubernetes for the PEM file required by the app.
How do I get the YAML for the Deployment, Service and Pod created by
Kubernetes by filling in the form?
How do I get the Secret into my Pod for usage?
To get the yaml for a deployment (service, pod, secret, etc):
kubectl get deploy deploymentname -o yaml
How do I get the YAML for the Deployment, Service and Pod created by
Kubernetes by filling in the form?
kubectl get deployment,service,pod yourapp -o yaml --export
Answering #Sinaesthetic question:
any idea how to do it for the full cluster (all deployments)?
kubectl get deploy --all-namespaces -o yaml --export
The problem with this method is that export doesn't include the namespace. So if you want to export many resources at the same time, I recommend doing it per namespace:
kubectl get deploy,sts,svc,configmap,secret -n default -o yaml --export > default.yaml
Unfortunately kubernetes still doesn't support a true get all command, so you need to list manually the type of resources you want to export. You can get a list of resource types with
kubectl api-resources
The same issue is discussed at kubernetes GitHub issues page and the user "alahijani" made a bash script that exports all yaml and writes them to single files and folders.
Since this question ranks well on Google and since I found that solution very good, I represent it here.
Bash script exporting yaml to sub-folders:
for n in $(kubectl get -o=name pvc,configmap,serviceaccount,secret,ingress,service,deployment,statefulset,hpa,job,cronjob)
do
mkdir -p $(dirname $n)
kubectl get -o=yaml --export $n > $n.yaml
done
Another user "acondrat" made a script that do not use directories, which makes it easy to make a kubectl apply -f later.
Bash script exporting yaml to current folder:
for n in $(kubectl get -o=name pvc,configmap,ingress,service,secret,deployment,statefulset,hpa,job,cronjob | grep -v 'secret/default-token')
do
kubectl get -o=yaml --export $n > $(dirname $n)_$(basename $n).yaml
done
The last script does not include service account.
Now that --export is deprecated, to get the output from your resources in the 'original' format (just cleaned up, without any information about the current object state (unnecessary metadata in this circumstance)) you can do the following using yq v4.x:
kubectl get <resource> -n <namespace> <resource-name> -o yaml \
| yq eval 'del(.metadata.resourceVersion, .metadata.uid, .metadata.annotations, .metadata.creationTimestamp, .metadata.selfLink, .metadata.managedFields)' -
Syntax for downloading yaml's from kubernetes
kubectl get [resource type] -n [namespace] [resource Name] -o yaml > [New file name]
Create yaml file from running pod:
kubectl get po -n nginx nginx-deployment-755cfc7dcf-5s7j8 -o yaml > podDetail.yaml
Create replicaset yaml file from running pod:
kubectl get rs -n nginx -o yaml > latestReplicaSet.yaml
Create deployement yaml file from running pod:
kubectl get deploy -n nginx -o yaml > latestDeployement.yaml
Also its possible to use the view-last-applied command e.g.
kubectl apply view-last-applied services --all > services.yaml
which will return all the manifests applied to create services. Also you can specify a certain k8 resource by services/resource-name label.
If you need to get 'clean' export, removing the annotations added by Kubernetes, there's an opensource project that does that by piping the output of kubectl get - https://github.com/itaysk/kubectl-neat.
It removes the timestamp metadata, etc.
kubectl get pod mypod -o yaml | kubectl neat
kubectl get pod mypod -oyaml | kubectl neat -o json
Use this command to get yaml format of your service
kubectl get service servicename -n <namespace> -o yaml
You can put it in some file also
kubectl get service servicename -n <namespace> -o yaml > service.yaml
The following code will extract all your K8s definitions at once and place them on individual folders below the current folder.
for OBJ in $(kubectl api-resources --verbs=list --namespaced -o name)
do
for DEF in $(kubectl get --show-kind --ignore-not-found $OBJ -o name)
do
mkdir -p $(dirname $DEF)
kubectl get $DEF -o yaml \
| yq eval 'del(.metadata.resourceVersion, .metadata.uid, .metadata.annotations, .metadata.creationTimestamp, .metadata.selfLink, .metadata.managedFields)' - > $DEF.yaml
done
done
You can store output of deployed kubernetes service by using below command -
kubectl get svc -n -o yaml > svc-output.yaml
For deployments -
kubectl get deploy <deployment-name> -n <your-namespace> -o yaml > deploy-output.yaml
For Pod -
kubectl get pod <pod-name> -n <your-namespace> -o yaml > pod-output.yaml
You can get your secret details using below command -
kubectl get secret -n -o yaml
In order to use update your deployment file by using below command -
kubectl edit deploy -n
Under your pod template add below -
this will go under pod containers section to mount secret volume to container
volumeMounts:
- name: foo
mountPath: "/etc/foo"
readOnly: true
this will go inside your pod template section in deployment
volumes:
- name: foo
secret:
secretName: mysecret
for the 2nd question regarding the secret, this is from the k8s documentation. see https://kubernetes.io/docs/concepts/configuration/secret/#using-secrets for more info.
Create a secret or use an existing one. Multiple pods can reference the same secret.
Modify your Pod definition to add a volume under spec.volumes[]. Name the volume anything, and have a spec.volumes[].secret.secretName field equal to the name of the secret object.
Add a spec.containers[].volumeMounts[] to each container that needs the secret. Specify spec.containers[].volumeMounts[].readOnly = true and spec.containers[].volumeMounts[].mountPath to an unused directory name where you would like the secrets to appear.
Modify your image and/or command line so that the program looks for files in that directory. Each key in the secret data map becomes the filename under mountPath.
I have used this and it works fine.
Like mentioned above "--export" is one option to get the manifest corresponding to the kubeernetes objects
But "--export" is considered to be buggy and there is a proposal to deprecate it
Currently the better option is to do "-o yaml" or "-o json" and remove the unnecessary fields
The main difference is "--export" is expected to remove the cluster specific settings (e.g. cluster service IP of a k8s service). But it is found to be inconsistent in this regard
All services
kubectl get service --all-namespaces -o yaml > all-service.yaml
All deployments
kubectl get deploy --all-namespaces -o yaml > all-deployment.yaml
We can get the contents associated with any kind from a Kubernetes cluster through the command line if you have the read access.
kubectl get <kind> <kindname> -n <namespace> -o <yaml or json>
For example, if you want to export a deployment from a namespace follow the below command -
kubectl get deploy mydeploy -n mynamespace -o yaml > mydeploy.yaml
kubectl get deploy mydeploy -n mynamespace -o json > mydeploy.json
To get all yaml file deployments backup (not a specific deployment):
kubectl get deployments -n <namespace> -o yaml > deployments.yaml
for getting all yaml file services backup (not a specific deployment):
kubectl get services -n <namespace> -o yaml > services.yaml
enjoy it.
To get YAML for current running deployment on kubernetes, you can run this command:
kubectl get deployment <deployment_name> -o yaml
To generate YAML for deployment you can run the imperative command.
kubectl create deployment <deployment_name>--image=<image_name> -o yaml
To generate and export the deployment you can run the imperative command.
kubectl create deployment <deployment_name>--image=<image_name> --dry-run=client -o yaml > example.yaml
kubectl -n <namespace> get <resource type> <resource Name> -o yaml
With the command above, any resource defined in Kubernetes can be exported in YAML format.
You can try use kube-dump bash script
With this utility, you can save Kubernetes cluster resources as a pure yaml manifest without unnecessary metadata.
GitHub repository
Review of the utility in blog page
We can get yaml for deployed resources using below command.
kubectl get <resource name> -o yaml
OR
kubectl get <resource name> <name of pod> -o yaml
example:-
kubectl get deploy Nginx -o yaml
above commands will give you yaml output.
if you want to store the output into any file you can use below command.
kubectl get pod nginx -o yaml > Nginx-pod.yaml
above command will redirect you output to Nginx-pod.yaml in your courrent directory.
If you need to view and edit the file use:
kubectl edit service servicename
You can get the yaml files of the resources using this command
kubectl -n <namespace> get <resource type> <resource Name> -o yaml
To get the secret into your pod,
use something like this
env
- valueFrom
secretKeyRef:
name: secret_name
key: key_name
or
envFrom
- secretRef:
name: secret_name
Is only minor difference from #Janos Lenart's answer!
kubectl get deploy deploymentname -o yaml > outputFile.yaml will do
I know it is too old to answer, but hopefully, someone will find it helpful.
We can try below command to fetch a kind export from all namespace -
kubectl get <kind> --all-namespaces --export -o yaml