学习
如何设置 Pod 到指定节点运行
· ☕ 4 分钟
1. 创建负载时,通过 nodeSelector 指定 Node 给节点添加标签 1 kubectl label node node2 project=A 指定 nodeSelector 创建工作负载 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 cat <<EOF | kubectl apply -f - apiVersion: apps/v1 kind: Deployment metadata: name: nginx-nodeselector spec: replicas: 1 selector: matchLabels: app: nginx-nodeselector template: metadata: labels: app: nginx-nodeselector spec: nodeSelector: project: A containers: - name: nginx image: nginx EOF 查看工作负载 1 2 3 4 kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES

使用 Blackbox Exporter 测试网络连通性
· ☕ 3 分钟
如果你需要监控两个主机、主机与外部服务之间的网络状况,那么就可以试一试本文提到的 Blackbox Exporter。 1. 安装 Blackbox 1.1 在主机上部署 下载二进制包 1 2 3 4 5 wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.21.0/blackbox_exporter-0.21.0.linux-amd64.tar.gz tar -xzvf blackbox_exporter-0.21.0.linux-amd64.tar.gz mv blackbox_exporter-0.21.0.linux-amd64/blackbox_exporter /usr/bin/ mkdir /etc/prometheus mv blackbox_exporter-0.21.0.linux-amd64/blackbox.yml /etc/prometheus/ 清理安装包 1 rm -rf blackbox_exporter-0.21.0.linux-amd64* 新建 Systemd 服务 1 vim /usr/lib/systemd/system/blackbox_exporter.service 新增如下内容: [Unit] Description=blackbox_exporter After=network.target [Service] Restart=on-failure ExecStart=/usr/bin/blackbox_exporter \ --config.file=/etc/prometheus/blackbox.yml Restart=on-failure [Install] WantedBy=multi-user.target

如何采集 Kubernetes 对象的 labels 和 annotations
· ☕ 2 分钟
1. 为什么需要 kube-status-metrics Kubernetes 的监控主要关注两类指标: 基础性能指标 CPU、内存、磁盘、网络等指标,可以通过 DaemonSet 部署 node-exporter,由 Prometheus 抓取相关指标。 资源对象指标 Deployment 的副本数量、Pod 的运行状态等。这些指标需要 kube-status-metrics 轮询 Kubernetes 的 API 查询,并暴露给 Prometheus 才能够看到

使用 systemd timer 配置定时任务
· ☕ 2 分钟
1. 准备定时脚步 如果是 Bash 脚本,第一行需要指定解释器。 1 2 mkdir -p /root/scripts vim /root/scripts/quick-clear.sh 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #!/bin/sh /usr/local/bin/kubectl --kubeconfig /root/.kube/config get pods --all-namespaces -o wide | grep Evicted | awk '{print $1,$2}' | xargs --no-run-if-empty -L1 /usr/local/bin/kubectl --kubeconfig /root/.kube/config delete pod -n || true /usr/local/bin/kubectl --kubeconfig /root/.kube/config get pods --all-namespaces -o wide | grep Error | awk '{print $1,$2}' | xargs --no-run-if-empty -L1 /usr/local/bin/kubectl --kubeconfig /root/.kube/config delete pod -n || true /usr/local/bin/kubectl --kubeconfig /root/.kube/config get pods --all-namespaces -o wide | grep Completed | awk '{print $1,$2}' | xargs --no-run-if-empty

Thanos 进阶使用指南
· ☕ 4 分钟
1. 使用 Query 聚合数据 如上图,Thanos Query 可以对接的组件有: Thanos Store Gateway Thanos Query Thanos Receive Prometheus,借助于 Sidecar 利用 Thanos Query 之间的级联,我们可以实现跨组件的关联查询,组建超大型的监控系统。这也意味着,每个对接的组件应该提供足够快的 Prometheus API。整个接口的响应时