Skip to Content
《使用 Kubernetes 实现云原生 DevOps(第二版)》
book

《使用 Kubernetes 实现云原生 DevOps(第二版)》

by Justin Domingus, John Arundel
May 2025
Intermediate to advanced
356 pages
3h 47m
Chinese
O'Reilly Media, Inc.
Content preview from 《使用 Kubernetes 实现云原生 DevOps(第二版)》

第 7 章 Kubernetes 强力工具

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

我的机械师告诉我 "我修不好你的刹车 所以我把你的喇叭弄得更响了"

史蒂文-赖特

人们总是问我们:"这些 Kubernetes 工具怎么样?我需要它们吗?如果需要,是哪些?它们都能做什么?

在本章中,我们将探索帮助您使用 Kubernetes 的工具和实用程序中的一小部分。我们将通过kubectl 向您展示一些高级技术,以及一些有用的实用工具,如jqkubectx/kubenskube-ps1 、 Click 、kubed-sh 、 Stern 和 BusyBox。

掌握 kubectl

第 2 章开始,我们已经认识了kubectl ,由于它是与 Kubernetes 交互的主要工具,你可能已经对基础知识了如指掌。现在我们来看看kubectl 的一些更高级的功能,包括一些对你来说可能很新的技巧和窍门。

外壳别名

为了让生活更轻松,大多数 Kubernetes 用户做的第一件事就是为kubectl 命令创建一个 shell 别名。例如,我们在.bash_profile.zshrc文件中设置了以下别名:

alias k=kubectl

我们不必为每条命令都键入kubectl ,而只需使用k

k get pods

如果您经常使用kubectl 命令,也可以为它们创建别名。下面是一些可能的例子:

alias kg=kubectl get
alias kgdep=kubectl get deployment
alias ksys=kubectl --namespace=kube-system
alias kd=kubectl describe

谷歌工程师艾哈迈德-阿尔普-巴尔坎(Ahmet Alp Balkan)已经建立了一套类似的别名逻辑系统,并创建了一个脚本来为你生成所有别名(目前大约有八百个别名)。

我们建议你从k 开始,然后为你最常用的命令添加你能记住的别名。

使用短标志

与大多数命令行工具一样,kubectl 支持许多标志和开关的缩写形式。这可以节省大量的键入时间。

例如,可以将--namespace 标志缩写为-n (请参阅"使用命名空间")

kubectl get pods -n kube-system

通常情况下,kubectl 使用--selector 标志(参见"标签")对与一组标签匹配的资源进行操作。幸运的是,这可以缩短为-l (labels):

kubectl get pods -l "environment=staging"

资源类型缩写

kubectl 的一个常见用途是列出各种类型的资源,如 Pod、部署、服务和命名空间。通常的做法是使用 ,然后是 等。kubectl get deployments

为了加快速度,kubectl 支持这些资源类型的简写形式:

kubectl get po
kubectl get deploy
kubectl get svc
kubectl get ns

其他有用的缩写包括nonodes,cmconfigmaps,sa 指 。 serviceaccountsds 表示daemonsetspv 表示persistentvolumes

自动完成 kubectl 命令

如果使用bashzsh shell,可以让它们自动完成kubectl 命令。运行此命令可查看如何为 shell 启用自动完成功能的说明: ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

在 Kubernetes 上管理云原生数据

在 Kubernetes 上管理云原生数据

Jeff Carpenter, Patrick McFadin
《Kubernetes 最佳实践》第二版

《Kubernetes 最佳实践》第二版

Brendan Burns, Eddie Villalba, Dave Strebel, Lachlan Evenson
生产 Kubernetes

生产 Kubernetes

Josh Rosso, Rich Lander, Alex Brand, John Harris
Cloud Native DevOps mit Kubernetes

Cloud Native DevOps mit Kubernetes

John Arundel, Justin Domingus

Publisher Resources

ISBN: 9798341659179