如何批量查看容器内部的进程?

脚本如下

#!/usr/bin/env bash
[[ -n $DEBUG ]] && set -x
set -ou pipefail

useage() {
    cat <<HELP
USAGE:
    kces.sh CMDLINE
HELP
}

exit_err() {
    echo >&2 "${1}"
    exit 1
}

if [ $# -lt 1 ]; then
    useage
    exit 1
fi
CMDLINE=$@
kubectl get pod --field-selector=status.phase==Running -o custom-columns='NAME:metadata.name,CONTAINER:spec.containers[*].name' --no-headers | while read -r line;do
    arr=(${line/,/ })
    num=${#arr[@]}
    for ((i=1;i<num;i++));do
        pod="${arr[0]}"
        container="${arr[i]}"
        echo "[${pod}/${container}]"
        kubectl exec "${pod}" -c "${container}" ${CMDLINE}
        echo -e "\n"
    done
done

使用举例

kubens istio-system
kces cat /proc/1/cmdline

使用效果

[istio-citadel-59c5db9b77-bsk9x/citadel]
/usr/local/bin/istio_ca--self-signed-ca--append-dns-names=true--grpc-port=8060--citadel-storage-namespace=istio-system--custom-dns-names=istio-pilot-service-account.istio-system:istio-pilot.istio-system--monitoring-port=15014--self-signed-ca=true--workload-cert-ttl=2160h

[istio-galley-f469f7d4f-x8kv7/galley]
/usr/local/bin/galleyserver--meshConfigFile=/etc/mesh-config/mesh--livenessProbeInterval=1s--livenessProbePath=/healthliveness--readinessProbePath=/healthready--readinessProbeInterval=1s--deployment-namespace=istio-system--insecure=true--validation-webhook-config-file/etc/config/validatingwebhookconfiguration.yaml--monitoringPort=15014--log_output_level=default:info

[istio-ingressgateway-6c89d587bf-5grvl/istio-proxy]
/usr/local/bin/pilot-agentproxyrouter--domainistio-system.svc.cluster.local--log_output_level=default:info--drainDuration45s--parentShutdownDuration1m0s--connectTimeout10s--serviceClusteristio-ingressgateway--zipkinAddressjaeger-collector.istio-system.svc:9411--proxyAdminPort15000--statusPort15020--controlPlaneAuthPolicyNONE--discoveryAddressistio-pilot:15010

[istio-pilot-5bb797c4bb-sjn6n/discovery]
/usr/local/bin/pilot-discoverydiscovery--monitoringAddr=:15014--log_output_level=default:info--domaincluster.local--secureGrpcAddr--keepaliveMaxServerConnectionAge30m

[istio-pilot-5bb797c4bb-sjn6n/istio-proxy]
/usr/local/bin/pilot-agentproxy--domainistio-system.svc.cluster.local--serviceClusteristio-pilot--templateFile/etc/istio/proxy/envoy_pilot.yaml.tmpl--controlPlaneAuthPolicyNONE

[istio-policy-86b448c565-5prs6/mixer]
OCI runtime exec failed: exec failed: container_linux.go:345: starting container process caused "exec: \"cat\": executable file not found in $PATH": unknown
command terminated with exit code 126


[istio-policy-86b448c565-5prs6/istio-proxy]
/usr/local/bin/pilot-agentproxy--domainistio-system.svc.cluster.local--serviceClusteristio-policy--templateFile/etc/istio/proxy/envoy_policy.yaml.tmpl--controlPlaneAuthPolicyNONE

[istio-sidecar-injector-d98dbbb8b-wbshm/sidecar-injector-webhook]
/usr/local/bin/sidecar-injector--caCertFile=/etc/istio/certs/root-cert.pem--tlsCertFile=/etc/istio/certs/cert-chain.pem--tlsKeyFile=/etc/istio/certs/key.pem--injectConfig=/etc/istio/inject/config--meshConfig=/etc/istio/config/mesh--healthCheckInterval=2s--healthCheckFile=/health

[istio-telemetry-676c67ff64-ftd4s/mixer]
OCI runtime exec failed: exec failed: container_linux.go:345: starting container process caused "exec: \"cat\": executable file not found in $PATH": unknown
command terminated with exit code 126


[istio-telemetry-676c67ff64-ftd4s/istio-proxy]
/usr/local/bin/pilot-agentproxy--domainistio-system.svc.cluster.local--serviceClusteristio-telemetry--templateFile/etc/istio/proxy/envoy_telemetry.yaml.tmpl--controlPlaneAuthPolicyNONE

[jaeger-collector-8698b58b55-qdh4z/jaeger-collector]
OCI runtime exec failed: exec failed: container_linux.go:345: starting container process caused "exec: \"cat\": executable file not found in $PATH": unknown
command terminated with exit code 126


[jaeger-operator-66ff476758-v8s7x/jaeger-operator]
/usr/local/bin/jaeger-operatorstart

[jaeger-query-7f9c7c84c-mpshg/jaeger-query]
OCI runtime exec failed: exec failed: container_linux.go:345: starting container process caused "exec: \"cat\": executable file not found in $PATH": unknown
command terminated with exit code 126


[jaeger-query-7f9c7c84c-mpshg/jaeger-agent]
OCI runtime exec failed: exec failed: container_linux.go:345: starting container process caused "exec: \"cat\": executable file not found in $PATH": unknown
command terminated with exit code 126

posted @ 2020-01-03 14:11  futuretea  阅读(958)  评论(0编辑  收藏  举报