|
| 1 | +--- |
| 2 | +title: kubernetes > retrieve container image inventory |
| 3 | +categories: kubernetes |
| 4 | +--- |
| 5 | +We would be using kubectl and then write some Python code to retrieve the list of images of containers |
| 6 | +running in a GKE Kubernetes cluster. Some familiarity with YAML, Python, Kubectl and fundamentals of |
| 7 | +Kubernetes would be essential to understand the post better. |
| 8 | + |
| 9 | +# Details of Pods |
| 10 | +Lets view the list pods in all namespaces. |
| 11 | +``` |
| 12 | +$ kubectl get po -A |
| 13 | +NAMESPACE NAME READY STATUS RESTARTS AGE |
| 14 | +kube-system event-exporter-gke-564fb97f9-6bt5p 2/2 Running 1 13m |
| 15 | +kube-system fluentbit-gke-2mfvq 2/2 Running 0 13m |
| 16 | +kube-system fluentbit-gke-s5cjj 2/2 Running 0 13m |
| 17 | +---TRUNCATED-- |
| 18 | +``` |
| 19 | + |
| 20 | +We can get more information about the pods by viewing their details in yaml format. |
| 21 | +``` |
| 22 | +$ kubectl get po -A -o yaml |
| 23 | +apiVersion: v1 |
| 24 | +items: |
| 25 | +- apiVersion: v1 |
| 26 | + kind: Pod |
| 27 | + metadata: |
| 28 | + annotations: |
| 29 | + components.gke.io/component-name: event-exporter |
| 30 | + components.gke.io/component-version: 1.0.9 |
| 31 | + creationTimestamp: "2021-03-18T11:33:52Z" |
| 32 | + generateName: event-exporter-gke-564fb97f9- |
| 33 | +---TRUNCATED--- |
| 34 | +``` |
| 35 | + |
| 36 | +Let's save this info in a file. |
| 37 | +``` |
| 38 | +$ kubectl get po -A -o yaml > /tmp/details-of-pods.yaml |
| 39 | +
|
| 40 | +$ cat /tmp/details-of-pods.yaml |
| 41 | +apiVersion: v1 |
| 42 | +items: |
| 43 | +- apiVersion: v1 |
| 44 | + kind: Pod |
| 45 | + metadata: |
| 46 | + annotations: |
| 47 | + components.gke.io/component-name: event-exporter |
| 48 | + components.gke.io/component-version: 1.0.9 |
| 49 | + creationTimestamp: "2021-03-18T11:33:52Z" |
| 50 | + generateName: event-exporter-gke-564fb97f9- |
| 51 | +---TRUNCATED--- |
| 52 | +``` |
| 53 | + |
| 54 | +# Retrive the images |
| 55 | +Since we have the required data in a file, we can parse it to retrieve the info we need, in this case |
| 56 | +the image of containers. |
| 57 | + |
| 58 | +Let's get into Python shell, and import the yaml package, that helps us parsing YAML content. |
| 59 | +``` |
| 60 | +$ python3 |
| 61 | +Python 3.8.5 (default, Jan 27 2021, 15:41:15) |
| 62 | +[GCC 9.3.0] on linux |
| 63 | +Type "help", "copyright", "credits" or "license" for more information. |
| 64 | +>>> import yaml |
| 65 | +``` |
| 66 | + |
| 67 | +We are now to going to read the file and create a new object that stores the YAML content as a |
| 68 | +dictionary object. |
| 69 | +``` |
| 70 | +with open('/tmp/details-of-pods.yaml') as f: |
| 71 | + pod_list = yaml.load(f, Loader=yaml.FullLoader) |
| 72 | + print(type(pod_list)) |
| 73 | + # should print <class 'dict'> |
| 74 | +``` |
| 75 | + |
| 76 | +What we now have is a dictionary object and we should be able to parse it using standard dictionary |
| 77 | +methods available in Python. |
| 78 | +``` |
| 79 | +for key in pod_list: |
| 80 | + print(key) |
| 81 | + # should print |
| 82 | + ``` |
| 83 | + apiVersion |
| 84 | + items |
| 85 | + kind |
| 86 | + metadata |
| 87 | + ``` |
| 88 | +``` |
| 89 | + |
| 90 | +There are 4 main keys in the dictionary, and we are interested in items, as that refers to the list of |
| 91 | +Pods. We can iterate over the items list, and each item would be a dictionary again as that refers to |
| 92 | +one complete Pod manifest. |
| 93 | + |
| 94 | +Let's create a file, where we can store the results, we are retreiving. |
| 95 | +``` |
| 96 | +file = open('/tmp/container-img-inventory.csv', 'w') |
| 97 | +file.write('Pod Name\tPod Namespace\tContainer Name\tContainer Image\n') |
| 98 | +``` |
| 99 | +Note that \t referes to tab \n refers to newline character. |
| 100 | + |
| 101 | +``` |
| 102 | +for item in pod_list['items']: |
| 103 | + print(item.keys()) |
| 104 | + # should print |
| 105 | + ``` |
| 106 | + dict_keys(['apiVersion', 'kind', 'metadata', 'spec', 'status']) |
| 107 | + dict_keys(['apiVersion', 'kind', 'metadata', 'spec', 'status']) |
| 108 | + --TRUNCATED |
| 109 | + ``` |
| 110 | +``` |
| 111 | + |
| 112 | +In a Pod manifest, its name would be present in metadata.name, its namespace would be present in |
| 113 | +metadata.namespace, the spec of its containers would be present in spec.containers. Let's check a |
| 114 | +sample Pod manifest for clarity. |
| 115 | +``` |
| 116 | +apiVersion: v1 |
| 117 | + kind: Pod |
| 118 | + metadata: |
| 119 | + ---TRUNCATED--- |
| 120 | + name: event-exporter-gke-564fb97f9-6bt5p |
| 121 | + namespace: kube-system |
| 122 | + --TRUNCATED--- |
| 123 | + spec: |
| 124 | + containers: |
| 125 | + - image: gke.gcr.io/prometheus-to-sd:v0.10.0-gke.0 |
| 126 | + name: prometheus-to-sd-exporter |
| 127 | + ---TRUNCATED--- |
| 128 | + status: |
| 129 | + ---TRUNCATED--- |
| 130 | +``` |
| 131 | + |
| 132 | +Note that spec.containers is a List and each list item i.e. container would have a name and image. |
| 133 | +So the mapping for those names and images in our Pod manifest would be spec.containers[].name and |
| 134 | +spec.containers[].image respectively. |
| 135 | + |
| 136 | +We are now going to get the details discussed above for each Pod. |
| 137 | + |
| 138 | +``` |
| 139 | +for item in pod_list['items']: |
| 140 | + print(item.keys()) |
| 141 | + pod_name = item['metadata']['name'] |
| 142 | + pod_namespace = item['metadata']['namespace'] |
| 143 | + container_list = item['spec']['containers'] |
| 144 | + for container in container_list: |
| 145 | + container_name = container['name'] |
| 146 | + container_image = container['image'] |
| 147 | + file.write(f'{pod_name}\t{pod_namespace}\t{container_name}\t{container_image}\n') |
| 148 | +``` |
| 149 | + |
| 150 | +The file should contain the details that we need. |
| 151 | +``` |
| 152 | +$ head /tmp/container-img-inventory.csv |
| 153 | +Pod Name Pod Namespace Container Name Container Image |
| 154 | +event-exporter-gke-564fb97f9-6bt5p kube-system event-exporter gke.gcr.io/event-exporter:v0.3.4-gke.0 |
| 155 | +event-exporter-gke-564fb97f9-6bt5p kube-system prometheus-to-sd-exporter gke.gcr.io/prometheus-to-sd:v0.10.0-gke.0 |
| 156 | +fluentbit-gke-2mfvq kube-system fluentbit gke.gcr.io/fluent-bit:v1.3.11-gke.0 |
| 157 | +fluentbit-gke-2mfvq kube-system fluentbit-gke gke.gcr.io/fluent-bit-gke-exporter:v0.11.4-gke.0 |
| 158 | +fluentbit-gke-s5cjj kube-system fluentbit gke.gcr.io/fluent-bit:v1.3.11-gke.0 |
| 159 | +fluentbit-gke-s5cjj kube-system fluentbit-gke gke.gcr.io/fluent-bit-gke-exporter:v0.11.4-gke.0 |
| 160 | +fluentbit-gke-xxpgg kube-system fluentbit gke.gcr.io/fluent-bit:v1.3.11-gke.0 |
| 161 | +fluentbit-gke-xxpgg kube-system fluentbit-gke gke.gcr.io/fluent-bit-gke-exporter:v0.11.4-gke.0 |
| 162 | +gke-metrics-agent-9q24j kube-system gke-metrics-agent gke.gcr.io/gke-metrics-agent:0.2.1-gke.0 |
| 163 | +``` |
| 164 | + |
| 165 | +Since the file is in csv format, we could use utilities like Excel or Google sheets to view the |
| 166 | +content in a graphical way. |
| 167 | + |
| 168 | +---end-of-post--- |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | + |
0 commit comments