如何在k8s工作節點上,查看容器對應的pod的名字?


在k8s中,所有的容器,工作負載,最終都是要運行到節點上的,以容器的方式運行

 

那麼,如果要在節點上,查看該節點上運行的容器的pod的名字,pod的信息,該怎麼查看?

 

方法非常的簡單。

 

1、登錄到任意的工作節點

 

2、查看容器的ID

 

4f5de6bafde3就是容器的ID

[email protected]19 ~]# docker ps | grep dev-ssc-ssc 
4f5de6bafde3   reg.yyuap.io:81/yonbip/ssc-ssc          "/usr/bin/dumb-init …"   7 weeks ago    Up 7 weeks              k8s_dev-ssc-ssc_dev-ssc-ssc-78b477b487-z77xz_c87e2267-1001-4c70-bb2a-ab41f3b81aa3_c69a75d3-e339-407c-aa60-424d664dd0da_0
72838c69746a   reg.yyuap.io:81/kubernetes/pause:3.3    "/pause"                 7 weeks ago    Up 7 weeks              k8s_POD_dev-ssc-ssc-78b477b487-z77xz_c87e2267-1001-4c70-bb2a-ab41f3b81aa3_c69a75d3-e339-407c-aa60-424d664dd0da_0
[[email protected]-node-19 ~]# 

 

 

3、查看容器的信息

 

因爲該容器是由k8s調度的,kubelet運行的,所以會有一些K8S相關的信息

 

通過docker inspect 命令來查看

 

docker inspect --format='{{index .Config.Labels "io.kubernetes.pod.name"}}' 4f5de6bafde3

 

 

就可以看到容器的pod的名字

[[email protected]19 ~]# docker inspect --format='{{index .Config.Labels "io.kubernetes.pod.name"}}' 4f5de6bafde3
dev-ssc-ssc-78b477b487-z77xz
[[email protected]-node-19 ~]# 

 

 

當然,還可以看到更多的信息

 

[[email protected]19 ~]# docker inspect 4f5de6bafde3 
[
    {
        "Id": "4f5de6bafde3e490da6740bae386e3b5b6319bf616bffae61c707bf36f9af8fc",
        "Created": "2022-08-01T05:34:59.164476102Z",
        "Path": "/usr/bin/dumb-init",
        "Args": [
            "--",
            "entrypoint.sh",
            "catalina.sh",
            "run"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 1350,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-08-01T05:34:59.403013016Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:a15996757a217a10bfd32f546b33d90cdaea98aee9a483cf7126f81c22aef46a",
        "ResolvConfPath": "/data/docker/containers/72838c69746adc497f5ef11aa32e5a409ffa95ea2ff0298c59ddbe363a8a6e33/resolv.conf",
        "HostnamePath": "/data/docker/containers/72838c69746adc497f5ef11aa32e5a409ffa95ea2ff0298c59ddbe363a8a6e33/hostname",
        "HostsPath": "/var/lib/kubelet/pods/c69a75d3-e339-407c-aa60-424d664dd0da/etc-hosts",
        "LogPath": "/data/docker/containers/4f5de6bafde3e490da6740bae386e3b5b6319bf616bffae61c707bf36f9af8fc/4f5de6bafde3e490da6740bae386e3b5b6319bf616bffae61c707bf36f9af8fc-json.log",
        "Name": "/k8s_dev-ssc-ssc_dev-ssc-ssc-78b477b487-z77xz_c87e2267-1001-4c70-bb2a-ab41f3b81aa3_c69a75d3-e339-407c-aa60-424d664dd0da_0",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "/data/log:/developerMountData/log",
                "/var/lib/kubelet/pods/c69a75d3-e339-407c-aa60-424d664dd0da/volumes/kubernetes.io~secret/default-token-tcdsn:/var/run/secrets/kubernetes.io/serviceaccount:ro",
                "/var/lib/kubelet/pods/c69a75d3-e339-407c-aa60-424d664dd0da/etc-hosts:/etc/hosts",
                "/var/lib/kubelet/pods/c69a75d3-e339-407c-aa60-424d664dd0da/containers/dev-ssc-ssc/96f72e2d:/dev/termination-log"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "container:72838c69746adc497f5ef11aa32e5a409ffa95ea2ff0298c59ddbe363a8a6e33",
            "PortBindings": null,
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": null,
            "DnsOptions": null,
            "DnsSearch": null,
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "container:72838c69746adc497f5ef11aa32e5a409ffa95ea2ff0298c59ddbe363a8a6e33",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": -997,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": [
                "seccomp=unconfined"
            ],
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 1024,
            "Memory": 2147483648,
            "NanoCpus": 0,
            "CgroupParent": "/kubepods/podc69a75d3-e339-407c-aa60-424d664dd0da",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 100000,
            "CpuQuota": 100000,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 2147483648,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/asound",
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/data/docker/overlay2/cd4896984a0be3ca5db5fbe686e7e5ff9d9274572e08e260f2a17c87e85aad6c-init/diff:/data/docker/overlay2/753f011a551637a19e5c7d92ba371e91fbbadb9fe2be5e9b175a0ecc3f7125c4/diff:/data/docker/overlay2/9a791305c782ffa259df2c393c34cba4e0987132e2b53b725676b8d660c9d578/diff:/data/docker/overlay2/66b1d7268215c8a3389af8a7f76d06ed134ebd09c092b5a2c2aa6e52bb33b23d/diff:/data/docker/overlay2/c95263219f555b844da582dd2d92883b2fae8e51998901d6bca34076c521346d/diff:/data/docker/overlay2/75bb3dbf7999568fda45991901df910dbb225d17862d242fc49a5a31095c467d/diff:/data/docker/overlay2/f08c31461a9196402af32aacbed7f620950af56d6ab0693bcd421f4a2d79dadb/diff:/data/docker/overlay2/4d009af171505cd397bc150d3d3228236a0be2c099f08f260493848dc1bea761/diff",
                "MergedDir": "/data/docker/overlay2/cd4896984a0be3ca5db5fbe686e7e5ff9d9274572e08e260f2a17c87e85aad6c/merged",
                "UpperDir": "/data/docker/overlay2/cd4896984a0be3ca5db5fbe686e7e5ff9d9274572e08e260f2a17c87e85aad6c/diff",
                "WorkDir": "/data/docker/overlay2/cd4896984a0be3ca5db5fbe686e7e5ff9d9274572e08e260f2a17c87e85aad6c/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/var/lib/kubelet/pods/c69a75d3-e339-407c-aa60-424d664dd0da/etc-hosts",
                "Destination": "/etc/hosts",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            },
            {
                "Type": "bind",
                "Source": "/var/lib/kubelet/pods/c69a75d3-e339-407c-aa60-424d664dd0da/containers/dev-ssc-ssc/96f72e2d",
                "Destination": "/dev/termination-log",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            },
            {
                "Type": "bind",
                "Source": "/data/log",
                "Destination": "/developerMountData/log",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            },
            {
                "Type": "bind",
                "Source": "/var/lib/kubelet/pods/c69a75d3-e339-407c-aa60-424d664dd0da/volumes/kubernetes.io~secret/default-token-tcdsn",
                "Destination": "/var/run/secrets/kubernetes.io/serviceaccount",
                "Mode": "ro",
                "RW": false,
                "Propagation": "rprivate"
            }
        ],
        "Config": {
            "Hostname": "dev-ssc-ssc-78b477b487-z77xz",
            "Domainname": "",
            "User": "0",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "8888/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "cf_disconf_user_define_download_dir_ncc_common=/",
                "cf_disconf_env_ncc_common=dev",
                "developer_hubble_apm=true",
                "MALLOC_ARENA_MAX=1",
                "LOG_INSTANCE_ID=dev-ssc-ssc-78b477b487-z77xz",
                "iris.serviceUrl.defaultZone=http://172.20.45.214:80/registry-0/eureka/",
                "DNS_SERVER=default",
                "dc_k8s_namespace=c87e2267-1001-4c70-bb2a-ab41f3b81aa3",
                "dc_k8s_terminate_seconds=360",
                "cf_disconf_user_define_config_path_dir_ncc_common={}",
                "FORMAT_MESSAGES_PATTERN_DISABLE_LOOKUPS=true",
                "disconf.conf_server_host=http://172.20.45.214:80/confcenter",
                "cf_disconf_conf_server_host=http://172.20.45.214/confcenter",
                "cf_clientAccessKey=fuaJUwsE69mccrCl",
                "hubble_agent_url=http://172.20.45.214:80/download/hubble-agent.jar",
                "dc_app_env=dev",
                "access.secret=evQeAcSH8jm2UysqVNbsNvDEG4a0C8",
                "INSTANCE_HOST_IP=172.20.45.40",
                "hubble_collect_url=http://172.20.45.214:80",
                "cf_disconf_version=1.0.0",
                "http_read_timeout=1200000",
                "isPremises=true",
                "podIP=172.23.37.7",
                "enable_jmx_monitor=true",
                "PORT=80",
                "dc_hostname=http://172.20.45.214",
                "developer_app_logs=c87e2267-1001-4c70-bb2a-ab41f3b81aa3;dev-ssc-ssc;/usr/local/tomcat/webapps/ROOT/WEB-INF/home/nclogs/",
                "dc_app_name=ssc-ssc",
                "dc_k8s_dns_policy=ClusterFirst",
                "cf_disconf_version_ncc_common=1.0.0",
                "dc_k8s_svc_name=dev-ssc-ssc",
                "dc_app_code=ssc-ssc",
                "HOST=dev-ssc-ssc.prod1.yonyoucloud-k8s.com",
                "JAVA_OPTS=-server -Xms512m -Xmx1792m -Xss8m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=768m -Duser.timezone=GMT+08 -Dfile.encoding=UTF8 -Dsun.jnu.encoding=UTF8 -Djava.awt.headless=true -XX:+DisableExplicitGC -Xbootclasspath/a:/usr/local/tomcat/webapps/ROOT/WEB-INF/home/yonyou-yyy-agent/probes/boot/boot.jar -javaagent:/usr/local/tomcat/webapps/ROOT/WEB-INF/home/yonyou-yyy-agent/probes/yonyou-yyy.jar",
                "cf_disconf_user_define_confing_file=authority.json,cbdegrade.json,moblie_imagescan.xml,strategy.json",
                "cf_disconf_download_order_ncc_common=2",
                "cf_disconf_app=ssc-ssc",
                "mw_profiles_active=dev",
                "cf_disconf_env=dev",
                "access.key=fuaJUwsE69mccrCl",
                "cf_disconf_user_define_download_dir=/",
                "cf_disconf_app_ncc_common=ncc-common",
                "LOG4J_log4j2_formatMsgNoLookups=true",
                "cf_clientAccessSecret=evQeAcSH8jm2UysqVNbsNvDEG4a0C8",
                "cf_disconf_user_define_confing_file_ncc_common=ca1.js,caRegisterCenter.xml,datawork.xml,dbdriverset.xml,dbmerge.properties,esn.properties,eureka.properties,gray.properties,group-mapping.properties,intelliv.properties,iTrusServerConfig.properties,iuap-licenseclient-conf.properties,key.properties,lock-conf.properties,logger-config.properties,macloud.properties,miscellaneous.xml,mq.properties,msconfig-public.properties,nccssoConfig.xml,nlinkpe.properties,nmc.properties,obmconfig.properties,prop.xml,redis-config.xml,safehost.properties,server.xml,shutdown.sh,signature.xml,tenantinfo.properties,ufs.properties,ufs_net.properties",
                "registry=http://172.20.45.214:80",
                "KUBERNETES_PORT_443_TCP_PORT=443",
                "KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1",
                "KUBERNETES_SERVICE_HOST=10.96.0.1",
                "KUBERNETES_SERVICE_PORT=443",
                "KUBERNETES_SERVICE_PORT_HTTPS=443",
                "KUBERNETES_PORT=tcp://10.96.0.1:443",
                "KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443",
                "KUBERNETES_PORT_443_TCP_PROTO=tcp",
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/java/bin:/usr/local/tomcat/lib:/usr/local/tomcat/bin",
                "LANG=zh_CN.UTF-8",
                "LANGUAGE=zh_CN.UTF-8",
                "LC_CTYPE=zh_CN.UTF-8",
                "LC_ALL=zh_CN.UTF-8",
                "TZ=Asia/Shanghai",
                "JAVA_MAJOR=8",
                "WORKDIR=/usr/local/tomcat",
                "BASE_IMAGE=tomcat:9.0.53-jdk8-alpine",
                "TOMCAT_VERSION=9.0.53",
                "JAVA_VERSION=8u202",
                "GLIBC_VERSION=2.33-r0",
                "JAVA_HOME=/usr/local/java",
                "CATALINA_HOME=/usr/local/tomcat",
                "CLASSPATH=/usr/local/java/lib/dt.jar:/usr/local/java/lib/tools.jar"
            ],
            "Cmd": [
                "catalina.sh",
                "run"
            ],
            "Healthcheck": {
                "Test": [
                    "NONE"
                ]
            },
            "Image": "reg.yyuap.io:81/yonbip/[email protected]:a0d5931b540f4c6bc87b0b2b6f4280ec8396ae275a8021f2557620ccb45919e2",
            "Volumes": null,
            "WorkingDir": "/usr/local/tomcat/webapps",
            "Entrypoint": [
                "/usr/bin/dumb-init",
                "--",
                "entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {
                "annotation.io.kubernetes.container.hash": "45e7140c",
                "annotation.io.kubernetes.container.ports": "[{\"containerPort\":8888,\"protocol\":\"TCP\"}]",
                "annotation.io.kubernetes.container.preStopHandler": "{\"exec\":{\"command\":[\"/bin/sh\",\"-c\",\"if [[ -f /bin/offline.sh ]]; then /bin/offline.sh;fi; if [[ -f /bin/shutdown.sh ]]; then /bin/shutdown.sh; fi\"]}}",
                "annotation.io.kubernetes.container.restartCount": "0",
                "annotation.io.kubernetes.container.terminationMessagePath": "/dev/termination-log",
                "annotation.io.kubernetes.container.terminationMessagePolicy": "File",
                "annotation.io.kubernetes.pod.terminationGracePeriod": "360",
                "io.kubernetes.container.logpath": "/var/log/pods/c87e2267-1001-4c70-bb2a-ab41f3b81aa3_dev-ssc-ssc-78b477b487-z77xz_c69a75d3-e339-407c-aa60-424d664dd0da/dev-ssc-ssc/0.log",
                "io.kubernetes.container.name": "dev-ssc-ssc",
                "io.kubernetes.docker.type": "container",
                "io.kubernetes.pod.name": "dev-ssc-ssc-78b477b487-z77xz",
                "io.kubernetes.pod.namespace": "c87e2267-1001-4c70-bb2a-ab41f3b81aa3",
                "io.kubernetes.pod.uid": "c69a75d3-e339-407c-aa60-424d664dd0da",
                "io.kubernetes.sandbox.id": "72838c69746adc497f5ef11aa32e5a409ffa95ea2ff0298c59ddbe363a8a6e33"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {}
        }
    }
]
[[email protected]-node-19 ~]# 
 

 

  • namespace的名字
  • pod的名字
  • prestop腳本
  • 容器的名字

 

很多的k8s給加上去的信息都是可以查看到的!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章