Tags
Categories
kubectl
Base
create -- 从文件或标准输入中创建资源
get -- 显示一个或多个资源
run -- 在集群中运行一个特定的镜像
expose -- 为pod、deployment或service创建一个新的service
delete -- 删除一个或多个资源
APP Management
apply -- 从文件、标准输入或计算资源中创建或更新配置
annotate -- 更新资源的注释
autoscale -- 自动调整pod的副本数
debug -- 为pod创建一个调试容器
diff -- 显示当前文件和计算资源之间的差异
edit -- 编辑资源
kustomize -- 从kustomization目录中构建配置
label -- 更新资源的标签
patch -- 使用补丁更新资源
replace -- 从文件、标准输入或计算资源中替换资源
rollout -- 管理资源的部署
scale -- 设置资源的副本数
set -- 设置资源的特定字段
wait -- 阻塞,直到资源满足特定条件
Working with Apps
attach -- 连接到pod中的容器
auth -- 检查授权
cp -- 在pod和本地文件系统之间复制文件和目录
describe -- 显示一个或多个资源的详细信息
exec -- 在pod中执行命令
logs -- 打印pod的日志
port-forward -- 将本地端口转发到pod
proxy -- 运行一个代理以访问kubernetes api
top -- 显示集群资源的资源使用情况
Cluster Management
api-versions -- 列出集群支持的api版本
certificate -- 修改证书相关的配置
cluster-info -- 显示集群信息
cordon -- 标记node为不可调度
drain -- 从node上删除pod,并标记node为不可调度
taint -- 更新node的taints
uncordon -- 标记node为可调度
Kuberctl Settings and Usage
alpha -- 用于测试的命令
api-resources -- 打印集群中的资源
completion -- 生成shell自动补全脚本
config -- 修改kubeconfig文件
explain -- 显示资源或数据结构的文档
options -- 打印kubernetes的命令行选项
plugin -- 运行一个kubernetes插件
version -- 打印客户端和服务器的版本信息
Examples
-
显示集群信息:
kubectl cluster-info -
显示集群节点信息:
kubectl get nodes -
显示所有命名空间中的所有资源:
kubectl get all --all-namespaces -
显示特定命名空间中的所有资源:
kubectl get all -n <namespace> -
显示特定资源的详细信息:
kubectl describe <resource_type> <resource_name> -
创建或应用配置文件中定义的资源:
kubectl apply -f <filename.yaml> -
删除资源:
kubectl delete <resource_type> <resource_name> -
获取 Pod 的日志:
kubectl logs <pod_name> -
进入 Pod 的容器:
kubectl exec -it <pod_name> -- /bin/bash -
在运行的 Pod 中执行命令:
kubectl exec <pod_name> -- <command> -
创建一个 Pod:
kubectl run <pod_name> --image=<image_name> -
创建一个 Deployment:
kubectl create deployment <deployment_name> --image=<image_name> -
创建一个 Service:
kubectl expose deployment <deployment_name> --port=<port> --target-port=<target_port>
资源编排
# 控制器定义
apiVersion: v1 # api版本
kind: Pod # 资源类型
metadata: # 元数据
name: nginx # 资源名称
labels: # 标签
app: nginx
spec: # 规格
replicas: 3 # 副本数
selector: # 选择器
matchLabels: # 匹配标签
app: nginx # 标签
template: # 模板
metadata:
labels:
app: nginx
spec:
containers: # 容器
- name: nginx # 容器名称
image: nginx:1.14.2 # 容器镜像
ports: # 容器端口
- containerPort: 80
使用 kubectl 创建资源
-
创建 pod:
kubectl create -f <pod.yaml> -
创建 deployment:
kubectl create -f <deployment.yaml> -
创建 service:
kubectl create -f <service.yaml> -
创建 yaml 文件:
kubectl create deployment <deployment_name> --image=<image_name> --dry-run=client -o yaml > <deployment.yaml> -
export yaml 文件:
kubectl get <resource_type> <resource_name> -o yaml > <resource.yaml>