Kubernetes - Imperative Commands π§
Description π
Imperative commands can help in getting one time tasks done quickly, as well as generate a definition template easily.
Familiarize yourself with the following commands
--dry-run- will execute, and the resource will be created.
--dry-run=client- will execute, but the resource will not be created.
-o yaml- will output the resource definition in
yamlformat.
- will output the resource definition in
Basic Commands π
Use 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.
-
create a
poddefinitionkubectl run <pod-name> --image=<image-name> --dry-run=client -o yaml -
create a
deploymentdefinitionkubectl create deployment --image=<image-name> --dry-run=client -o yaml -
create a
servicedefinitionkubectl expose <object-type> <object-name> --port=<port> --name <service-name> --dry-run=client -o yaml
Examples π§©
-
create a
servicenamed nginx of typeNodePortto exposepodnginxβs port 80 on port 30080 on thenodeskubectl expose pod nginx --port=80 --name nginx-service --type=NodePort --dry-run=client -o yamlThis 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 thenode portin manually before creating the service with thepod. -
deploy a redis
podusing the redis:alpine image with the labels set to tier=db.kubectl run redis --image=redis:alpine --labels="tier=db" --dry-run=client -o yaml > redis-pod.yaml-
output
apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: tier: db name: redis spec: containers: - image: redis:alpine name: redis resources: {} dnsPolicy: ClusterFirst restartPolicy: Always status: {}
-
-
create a
serviceredis-service to expose the redis application within the cluster on port 6379kubectl expose pod redis --port=6379 --name=redis-service --type=ClusterIP --dry-run=client -o yaml > redis-service.yaml -
create a
deploymentnamed webapp using the image kodekloud/webapp-color with three replicaskubectl create deployment webapp --image=kodekloud/webapp-color --replicas=3 --dry-run=client -o yaml > webapp-dep.yaml -
create a
podusing nginx withcontainerport 8080 exposedkubectl run custom-nginx --image=nginx --port=8080 -
create a new
deploymentcalled redis-deploy in the dev-ns namespace with the redis image, it should have two replicaskubectl -n dev-ns create deployment redis-deploy --image=redis --replicas=2 --dry-run=client -o yaml > rd-dev.yaml