Database

FAQ

Redis 连接报 ‘Connection refused’

排查步骤

  1. 确认 Redis pod 在运行:

    kubectl -n storage get pods -l app.kubernetes.io/instance=redis-shared
  2. 确认 Service 端点正常:

    kubectl -n storage get endpoints redis-shared-master
  3. 从客户端 pod 内测试连通:

    kubectl exec -it <pod> -- redis-cli -h redis-shared-master.storage -a <password> ping
  4. 如果外部应用连不上,确认使用的是正确的 namespace:

    redis-shared-master.storage.svc.cluster.local:6379
  5. 如果密码不对,从 secret 获取:

    kubectl -n storage get secret redis-shared-credentials -o jsonpath='{.data.redis-password}' | base64 -d
PostgreSQL 连接报 ‘password authentication failed’
  1. 确认密码 secret 存在:

    kubectl -n database get secret <app>-postgres-password -o jsonpath='{.data.password}' | base64 -d
  2. 如果密码改了,需要更新引用它的所有应用的 env。

  3. PostgreSQL 默认只允许集群内访问。外部访问需要通过 NodePort 或 port-forward:

    kubectl -n database port-forward svc/postgresql 5432:5432
Redis 如何清空所有数据
kubectl -n storage exec redis-shared-master-0 -- redis-cli -a <password> FLUSHALL
kubectl -n storage exec redis-shared-master-0 -- redis-cli -a <password> FLUSHDB

⚠️ FLUSHALL 默认已被禁用(disableCommands: [FLUSHDB, FLUSHALL])。如需使用,临时移除 disableCommands 后重建 Redis。

查看 Redis 内存使用和 key 数量
kubectl -n storage exec redis-shared-master-0 -- redis-cli -a <password> INFO memory
kubectl -n storage exec redis-shared-master-0 -- redis-cli -a <password> DBSIZE
kubectl -n storage exec redis-shared-master-0 -- redis-cli -a <password> INFO stats
PostgreSQL 数据库备份和恢复
# 备份
kubectl -n database exec deployment/postgresql -- \
  pg_dump -U postgres <database> > backup.sql

# 恢复
cat backup.sql | kubectl -n database exec -i deployment/postgresql -- \
  psql -U postgres <database>
Mar 7, 2024

Subsections of Database

Elastic Search DSL

Basic Query

exist query

Returns documents that contain an indexed value for a field.

GET /_search
{
  "query": {
    "exists": {
      "field": "user"
    }
  }
}

The following search returns documents that are missing an indexed value for the user.id field.

GET /_search
{
  "query": {
    "bool": {
      "must_not": {
        "exists": {
          "field": "user.id"
        }
      }
    }
  }
}
fuzz query

Returns documents that contain terms similar to the search term, as measured by a Levenshtein edit distance.

GET /_search
{
  "query": {
    "fuzzy": {
      "filed_A": {
        "value": "ki"
      }
    }
  }
}

Returns documents that contain terms similar to the search term, as measured by a Levenshtein edit distance.

GET /_search
{
  "query": {
    "fuzzy": {
      "filed_A": {
        "value": "ki",
        "fuzziness": "AUTO",
        "max_expansions": 50,
        "prefix_length": 0,
        "transpositions": true,
        "rewrite": "constant_score_blended"
      }
    }
  }
}

rewrite:

  • constant_score_boolean
  • constant_score_filter
  • top_terms_blended_freqs_N
  • top_terms_boost_N, top_terms_N
  • frequent_terms, score_delegating
ids query

Returns documents based on their IDs. This query uses document IDs stored in the _id field.

GET /_search
{
  "query": {
    "ids" : {
      "values" : ["2NTC5ZIBNLuBWC6V5_0Y"]
    }
  }
}
prefix query

The following search returns documents where the filed_A field contains a term that begins with ki.

GET /_search
{
  "query": {
    "prefix": {
      "filed_A": {
        "value": "ki",
         "rewrite": "constant_score_blended",
         "case_insensitive": true
      }
    }
  }
}

You can simplify the prefix query syntax by combining the <field> and value parameters.

GET /_search
{
  "query": {
    "prefix" : { "filed_A" : "ki" }
  }
}
range query

Returns documents that contain terms within a provided range.

GET /_search
{
  "query": {
    "range": {
      "filed_number": {
        "gte": 10,
        "lte": 20,
        "boost": 2.0
      }
    }
  }
}
GET /_search
{
  "query": {
    "range": {
      "filed_timestamp": {
        "time_zone": "+01:00",        
        "gte": "2020-01-01T00:00:00", 
        "lte": "now"                  
      }
    }
  }
}
regex query

Returns documents that contain terms matching a regular expression.

GET /_search
{
  "query": {
    "regexp": {
      "filed_A": {
        "value": "k.*y",
        "flags": "ALL",
        "case_insensitive": true,
        "max_determinized_states": 10000,
        "rewrite": "constant_score_blended"
      }
    }
  }
}
term query

Returns documents that contain an exact term in a provided field.

You can use the term query to find documents based on a precise value such as a price, a product ID, or a username.

GET /_search
{
  "query": {
    "term": {
      "filed_A": {
        "value": "kimchy",
        "boost": 1.0
      }
    }
  }
}
wildcard query

Returns documents that contain terms matching a wildcard pattern.

A wildcard operator is a placeholder that matches one or more characters. For example, the * wildcard operator matches zero or more characters. You can combine wildcard operators with other characters to create a wildcard pattern.

GET /_search
{
  "query": {
    "wildcard": {
      "filed_A": {
        "value": "ki*y",
        "boost": 1.0,
        "rewrite": "constant_score_blended"
      }
    }
  }
}