a few kubectl command examples

In this post, I will list a few examples for kubectl comman, which can be referenced in daily work.

1. create tls secret

kubectl create secret tls secret-name –cert=/path/to/cert –key=/path/to/key

2. get information from pod

get the ready status of the containers in the pod

kubectl get pods -o jsonpath=”{.status.containerStatuses[*][‘name’, ‘ready’]}” pod-name

get the ready status of the container with name ‘container-name’ in the pod

kubectl get pods -o jsonpath=”{.status.containerStatuses[?(@.name==’container-name’)][‘ready’]}” pod-name

get the image names of the containers in the pod

kubectl get pods -o jsonpath=”{.spec.containers[*].image}” pod-name

the the name and resources of the containers in the pod

kubectl get pods -o jsonpath=”{.spec.containers[*][‘name’, ‘resources.limits’, ‘resources.requests’]}” pod-name

These commands use json path to specify the path of the object in a json data. More information about json path can be found as below:

https://kubernetes.io/docs/reference/kubectl/jsonpath/
https://github.com/json-path/JsonPath
https://goessner.net/articles/JsonPath
https://support.smartbear.com/alertsite/docs/monitors/api/endpoint/jsonpath.html