1. 获取kubernetes cluster master节点ip地址
kubectl get nodes --selector=node-role.kubernetes.io/control-plane="" -o=jsonpath='{.items[*].status.addresses[?(@.type=="InternalIP")].address}'

  1. 获取kubernetes cluster node节点ip地址
kubectl get nodes --selector=node-role.kubernetes.io/node="" -o=jsonpath='{.items[*].status.addresses[?(@.type=="InternalIP")].address}'
  1. 通过ping获取丢包率与平均延迟
# 执行ping命令并获取统计信息
ping_result=$(ping -c 5 "$target" | tail -2)
# 提取丢包率和平均延迟
packet_loss=$(echo "$ping_result" | grep -oP '\d+(?=% packet loss)')
avg_latency=$(echo "$ping_result" | grep -oP '(?<=rtt min/avg/max/mdev = )[\d.]+/[\d.]+/[\d.]+/[\d.]+' | awk -F '/' '{print $2}')
  1. cpu使用率
top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}'
  1. 内存使用率
free | awk 'FNR==2{printf "%.2f", $3/$2*100}'
  1. 随机选择一个ip地址
echo $IPs | tr ' ' '\n' | shuf -n 1
  1. 文件数量限制,清除多余文件
cleanup_old_files() {
  local DIR="$1"
  local MAX_FILES="$2"

  # 检查目录是否存在
  if [ ! -d "$DIR" ]; then
    echo "Directory $DIR does not exist."
    return 1
  fi

  # 获取所有文件,并按时间排序
  FILES=$(ls -1t "$DIR")

  COUNT=$(echo "$FILES" | wc -l)

  # 如果文件数量超过最大限制,则删除最旧的文件
  if [ $COUNT -gt $MAX_FILES ]; then
    # 获取最旧的文件
    OLD_FILES=$(echo "$FILES" | tail -n $((COUNT - MAX_FILES)))
    # 删除这些旧的文件
    for FILE in $OLD_FILES; do
      rm "$DIR/$FILE"
      echo "Deleted old file: $FILE"
    done
  fi
}
## 例如目录/opt/test中只保留5份:
cleanup_old_files /opt/test 5

例如需要通过ssh远端调用此函数

ssh -q -o StrictHostKeyChecking=no "root@10.20.30.40" "$(typeset -f cleanup_old_files); cleanup_old_files /opt/test 5"
  1. dd测试写速率
# dd输出如下
[root@abcdefg01 ~]# dd if=/dev/zero of=.tmp.test bs=1M count=100
记录了100+0 的读入
记录了100+0 的写出
104857600字节(105 MB,100 MiB)已复制,0.233213 s,450 MB/s
# 输出写速率
dd if=/dev/zero of=.tmp.test bs=1M count=100 2>&1 | awk 'NR==3{print $0}' | grep -oP '\d+(\.\d+)?\s+[A-Za-z]+/s'
# 不要单位
dd if=/dev/zero of=.tmp.test bs=1M count=100 2>&1 | awk 'NR==3{print $0}' | grep -oP '\d+(\.\d+)?(?=\s+[A-Za-z]+/s)'
  1. alertmanager告警接口,产生告警
curl -X POST http://172.20.41.32:30300/api/v2/alerts \
  -H "Content-Type: application/json" \
  -d '[
    {
      "labels": {
        "alertname": "系统连续崩溃,已经出现雪崩状况!",
        "dev": "sda1",
        "instance": "实例1",
        "msgtype": "testing"
      },
      "annotations": {
        "info": "程序员小王提示您:这个系统雪崩了,快处理!",
        "summary": "请检查实例示例1"
      }
    },
    {
      "labels": {
        "alertname": "管理系统损坏",
        "dev": "sda2",
        "instance": "实例1",
        "msgtype": "testing"
      },
      "annotations": {
        "info": "程序员小王提示您:电子商务管理系统中订单,仓库模块已经雪崩,快处理!",
        "summary": "请检查实例示例1",
        "runbook": "以下链接http://192.168.5.128:9093/api/v2/alerts应该是可点击的"
      }
    }
  ]'

  1. 集群中全部容器运行date命令检查时区(date命令不存在的情况下报错)
kubectl get pods -A -o 'jsonpath={range .items[*]}{.metadata.namespace}{"\t"}{.metadata.name}{"\t"}{range .spec.containers[*]}{.name}{"\t"}{end}{"\n"}{end}' | while IFS=$'\t' read -r namespace pod containers; do
    for container in $containers; do
        if [[ -z "$container" ]]; then
            echo -e "$namespace,$pod,No container found,-"
            continue
        fi
        output=$(kubectl exec -n "$namespace" "$pod" -c "$container" -- date 2>&1)
        echo -e "$namespace,$pod,$container,$output"
    done
done
  1. 去除重复行
awk '!a[$0]++' /path/to/file
  1. 拉取外网镜像上传到内网harbor,通过代理拉镜像,通过docker manifest做multi。
image=registry.k8s.io/kube-proxy:v1.29.10
set -e
local_repo=harbor.myharbor.com
arm_image=$local_repo/$image-arm64
amd_image=$local_repo/$image-amd64

sed -i '/^#Environment="HTTPS_PROXY=http:\/\/10\.42\.16\.241:7890"/s/^#//' /usr/lib/systemd/system/docker.service
sed -i '/^#Environment="HTTP_PROXY=http:\/\/10\.42\.16\.241:7890"/s/^#//' /usr/lib/systemd/system/docker.service
systemctl daemon-reload
systemctl restart docker
docker pull $image
docker tag $image $arm_image
docker rmi $image
docker pull --platform amd64 $image
docker tag $image $amd_image
docker rmi $image

sed -i '/^Environment="HTTPS_PROXY=http:\/\/10\.42\.16\.241:7890"/s/^/#/' /usr/lib/systemd/system/docker.service
sed -i '/^Environment="HTTP_PROXY=http:\/\/10\.42\.16\.241:7890"/s/^/#/' /usr/lib/systemd/system/docker.service
systemctl daemon-reload
systemctl restart docker

docker push $arm_image
docker push $amd_image
docker manifest create $local_repo/$image --amend $arm_image --amend $amd_image
docker manifest push $local_repo/$image
docker manifest rm $local_repo/$image