K8S命令大全
开始使用命令
一般用kubectl命令来进行管理
命令格式:kubectl【command】【TYPE】【NAME】【flags】
选项和参数:
- commad:对资源具体的操作,如create创建、get获取、delete删除
- TYPE:指定资源类型,大小写敏感
- NAME:指定资源的名称,大小写敏感,如果省略名称则显示所有资源
- flags:指定可选的参数,如可用-s或者-server指定Kubernetes API server的地址和端口
可以使用–help来查看具体的命令,如下所示:
[root@master1 ~]# kubectl --help
kubectl controls the Kubernetes cluster manager.
Find more information at: https://kubernetes.io/docs/reference/kubectl/
Basic Commands (Beginner):
create Create a resource from a file or from stdin
expose Take a replication controller, service, deployment or pod and expose it as a new Kubernetes service
run 在集群上运行特定镜像
set 为对象设置指定特性
Basic Commands (Intermediate):
explain Get documentation for a resource
get 显示一个或多个资源
edit 编辑服务器上的资源
delete Delete resources by file names, stdin, resources and names, or by resources and label selector
Deploy Commands:
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 Management Commands:
certificate 修改证书资源。
cluster-info Display cluster information
top Display resource (CPU/memory) usage
cordon 标记节点为不可调度
uncordon 标记节点为可调度
drain 清空节点以准备维护
taint 更新一个或者多个节点上的污点
Troubleshooting and Debugging Commands:
describe 显示特定资源或资源组的详细信息
logs 打印 Pod 中容器的日志
attach 挂接到一个运行中的容器
exec 在某个容器中执行一个命令
port-forward 将一个或多个本地端口转发到某个 Pod
proxy 运行一个指向 Kubernetes API 服务器的代理
cp Copy files and directories to and from containers
auth Inspect authorization
debug Create debugging sessions for troubleshooting workloads and nodes
Advanced Commands:
diff Diff the live version against a would-be applied version
apply Apply a configuration to a resource by file name or stdin
patch Update fields of a resource
replace Replace a resource by file name or stdin
wait Experimental: Wait for a specific condition on one or many resources
kustomize Build a kustomization target from a directory or URL.
Settings Commands:
label 更新某资源上的标签
annotate 更新一个资源的注解
completion Output shell completion code for the specified shell (bash, zsh, fish, or powershell)
Other Commands:
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 输出客户端和服务端的版本信息
Usage:
kubectl [flags] [options]
Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).
补充:所有的资源对象都可以用空格和斜杠来表达(如下所示)
#查看名为nginx的deployment资源可以有两种命令表达格式
kubectl get deployment nginx
kubectl get deployment/nginx
k8s资源列表和简称
| 资源名称 | 资源简称 |
|---|---|
| pod | po |
| node | no |
| namespace | ns |
| service | svc |
| ingress | ing |
| configmap | cm |
| replicaset | rs |
| deployment | deploy |
| daemonset | ds |
| statefulsets | sts |
| horizontalpodautoscaler | hpa |
| persistentvolume | pv |
| persistentvolumeclaim | pvc |
| componentstatus | cs |
| job | 无 |
| cronjob | cj |
基础常用类命令
这里的可操作资源对象一般有pod、node、namespace、service、ingress、configmap、replicaset、deployment、daemonset、statefulsets、job、cronjob等
get命令
语法:kubectl get【资源对象】【资源对象名】
常见可选参数:
- -A,–all-namespaces:选择所有命名空间下的符合条件的资源对象
- -n,–namespace:指定命令空间
- -o,–output:选择输出的格式(json, yaml, name, go-template, go-template-file, template, templatefile, jsonpath,jsonpath-as-json, jsonpath-file, custom-columns, custom-columns-file, wide)
- –show-labels:显示该资源下的标签
#查看容器信息
kubectl get pod
#查看节点信息
kubectl get node
#查看命名空间信息
kubectl get ns
#也可以多个资源一起查看
kubectl get pod,node,ns,deployment
#。。。。。等等
如果需要查看具体某个资源的信息只需要在后面跟上资源名字即可
create命令
创建Deployment、Service等资源
语法:kubectl create -f【yaml文件】
#指定单个yaml文件创建资源
kubectl create -f deployment.yaml
#使用目录下的所有清单文件来创建资源
kubectl create -f /test
#使用url下的文件创建资源
kubectl create -f https://git.io/test.yaml
run命令
运行某个资源
#运行一个nginx容器
kubectl run nginx --image=nginx
expose命令
将资源暴露为新的Kubernetes Service
语法:kubectl expose【资源对象】【资源对象名】【-f yaml文件】 【–port=port】 【–protocol=TCP|UDP】【–target-port=number-or-name】【–name=name】【–external-ip=external-ip-of-service】【–type=type】
【示例】
#为deployment的nginx创建service,并通过Service的80端口转发至容器的8000端口上
kubectl expose rc nginx --port=80 --target-port=8000
explain命令
查看资源对象的详细情况
语法:kubectl explain【资源对象】【资源对象名】
#查看deployment的详细情况
kubectl explain deployment nginx
edit命令
使用默认编辑器编辑服务器上定义的资源对象
语法:kubectl edit【资源对象】【资源对象名】
#编辑名为nginx的configmap值
kubectl edit configmap nginx
delete命令
删除对象资源
语法:kubectl delete【资源对象】【资源对象名】或kubectl delete -f【yaml文件】
#删除名为nginx的deployment
kubectl delete deployment nginx
#删除指定资源清单的资源对象
kubectl delete -f deployment.yaml
apply命令
过json/yaml文件标准输入对资源进行配置更新或者创建
语法:kubectl apply -f【yaml文件】
#指定单个yaml文件创建资源
kubectl apply -f deployment.yaml
#使用目录下的所有清单文件来创建资源
kubectl apply -f /test
#使用url下的文件创建资源
kubectl apply -f https://git.io/test.yaml
管理操作类命令
set命令
修改资源对象的配置设置特定功能
语法:kubectl set 【image|resources】【-f yaml文件】 【资源对象】【资源对象名】【修改值】
kubectl set image:
#将deployment名为nginx的容器设置为nginx:1.21.3
kubectl set image deployment nginx nginx=nginx:1.21.3
#将deployment、rc、cronjob对象资源名为nginx的容器全部设置为nginx:1.21.3
kubectl set image deployment,rc,cronjob nginx=nginx:1.21.3 --all
kubectl set resources:
#部署niginx容器,cpu限制为200m,内存设置为512Mi
kubectl set resources deployment nginx \
--containers=nginx \
--limits=cpu=200m,memory=512Mi
#为deployment nginx中的所有容器设置资源请求限制
kubectl set resources deployment nginx \
--limits=cpu=200m,memory=512Mi \
--requests=cpu=100m,memory=256Mi
#删除deployment nginx中容器资源的资源请求
kubectl set resources deployment nginx \
--limits=cpu=200m,memory=512Mi \
--requests=cpu=200m,memory=512Mi
scale命令
扩容或者缩容deployment、replicaset等资源对象
语法:kubectl scale【–replicas=】【资源对象】【资源对象名】
#将名为nginx的deployment副本数扩容到3
kubectl scale --replicas=3 deployment nginx
#或者可以用斜杠来表达
kubectl scale --replicas=3 deployment/nginx
autoscale命令
根据pod资源自动伸缩
语法:kubectl autoscale【–min=】【–max=】【–cpu-percent=8】
#使其Pod的数量介于1和5之间,CPU使用率维持在80%
kubectl autoscale deployment nginx --min=2 --max=5 --cpu-percent=80
rollout命令
对资源进行回滚管理
语法:kubectl rollout 【undo】【history】【–to-revision=】
查看版本历史记录
[root@master ~]# kubectl rollout history deployment/nginx
deployment.apps/nginx
REVISION CHANGE-CAUSE
1 <none>
2 <none>
3 <none>
进行回滚操作
#回滚到上一个版本
kubectl rollout undo deployment/nginx
#回滚到revision2这个版本
kubectl rollout undo deployment/nginx --to-revision=2
rolling-update命令
滚动更新指定的ReplicationController
语法:kubectl rolling-update【旧控制器名】【新控制器名】【–image=】|【-f yaml或json文件】
#使用nginx-v2.json中的新RC数据更新nginx-v1的pod
kubectl rolling-update nginx-v1 -f nginx-v2.json
#更新nginx-v1名为nginx-v2,镜像更新成nginx:1.21.3
kubectl rolling-update nginx-v1 nginx-v2 --image=nginx:1.21.3
cp命令
在pod和本地之间copy文件
语法:kubectl cp【源地址】【目的地址】
#拷贝本地test.txt文件到pod名为nginx的/test目录中
kubectl cp test.txt nginx:/test
#相反从pod中拷贝文件出来,需要交换地址
kubectl cp nginx:/test/test.txt test.txt
#拷贝test命名空间的test.txt文件到本地
kubectl cp test/nginx:/test/test.txt test.txt
label命令
给资源对象定义标签
语法:kubectl label【资源对象】【资源对象名】【key=value】
#给名为nginx的deploy定义标签为app=nginx
kubectl label deploy nginx app=nginx
#修改名为nginx的deploy标签
kubectl label deploy nginx app=nginx2 --overwrite
#删除名为nginx的deploy标签
kubectl label deploy nginx app-
故障诊断和调试类命令
logs命令
查看pod日志
#查看容器当前所有日志
kubectl logs nginx-f988b7cbd-9ggrq
#实时查看容器当前日志
kubectl logs -f nginx-f988b7cbd-9ggrq
#查看显示200行的日志
kubectl logs --tail 200 nginx-f988b7cbd-9ggrq
#查看2h之前的日志
kubectl logs --since 2h nginx-f988b7cbd-9ggrq
describe命令
输出指定的一个或多个资源的详细信息
语法:kubectl describe【资源对象】【资源对象名】
#查看名为nginx的deploy详细信息
kubectl describe deploy nginx
exec命令
类似于docker exec命令,控制pod终端
#进入pod内部
kubectl exec -it nginx-f988b7cbd-9ggrq -- /bin/bash
#使容器执行date命令
kubectl exec -it nginx-f988b7cbd-9ggrq -- date
config配置管理命令
用于管理 Kubernetes 集群的配置信息
语法:kubectl config【子选项】【资源对象名】
配置信息管理
# 查看kubeconfig配置文件内容
kubectl config view
# 删除指定配置项
kubectl config unset <property_name>
# 合并配置项
kubectl config view --merge --flatten
# 查看当前Context配置文件内容
kubectl config view --minify --flatten
群集配置管理
# 获取群集列表
kubectl config get-clusters
# 创建或修改设置指定群集信息
kubectl config set-cluster <cluster_name> --server=<api_server_url> --certificate-authority=<ca_cert_path>
# 删除指定群集配置
kubectl config delete-cluster <cluster_name>
上下文Context管理
# 查看所有的context
kubectl config get-contexts
# 创建或者修改设置一个context
kubectl config set-context <context_name>
# 修改context名字
kubectl config rename-context <context_name> <new_name>
# 删除指定context
kubectl config delete-context <context_name>
# 切换使用指定context
kubectl config use-context <context_name>
用户管理
设置创建一个用户凭证
注意:一共有三种方法,不推荐普通用户认证方法,建议用另外两种方法创建
# 普通用户认证
kubectl config set-credentials <user_name> --username=<username> --password=<password>
# 通过私钥证书文件创建
kubectl config set-credentials <user_name> --client-certificate=<cert_path> --client-key=<key_path>
# 通过token创建sa用户
kubectl config set-credentials <user_name> --token=<token_value>
管理用户凭证
# 获取用户列表
kubectl config get-users
# 删除指定用户
kubectl config delete-user <user_name>
实用命令干货
查看服务资源限制和请求申请详情
kubectl get pods -o jsonpath="{range .items[*]}{'服务名称:'}{.metadata.name}{'\n资源限制:'}{.spec.containers[*].resources.limits}{'\n请求申请:'}{.spec.containers[*].resources.requests}{'\n\n'}{end}"
#结果显示如下:
服务名称:proxy-584d44bd4f-lp5gf
资源限制:{"cpu":"500m","memory":"250Mi"}
请求申请:{"cpu":"500m","memory":"250Mi"}
查看cronjob控制器的apiVersion
kubectl get cronjob -o=jsonpath='{range .items[*]}{.metadata.name}{"\t\tVersion版本:"}{.apiVersion}{"\n"}{end}'
#结果显示如下:
cronjob-demo Version版本:batch/v1
导出当前Context上下文的kubeconfig配置文件
kubectl config view --minify --flatten > kubeconfig