github.com/inspektor-gadget/inspektor-gadget@v0.28.1/docs/guides/common-features.md (about)

     1  ---
     2  title: 'Common Features'
     3  weight: 10
     4  description: 'An overview of the common features shared across gadgets'
     5  ---
     6  
     7  Inspektor Gadget comes with a wide array of gadgets that allow us to
     8  inspect what's going on in our clusters. The flags and parameters that we
     9  can pass to each specific gadget may differ, but there are some that are
    10  supported across all gadgets.
    11  
    12  ## Event Filtering
    13  
    14  There are several ways to choose the pods or containers that we want to
    15  trace:
    16  
    17   * `--node string`, show only data from pods running in that node
    18   * `-n string`, `--namespace string`, show data from pods in that namespace
    19   * `-A`, `--all-namespaces`, show data from pods in all namespaces
    20   * `-p string`, `--podname string`, show only data from pods with that name
    21   * `-c string`, `--containername string`, show only data from containers with that name
    22   * `-l string`, `--selector string`: show only data that matches the given
    23     label or selector. Only `=` is currently supported (e.g. `key1=value1,key2=value2`).
    24  
    25  We can use one or more of these parameters to choose which pods or
    26  containers will be inspected by our gadgets.
    27  
    28  For example:
    29  
    30  ```bash
    31  $ kubectl gadget trace exec -n demo -l app=myapp
    32  ```
    33  
    34  Will run the `exec` tracer for all pods in the `demo` namespace that have
    35  the `app=myapp` label.
    36  
    37  ```bash
    38  $ kubectl gadget snapshot socket -A -p nginx
    39  ```
    40  
    41  Will get the `socket` snapshot for all pods with name `nginx`, regardless
    42  of which namespace they are in.
    43  
    44  ## Output Format
    45  
    46  The `-o` or `--output` flag lets us decide the format for the output the
    47  gadget will generate. The default `columns` output shows some of the
    48  information gathered, arranged in text columns on the console.
    49  
    50  This can be overridden with:
    51  - `json`
    52  - `jsonpretty`
    53  - `yaml`
    54  - `columns`
    55  
    56  ### JSON Output
    57  
    58  Passing `-o json` will print all the information gathered in JSON format.
    59  Each entry is printed on a single line, so the output can be easily parsed line by line.
    60  
    61  For example:
    62  ```bash
    63  $ kubectl gadget trace tcp -A -o json | jq
    64  {
    65    "type": "normal",
    66    "node": "minikube",
    67    "namespace": "kube-system",
    68    "pod": "coredns-66bff467f8-8ftjm",
    69    "container": "coredns",
    70    "pid": 2688,
    71    "comm": "coredns",
    72    "ipversion": 4,
    73    "saddr": "127.0.0.1",
    74    "daddr": "127.0.0.1",
    75    "sport": 46010,
    76    "dport": 8080,
    77    "operation": "connect"
    78  }
    79  ```
    80  
    81  ### JSON Pretty Output
    82  
    83  Passing `-o jsonpretty` will print all the information gathered in JSON format but with indentation making it easier to read.
    84  
    85  ### YAML Output
    86  
    87  Passing `-o yaml` will print all the information gathered in YAML format.
    88  Each entry is preceded by the end of directives markers (`---`).
    89  
    90  ### Custom Columns
    91  
    92  Using `-o columns=column1,column2` we can choose which columns to
    93  print. You can use gadget help to see which columns are available:
    94  
    95  ```bash
    96  $ kubectl gadget trace open -h
    97  ...
    98    Available columns:
    99      comm
   100      err
   101      fd
   102      flags
   103      fullPath (requires --full-path)
   104      gid
   105      k8s.container
   106  ...
   107  ```
   108  
   109  For example, when tracing which processes were killed because of the node
   110  running out of memory, we can choose to only print the PID and command of
   111  the killed process:
   112  
   113  ```bash
   114  $ kubectl gadget trace oomkill -A -o columns=kpid,kcomm
   115  KPID   KCOMM
   116  15182  tail
   117  ```
   118  
   119  Also, we can use the `+` and `-` prefixes to add or remove columns relative to the default columns.
   120  For example, in case we want to add `uid` and `gid` columns to the default columns for trace open gadget:
   121  
   122  ```bash
   123  $ kubectl gadget trace open -A -o columns=+uid,+gid
   124  K8S.NODE      K8S.NAMESPACE K8S.POD       K8S.CONTAINER PID     COMM   FD ERR PATH                     UID        GID
   125  miniku…docker default       test-p…-v6rqg nginx         1149213 docke… 3  0   /etc/ld.so.cache         0          0
   126  
   127  ```
   128  
   129  Columns `k8s` and `runtime` are expanded to all available columns for the respective environment.
   130  For example, in case we want `trace tcp` gadget to print events with only `runtime` columns:
   131  
   132  ```bash
   133  $ kubectl gadget trace tcp -o columns=-k8s,+runtime
   134  RUNTIME.RUNTIMENAME RUNTIM… RUNTIME.CONTAIN… RUNTIME… T PID      COMM     IP SRC                    DST
   135  docker              8df2cb… k8s_nginx_test-… nginx    A 1163696  nginx    4  p/default/test-pod-67c r/10.244.0.1:58570
   136  docker              8df2cb… k8s_nginx_test-… nginx    X 1163696  nginx    4  p/default/test-pod-67c r/10.244.0.1:58570
   137  ```
   138  
   139  ## Run for a specific amount of time
   140  
   141  Many gadgets will run forever, printing the gathered output until we press
   142  Ctrl-C to stop them. If we want to run a gadget only for a window of time,
   143  we can use the `--timeout int` flag, passing the number of seconds during which
   144  we want to run the gadget.
   145  
   146  For example, we can trace files that get opened by pods in the `gadget`
   147  namespace during a window of 5 seconds, like this:
   148  
   149  ```bash
   150  $ kubectl gadget trace open -n gadget --timeout 5
   151  K8S.NODE         K8S.NAMESPACE    K8S.POD          K8S.CONTAINER    PID     COMM             FD    ERR PATH
   152  minikube         gadget           gadget-vhcj7     gadget           1303299 gadgettracerman  3     0   /etc/ld.so.cache
   153  minikube         gadget           gadget-vhcj7     gadget           1303299 gadgettracerman  3     0   /lib/x86_64-linux-gnu/libpthread.so.0
   154  minikube         gadget           gadget-vhcj7     gadget           1303299 gadgettracerman  3     0   /lib/x86_64-linux-gnu/libseccomp.so.2
   155  minikube         gadget           gadget-vhcj7     gadget           1303299 gadgettracerman  3     0   /lib/x86_64-linux-gnu/libc.so.6
   156  minikube         gadget           gadget-vhcj7     gadget           1303299 gadgettracerman  3     0   /sys/kernel/mm/transparent_hugepage/hpage_pmd_size
   157  minikube         gadget           gadget-vhcj7     gadget           1303299 gadgettracerman  6     0   /usr/bin/gadgettracermanager
   158  minikube         gadget           gadget-vhcj7     gadget           1303299 gadgettracerman  6     0   /etc/localtime
   159  ```
   160  
   161  ## Kubernetes CLI Runtime options
   162  
   163  The Inspektor Gadget `kubectl` plugin uses the [kubernetes
   164  cli-runtime](https://github.com/kubernetes/cli-runtime) helpers. This adds
   165  support for many CLI options that are common to many Kubernetes tools,
   166  which let us specify how to connect to the cluster, which kubeconfig to
   167  use, and so on.
   168  
   169  ```bash
   170    --as string                      Username to impersonate for the operation
   171    --as-group stringArray           Group to impersonate for the operation, this flag can be repeated to specify multiple groups.
   172    --cache-dir string               Default cache directory (default "/home/marga/.kube/cache")
   173    --certificate-authority string   Path to a cert file for the certificate authority
   174    --client-certificate string      Path to a client certificate file for TLS
   175    --client-key string              Path to a client key file for TLS
   176    --cluster string                 The name of the kubeconfig cluster to use
   177    --context string                 The name of the kubeconfig context to use
   178    --insecure-skip-tls-verify       If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure
   179    --kubeconfig string              Path to the kubeconfig file to use for CLI requests.
   180    --request-timeout string         The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests. (default "0")
   181  , --server string                  The address and port of the Kubernetes API server
   182    --tls-server-name string         Server name to use for server certificate validation. If it is not provided, the hostname used to contact the server is used
   183    --token string                   Bearer token for authentication to the API server
   184    --user string                    The name of the kubeconfig user to use
   185  ```
   186  
   187  If none of these options are specified, Inspektor Gadget will connect to the
   188  cluster configured in the default kubeconfig location, with the default
   189  connection options.