查询进程打开的目录或者文件
本文永久链接: https://www.xtplayer.cn/linux/query-dir-used-by-process/
在容器环境中,有时候可能发现某个容器占用了很大的磁盘空间。如果在特定的目录中没有看到占用空间的文件或者目录,那么可能进程还打开了其他的文件或者目录。
以 rancher 进程为例,在主机上执行以下脚本可以查询到 rancher 进程打开的目录和文件
#!/bin/bash# 查看指定进程占用的所有目录PID=$(pgrep rancher)if [ -z "$PID" ]; then echo "Usage: $0 <PID>" exit 1fiecho "进程 $PID 的目录信息:"echo "===================="# 工作目录echo -n "工作目录: "readlink /proc/$PID/cwdecho -e "\n打开的文件和目录:"lsof -p $PID | grep -E ...
阅读全文查询容器中进程名称和 Pid 对应关系
本文永久链接: https://www.xtplayer.cn/linux/query-process-and-pid/
现在很多容器都做了深度精简,其中可能都没有 ps 命令。排查问题时,有时候需要知道容器中有哪些进程,或者说进程与进程 pid 的对应关系。如果没有 ps 命令,可以使用以下脚本在容器 shell 中查询。
for pid in $(ls -1 /proc | grep -E '^[0-9]+$'); do echo -n "$pid: "; cat /proc/$pid/cmdline 2>/dev/null; echo; done
阅读全文rke2 /run 目录报 no space left on device
本文永久链接: https://www.xtplayer.cn/rke2/rke2-run-dir-no-space-left-on-device/
当 containerd 作为 runtime 时,容器运行的相关文件( merger 层等)都保存在 /run/k3s/containerd/ 目录。rke2-containerd 默认配置下 containerd state 目录为/run/k3s/containerd,可以通过在主机上执行 cat /var/lib/rancher/rke2/agent/etc/containerd/config.toml 查看 rke2-containerd 配置。当容器镜像越大、容器产生临时文件越多,就会导致 /run/k3s/containerd/io.containerd.runtime.v2.task/k8s.io 下的子目录使用越来越大,最终导致 /run 目录被耗尽。具体请参考 issue https://github.com/k3s-io/k3s/issues/4327 的说明。
处 ...
阅读全文Neuvector 与 Rancher RBAC 集成
本文永久链接: https://www.xtplayer.cn/neuvector/rancher-neuvector-sso-rbac/
创建角色模板将以下的角色模板导入 local 集群,
administrative: falseapiVersion: management.cattle.io/v3builtin: falseclusterCreatorDefault: falsecontext: clusterdisplayName: Neuvector UI Proxyexternal: falsehidden: falsekind: RoleTemplatelocked: falsemetadata: annotations: cleanup.cattle.io/rtUpgradeCluster: 'true' lifecycle.cattle.io/create.mgmt-auth-roletemplate-lifecycle: 'true' finalizers: - controller.cattle.i ...
阅读全文普通用户在 rancher-ui 查看 pod 监控图表的权限设置
本文永久链接: https://www.xtplayer.cn/rancher/rancher-ui-pod-metrics-permission/
1,将以下 yaml 导入 local 集群创建项目角色。
apiVersion: management.cattle.io/v3builtin: falsecontext: projectdescription: Members can only view the metrics of resources inside the Project.displayName: Monitoring Viewexternal: falsehidden: falsekind: RoleTemplatemetadata: annotations: cleanup.cattle.io/rtUpgradeCluster: 'true' lifecycle.cattle.io/create.mgmt-auth-roletemplate-lifecycle: 'true' finalizers: ...
阅读全文rancher-rke2 集群 kube-proxy 切换为 ipvs 模式
本文永久链接: https://www.xtplayer.cn/rke2/rancher-rke2-kubeproxy-switch-to-ipvs/
在 rancher RKE2 中切换 kube-proxy 从默认的 iptables 模式到 IPVS 模式需要进行一些配置修改。以下是详细步骤:
检查必要的内核模块确保节点上加载了 IPVS 所需的内核模块:
# 检查已加载的模块lsmod | grep ip_vs# 如果需要手动加载sudo modprobe ip_vssudo modprobe ip_vs_rrsudo modprobe ip_vs_wrrsudo modprobe ip_vs_shsudo modprobe nf_conntrack
手动安装 rke2 集群创建或者编辑已有的 RKE2 配置文件sudo mkdir -p /etc/rancher/rke2/sudo vim /etc/rancher/rke2/config.yaml
配置 kube-proxy 使用 IPVS在配置文件中添加以下内容:
kube-proxy-arg:- "pro ...
阅读全文Rancher RKE2 自定义集群字段说明
本文永久链接: https://www.xtplayer.cn/rancher/rancher-rke2-custom-cluster-field-description/
apiVersion: provisioning.cattle.io/v1kind: Clustermetadata: name: rke2-test annotations: {} # key: string labels: {} # key: string namespace: fleet-defaultspec:# 自定义 cluster agent deployment 的一些配置参数,一般不需要自定义配置参数。一些特殊情况下可能需要用到,比如节点都打了 Taint,需要添加自定义 Toleration pod 才能在节点运行。 clusterAgentDeploymentCustomization: appendTolerations:# - effect: string# key: string ...
阅读全文k8s Tolerations 模糊匹配
本文永久链接: https://www.xtplayer.cn/kubernetes/tolerations-fuzzy-matching/
在 Kubernetes 中,Tolerations(容忍) 是 Pod 的一个属性,允许(但并不要求)Pod 被调度到具有相应 Taints(污点) 的节点上。
Toleration 的匹配规则不仅仅是“完全相等”,它提供了几种灵活的匹配方式,这也就是所说的 “模糊匹配”。核心的模糊匹配体现在对 operator 和 effect 字段的运用上。
Toleration 的核心结构一个典型的 Toleration 定义如下:
tolerations:- key: "key1" operator: "Equal" value: "value1" effect: "NoSchedule" tolerationSeconds: 3600 # 仅对 `NoExecute` 效果有效
关键字段:
key: 污点的键。
operator: 匹配操作符,这是实现“模 ...
阅读全文监控 RKE2 集群证书有效期
本文永久链接: https://www.xtplayer.cn/rke2/monitor-rke2-cluster-certificates-validity/
根据 rke2 官方文档说明 https://docs.rke2.io/reference/metrics ,rke2 目前支持一些指标的输出,比如 rke2_certificate_expiration_seconds。通过 rke2_certificate_expiration_seconds 指标,可以查询 rke2 集群各组件的有效期。并可以通过配置集群监控告警来对证书有效时间进行提醒。
启用 rke2 Metrics 功能rke2 Metrics 功能默认没有启用,需要在 /etc/rancher/rke2/config.yaml 配置中添加 supervisor-metrics: true 来启用。
通过 rke2 安装脚本或者 rke2 二进制文件部署 rke2 集群如果是通过 rke2 安装脚本或者 rke2 二进制文件部署的集群,需要在每个 server 节点上创建或者修改 /etc/rancher/rke ...
阅读全文k8s 测试集群 pod 网络连通性
本文永久链接: https://www.xtplayer.cn/kubernetes/test-cluster-network-connectivity/
使用 busybox 镜像通过以下命令创建 DaemonSet 类型的测试应用,
kubectl create deploy net-test --image=registry.cn-hangzhou.aliyuncs.com/rancher/busybox \--dry-run=client -o yaml | \sed '/null\|{}\|replicas/d;/status/,$d;s/Deployment/DaemonSet/g' | \sed '/image: .*/a \ stdin: true\n tty: true' | \kubectl apply -f -
等 DaemonSet pod 创建完成后,使用以下脚本去交叉测试跨节点 pod 与 pod 之间的网络连通性。
#!/bin/bash# 获取所有 Pod 名称和 ...
阅读全文rke2 部署 terway 驱动 kubelet 报: dial unix var/run/eni/eni.socket: connect: no such file or directory 错误
本文永久链接: https://www.xtplayer.cn/rke2/kubelet-error-dial-unix-var-run-eni-eni-socket-connect-no-such-file-or-directory/
根据 terway 文档进行部署 https://github.com/AliyunContainerService/terway/blob/main/docs/terway-with-cilium.md ,部署完成后在执行 tail -f /var/lib/rancher/rke2/agent/logs/kubelet.log 可以看到如下的错误:
E0514 10:59:01.271853 1314 kuberuntime_manager.go:705] "killPodWithSyncResult failed" err="failed to \"KillPodSandbox\" for \"ebba0c90-bf0d-400e-b90d-f46e0f86dad3\" ...
阅读全文为什么通过 rke 或 Rancher v2.x 配置的集群中执行集群证书轮换后,kubelet 日志仍然显示证书过期?
本文永久链接: https://www.xtplayer.cn/kubernetes/kubelet-cert-expired/
问题现象
有时间可能会出现节点状态不正常,然后查看 kubelet 容器日志时,会看到如上图的证书过期的错误信息。
问题排查
如果你是 rke1 版本创建的 local 集群或者下游自定义集群,可以使用以下命令查询证书有效期:
printf '\n%-16s %-16s %-14s %s\n' "开始时间" "过期时间" "剩余天数" "证书路径"echo "----------------------------------------------------------------------------"current_timestamp=$(date +%s)for crt in $(ls /etc/kubernetes/ssl/*.pem | grep -v '\-key\.'); do # 获取开 ...
阅读全文admission webhook "rancher.cattle.io.namespaces" denied the request Unauthorized
本文永久链接: https://www.xtplayer.cn/rancher/admission-webhook-rancher-cattle-io-namespaces-denied-the-request-unauthorized/
目前 admission webhook “rancher.cattle.io.namespaces” denied the request Unauthorized 这个报错,已知的在 tigera-operator 和 NeuVector controller 中出现过,主要原因是缺少了某些权限。可以根据实际使用,拷贝以下 yaml 导入集群添加对应的权限。
tigera-operator
apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata: name: tigera-operator-psarules:- apiGroups: - management.cattle.io resources: - projects verbs: - updatep ...
阅读全文rancher logging v2 collect rke1-k8s logs
本文永久链接: https://www.xtplayer.cn/rancher/rancher-logging-v2-collect-rke1-k8s-logs/
问题背景rke1 创建的 k8s 集群,k8s 系统基础组件(比如 kubelet、etcd、kube-scheduler等)都是以 docker 容器方式部署。在使用 rancher v2.6+ 版本,建议升级 rancher logging 到 v2 版本。rancher logging 基于开源的 logging operator,对应的 git repo:https://github.com/kube-logging/logging-operator 和文档地址:https://kube-logging.dev/docs/ 。
logging operator 默认只针对 k8s pod 日志和 k8s events,对于主机上 docker run 运行的容器日志不是很友好。因此针对这个问题,rancher 在原有的 logging operator 基础上增加了 rancher-logging-rke-agg ...
阅读全文etcd snapshots showing 0kB size in the Rancher UI
本文永久链接: https://www.xtplayer.cn/rancher/etcd-snapshots-showing-0kb-size-in-the-rancher-ui/
在 rancher 2.6.x 和 2.7.x 版本中,创建的下游集群可能存在集群 etcd 备份大小为 0 kb 的情况。根据 suse 知识库文档 https://www.suse.com/support/kb/doc/?id=000021447 说明,这个问题主要是 rke2 和 rke2-etcd、k3s 和 k3s-etcd 租约的租约持有者不匹配导致。
对于 rke2 集群,可以使用以下命令去修复。
kubectl -n kube-system get cm rke2-etcd-snapshots -oyaml > rke2-etcd-snapshots-cm-bak.yamlkubectl -n kube-system patch cm --type=json -p='[{"op": "remove", "path ...
阅读全文couldn't get resource list for external.metrics.k8s.io/v1beta1
如上图,有时候在执行 kubectl 命令时候会提示 "memcache.go:255] couldn't get resource list for external.metrics.k8s.io/v1beta1: the server is currently unable to handle the request" 错误。或者在某些系统组件的日志中也可以看到类似 couldn't get resource list for xxx.xxx.k8s.io/v1beta1 这样的错误。
根据以上信息可以知道此问题与 external.metrics.k8s.io/v1beta1 相关,从报错现象看是无法连接到 external metrics server 获取相关信息。
分析rancher\rke\rke2 默认不会部署与 external.metrics 相关的资源,因此如果 k8s 提示 external.metrics.k8s.io/v1beta1 相关信息,那么可以说明集群中有人为部署 external.metrics 相 ...
阅读全文rke2 Enable audit logs of the downstream cluster
本文永久链接: https://www.xtplayer.cn/rke2/rke2-enable-audit-logs-of-the-downstream-cluster/
自定义 rke2 集群以下内容以自定义 rke2 集群为例,编辑 rke2 集群的 yaml,在 machineGlobalConfig 层级下添加 audit-policy-file 配置。以下为设置好的 rke2 集群 yaml 的部分内容
# 默认审计日志输出配置spec:...... rkeConfig:...... machineGlobalConfig: audit-policy-file: | # 记录所有请求的元数据(如时间戳、用户、资源、动词等),但不记录请求体或响应体。 apiVersion: audit.k8s.io/v1 # 对于 k8s >= v1.16, 更老的版本可能是 v1beta1 kind: Policy # 忽略 "RequestReceived" 阶段的请求,以减少日志量。 ...
阅读全文rke2 启用 event_rate_limit 和 always_pull_images 功能
本文永久链接: https://www.xtplayer.cn/rke2/rke2-enable-event-rate-limit-always-pull-images/
rke2 中如果要启用 event_rate_limit 和 always_pull_images 功能,需要单独手动配置。这两个参数对应 k8s 的
https://kubernetes.io/zh-cn/docs/reference/access-authn-authz/admission-controllers/#alwayspullimages
https://kubernetes.io/zh-cn/docs/reference/access-authn-authz/admission-controllers/#eventratelimit
启用插件
如果要启用这两个功能,先在主机上执行 ps -ef| grep kube-apiserver ,找到 enable-admission-plugins 后面已启用的插件。然后在 kube-apiserver-arg 下添加 enable-admission-p ...
阅读全文rke2-cilium app 升级到 1.15.000+ 版本之后 bandwidth manager 不工作
本文永久链接: https://www.xtplayer.cn/cilium/bandwidth-manager-not-work/
问题现象在 rke2-cilium app 版本升级到 1.15.000+ 后,可能会导致 bandwidthManager 无法使用,通过 cilium status 命令查看 BandwidthManager 是禁用状态。在 应用|已安装应用中,编辑 rke2-cilium app 将其降级到 1.15 以下版本就可以正常使用。
问题分析通过以下配置,在 rke2 配置中启用 cilium 的 debug。
rkeConfig: additionalManifest: '' chartValues: rke2-cilium: bandwidthManager: bbr: false enabled: true debug: enabled: true ipv6: enabled: false etcd:
在 cilium pod ...
阅读全文Readiness probe failed: calico/node is not ready: BIRD is not ready
本文永久链接: https://www.xtplayer.cn/calico/readiness-probe-failed-calico-node-is-not-ready-bird-is-not-ready/
问题现象在通过 rancher ui 或者 rke 安装 k8s 集群时,如果使用的是 calico 网络驱动,可能会出现 calico-node pod 一直无法正常 running。通过执行 kubectl -n kube-system describe calico-node-xxx 命令查看 calico-node pod 事件,可以看到类似如下的报错。
Readiness probe failed: calico/node is not ready: BIRD is not ready: Error querying BIRD: unable to connect to BIRDv4 socket: dial unix /var/run/calico/bird.ctl: connect: connection refused或者calico/node is not ...
阅读全文calico 各组件日志等级设置
本文永久链接: https://www.xtplayer.cn/calico/calico-log-level-set/
增加 calico-typha 组件的日志级别
运行以下命令以编辑 calico-typha 部署。
kubectl edit deploy calico-typha -n kube-system
将 TYPHA_LOGSEVERITYSCREEN 环境变量从 info 更改为 debug。
containers:- env: - name: TYPHA_LOGSEVERITYSCREEN value: debug
保存并关闭文件以应用更改,然后重新启动 calico-typha 部署。
增加 calico-cni 组件的日志级别
运行以下命令以编辑 calico-config ConfigMap。
kubectl edit cm -n kube-system calico-config 或者 canal-config
将 cni_network_config > plugins > log_level 环境变量更改为 debug ...
阅读全文