You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -29,7 +34,7 @@ This will download and compile `kubectl-trace` so that you can use it as a kubec
29
34
You don't need to setup anything on your cluster before using it, please don't use it already
30
35
on a production system, just because this isn't yet 100% ready.
31
36
32
-
**Run a program from string literal:**
37
+
### Run a program from string literal
33
38
34
39
In this case we are running a program that probes a tracepoint
35
40
on the node `ip-180-12-0-152.ec2.internal`.
@@ -39,15 +44,15 @@ kubectl trace run ip-180-12-0-152.ec2.internal -e "tracepoint:syscalls:sys_enter
39
44
```
40
45
41
46
42
-
**Run a program from file:**
47
+
### Run a program from file
43
48
44
49
Here we run a program named `read.bt` against the node `ip-180-12-0-152.ec2.internal`
45
50
46
51
```
47
52
kubectl trace run ip-180-12-0-152.ec2.internal -f read.bt
48
53
```
49
54
50
-
**Run a program against a Pod**
55
+
### Run a program against a Pod
51
56
52
57

53
58
@@ -83,6 +88,114 @@ So, running against a pod **doesn't mean** that your bpftrace program will be co
83
88
knowledge of the context of a container, in this case only the root process id is supported via the `$container_pid` variable.
84
89
85
90
91
+
### Using a custom service account
92
+
93
+
By default `kubectl trace` will use the `default` service account in the target namespace (that is also `default`), to schedule the pods needed for your bpftrace program.
94
+
95
+
If you need to pass a service account you can use the `--serviceaccount` flag.
96
+
97
+
```bash
98
+
kubectl trace run --serviceaccount=kubectltrace ip-180-12-0-152.ec2.internal -f read.bt
99
+
```
100
+
101
+
### Executing in a cluster using Pod Security Policies
102
+
103
+
If your cluster has pod security policies you will need to make so that `kubectl trace` can
104
+
use a service account that can run privileged containers.
105
+
106
+
That service account, then will need to be in a group that uses the proper privileged `PodSecurityPolicy`.
107
+
108
+
First, create the service account that you will use with `kubectl trace`,
109
+
you can use a different namespace other than `default`, just remember to pass that namespace to the `run` command when you will use `kubectl trace`:
110
+
111
+
```yaml
112
+
apiVersion: v1
113
+
kind: ServiceAccount
114
+
metadata:
115
+
name: kubectltrace
116
+
namespace: default
117
+
```
118
+
119
+
Now that we have a `kubectltrace` service account let's create a Pod Security Policy:
120
+
121
+
```yaml
122
+
apiVersion: policy/v1beta1
123
+
kind: PodSecurityPolicy
124
+
metadata:
125
+
name: kubectltrace
126
+
spec:
127
+
fsGroup:
128
+
rule: RunAsAny
129
+
privileged: true
130
+
runAsUser:
131
+
rule: RunAsAny
132
+
seLinux:
133
+
rule: RunAsAny
134
+
supplementalGroups:
135
+
rule: RunAsAny
136
+
volumes:
137
+
- '*'
138
+
allowedCapabilities:
139
+
- '*'
140
+
hostPID: true
141
+
hostIPC: true
142
+
hostNetwork: true
143
+
hostPorts:
144
+
- min: 1
145
+
max: 65536
146
+
```
147
+
148
+
Ok, this `PodSecurityPolicy` will allow users assigned to it to run privileged containers,
149
+
`kubectl trace`needs that because of the extended privileges eBPF programs need to run with
150
+
to trace your kernel and programs running in it.
151
+
152
+
Now with a `ClusterRoleBinding` you bind the `ClusterRole` with the `ServiceAccount`, so that
153
+
they can work together with the `PodSecurityPolicy` we just created.
154
+
155
+
You can change the `namespace: default` here if you created the service account in a namespace other than `default`.
156
+
157
+
```yaml
158
+
apiVersion: rbac.authorization.k8s.io/v1
159
+
kind: ClusterRole
160
+
metadata:
161
+
name: kubectltrace-psp
162
+
rules:
163
+
- apiGroups:
164
+
- policy
165
+
resources:
166
+
- podsecuritypolicies
167
+
resourceNames:
168
+
- kubectltrace
169
+
verbs:
170
+
- use
171
+
---
172
+
apiVersion: rbac.authorization.k8s.io/v1
173
+
kind: ClusterRoleBinding
174
+
metadata:
175
+
name: kubectltrace-psp
176
+
subjects:
177
+
- kind: ServiceAccount
178
+
name: kubectltrace
179
+
namespace: default
180
+
roleRef:
181
+
apiGroup: rbac.authorization.k8s.io
182
+
kind: ClusterRole
183
+
name: kubectltrace-psp
184
+
```
185
+
186
+
OK! Now that we are all set we can just run the program by specifying the service account
187
+
we just created and it will use our pod security policy!
188
+
189
+
```bash
190
+
kubectl trace run --serviceaccount=kubectltrace ip-180-12-0-152.ec2.internal -f read.bt
191
+
```
192
+
193
+
If you used a different namespace other than default for your service account, you will want to specify the namespace too, like this:
194
+
195
+
```bash
196
+
kubectl trace run --namespace=mynamespace --serviceaccount=kubectltrace ip-180-12-0-152.ec2.internal -f read.bt
197
+
```
198
+
86
199
### More bpftrace programs
87
200
88
201
Need more programs? Look [here](https://github.com/iovisor/bpftrace/tree/master/tools).
@@ -119,10 +232,6 @@ kubectl trace run pod/<pod-name> -c <container> f read.bt
119
232
120
233
So I would say, the next thing is to run bpftrace programs at a pod scope other than at node scope.</strike>
121
234
122
-
**bpftrace work**
123
-
124
-
I also plan to contribute some IO functions to bpftrace to send data to a backend database like InfluxDB instead of only stdout
125
-
because that would enable having things like graphs showing
0 commit comments