2.Kubernetes 管理工具 Kubectl

Kubernetes 管理工具 Kubectl

工具版本V1.64

kubectl --help 查看帮助信息

基础命令 Basic Commands

Beginner

命令 描述
create 通过文件名或标准输入创建资源
expose 将一个资源公开为一个新的Service

Intermediate

命令 描述
explain 文档参考资料
get 显示一个或多个资源
edit 使用默认的编辑器编辑一个资源。
delete 通过文件名、标准输入、资源名称或标签选择器来删 除资源。

Kubernetes Service

命令 描述
run 在集群中运行一个特定的镜像
set 在对象上设置特定的功能

部署命令 Deploy Commands

命令 描述
rollout 管理资源的发布
rollingupdate 对给定的复制控制器滚动更新
scale 扩容或缩容Pod数量,Deployment、ReplicaSet、 RC或Job
autoscale 创建一个自动选择扩容或缩容并设置Pod数量

集群管理命令 Cluster Management Commands

命令 描述
certificate 修改证书资源
cluster-info 显示集群信息
top 显示资源(CPU/Memory/Storage)使用。需要 Heapster运行
cordon 标记节点不可调度
uncordon 标记节点可调度
drain 驱逐节点上的应用,准备下线维护
taint 修改节点taint标记

故障诊断和调试命令 Troubleshooting and Debugging Commands

命令 描述
describe 显示特定资源或资源组的详细信息
logs 在一个Pod中打印一个容器日志。如果Pod只有一个容器, 容器名称是可选的
attach 附加到一个运行的容器
exec 执行命令到容器
port-forward 转发一个或多个本地端口到一个pod
proxy 运行一个proxy到Kubernetes API server
cp 拷贝文件或目录到容器中
auth 检查授权

高级命令 Advanced Commands

命令 描述
apply 通过文件名或标准输入对资源应用配置
patch 使用补丁修改、更新资源的字段
replace 通过文件名或标准输入替换一个资源
convert 不同的API版本之间转换配置文件

设置命令 Settings Commands

命令 描述
label 更新资源上的标签
annotate 更新资源上的注释
completion 用于实现kubectl工具自动补全

其他命令 Other Commands

命令 描述
api-resources 打印支持的API资源
api-versions 打印受支持的API版本
config 修改kubeconfig文件(用于访问API,比如配置认证信息)
help 所有命令帮助
plugin 运行一个命令行插件
version 打印客户端和服务版本信息

简单使用示例

升级与回滚

1、创建

kubectl create deployment web --image=nginx
kubectl get deploy,pods

2、发布

kubectl expose deployment web --port=80 --type=NodePort --target-port=80 --name=web
kubectl get service

3、升级

kubectl set image deployment/web nginx=nginx:1.15
kubectl rollout status deployment/nginx-deployment 
# 查看升级状态

4、回滚

kubectl rollout history deployment/web  
# 查看发布记录
kubectl rollout undo deployment/web   
# 回滚最新版本
kubectl rollout undo deployment/web --revision=2 
# 回滚指定版本

5、删除

kubectl delete deploy/web
kubectl delete svc/web

横向伸缩扩展

1、创建

kubectl run myapp --image=ikubernetes/myapp:v1 --replicas=2

2、查看

kubectl get pods -o wide

3、扩展pod

kubectl scale --replicas=5 deployment myapp

4、缩减pod

kubectl scale --replicas=3 deployment myapp

API资源

root@kubeadm-master1:~# kubectl api-resources 
NAME                              SHORTNAMES   APIGROUP                       NAMESPACED   KIND
bindings                                                                      true         Binding
componentstatuses                 cs                                          false        ComponentStatus
configmaps                        cm                                          true         ConfigMap
endpoints                         ep                                          true         Endpoints
events                            ev                                          true         Event
limitranges                       limits                                      true         LimitRange
namespaces                        ns                                          false        Namespace
nodes                             no                                          false        Node
persistentvolumeclaims            pvc                                         true         PersistentVolumeClaim
persistentvolumes                 pv                                          false        PersistentVolume
pods                              po                                          true         Pod
podtemplates                                                                  true         PodTemplate
replicationcontrollers            rc                                          true         ReplicationController
resourcequotas                    quota                                       true         ResourceQuota
secrets                                                                       true         Secret
serviceaccounts                   sa                                          true         ServiceAccount
services                          svc                                         true         Service
mutatingwebhookconfigurations                  admissionregistration.k8s.io   false        MutatingWebhookConfiguration
validatingwebhookconfigurations                admissionregistration.k8s.io   false        ValidatingWebhookConfiguration
customresourcedefinitions         crd,crds     apiextensions.k8s.io           false        CustomResourceDefinition
apiservices                                    apiregistration.k8s.io         false        APIService
controllerrevisions                            apps                           true         ControllerRevision
daemonsets                        ds           apps                           true         DaemonSet
deployments                       deploy       apps                           true         Deployment
replicasets                       rs           apps                           true         ReplicaSet
statefulsets                      sts          apps                           true         StatefulSet
tokenreviews                                   authentication.k8s.io          false        TokenReview
localsubjectaccessreviews                      authorization.k8s.io           true         LocalSubjectAccessReview
selfsubjectaccessreviews                       authorization.k8s.io           false        SelfSubjectAccessReview
selfsubjectrulesreviews                        authorization.k8s.io           false        SelfSubjectRulesReview
subjectaccessreviews                           authorization.k8s.io           false        SubjectAccessReview
horizontalpodautoscalers          hpa          autoscaling                    true         HorizontalPodAutoscaler
cronjobs                          cj           batch                          true         CronJob
jobs                                           batch                          true         Job
certificatesigningrequests        csr          certificates.k8s.io            false        CertificateSigningRequest
leases                                         coordination.k8s.io            true         Lease
endpointslices                                 discovery.k8s.io               true         EndpointSlice
events                            ev           events.k8s.io                  true         Event
ingresses                         ing          extensions                     true         Ingress
ingressclasses                                 networking.k8s.io              false        IngressClass
ingresses                         ing          networking.k8s.io              true         Ingress
networkpolicies                   netpol       networking.k8s.io              true         NetworkPolicy
runtimeclasses                                 node.k8s.io                    false        RuntimeClass
poddisruptionbudgets              pdb          policy                         true         PodDisruptionBudget
podsecuritypolicies               psp          policy                         false        PodSecurityPolicy
clusterrolebindings                            rbac.authorization.k8s.io      false        ClusterRoleBinding
clusterroles                                   rbac.authorization.k8s.io      false        ClusterRole
rolebindings                                   rbac.authorization.k8s.io      true         RoleBinding
roles                                          rbac.authorization.k8s.io      true         Role
priorityclasses                   pc           scheduling.k8s.io              false        PriorityClass
csidrivers                                     storage.k8s.io                 false        CSIDriver
csinodes                                       storage.k8s.io                 false        CSINode
storageclasses                    sc           storage.k8s.io                 false        StorageClass
volumeattachments                              storage.k8s.io                 false        VolumeAttachment
原文地址:https://www.cnblogs.com/Gmiaomiao/p/12943774.html