参考资料
目录
- [一. 简介](#一. 简介)
- [二. 安装](#二. 安装)
- [三. 常用](#三. 常用)
-
- [3.0 文件准备](#3.0 文件准备)
- [3.1 查询](#3.1 查询)
-
- [3.1.1 普通查询](#3.1.1 普通查询)
- [3.1.2 过滤查询](#3.1.2 过滤查询)
- [3.2 更新](#3.2 更新)
- [3.3 删除](#3.3 删除)
- [3.4 转换](#3.4 转换)
-
- [3.4.1 yaml 转换 json](#3.4.1 yaml 转换 json)
- [3.4.2 json 转换 yaml](#3.4.2 json 转换 yaml)
- [3.5 统计](#3.5 统计)
- [3.6 判断](#3.6 判断)
- [四. 案例](#四. 案例)
-
- [4.1 案例1](#4.1 案例1)
-
- [4.1.1 文件准备](#4.1.1 文件准备)
- [4.1.2 查看所有的服务名](#4.1.2 查看所有的服务名)
- [4.1.3 查看某个服务的镜像](#4.1.3 查看某个服务的镜像)
- [4.1.4 批量查看所有服务的 image](#4.1.4 批量查看所有服务的 image)
- [4.2 案例2](#4.2 案例2)
-
- [4.2.1 文件准备](#4.2.1 文件准备)
- [4.2.2 查看所有 `kind` 字段](#4.2.2 查看所有
kind字段) - [4.2.3 只修改 Deployment,不碰 Service](#4.2.3 只修改 Deployment,不碰 Service)
一. 简介
🔷在Linux中,处理Json数据的话,会用到jq命令。与此相对的是,处理Yaml文件的话,会用到yq命令。
jq 和 yq 的使用方法类似,基本上掌握了jq之后,yq也差不多就掌握了。
| 工具 | 处理格式 | 核心用途 |
|---|---|---|
jq |
JSON | 查询 / 修改 JSON |
yq |
YAML | 查询 / 修改 YAML(也能转 JSON) |
🔷在Kubernetes中,有很多Yaml的配置文件,使用yq命令,可以更加方便的对其进行查询,编辑与修改。
二. 安装
🔷Ubuntu安装
bash
# 切换为root用户
wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O yq
chmod +x yq
mv yq /usr/local/bin/
🔷验证安装效果
bash
apluser@FengYeHong-HP:~$ yq --version
yq (https://github.com/mikefarah/yq/) version v4.52.2
💥注意事项
yq命令不是 Ubuntu 官方稳定包,无法通过apt install yq进行安装- 早年有个
Python版的yq,现在基本没人用了,目前主流的是使用Go写的mikefarah/yq(v4)
三. 常用
3.0 文件准备
🔷config.yaml
bash
server:
host: localhost
port: 8080
users:
- name: Tom
age: 18
- name: Jack
age: 20
3.1 查询
3.1.1 普通查询
🔷普通查询
bash
apluser@FengYeHong-HP:20260208$ yq '.server.host' config.yaml
localhost
apluser@FengYeHong-HP:20260208$ yq '.server.port' config.yaml
8080
apluser@FengYeHong-HP:20260208$
🔷数组查询
bash
apluser@FengYeHong-HP:20260208$ yq '.users' config.yaml
- name: Tom
age: 18
- name: Jack
age: 20
apluser@FengYeHong-HP:20260208$ yq '.users[0]' config.yaml
name: Tom
age: 18
apluser@FengYeHong-HP:20260208$
apluser@FengYeHong-HP:20260208$ yq '.users[0].name' config.yaml
Tom
3.1.2 过滤查询
🔷age > 18 的人
bash
apluser@FengYeHong-HP:20260208$ yq '.users[] | select(.age > 18)' config.yaml
name: Jack
age: 20
🔷name为Tom的人
bash
apluser@FengYeHong-HP:20260208$ yq '.users[] | select(.name == "Tom")' config.yaml
name: Tom
age: 18
🔷name为Tom的人的年龄
--unwrapScalar可以保证获取到的数据没有yaml样式
bash
apluser@FengYeHong-HP:20260208$ yq '.users[] | select(.name == "Tom") | .age' --unwrapScalar config.yaml
18
🔷格式化输出
bash
apluser@FengYeHong-HP:20260208$ yq '.users[] | select(.name == "Tom") | "\(.name),\(.age)"' config.yaml
Tom,18
3.2 更新
🔷修改字段(不覆盖文件)
bash
apluser@FengYeHong-HP:20260208$ yq '.server.port = 9090' config.yaml
server:
host: localhost
port: 9090
users:
- name: Tom
age: 18
- name: Jack
age: 20
🔷-i直接修改原文件
bash
apluser@FengYeHong-HP:20260208$ yq -i '.server.port = 9090' config.yaml
apluser@FengYeHong-HP:20260208$
apluser@FengYeHong-HP:20260208$ cat config.yaml
server:
host: localhost
port: 9090
users:
- name: Tom
age: 18
- name: Jack
age: 20
apluser@FengYeHong-HP:20260208$
🔷直接新增字段
bash
apluser@FengYeHong-HP:20260208$ yq -i '.server.debug = true' config.yaml
apluser@FengYeHong-HP:20260208$
apluser@FengYeHong-HP:20260208$ cat config.yaml
server:
host: localhost
port: 9090
debug: true
users:
- name: Tom
age: 18
- name: Jack
age: 20
3.3 删除
bash
apluser@FengYeHong-HP:20260208$ yq -i 'del(.server.debug)' config.yaml
apluser@FengYeHong-HP:20260208$
apluser@FengYeHong-HP:20260208$ cat config.yaml
server:
host: localhost
port: 9090
users:
- name: Tom
age: 18
- name: Jack
age: 20
3.4 转换
3.4.1 yaml 转换 json
bash
apluser@FengYeHong-HP:20260208$ yq -o=json '.' config.yaml
{
"server": {
"host": "localhost",
"port": 9090
},
"users": [
{
"name": "Tom",
"age": 18
},
{
"name": "Jack",
"age": 20
}
]
}
3.4.2 json 转换 yaml
bash
apluser@FengYeHong-HP:20260208$ cat config.json
{
"server": {
"host": "localhost",
"port": 9090
},
"users": [
{
"name": "Tom",
"age": 18
},
{
"name": "Jack",
"age": 20
}
]
}
apluser@FengYeHong-HP:20260208$ yq -o=yaml '.' config.json
server:
host: localhost
port: 9090
users:
- name: Tom
age: 18
- name: Jack
age: 20
3.5 统计
🔷获取数组长度
bash
apluser@FengYeHong-HP:20260208$ yq '.users | length' config.yaml
2
3.6 判断
🔷判断指定的字段是否存在
bash
apluser@FengYeHong-HP:20260208$ yq 'has("server")' config.yaml
true
四. 案例
4.1 案例1
4.1.1 文件准备
🔷docker-compose.yml
bash
version: "3.9"
services:
web:
image: nginx:1.25
ports:
- "8080:80"
environment:
- ENV=prod
- DEBUG=false
api:
image: myapi:1.0
ports:
- "9000:9000"
environment:
ENV: dev
LOG_LEVEL: info
4.1.2 查看所有的服务名
bash
apluser@FengYeHong-HP:20260208$ yq '.services | keys' docker-compose.yml
- web
- api
4.1.3 查看某个服务的镜像
bash
apluser@FengYeHong-HP:20260208$ yq '.services.web.image' docker-compose.yml
nginx:1.25
4.1.4 批量查看所有服务的 image
bash
apluser@FengYeHong-HP:20260208$ yq '.services[].image' docker-compose.yml
nginx:1.25
myapi:1.0
bash
apluser@FengYeHong-HP:20260208$ yq '.services | to_entries[] | "\(.key): \(.value.image)"' docker-compose.yml
web: nginx:1.25
api: myapi:1.0
4.2 案例2
4.2.1 文件准备
🔷多文档 YAML → all.yaml
bash
---
apiVersion: v1
kind: Service
metadata:
name: myapp
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
4.2.2 查看所有 kind 字段
bash
apluser@FengYeHong-HP:20260208$ yq 'select(.kind != null) | .kind' all.yaml
Service
---
Deployment
4.2.3 只修改 Deployment,不碰 Service
bash
apluser@FengYeHong-HP:20260208$ cat all.yaml
---
apiVersion: v1
kind: Service
metadata:
name: myapp
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
apluser@FengYeHong-HP:20260208$ yq 'select(.kind == "Deployment") | .metadata.name = "myapp_v1"' all.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp_v1