kubectl get pods --all-namespaces -o custom-columns=NS:.metadata.namespace,NAME:.metadata.name,"CPU(requests)":.spec.containers[*].resources.requests.cpu,"CPU(limits)":.spec.containers[*].resources.limits.cpu,"MEMORY(requests)":.spec.containers[*].resources.requests.memory,"MEMORY(limits)":.spec.containers[*].resources.limits.memory
日志查看
kubectl logs 用于显示 pod 运行中,容器内程序输出到标准输出的内容。跟 docker 的 logs 命令类似。
# Return snapshot logs from pod nginx with only one containerkubectllogsnginx# Return snapshot of previous terminated ruby container logs from pod web-1kubectllogs-p-crubyweb-1# Begin streaming the logs of the ruby container in pod web-1kubectllogs-f-crubyweb-1
注:kubectl 只可以查看单个容器的日志,如果想要同时查看多个 Pod 的日志,可以使用 stern。比如: stern --all-namespaces -l run=nginx。
# Get output from running pod 123456-7890, using the first container by defaultkubectlattach123456-7890# Get output from ruby-container from pod 123456-7890kubectlattach123456-7890-cruby-container# Switch to raw terminal mode, sends stdin to 'bash' in ruby-container from pod 123456-7890# and sends stdout/stderr from 'bash' back to the clientkubectlattach123456-7890-cruby-container-i-tOptions:-c,--container='':Containername.Ifomitted,thefirstcontainerinthepodwillbechosen-i,--stdin=false:Passstdintothecontainer-t,--tty=false:StdinisaTTY
多容器 Pod 可通过 kubectl.kubernetes.io/default-container annotation 配置 kubectl 命令的默认容器。
# Get output from running 'date' from pod 123456-7890, using the first container by defaultkubectlexec123456-7890date# Get output from running 'date' in ruby-container from pod 123456-7890kubectlexec123456-7890-cruby-containerdate# Switch to raw terminal mode, sends stdin to 'bash' in ruby-container from pod 123456-7890# and sends stdout/stderr from 'bash' back to the clientkubectlexec123456-7890-cruby-container-i-t--bash-ilOptions:-c,--container='':Containername.Ifomitted,thefirstcontainerinthepodwillbechosen-p,--pod='':Podname-i,--stdin=false:Passstdintothecontainer-t,--tty=false:StdinisaTT
端口转发
kubectl port-forward 用于将本地端口转发到指定的 Pod。
# Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in the podkubectlport-forwardmypod50006000# Listen on port 8888 locally, forwarding to 5000 in the podkubectlport-forwardmypod8888:5000# Listen on a random port locally, forwarding to 5000 in the podkubectlport-forwardmypod:5000# Listen on a random port locally, forwarding to 5000 in the podkubectlport-forwardmypod0:5000
也可以将本地端口转发到服务、复制控制器或者部署的端口。
# Forward to deploymentkubectlport-forwarddeployment/redis-master6379:6379# Forward to replicaSetkubectlport-forwardrs/redis-master6379:6379# Forward to servicekubectlport-forwardsvc/redis-master6379:6379
# Copy /tmp/foo_dir local directory to /tmp/bar_dir in a remote pod in the default namespacekubectlcp/tmp/foo_dir<some-pod>:/tmp/bar_dir# Copy /tmp/foo local file to /tmp/bar in a remote pod in a specific containerkubectlcp/tmp/foo<some-pod>:/tmp/bar-c<specific-container># Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace <some-namespace>kubectlcp/tmp/foo<some-namespace>/<some-pod>:/tmp/bar# Copy /tmp/foo from a remote pod to /tmp/bar locallykubectlcp<some-namespace>/<some-pod>:/tmp/foo/tmp/barOptions:-c,--container='':Containername.Ifomitted,thefirstcontainerinthepodwillbechosen
注意:文件拷贝依赖于 tar 命令,所以容器中需要能够执行 tar 命令
kubectl drain
kubectldrainNODE [Options]
它会删除该 NODE 上由 ReplicationController, ReplicaSet, DaemonSet, StatefulSet or Job 创建的 Pod
# Check to see if I can create pods in any namespacekubectlauthcan-icreatepods--all-namespaces# Check to see if I can list deployments in my current namespacekubectlauthcan-ilistdeployments.extensions# Check to see if I can do everything in my current namespace ("*" means all)kubectlauthcan-i'*''*'# Check to see if I can get the job named "bar" in namespace "foo"kubectlauthcan-ilistjobs.batch/bar-nfoo
kubectl auth reconcile 自动修复有问题的 RBAC 策略,如
# Reconcile rbac resources from a filekubectlauthreconcile-fmy-rbac-rules.yaml