Kubernetes

10 kubectl管理命令详细表:

Linux · 3月19日 · 2020年

kubectl管理命令概要

kubectl命令简写帮组

[root@docker-k8s-master opt]# 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
bgpconfigurations                              crd.projectcalico.org          false        BGPConfiguration
bgppeers                                       crd.projectcalico.org          false        BGPPeer
blockaffinities                                crd.projectcalico.org          false        BlockAffinity
clusterinformations                            crd.projectcalico.org          false        ClusterInformation
felixconfigurations                            crd.projectcalico.org          false        FelixConfiguration
globalnetworkpolicies                          crd.projectcalico.org          false        GlobalNetworkPolicy
globalnetworksets                              crd.projectcalico.org          false        GlobalNetworkSet
hostendpoints                                  crd.projectcalico.org          false        HostEndpoint
ipamblocks                                     crd.projectcalico.org          false        IPAMBlock
ipamconfigs                                    crd.projectcalico.org          false        IPAMConfig
ipamhandles                                    crd.projectcalico.org          false        IPAMHandle
ippools                                        crd.projectcalico.org          false        IPPool
kubecontrollersconfigurations                  crd.projectcalico.org          false        KubeControllersConfiguration
networkpolicies                                crd.projectcalico.org          true         NetworkPolicy
networksets                                    crd.projectcalico.org          true         NetworkSet
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

看集群状

查看master组件状态:

kubectl get cs

查看node节点状态

kuberct get node

查看apiserver代的URL:

kubectl cluster-info

集群详细信息:

kubectl cluster-info dump

查看资源信息:

kubectl describe <资源><名称>

使用Deployment控制器部署镜像

kubectl create deployment java-demo --image=lizhenliang/java-demo
kubectl get deploy,pods

使Service将Pod露出去

kubectl expose deployment java-demo --port=80 --target-port=8080 --type=NodePort 
kubectl get service

示namespaces

[root@docker-k8s-master opt]# kubectl get ns
NAME                   STATUS   AGE
default                Active   27h   默认命名空间,当创建实例不指定命名空间,默认会使用该命名空间
kube-node-lease        Active   27h   k8s内部命名空间
kube-public            Active   27h   公开的命令空间,谁都可以访问
kube-system            Active   27h   k8s系统方面的命令空间

查看namespaces命令空间

[root@docker-k8s-master opt]# kubectl get pod --all-namespaces
NAMESPACE              NAME                                         READY   STATUS    RESTARTS   AGE
default                java-demo-56d54df448-248z4                   1/1     Running   0          168m
default                java-demo-56d54df448-7cx5p                   1/1     Running   0          3h34m
default                java-demo-56d54df448-9tn5s                   1/1     Running   0          29m
default                java-demo-56d54df448-gtcvz                   1/1     Running   0          168m
default                java-demo-56d54df448-vstcj                   1/1     Running   0          29m
default                nginx-84b9b46f96-8gp5j                       1/1     Running   0          24m
default                nginx-84b9b46f96-bhlz7                       1/1     Running   0          24m
default                nginx-84b9b46f96-jwr2r                       1/1     Running   0          95m
default                nginx-84b9b46f96-mcdnd                       1/1     Running   0          24m
default                nginx-84b9b46f96-q5dsv                       1/1     Running   0          24m
kube-system            calico-kube-controllers-97769f7c7-kqvlf      1/1     Running   1          23h
kube-system            calico-node-7kb8r                            1/1     Running   1          23h
kube-system            calico-node-9zbfz                            1/1     Running   1          23h
kube-system            calico-node-g8km5                            1/1     Running   1          23h
kube-system            coredns-6d56c8448f-5prdm                     1/1     Running   1          27h
kube-system            coredns-6d56c8448f-xml8w                     1/1     Running   1          27h
kube-system            etcd-docker-k8s-master                       1/1     Running   1          27h
kube-system            kube-apiserver-docker-k8s-master             1/1     Running   1          27h
kube-system            kube-controller-manager-docker-k8s-master    1/1     Running   0          6h8m
kube-system            kube-proxy-lc2vj                             1/1     Running   1          27h
kube-system            kube-proxy-mf5c6                             1/1     Running   1          26h
kube-system            kube-proxy-wdhzv                             1/1     Running   1          26h
kube-system            kube-scheduler-docker-k8s-master             1/1     Running   1          6h7m
kubernetes-dashboard   dashboard-metrics-scraper-7b59f7d4df-q6rc2   1/1     Running   1          22h
kubernetes-dashboard   kubernetes-dashboard-5dbf55bd9d-qpzgq        1/1     Running   1          22h

-n namespaces 指定查看一个命令空间运行容器

[root@docker-k8s-master opt]# kubectl get pod -n kube-system
NAME                                        READY   STATUS    RESTARTS   AGE
calico-kube-controllers-97769f7c7-kqvlf     1/1     Running   1          23h
calico-node-7kb8r                           1/1     Running   1          23h
calico-node-9zbfz                           1/1     Running   1          23h
calico-node-g8km5                           1/1     Running   1          23h
coredns-6d56c8448f-5prdm                    1/1     Running   1          27h
coredns-6d56c8448f-xml8w                    1/1     Running   1          27h
etcd-docker-k8s-master                      1/1     Running   1          27h
kube-apiserver-docker-k8s-master            1/1     Running   1          27h
kube-controller-manager-docker-k8s-master   1/1     Running   0          6h8m
kube-proxy-lc2vj                            1/1     Running   1          27h
kube-proxy-mf5c6                            1/1     Running   1          26h
kube-proxy-wdhzv                            1/1     Running   1          26h
kube-scheduler-docker-k8s-master            1/1     Running   1          6h8m

&nbsp;

查看pod资源–show-labeles标签

[root@docker-k8s-master opt]# kubectl get pod --show-labels
NAME                         READY   STATUS    RESTARTS   AGE     LABELS
java-demo-56d54df448-248z4   1/1     Running   0          141m    app=java-demo,pod-template-hash=56d54df448
java-demo-56d54df448-7cx5p   1/1     Running   0          3h7m    app=java-demo,pod-template-hash=56d54df448
java-demo-56d54df448-9tn5s   1/1     Running   0          2m21s   app=java-demo,pod-template-hash=56d54df448
java-demo-56d54df448-gtcvz   1/1     Running   0          141m    app=java-demo,pod-template-hash=56d54df448
java-demo-56d54df448-vstcj   1/1     Running   0          2m21s   app=java-demo,pod-template-hash=56d54df448
nginx-84b9b46f96-jwr2r       1/1     Running   0          68m     app=nginx,pod-template-hash=84b9b46f96

根据标签看资源-l

[root@docker-k8s-master opt]# kubectl get pod -l app=java-demo
NAME                         READY   STATUS    RESTARTS   AGE
java-demo-56d54df448-248z4   1/1     Running   0          156m
java-demo-56d54df448-7cx5p   1/1     Running   0          3h22m
java-demo-56d54df448-9tn5s   1/1     Running   0          17m
java-demo-56d54df448-gtcvz   1/1     Running   0          156m
java-demo-56d54df448-vstcj   1/1     Running   0          17m

资源 scale 扩容与缩容

 kubectl scale deployment java-demo --replicas=3
 kubectl scale deployment java-demo --replicas=0

更新

kubectl set image deployment/nginx nginx=nginx:1.15

回滚

kubectl rollout history deployment/nginx
kubectl rollout undu deployment/nginx

 

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

 ;

0 条回应