Home > Net >  Python: get OS version of server with kubernetes running (inside containers)
Python: get OS version of server with kubernetes running (inside containers)

Time:06-16

im looking for the OS version(such as Ubuntu 20.04.1 LTS) to get it from container that run on kuberentes server. i mean, i need to OS of the server which on that server i have kubernetes with number of pods(and containers). i saw there is a library which call "kubernetes" but didn't found any relevant info on this specific subject. is there a way to get this info with python? many thanks for the help!

CodePudding user response:

If you need to get an OS version of running container you should read

https://kubernetes.io/docs/tasks/debug/debug-application/get-shell-running-container/

as it described above you can get access to your running pod by command:

kubectl exec --stdin --tty <pod_name> -- /bin/bash

then just type "cat /etc/os-release" and you will see the OS info which your pod running on. In most cases containers run on unix systems and you will find current pod OS.

You also can install python or anything else inside your pod. But I do not recommend to do it. Containers have minimum thing to make you app work. For checking it is ok, but after it just deploy new container.

CodePudding user response:

Option-1: Using the node info on which pod is running via kubectl. In the below command, replace the <PODNAME> with your pod name.

kubectl get node $(kubectl get pod <PODNAME>  -o jsonpath='{.spec.nodeName}') -o jsonpath='{.status.nodeInfo.osImage}'

Option-2: using /proc/version from within the container.

My pod simple-app is running on kube-worker-1 node.

kubectl get pod simple-app  -o wide
NAME         READY   STATUS    RESTARTS        AGE     IP             NODE            NOMINATED NODE   READINESS GATES
simple-app   1/1     Running   6 (4h12m ago)   6d23h   10.233.78.38   kube-worker-1   <none>           <none>

Here is the os of the container:(alpine) , that you are not interested.

kubectl exec  -it simple-app  -- cat  /etc/os-release
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.12.0
PRETTY_NAME="Alpine Linux v3.12"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://bugs.alpinelinux.org/"

Here is the os info from the node.(which you are interested but not via SSH) This output is for explanation only.

ssh ps@kube-worker-1  "cat /etc/os-release"
NAME="Ubuntu"
VERSION="20.04.3 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.3 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

Here is the os info fetched(of the host node running the container) form within the pod. NOw compare the three outputs.

kubectl exec  -it simple-app  -- cat  /proc/version
Linux version 5.4.0-117-generic (buildd@lcy02-amd64-006) (gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)) #132-Ubuntu SMP Thu Jun 2 00:39:06 UTC 2022
  • Related