SoFunction
Updated on 2025-03-10

kubernetes Use jq command to view resource configuration

Use jq command to view resource configuration

If you have graphical, you can directly see various resources, such as Deployment, Pod and other resources.

Write a jq command here

The jq command allows operations against json, such as filtering

jq command to install in centos environment

# yum -y install jq

Suppose we have a file

# cat pod-yaml
 
{
    "apiVersion": "v1",
    "kind": "Pod",
    "metadata": {
        "name": "nginx-pod",
        "namespace": "default"
    },
    "spec": {
        "containers": [
            {
                "image": "nginx:1.20",
                "imagePullPolicy": "IfNotPresent",
                "name": "nginx-pod",
                "resources": {
                    "limits": {
                        "cpu": "20m",
                        "memory": "120Mi"
                    },
                    "requests": {
                        "cpu": "10m",
                        "memory": "100Mi"
                    }
                }
            }
        ]
    }
}

We'll take out the following paragraph directly

  "limits": {
    "cpu": "20m",
    "memory": "120Mi"
  },
  "requests": {
    "cpu": "10m",
    "memory": "100Mi"
  }

This can be performed

# cat pod-yaml | jq .[].resources
 
Output:
{
  "limits": {
    "cpu": "20m",
    "memory": "120Mi"
  },
  "requests": {
    "cpu": "10m",
    "memory": "100Mi"
  }
}

The rules are easy to see, which is to take the value of the key

Similarly, this is also possible when viewing resources

For example, see a pod resource limit:

Checkpodname
# kubectl get pod
NAME        READY   STATUS    RESTARTS   AGE
nginx-pod   1/1     Running   0          22s
 
 
Check该podResource limitations
# kubectl get pod/nginx-pod -o json | jq .[].resources
{
  "limits": {
    "cpu": "20m",
    "memory": "120Mi"
  },
  "requests": {
    "cpu": "10m",
    "memory": "100Mi"
  }
}
 
 
Check该Podcontainer restart strategy
# kubectl get pod/nginx-pod -o json | jq .
"Always"

Summary of common commands for kubernetes

Common commands for k8s

Commonly used kubectl commands

Create a resource object

kubectl create -f (document)、kubectl create -f <directory>(目录下所有document)

View resource objects

kubectl get nodes
kubectl get pods -n <namespace> -o wide

Describe the resource object

kubectl describe nodes <node-name>
kubectl describe pods -n <namespace> kubectl describe <pod-name>
kubectl describe pods <rc-name>

Delete resource objects

kubectl delete -f &lt;filename&gt;
kubectl delete pods,services -l name=&lt;label-name&gt;
kubectl delete pods --all(Use it with caution in the production environment)

Execute the container command

kubectl exec &lt;pod-name&gt; date(The first container is executed by defaultPodofdateOrder)
kubectl exec &lt;pod-name&gt; -c &lt;container-name&gt; date(SpecifyPod中of某个容器执行dateOrder)
kubectl exec -it &lt;pod-name&gt; -c &lt;container-name&gt; /bin/bash (Quite similar todocker exec -it &lt;container-name&gt; /bin/bash)

View container logs

kubectl logs &lt;pod-name&gt;
kubectl logs -f &lt;pod-name&gt; -c &lt;container-name&gt; (Equivalent totail -f Order)

kubectl format output

Show more information about Pods

kubectl get pods -n <namespace> -o wide

Displayed in yaml format

kubectl get pods -n <namespace> -o yaml

Show Pod information in a custom list

kubectl get pod <pod-name> -o =custom-columns=NAME:.,RSRC:.

File-based custom column name output

kubectl get pods <pod-name> -o=custom-columns-file=

Sorting output results

kubectl get pods --sort-by=.

kubernetes cluster management guide

Node management

Order:

kubectl replace -f 
kubectl patch
kubectl cordon &lt;node_name&gt; kubectl uncordon &lt;node_name&gt;(rightnodeIsolation and recovery of nodes)

Delete nodes:

kubectl drain swarm1 --delete-local-data --force --ignore-daemonsets
kubectl delete node swarm1

use:

kubectl get nodes
kubectl cordon <node_name> kubectl uncordon <node_name>

Label management:

Set tags for node

  • Added: kubectl label node lustre-manager-1 /minion-1=
  • Delete: kubectl label node lustre-manager-1 /minion-1-
  • Modify: kubectl label node lustre-manager-1 /minion-1= --overwrite

Set tags for pods

Change node to pod

Other commands:

Join the node master:

kubeadm join 192.168.138.131:6443 --token zlk694.ev3odwj7rbyaggz6 --discovery-token-ca-cert-hash 

sha256:eefe51ccf1c54149f5ce89423c100b1e0de8f8081c7c2c0e07a7613ef2025146

Generate the command to join the master: kubeadm token create --print-join-command

Delete node node: 1) kubectl drain swarm1 --delete-local-data --force --ignore-daemonsets 2) kubectl delete node swarm1

The above is personal experience. I hope you can give you a reference and I hope you can support me more.