k8s tips

–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.

example:

kubectl run nginx –image=nginx –dry-run=client -o yaml kubectl create deployment –image=nginx nginx –dry-run=client -o yaml

kubectl scale deployment nginx –replicas=4 kubectl create deployment nginx –image=nginx –dry-run=client -o yaml > nginx-deployment.yaml

Create a Service named redis-service of type ClusterIP to expose pod redis on port 6379

kubectl expose pod redis –port=6379 –name redis-service –dry-run=client -o yaml

kubectl create service clusterip redis –tcp=6379:6379 –dry-run=client -o yaml (This will not use the pods labels as selectors, instead it will assume selectors as app=redis. You cannot pass in selectors as an option. So it does not work very well if your pod has a different label set. So generate the file and modify the selectors before creating the service)

kubectl expose pod nginx –type=NodePort –port=80 –name=nginx-service –dry-run=client -o yaml

Create a pod called httpd using the image httpd:alpine in the default namespace. Next, create a service of type ClusterIP by the same name (httpd). The target port for the service should be 80. kubectl run httpd –image=httpd:alpine –port=80 –expose

Written on August 15, 2024