Skip to content

Commit 3bd704c

Browse files
committed
Added new post about inotify leaking in kubelet
1 parent c59459f commit 3bd704c

19 files changed

+549
-237
lines changed
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
I"v{"source"=>"/home/coder/share/CoderSherlock.github.io", "destination"=>"/home/coder/share/CoderSherlock.github.io/_site", "collections_dir"=>"", "cache_dir"=>".jekyll-cache", "plugins_dir"=>"_plugins", "layouts_dir"=>"_layouts", "data_dir"=>"_data", "includes_dir"=>"_includes", "collections"=>{"posts"=>{"output"=>true, "permalink"=>"/posts/:title"}}, "safe"=>false, "include"=>[".htaccess"], "exclude"=>["CHANGELOG.md", "HOW_TO_RELEASE.md", "Gemfile", "Gemfile.lock", "LICENSE", "README-*.md", "README.md", "gulpfile.js", "jekyll-text-theme.gemspec", "package-lock.json", "package.json", "/docs", "/node_modules", "/screenshots", "/test", "/vendor", "configure.sh", ".sass-cache", ".jekyll-cache", "gemfiles", "node_modules", "vendor/bundle/", "vendor/cache/", "vendor/gems/", "vendor/ruby/"], "keep_files"=>[".git", ".svn"], "encoding"=>"utf-8", "markdown_ext"=>"markdown,mkdown,mkdn,mkd,md", "strict_front_matter"=>false, "show_drafts"=>nil, "limit_posts"=>0, "future"=>false, "unpublished"=>false, "whitelist"=>[], "plugins"=>["jekyll-feed", "jekyll-paginate", "jekyll-sitemap", "jemoji", "jekyll-last-modified-at", "jekyll-seo-tag"], "markdown"=>"kramdown", "highlighter"=>"rouge", "lsi"=>false, "excerpt_separator"=>"<!--more-->", "incremental"=>false, "detach"=>false, "port"=>"4000", "host"=>"127.0.0.1", "baseurl"=>"", "show_dir_listing"=>false, "permalink"=>"/posts/:title", "paginate_path"=>"/page:num", "timezone"=>"America/New_York", "quiet"=>false, "verbose"=>false, "defaults"=>[{"scope"=>{"path"=>"", "type"=>"posts"}, "values"=>{"layout"=>"article", "sharing"=>true, "license"=>true, "aside"=>{"toc"=>true}, "show_edit_on_github"=>false, "show_subscribe"=>true, "pageview"=>true}}], "liquid"=>{"error_mode"=>"warn", "strict_filters"=>false, "strict_variables"=>false}, "kramdown"=>{"auto_ids"=>true, "toc_levels"=>[1, 2, 3, 4, 5, 6], "entity_output"=>"as_char", "smart_quotes"=>"lsquo,rsquo,ldquo,rdquo", "input"=>"GFM", "hard_wrap"=>false, "guess_lang"=>true, "footnote_nr"=>1, "show_warnings"=>false}, "text_skin"=>"default", "highlight_theme"=>"default", "url"=>"https://blog.pengzhan.dev", "fakebaseurl"=>"", "title"=>"STSD", "description"=>"My personal blog, some contents are useful, the others are not. Just like my mediocre life.\n", "lang"=>"en", "author"=>{"type"=>nil, "name"=>"Pengzhan Hao", "url"=>nil, "avatar"=>"/static/avatar.jpg", "bio"=>nil, "email"=>"[email protected]", "facebook"=>nil, "twitter"=>nil, "weibo"=>nil, "googleplus"=>nil, "telegram"=>nil, "medium"=>nil, "zhihu"=>nil, "douban"=>nil, "linkedin"=>"pengzhanhao", "github"=>"codersherlock", "npm"=>nil}, "repository"=>"CoderSherlock/CoderSherlock.github.io", "repository_tree"=>"master", "paths"=>{"root"=>nil, "home"=>nil, "archive"=>nil, "rss"=>nil}, "license"=>nil, "toc"=>{"selectors"=>nil}, "mathjax"=>nil, "mathjax_autoNumber"=>nil, "mermaid"=>nil, "chart"=>nil, "paginate"=>8, "sources"=>nil, "sharing"=>{"provider"=>false, "addthis"=>{"id"=>nil}}, "comments"=>{"provider"=>"disqus", "disqus"=>{"shortname"=>"codersherlockblog"}, "gitalk"=>{"clientID"=>nil, "clientSecret"=>nil, "repository"=>nil, "owner"=>nil, "admin"=>nil}, "valine"=>{"app_id"=>nil, "app_key"=>nil, "placeholder"=>nil, "visitor"=>nil, "meta"=>nil}}, "pageview"=>{"provider"=>false, "leancloud"=>{"app_id"=>nil, "app_key"=>nil, "app_class"=>nil}}, "search"=>{"provider"=>"default", "google"=>{"custom_search_engine_id"=>nil}}, "analytics"=>{"provider"=>"google", "google"=>{"tracking_id"=>"UA-82637164-1", "anonymize_ip"=>false}}, "jekyll_compose"=>{"default_front_matter"=>{"posts"=>{"description"=>nil, "cover"=>nil, "category"=>nil, "tags"=>nil, "published"=>false, "sitemap"=>true, "permalink"=>nil}}}, "serving"=>false}:ET
1+
I"�{"source"=>"/home/coder/share/CoderSherlock.github.io", "destination"=>"/home/coder/share/CoderSherlock.github.io/_site", "collections_dir"=>"", "cache_dir"=>".jekyll-cache", "plugins_dir"=>"_plugins", "layouts_dir"=>"_layouts", "data_dir"=>"_data", "includes_dir"=>"_includes", "collections"=>{"posts"=>{"output"=>true, "permalink"=>"/posts/:title"}}, "safe"=>false, "include"=>[".htaccess"], "exclude"=>["CHANGELOG.md", "HOW_TO_RELEASE.md", "Gemfile", "Gemfile.lock", "LICENSE", "README-*.md", "README.md", "gulpfile.js", "jekyll-text-theme.gemspec", "package-lock.json", "package.json", "/docs", "/node_modules", "/screenshots", "/test", "/vendor", "configure.sh", ".sass-cache", ".jekyll-cache", "gemfiles", "node_modules", "vendor/bundle/", "vendor/cache/", "vendor/gems/", "vendor/ruby/"], "keep_files"=>[".git", ".svn"], "encoding"=>"utf-8", "markdown_ext"=>"markdown,mkdown,mkdn,mkd,md", "strict_front_matter"=>false, "show_drafts"=>nil, "limit_posts"=>0, "future"=>false, "unpublished"=>false, "whitelist"=>[], "plugins"=>["jekyll-feed", "jekyll-paginate", "jekyll-sitemap", "jemoji", "jekyll-last-modified-at", "jekyll-seo-tag"], "markdown"=>"kramdown", "highlighter"=>"rouge", "lsi"=>false, "excerpt_separator"=>"<!--more-->", "incremental"=>false, "detach"=>false, "port"=>"4000", "host"=>"127.0.0.1", "baseurl"=>"", "show_dir_listing"=>false, "permalink"=>"/posts/:title", "paginate_path"=>"/page:num", "timezone"=>"America/New_York", "quiet"=>false, "verbose"=>false, "defaults"=>[{"scope"=>{"path"=>"", "type"=>"posts"}, "values"=>{"layout"=>"article", "sharing"=>true, "license"=>true, "aside"=>{"toc"=>true}, "show_edit_on_github"=>false, "show_subscribe"=>true, "pageview"=>true}}], "liquid"=>{"error_mode"=>"warn", "strict_filters"=>false, "strict_variables"=>false}, "kramdown"=>{"auto_ids"=>true, "toc_levels"=>[1, 2, 3, 4, 5, 6], "entity_output"=>"as_char", "smart_quotes"=>"lsquo,rsquo,ldquo,rdquo", "input"=>"GFM", "hard_wrap"=>false, "guess_lang"=>true, "footnote_nr"=>1, "show_warnings"=>false}, "text_skin"=>"default", "highlight_theme"=>"default", "url"=>"https://dev.pengzhan.dev", "fakebaseurl"=>"/proxy/4000", "title"=>"STSD", "description"=>"My personal blog, some contents are useful, the others are not. Just like my mediocre life.\n", "lang"=>"en", "author"=>{"type"=>nil, "name"=>"Pengzhan Hao", "url"=>nil, "avatar"=>"/static/avatar.jpg", "bio"=>nil, "email"=>"[email protected]", "facebook"=>nil, "twitter"=>nil, "weibo"=>nil, "googleplus"=>nil, "telegram"=>nil, "medium"=>nil, "zhihu"=>nil, "douban"=>nil, "linkedin"=>"pengzhanhao", "github"=>"codersherlock", "npm"=>nil}, "repository"=>"CoderSherlock/CoderSherlock.github.io", "repository_tree"=>"master", "paths"=>{"root"=>nil, "home"=>nil, "archive"=>nil, "rss"=>nil}, "license"=>nil, "toc"=>{"selectors"=>nil}, "mathjax"=>nil, "mathjax_autoNumber"=>nil, "mermaid"=>nil, "chart"=>nil, "paginate"=>8, "sources"=>nil, "sharing"=>{"provider"=>false, "addthis"=>{"id"=>nil}}, "comments"=>{"provider"=>"disqus", "disqus"=>{"shortname"=>"codersherlockblog"}, "gitalk"=>{"clientID"=>nil, "clientSecret"=>nil, "repository"=>nil, "owner"=>nil, "admin"=>nil}, "valine"=>{"app_id"=>nil, "app_key"=>nil, "placeholder"=>nil, "visitor"=>nil, "meta"=>nil}}, "pageview"=>{"provider"=>false, "leancloud"=>{"app_id"=>nil, "app_key"=>nil, "app_class"=>nil}}, "search"=>{"provider"=>"default", "google"=>{"custom_search_engine_id"=>nil}}, "analytics"=>{"provider"=>"google", "google"=>{"tracking_id"=>"UA-82637164-1", "anonymize_ip"=>false}}, "jekyll_compose"=>{"default_front_matter"=>{"posts"=>{"description"=>nil, "cover"=>nil, "category"=>nil, "tags"=>nil, "published"=>false, "sitemap"=>true, "permalink"=>nil}}}, "serving"=>false}:ET

_config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
##############################
1919
text_skin: default # "default" (default), "dark", "forest", "ocean", "chocolate", "orange"
2020
highlight_theme: default # "default" (default), "tomorrow", "tomorrow-night", "tomorrow-night-eighties", "tomorrow-night-blue", "tomorrow-night-bright"
21-
url : "https://blog.pengzhan.dev" # "https://dev.pengzhan.dev"
21+
url : "https://dev.pengzhan.dev"
2222
baseurl : ""
23-
fakebaseurl: "" # "/proxy/4000"
23+
fakebaseurl: "/proxy/4000"
2424
title : "STSD"
2525
description: > # this means to ignore newlines until "Language & timezone"
2626
My personal blog, some contents are useful, the others are not. Just like my mediocre life.

_posts/2024-04-11-Debug-kubelet.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ TL;DR: Bumping up to level 5 would satisfy most debugging needs.
2727
By the time, this note was written. In `kubelet` related code, level 8 was only used in `pkg/kubelet/prober/prober_manager.go` and level 7 was only used in `pkg/kubelet/logs/container_log_manager.go`. And there are 11 occurrences that level 6 was used, and all of them are not part of workload lifecycle related.
2828

2929
## Further readings
30-
[Inotify watcher leaks in Kubelet]
30+
[Inotify watcher leaks in Kubelet](/posts/inotify-watcher-leaks-in-kubelet.html)
Lines changed: 149 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,156 @@
11
---
2-
layout: post
32
title: Inotify watcher leaks in Kubelet
4-
date: 2024-04-11 16:35 -0400
3+
date: 2024-04-18 16:35 -0400
54
description:
6-
cover:
5+
cover: '/static/2024-04/kubelet_inotify_leak_logo.png'
76
category:
8-
tags:
9-
published: false
7+
tags: ["Kubernetes", "Kubelet", "Debug", "Inotify"]
8+
published: true
109
sitemap: true
1110
permalink:
11+
author: Pengzhan Hao
1212
---
13+
14+
## Symptom
15+
Recently, I faced an issue where Kubelet on a node reported error message failed to create file descriptors.
16+
17+
```bash
18+
error creating file watcher: too many open files
19+
error creating file watcher: no space left on device
20+
```
21+
22+
After short checking, I found the node has `max_user_watches` of 10000, but the `TotalinotifyWatches` is beyond this value. (P. S still not sure why watcher can initiate more than cap). In order to find which process occupied the most watchers. I used following command[^flbit_ino] to find it out.
23+
24+
```bash
25+
echo -e "COUNT\tPID\tUSER\tCOMMAND"; sudo find /proc/[0-9]*/fdinfo -type f 2>/dev/null | sudo xargs grep ^inotify 2>/dev/null | cut -d/ -f 3 | uniq -c | sort -nr | { while read -rs COUNT PID; do echo -en "$COUNT\t$PID\t"; ps -p $PID -o user=,command=; done}
26+
27+
COUNT PID USER COMMAND
28+
7491 8412 root /home/kubernetes/bin/kubelet --v=2 --cloud-provide=gce --experi
29+
2620 1 root /sbin/init
30+
....
31+
```
32+
33+
Surprisingly, Kubelet initiated more than 7000 inotify watchers. I assumed there was an inotify leakage in Kubelet.
34+
## Leakage check
35+
36+
### Clean Kubelet
37+
To better understand the situation, I created a clean cluster with only 1 clean node on GKE. Roughly 70 inotify watchers were there. I created a single nginx pod and the number increased by 3. Theoretically, these 3 watchers are used by Kubelet to monitor any changes on `rootfs `, `kube-api-access` and `PodSandbox`. But to verify it, we need to check more details on which inodes are monitored by Kubelet.
38+
### Check inotify file descriptors
39+
To do so, let's take a look how to track a single inotify file descriptor. Opened processes' `fdinfo` folder, we can examine each or them to find an inotify fd.
40+
41+
```bash
42+
# Find kubelet pid
43+
ps -aux | grep kubelet
44+
KPID=2430
45+
46+
# File the an example fd
47+
sudo ls /proc/2430/fdinfo
48+
49+
0 1 10 11 12 13 14 2 3 4 5 6 7 8 9
50+
51+
...
52+
53+
sudo cat /proc/2430/fdinfo/8
54+
55+
pos: 0
56+
flags: 02004000
57+
mnt_id: 15
58+
ino: 1057
59+
inotify wd:1 ino:3f327 sdev:800001 mask:fc6 ignored_mask:0 fhandle-bytes:8 fhandle-type:1 f_handle:27f30300e5059ea2
60+
```
61+
62+
This is very confusing, so I rely on `man proc`[^man_proc] to understand every piece of them. In given fd, the needed information to continue sit in the last line. It's an inotify entry represents the 1 file or folder to be monitored. And the most useful data is `ino:3f327` which represents the inode number of target file (in hexadecimal). And `sdev:800001`, which represents the ID of device where the inode sit on, and it's also in hex.
63+
64+
Using `lsblk`, I can see there's only 1 disk I'm using on the node, so finding the target file would be easy.
65+
66+
```bash
67+
# Cast to decimal
68+
ino=3f327
69+
dec="$((16#${ino}))"
70+
71+
# Find the target file
72+
loc="debugfs -R 'ncheck ${dec}' /dev/sda1"
73+
sudo eval $loc 2>/dev/null
74+
75+
debugfd 1.46.5 (30-Dec-2021)
76+
Inode Pathname
77+
258855 /etc/srv/kubernetes/pki/ca-certificates.crt
78+
```
79+
80+
Put all processes above into one single script, I can retrieve all target files, that would help to understand if there's a real leakage. Also, I count the unique inode amount, this could also help to know which inode are monitored multiple times.
81+
82+
```bash
83+
cat << EOF | sudo tee -a test.sh
84+
echo "kubelet pid="${PID}
85+
in_fds=$(find /proc/${PID}/fdinfo -type f 2>/dev/null | xargs grep ^inotify | cut -d " " -f 3 | cut -d ":" -f 2)
86+
echo ${in_fds}
87+
echo "Count: $(find /proc/${PID}/fdinfo -type f 2>/dev/null | xargs grep ^inotify | wc -l)"
88+
89+
uniq_fds=$(echo "${in_fds[@]}" | sort | uniq)
90+
echo ${uniq_fds}
91+
92+
while read -r element;
93+
do
94+
count=$(echo "${in_fds[@]}" | grep -o "${element}" | wc -l)
95+
dec="$((16#${element}))"
96+
loc="debugfs -R 'ncheck ${dec}' /dev/sda1"
97+
loc=$(eval $loc 2>/dev/null | tail -1 | cut -d " " -f 4)
98+
printf "%-6s %-10s %-6s %s\n" "${element}" "${dec}" "${count}" "${loc}"
99+
done <<< "${uniq_fds}"
100+
EOF
101+
102+
sudo bash test.sh
103+
104+
kubelet pid=2430
105+
3f327 3f321 ...
106+
Count: 120
107+
1 10b 1259 128a ...
108+
1 1 72 Inode Pathname
109+
10b 267 1 267 /etc/systemd/system/multi-user.target.wants/snapd.service
110+
...
111+
```
112+
113+
The given results are consists by following parts:
114+
- One line for get Kubelet pid
115+
- One line for all target inode numbers
116+
- One line for tell how many unique inode (120)
117+
- One line of sorted target
118+
- Following 120 lines, each of them represents a unique inode number, its decimal number, count, another time of decimal number and the target file path.
119+
120+
I used the same script to the problematic node, and it showed the following result. In summary, most Kubelet watchers were targeting `ino:1 `. And there are 6649 targets files, which likely to be leakage, because there were only 150 pods on this pod. Unfortunately, `debugfs` can't find any target files, so the output showed as meaningless string `"Inode Pathname"`.
121+
122+
```
123+
kubelet pid=8412
124+
...
125+
Count: 7491
126+
...
127+
1 1 6649 Inode Pathname
128+
```
129+
130+
### Bad apple
131+
Why `debugfs` can't help anymore? The reason is simple, each cgroup for a pod is using its own rootfs. This means the watcher are somehow residing on different rootfs and using independent inode index. There are some other ways to do it, I choose the most common tool `grep` to find out.
132+
133+
```
134+
sudo grep / -inum 1
135+
/home/kubernetes/containerized_mounter/rootfs/dev
136+
/home/kubernetes/containerized_mounter/rootfs/proc
137+
...
138+
/home/kubernetes/containerized_mounter/rootfs/var/lib/kubelet/pods/5325873d-f2a0-48df-83e2-0b911df2f77f/volumes/kubernetes.io~projected/kube-api-access-227jg
139+
...
140+
/dev
141+
/boot/efi
142+
...
143+
```
144+
145+
This turns things easy, because I can just use pod ID to compare between running pods to find out if there are any terminated pods are there. And it did show there's some non-existence pods still being watched somehow.
146+
147+
## What to expect next
148+
149+
- [How Kubelet leaked inotify watchers?]()
150+
- [debugfs]()
151+
152+
## References
153+
154+
[^flbit_ino]: [Fluentbit error "cannot adjust chunk size" on GKE](https://stackoverflow.com/a/76712244)
155+
[^man_proc]: [proc(5)](https://manpages.courier-mta.org/htmlman5/proc.5.html)
156+
[^list_ino]: [Listing the files that are being watched by `inotify` instances](https://unix.stackexchange.com/a/646113)

0 commit comments

Comments
 (0)