首页 > 其他分享 >kubernetes 使用jq命令对资源配置查看(yaml json文件解析工具)

kubernetes 使用jq命令对资源配置查看(yaml json文件解析工具)

时间:2023-02-15 11:11:43浏览次数:61  
标签:name kubernetes jq rabbitmq yaml json spec

有图形化的直接从图形化可以看到各种资源,如Deployment、Pod等资源的配置

安装

1.jq命令centos环境下安装

# yum -y install jq

2.https://github.com/stedolan/jq 下载
    http://stedolan.github.io/jq/
 https://github.com/stedolan/jq/wiki
   stedolan下有40多个项目

  https://codeload.github.com/kubernetes/k8s.io/zip/refs/heads/main
  项目里面有关于jq的管理,一共8个版本,
  E:\k8s源码子项目\k8s.io-main\k8s.io-main\images\k8s-infra\Dockerfile
 && echo "Installing jq ..." \
 && curl -fsSL "https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64" --output /usr/local/bin/jq \
 && chmod +x /usr/local/bin/jq \
//直接可使用了

 查看测试环境中的manifest文件, rabbitmq.yaml

[root@k2 src]# cat rabbitmq.yaml 
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
    kubectl.kubernetes.io/last-applied-configuration: |
      ......
  creationTimestamp: null
  generation: 1
  labels:
    app: rabbitmq
  name: rabbitmq
  selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/rabbitmq
spec:
  progressDeadlineSeconds: 600
  replicas: 2
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: rabbitmq
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: rabbitmq
    spec:
      containers:
      - env:
        - name: HOSTNAME
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: status.podIP
        - name: MY_POD_IP
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: status.podIP
        - name: AUTOCLUSTER_CLEANUP
          value: "true"
        - name: CLEANUP_INTERVAL
          value: "60"
        - name: CLEANUP_WARN_ONLY
          value: "false"
        - name: RABBITMQ_ERLANG_COOKIE
          valueFrom:
            secretKeyRef:
              key: erlang.cookie
              name: erlang.cookie
        image: 127.0.0.1:35000/test/rabbitmq:v1
        imagePullPolicy: IfNotPresent
        name: rabbitmq
        ports:
        - containerPort: 5672
          name: port-5672
          protocol: TCP
        - containerPort: 4369
          name: port-4369
          protocol: TCP
        - containerPort: 5671
          name: port-5671
          protocol: TCP
        - containerPort: 15672
          name: port-15672
          protocol: TCP
        - containerPort: 25672
          name: port-25672
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      nodeSelector:
        role: ht
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status: {}



我们想取上面文件中的红色部分内容(1.格式化为json, 2.jq操作)

//json格式化输出... 
kubectl get -f rabbitmq.yaml -o json
//对输出结果用jq来提取工作.
[root@k2 src]# kubectl get -f rabbitmq.yaml -o json | jq .spec.template.spec.containers[].env
[
  {
    "name": "HOSTNAME",
    "valueFrom": {
      "fieldRef": {
        "apiVersion": "v1",
        "fieldPath": "status.podIP"
      }
    }
  },
  {
    "name": "MY_POD_IP",
    "valueFrom": {
      "fieldRef": {
        "apiVersion": "v1",
        "fieldPath": "status.podIP"
      }
    }
  },
  {
    "name": "AUTOCLUSTER_CLEANUP",
    "value": "true"
  },
  {
    "name": "CLEANUP_INTERVAL",
    "value": "60"
  },
  {
    "name": "CLEANUP_WARN_ONLY",
    "value": "false"
  },
  {
    "name": "RABBITMQ_ERLANG_COOKIE",
    "valueFrom": {
      "secretKeyRef": {
        "key": "erlang.cookie",
        "name": "erlang.cookie"
      }
    }
  }
]

想取得image镜像的内容

[root@k2 src]# kubectl get -f rabbitmq.yaml -o json | jq .spec.template.spec.containers[].image
"127.0.0.1:35000/test/rabbitmq:v1"


只查看meta数据

[root@k2 src]# kubectl get -f rabbitmq.yaml -o json | jq .metadata
{
  "annotations": {
    "deployment.kubernetes.io/revision": "12",
    "field.cattle.io/publicEndpoints": "[{\"addresses\":[\"192.168.51.204\"],\"port\":49869,\"protocol\":\"TCP\",\"serviceName\":\"default:rabbitmq-management\",\"allNodes\":true}]"
  },
  "creationTimestamp": "2021-04-19T02:41:04Z",
  "generation": 25,
  "labels": {
    "app": "rabbitmq"
  },
  "name": "rabbitmq",
  "namespace": "default",
  "resourceVersion": "320640855",
  "selfLink": "/apis/extensions/v1beta1/namespaces/default/deployments/rabbitmq",
  "uid": "28901342-bf8a-11ec-84f7-063598000e9e"
}

//查看某个pod的运行情况

[root@k2 src]# kubectl get pod  -o template bz-node-0 --template={{.status.phase}}
Running

 

查看Pod的容器重启策略

[root@k2 src]# kubectl get -f rabbitmq.yaml -o json | jq .spec.template.spec.restartPolicy
"Always"
//如果你确认不了,yaml文件中层级关系,先输出json格式,然后在顶层就那么几个,.spec, .meta ,.kind

  [root@k2 src]# kubectl get -f rabbitmq.yaml -o json | jq .kind
  "Deployment"

  [root@k2 src]# kubectl get -f rabbitmq.yaml -o json | jq .apiVersion
  "extensions/v1beta1"

//注意分大小写的,不能是.apiVersion

标签:name,kubernetes,jq,rabbitmq,yaml,json,spec
From: https://www.cnblogs.com/aozhejin/p/16297564.html

相关文章