在K8s集群中创建的任何东西都可以被视为资源:部署(deployment)、Pod、Service等等
直接使用命令去操作k8s的资源,操作简单,只能操作活动对象,无法审计和跟踪,所以通常用于测试
命令语法:kubectl command [type] [name] [flags]command:指定要对资源执行的操作,run、create、get、deletetype:指定资源的类型,deployment、pod、servicename:指定资源的名称(名称大小写敏感)flags:指定额外的可选参数
kubectl --help # 查询所有command
基础命令
create【创建资源】
edit【编辑资源】
set【为对象设置指定特性】
get【显示资源】
delete【删除资源】
运行、调试命令
run【在集群上运行特定镜像】
expose【暴露资源为Service】
describe【 显示特定资源或资源组的详细信息】
explain【显示资源文档】
logs【打印 Pod 中容器的日志】
attach【挂接到一个运行中的容器】
exec【在某个容器中执行一个命令】
port-forward【将一个或多个本地端口转发到某个 Pod】
proxy【运行一个指向 Kubernetes API 服务器的代理】
cp【在Pod内、外复制文件】
auth【检查授权】
debug【创建调试会话以排除工作负载和节点故障】
events【列出事件】
高级命令
apply【根据文件名或stdin创建资源】
replace【根据文件名或stdin替换资源】
patch【更新资源】
diff【区分实时版本与潜在应用版本】
wait【Experimental: Wait for a specific condition on one or many resources】
kustomize【Build a kustomization target from a directory or URL】
设置指令
label【更新某资源上的标签】
annotate【更新一个资源的注解】
completion【输出指定shell(bash、zsh、fish或powershell)的shell完成代码】
部署指令
rollout【Manage the rollout of a resource】
scale【Set a new size for a deployment, replica set, or replication controller】
autoscale【Auto-scale a deployment, replica set, stateful set, or replication controller】
集群管理指令
cluster-info【展示集群信息】
top【Display resource (CPU/memory) usage】
cordon【标记节点为不可调度】
uncordon【标记节点为可调度】
drain【 清空节点以准备维护】
taint【更新一个或者多个节点上的污点】
certificate【修改证书资源】
其它指令
alpha【Commands for features in alpha】
api-resources【Print the supported API resources on the server】
api-versions【Print the supported API versions on the server, in the form of “group/version”】
config【修改 kubeconfig 文件】
plugin【Provides utilities for interacting with plugins】
version【输出客户端和服务端的版本信息】
kubectl api-resources # 查询所有type
集群级别资源
namespaces【命名空间,隔离Pod】
nodes【集群组成部分】
Pod资源
pods【装载容器】
Pod资源控制器
deployments【Pod控制器】
replicationcontrollers
replicasets
daemonsets
statefulsets
controllerrevisions
jobs
cronjobs
horizontalpodautoscalers
服务发现资源
services【统一Pod对外接口】
ingresses【统一Pod对外接口】
ingressclasses
networkpolicies
存储资源
csidrivers
csinodes
csistoragecapacities
storageclasses
volumeattachments
配置资源
configmaps
secrets
其它
bindings
componentstatuses
endpoints
events
limitranges
persistentvolumeclaims
persistentvolumes
podtemplates
resourcequotas
serviceaccounts
localsubjectaccessreviews
selfsubjectaccessreviews
selfsubjectrulesreviews
subjectaccessreviews
clusterrolebindings
clusterroles
rolebindings
roles
mutatingwebhookconfigurations
validatingwebhookconfigurations
理解:通过create命令和配置文件去操作kubernetes的资源
cd /home/lixing && kubectl create -f nginx-pod.yaml # 创建Pod
cd /home/lixing && kubectl get -f nginx-pod.yaml # 查询Pod
cd /home/lixing && kubectl delete -f nginx-pod.yaml # 删除Pod
apiVersion: v1
kind: Pod
metadata: name: nginx-podnamespace: devlabels: env: "dev"version: "1.0"
spec: containers: - image: nginx:1.17.1imagePullPolicy: IfNotPresentname: nginx-containerports:- name: nginx-portcontainerPort: 80protocol: TCP
通过apply命令和配置文件去操作kubernetes的资源。可以同时操作一个目录下的多个配置文件,出现错误难调试。
apply的资源不存在则创建,存在则更新,相当于create和patch的组合
cd /home/lixing && kubectl apply -f nginx-pod.yaml # 创建Pod