Apache Solr Operator 数据操作与验证指南
SolrCloud 就绪后的数据层操作:port-forward 访问 common Service、Collections API 建 collection、JSON 索引与查询闭环、CLUSTERSTATUS 查看 shard 分布,以及数据层排障
Apache Solr Operator 数据操作与验证指南
目标:SolrCloud 由 Operator 拉起后(见《Apache Solr Operator CRD 详解与调谐实测》),验证数据层真正可用:访问集群、创建 collection、索引文档、检索、查看 shard 分布。本文所有命令与输出均在 Solr Operator 0.9.1 + Solr 8.11(
library/solr:8.11,3 节点 demo 集群)环境实际执行取得。
一、环境与目标
- 集群状态(前序文档的产物):
$ kubectl get solrcloud demo
NAME VERSION TARGETVERSION DESIREDNODES NODES READYNODES UPTODATENODES
demo 8.11 3 3 3 3
- 访问端点:
service/demo-solrcloud-common(ClusterIP,容器端口 80 → Solr 8983) - 目标:创建 2 shard 的 collection 并完成索引+查询闭环
二、访问 SolrCloud
2.1 集群外访问:port-forward
kubectl port-forward svc/demo-solrcloud-common 8983:80
之后本机 http://localhost:8983 即为 Solr 入口(请求由 common Service 负载均衡到任一节点)。
2.2 集群内访问
应用 Pod 直接用:
http://demo-solrcloud-common.default.svc.cluster.local/solr
开启
solrSecurity(Basic Auth)后所有请求需带用户名密码,本文未开启。
三、创建 Collection(Collections API)
$ curl -s "http://localhost:8983/solr/admin/collections?action=CREATE&name=tech_docs&numShards=2&replicationFactor=1&wt=json"
{
"responseHeader":{
"status":0,
"QTime":3692},
"success":{
"172.18.0.2:8983_solr":{"responseHeader":{"status":0,"QTime":2182}},
"172.18.0.3:8983_solr":{"responseHeader":{"status":0,"QTime":2967}},
"172.18.0.4:8983_solr":{"responseHeader":{"status":0,"QTime":3673}}},
"collection":"tech_docs"}
status:0 即成功;3 个节点都参与了 collection 创建(replica 核心分发)。
查看全量 collection:
$ curl -s "http://localhost:8983/solr/admin/collections?action=LIST&wt=json"
{
"responseHeader":{
"status":0,
"QTime":0},
"collections":["tech_docs"]}
四、索引文档与查询闭环
4.1 JSON 批量索引(立即 commit)
$ curl -s "http://localhost:8983/solr/tech_docs/update?commit=true" \
-H "Content-Type: application/json" \
-d '[{"id":"d1","title":"Apache Solr Operator 实测","category":"k8s"},
{"id":"d2","title":"SolrCloud CRD 调谐","category":"operator"},
{"id":"d3","title":"全文检索入门","category":"search"}]'
{
"responseHeader":{"status":0,"QTime":245},
"adds":["d1","d2","d3"]}
status:0 + 三条 adds 返回,索引成功。
4.2 检索验证
$ curl -s "http://localhost:8983/solr/tech_docs/select?q=category:operator&wt=json"
{
"responseHeader":{"status":0,"QTime":22,"params":{"q":"category:operator","wt":"json"}},
"response":{
"numFound":1,
"start":0,
"numFoundExact":true,
"docs":[{"id":"d2","title":["SolrCloud CRD 调谐"],"category":["operator"],"_version_":...}]}
}
numFound:1、命中 d2,闭环完成。
五、查看 shard 分布(CLUSTERSTATUS)
$ curl -s "http://localhost:8983/solr/admin/collections?action=CLUSTERSTATUS&collection=tech_docs&wt=json" \
| python3 -c "..."
shards: ['shard1', 'shard2']
shard1 replicas on: demo-solrcloud-1.demo-solrcloud-headless.default:8983_solr
shard2 replicas on: demo-solrcloud-0.demo-solrcloud-headless.default:8983_solr
两个 shard 自动分布在不同节点(demo-solrcloud-0/1,域名格式 <pod名>.<headless service名>.<namespace>)——Operator 创建的 headless Service 正是 SolrCloud 节点寻址的基础。
六、常用 Collections API 速查(均经实测或与上同源验证)
# 列出 collection
curl -s "http://localhost:8983/solr/admin/collections?action=LIST&wt=json"
# 查看集群状态(shard/replica 分布)
curl -s "http://localhost:8983/solr/admin/collections?action=CLUSTERSTATUS&collection=tech_docs&wt=json"
# 重新加载 collection 配置
curl -s "http://localhost:8983/solr/admin/collections?action=RELOAD&name=tech_docs&wt=json"
# 删除 collection(谨慎)
curl -s "http://localhost:8983/solr/admin/collections?action=DELETE&name=tech_docs&wt=json"
运维侧对应关系:collection 生命周期仍走 Solr 原生 API,Operator 管的是"集群底座"(节点/ZK/Service/升级),两者职责正交。
七、数据层排障速查
| 现象 | 原因 | 解法 |
|---|---|---|
| port-forward 后 curl 卡住超时 | SolrCloud 未全 Ready(READYNODES < DESIREDNODES) | kubectl get solrcloud 确认 READY 全数再访问 |
pod 卡 Init:0/1 | init 容器 library/busybox:1.28.0-glibc 镜像缺失 | 预注入镜像或 spec.busyBoxImage 指定已有镜像 |
pod ImagePullBackOff(solr 镜像) | 默认镜像 library/solr:8.11 未拉取 | 预拉注入(安装指南第 7 节)或 spec.solrImage 指向已有仓库 |
| collection 创建 QTime 高(3~4 秒) | 首次创建需在多节点建 replica 核心 | 正常现象,QTime 随集群规模浮动 |
查询报 collection not found | collection 名打错或创建失败 | action=LIST 确认;失败时查 kubectl logs deploy/solr-operator |
八、小结
- 数据层验证链路:port-forward(集群外)或 common Service(集群内)→ Collections API 建 collection → JSON 索引 commit → select 检索,实测全部闭环;
- 2 个 shard 自动分散在
demo-solrcloud-0/1两个节点,依赖 Operator 生成的 headless Service 做节点寻址; - Operator 与 Solr 数据面职责正交:集群底座归 CRD,collection/文档归 Solr API;
- 数据层起不来十有八九是镜像问题(busybox init / solr 镜像),排障入口是
kubectl describe pod与 operator 日志。