在 k8s 中导出(kubectl get xx -o yaml)资源描述信息时,会带出一些k8s系统添加的信息,但这些都不是我们需要的信息,官方没有提供过滤的选项,在下面我给出了几种方式来处理这种情况。

使用 yq 项目过滤 yaml 信息

1.使用 yq 项目过滤 yaml 信息

输出 yaml

wget https://gh.con.sh/https://github.com/mikefarah/yq/releases/download/3.4.1/yq_linux_amd64 -O /usr/local/sbin/yq
chmod +x /usr/local/sbin/yq

kubectl_yaml() {
  kubectl -o yaml "$@" \
    | yq d - 'items[*].metadata.managedFields' \
    | yq d - 'metadata.managedFields' \
    | yq d - 'items[*].metadata.ownerReferences' \
    | yq d - 'metadata.ownerReferences' \
    | yq d - 'items[*].metadata.annotations."kubectl.kubernetes.io/last-applied-configuration"' \
    | yq d - 'metadata.annotations."kubectl.kubernetes.io/last-applied-configuration"' \
    | yq d - 'items[*].metadata.creationTimestamp' \
    | yq d - 'metadata.creationTimestamp' \
    | yq d - 'items[*].metadata.resourceVersion' \
    | yq d - 'metadata.resourceVersion' \
    | yq d - 'items[*].metadata.generateName' \
    | yq d - 'metadata.generateName' \
    | yq d - 'items[*].metadata.selfLink' \
    | yq d - 'metadata.selfLink' \
    | yq d - 'items[*].metadata.uid' \
    | yq d - 'metadata.uid' \
    | yq d - 'items[*].status' \
    | yq d - 'status'
}

输出 json

wget https://gh.con.sh/https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 -O /usr/local/sbin/jq
chmod +x /usr/local/sbin/jq

kubectl_json() {
  kubectl -o json "$@" \
    | jq 'del(.items[]?.metadata.managedFields)' \
    | jq 'del(.metadata.managedFields)' \
    | jq 'del(.items[]?.metadata.ownerReferences)' \
    | jq 'del(.metadata.ownerReferences)' \
    | jq 'del(.items[]?.metadata.annotations."kubectl.kubernetes.io/last-applied-configuration")' \
    | jq 'del(.metadata.annotations."kubectl.kubernetes.io/last-applied-configuration")' \
    | jq 'del(.items[]?.metadata.creationTimestamp)' \
    | jq 'del(.metadata.creationTimestamp)' \
    | jq 'del(.items[]?.metadata.resourceVersion)' \
    | jq 'del(.metadata.resourceVersion)' \
    | jq 'del(.items[]?.metadata.generateName)' \
    | jq 'del(.metadata.generateName)' \
    | jq 'del(.items[]?.metadata.selfLink)' \
    | jq 'del(.metadata.selfLink)' \
    | jq 'del(.items[]?.metadata.uid)' \
    | jq 'del(.metadata.uid)' \
    | jq 'del(.items[]?.status)' \
    | jq 'del(.status)'
}

2.使用插件kubectl-neat 清理Kuberntes yaml和json输出,使其具有可读性.

wget https://gh.con.sh/https://github.com/itaysk/kubectl-neat/releases/download/v2.0.1/kubectl-neat_linux.tar.gz
tar zxf kubectl-neat_linux.tar.gz  -C /usr/local/sbin/
chmod +x /usr/local/sbin/kubectl-neat
#常用命令
kubectl get pod mypod -o yaml | kubectl neat
kubectl get pod mypod -oyaml | kubectl neat -o json
kubectl neat -f - <./my-pod.json
kubectl neat -f ./my-pod.json
kubectl neat -f ./my-pod.json --output yaml
kubectl neat get -- pod mypod -oyaml
kubectl neat get -- svc -n default myservice --output json

3.使用 sed 使用sed时,需时刻注意匹配关键字

# kubectl get svc ingress-demo-app -o yaml \
  | sed -n '/ managedFields:/{p; :a; N; / name: ingress-demo-app/!ba; s/.*\n//}; p' \
  | sed -e 's/ uid:.*//g' \
        -e 's/ resourceVersion:.*//g' \
        -e 's/ selfLink:.*//g' \
        -e 's/ creationTimestamp:.*//g' \
        -e 's/ managedFields:.*//g' \
        -e 's/ generateName:.*//g' \
        -e '/^\s*$/d'
apiVersion: v1
kind: Service
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"ingress-demo-app","namespace":"default"},"spec":{"ports":[{"name":"http","port":80,"targetPort":80}],"selector":{"app":"ingress-demo-app"},"type":"ClusterIP"}}
  name: ingress-demo-app
  namespace: default
spec:
  clusterIP: 10.96.44.53
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: ingress-demo-app
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}