Certification Tips - Imperative Commands With Kubectl
Certification Tips - Imperative Commands With Kubectl
Before we begin, familiarize with the two options that can come in handy while
working with the below commands:
--dry-run: By default as soon as the command is run, the resource will be created. If
you simply want to test your command , use the --dry-run=client option. This will
not create the resource, instead, tell you whether the resource can be created and if your
command is right.
-o yaml: This will output the resource definition in YAML format on screen.
Use the above two in combination to generate a resource definition file quickly, that you
can then modify and create resources as required, instead of creating the files from
scratch.
POD
Generate POD Manifest YAML file (-o yaml). Don’t create it(–dry-run)
Deployment
Create a deployment
You can also scale a deployment using the kubectl scale command.
Another way to do this is to save the YAML definition to a file and modify
You can then update the YAML file with the replicas or any other field before creating
the deployment.
Service
Create a Service named redis-service of type ClusterIP to expose pod redis on port
6379
Or
Create a Service named nginx of type NodePort to expose pod nginx’s port 80 on
port 30080 on the nodes:
kubectl expose pod nginx --type=NodePort --port=80 --name=nginx-
service --dry-run=client -o yaml
(This will automatically use the pod’s labels as selectors, but you cannot specify the
node port. You have to generate a definition file and then add the node port in manually
before creating the service with the pod.)
Or
Both the above commands have their own challenges. While one of it cannot accept a
selector the other cannot accept a node port. I would recommend going with the
`kubectl expose` command. If you need to specify a node port, generate a definition file
using the same command and manually input the nodeport before creating the service.
Reference:
https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands
https://kubernetes.io/docs/reference/kubectl/conventions/