深入学习Kubernetes(七):kubectl命令用法

 语法:

kubectl [command] [TYPE] [NAME] [flags]

1 command:子命令,用于操作Kubernetes集群资源对象的命令,如create, delete, describe, get, apply等

2 TYPE:资源对象的类型,如pod, service, rc, deployment, node等,可以单数、复数以及简写(pod, pods, po/service, services, svc)

3 NAME:资源对象的名称,不指定则返回所有,如get pod 会返回所有pod, get pod nginx, 只返回nginx这个pod

4 flags:kubectl子命令的可选参数,例如-n 指定namespace,-s 指定apiserver的URL

资源对象类型列表 可以用这个命令获取到:

[root@marster1 ~]# 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
events                            ev           events.k8s.io                  true         Event
daemonsets                        ds           extensions                     true         DaemonSet
deployments                       deploy       extensions                     true         Deployment
ingresses                         ing          extensions                     true         Ingress
networkpolicies                   netpol       extensions                     true         NetworkPolicy
podsecuritypolicies               psp          extensions                     false        PodSecurityPolicy
replicasets                       rs           extensions                     true         ReplicaSet
nodes                                          metrics.k8s.io                 false        NodeMetrics
pods                                           metrics.k8s.io                 true         PodMetrics
networkpolicies                   netpol       networking.k8s.io              true         NetworkPolicy
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
storageclasses                    sc           storage.k8s.io                 false        StorageClass
volumeattachments                              storage.k8s.io                 false        VolumeAttachment

 

  • 这个命令也可获取到 kubectl explain 

 

特殊用法:

##同时查看多个pod
[root@marster1 ~]# kubectl get pods dz-mysql-77c6c66578-fzdd2  dz-web-648dcfbfc9-68wwf
NAME                        READY     STATUS    RESTARTS   AGE
dz-mysql-77c6c66578-fzdd2   1/1       Running   1          11h
NAME                        READY     STATUS    RESTARTS   AGE
dz-web-648dcfbfc9-68wwf     1/1       Running   1          11h
## 查看pod同时查看deploy
[root@marster1 ~]# kubectl get pods/dz-mysql-77c6c66578-fzdd2 deploy/dz-web 
NAME                            READY     STATUS    RESTARTS   AGE
pod/dz-mysql-77c6c66578-fzdd2   1/1       Running   1          11h

NAME                           DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.extensions/dz-web   1         1         1            1           11h
##同时创建不同资源对象
kubectl create -f pod1.yaml -f rc1.yaml -f service1.yaml

 kubectl子命令:主要包括对资源的创建、删除、查看、修改、配置、运行等

查看kubectl所有子命令:

 

#kubectl --help

 

查看kubectl支持的参数:

#kubectl options

kubectl输出格式:kubectl命令可以用多种格式对结果进行显示,输出格式通过-o参数指定  

-o支持的格式有:

实例:

##能查看到pod在那个node节点上创建
[root@marster1 ~]# kubectl get pod -o wide
NAME                        READY     STATUS             RESTARTS   AGE       IP            NODE            NOMINATED NODE
dz-mysql-77c6c66578-fzdd2   1/1       Running            2          2d        172.20.2.37   192.168.0.112   <none>
[root@marster1 ~]#kubectl get pod -o yaml
[root@marster1 ~]#kubectl get pod -o custom-columns=NAME:.metadata.name,RESC:.metadata.resourceVersion
NAME                        RESC
dz-mysql-77c6c66578-fzdd2   107255
dz-web-648dcfbfc9-68wwf     108898
mysql-665554f76b-27qc8      108698
nginx-6f858d4d45-jpjgg      107030
[root@marster1 ~]#kubectl get pod --sort-by=.metadata.name 
//按name排序
NAME                        READY     STATUS             RESTARTS   AGE
dz-mysql-77c6c66578-fzdd2   1/1       Running            2          2d
dz-web-648dcfbfc9-68wwf     0/1       ImagePullBackOff   1          2d
mysql-665554f76b-27qc8      0/1       CrashLoopBackOff   55         2d
nginx-6f858d4d45-jpjgg      1/1       Running            3          2d

 

kubectl命令示例:

1.创建资源对象

1.1根据yaml文件创建service和deployment

#kubectl create -f my-service.yaml -f my-deploy.yaml

 

1.2可以指定一个目录,这样可以一次性根据该目录下所有yaml或json文件定义资源

#kubectl create -f <directory>

2.查看资源对象

2.1查看所有pod

#kubectl get pods

 

2.2查看deployment和service

#kubectl get deploy,svc

3.描述资源对象

3.1显示node的详细信息

#kubectl describe nodes <node-name>

3.2显示pod的详细信息

#kubectl describe pods/<pod-name>

 

3.3显示deployment管理的pod信息

#kubectl describe pods <deployment-name>

4.删除资源对象

4.1基于yaml文件删除

#kubectl delete -f pod.yaml

4.2删除所有pod

#kubectl delete po --all

 

5.执行容器的命令

5.1在pod中执行某个命令,如date

kubectl exec <pod-name> date //pod-name如果不加,默认会选择第一个pod

5.2指定pod的某个容器执行命令

kubectl exec <pod-name> date

5.3进入到pod的容器里

kubectl exec -it <pod-name> bash

6 查看容器日志

kubectl logs <pod-name>

 

可以动态查看,类似于tail -f

kubectl logs -f <pod-name> -c <container-name>

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

 

  

 

  

 

  

  

 

posted @ 2019-01-20 10:55  学习记事本  阅读(604)  评论(0编辑  收藏  举报