我的编程空间,编程开发者的网络收藏夹
学习永远不晚

云原生之kubectl命令详解

短信预约 -IT技能 免费直播动态提醒
省份

北京

  • 北京
  • 上海
  • 天津
  • 重庆
  • 河北
  • 山东
  • 辽宁
  • 黑龙江
  • 吉林
  • 甘肃
  • 青海
  • 河南
  • 江苏
  • 湖北
  • 湖南
  • 江西
  • 浙江
  • 广东
  • 云南
  • 福建
  • 海南
  • 山西
  • 四川
  • 陕西
  • 贵州
  • 安徽
  • 广西
  • 内蒙
  • 西藏
  • 新疆
  • 宁夏
  • 兵团
手机号立即预约

请填写图片验证码后获取短信验证码

看不清楚,换张图片

免费获取短信验证码

云原生之kubectl命令详解

目录

1、查看版本信息:kubectl version

2、查看资源对象简写(缩写):kubectl api-resources

3、查看集群信息:kubectl cluster-info

4、查看帮助信息:kubectl --help

5、node节点日志查看:journalctl -u kubelet -f

6、获取一个或多个资源信息:kubectl get 

6.1、查看所有命名空间运行的pod信息: kubectl get pods -A

6.2、查看所有命名空间运行的pod详细信息: kubectl get pods -A -o wide

6.3、 查看所有资源对象:kubectl get all -A

6.4、查看node节点上的标签:kube get nodes --show-labels 

6.5、查看pod节点上的标签:kubectl get pods --show-labels -A

6.6、查看节点状态信息:kubectl get cs

6.7、查看命名空间:kubectl get namespaces

7、创建命名空间 :kubectl create ns app

8、删除命名空间:kubectl delete ns ceshi

9、在命名空间kube-public创建无状态控制器deployment来启动pod,暴露80端口,副本集为3

11、暴露发布pod中的服务供用户访问

12、删除pod中的nginx服务及service

13、查看endpoint的信息

14、修改/更新(镜像、参数......) kubectl set 

 15、调整副本集的数量:kubectl scale

16、查看详细信息:kubectl describe

16.1、显示所有的nodes的详细信息:kubectl describe nodes

16.2、查看某个node的详细信息

16.3、显示所有Pod的详细信息:kubectl describe pods

16.4、显示一个pod的详细信息 :kubectl describe deploy/nginx

16.5、显示所有svc详细信息:kubectl describe svc

16.6、显示指定svc的详细信息:kubectl describe svc nginx-service

16.7、显示所有namespace的详细信息:kubectl describe ns

16.8、显示指定namespace的详细信息:kubectl describe ns kube-public


1、查看版本信息:kubectl version

[root@master ~]# kubectl version

[root@master ~]# kubectl versionClient Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.3", GitCommit:"ca643a4d1f7bfe34773c74f79527be4afd95bf39", GitTreeState:"clean", BuildDate:"2021-07-15T21:04:39Z", GoVersion:"go1.16.6", Compiler:"gc", Platform:"linux/amd64"}Server Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.3", GitCommit:"ca643a4d1f7bfe34773c74f79527be4afd95bf39", GitTreeState:"clean", BuildDate:"2021-07-15T20:59:07Z", GoVersion:"go1.16.6", Compiler:"gc", Platform:"linux/amd64"}[root@master ~]# kubectl 

2、查看资源对象简写(缩写):kubectl api-resources

[root@master ~]# kubectl api-resources

Client Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.3", GitCommit:"ca643a4d1f7bfe34773c74f79527be4afd95bf39", GitTreeState:"clean", BuildDate:"2021-07-15T21:04:39Z", GoVersion:"go1.16.6", Compiler:"gc", Platform:"linux/amd64"}Server Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.3", GitCommit:"ca643a4d1f7bfe34773c74f79527be4afd95bf39", GitTreeState:"clean", BuildDate:"2021-07-15T20:59:07Z", GoVersion:"go1.16.6", Compiler:"gc", Platform:"linux/amd64"}[root@master ~]# kubectl api-resourcesNAME  SHORTNAMES   APIVERSION NAMESPACED   KINDbindings           v1         true         Bindingcomponentstatuses                 cs           v1         false        ComponentStatusconfigmaps                        cm           v1         true         ConfigMapendpoints                         ep           v1         true         Endpointseventsev           v1         true         Eventlimitranges                       limits       v1         true         LimitRangenamespaces                        ns           v1         false        Namespacenodes no           v1         false        Nodepersistentvolumeclaims            pvc          v1         true         PersistentVolumeClaimpersistentvolumes                 pv           v1         false        PersistentVolumepods  po           v1         true         Podpodtemplates       v1         true         PodTemplatereplicationcontrollers            rc           v1         true         ReplicationControllerresourcequotas                    quota        v1         true         ResourceQuotasecrets            v1         true         Secretserviceaccounts                   sa           v1         true         ServiceAccountservices                          svc          v1         true         Servicemutatingwebhookconfigurations                  admissionregistration.k8s.io/v1        false        MutatingWebhookConfigurationvalidatingwebhookconfigurations                admissionregistration.k8s.io/v1        false        ValidatingWebhookConfigurationcustomresourcedefinitions         crd,crds     apiextensions.k8s.io/v1                false        CustomResourceDefinitionapiservices        apiregistration.k8s.io/v1              false        APIServicecontrollerrevisionsapps/v1    true         ControllerRevisiondaemonsets                        ds           apps/v1    true         DaemonSetdeployments                       deploy       apps/v1    true         Deploymentreplicasets                       rs           apps/v1    true         ReplicaSetstatefulsets                      sts          apps/v1    true         StatefulSettokenreviews       authentication.k8s.io/v1               false        TokenReviewlocalsubjectaccessreviews                      authorization.k8s.io/v1                true         LocalSubjectAccessReviewselfsubjectaccessreviews                       authorization.k8s.io/v1                false        SelfSubjectAccessReviewselfsubjectrulesreviews                        authorization.k8s.io/v1                false        SelfSubjectRulesReviewsubjectaccessreviews                           authorization.k8s.io/v1                false        SubjectAccessReviewhorizontalpodautoscalers          hpa          autoscaling/v1                         true         HorizontalPodAutoscalercronjobs                          cj           batch/v1   true         CronJobjobs               batch/v1   true         Jobcertificatesigningrequests        csr          certificates.k8s.io/v1                 false        CertificateSigningRequestleases             coordination.k8s.io/v1                 true         Leaseendpointslices     discovery.k8s.io/v1                    true         EndpointSliceeventsev           events.k8s.io/v1                       true         Eventingresses                         ing          extensions/v1beta1                     true         Ingressflowschemas        flowcontrol.apiserver.k8s.io/v1beta1   false        FlowSchemaprioritylevelconfigurations                    flowcontrol.apiserver.k8s.io/v1beta1   false        PriorityLevelConfigurationingressclasses     networking.k8s.io/v1                   false        IngressClassingresses                         ing          networking.k8s.io/v1                   true         Ingressnetworkpolicies                   netpol       networking.k8s.io/v1                   true         NetworkPolicyruntimeclasses     node.k8s.io/v1                         false        RuntimeClasspoddisruptionbudgets              pdb          policy/v1  true         PodDisruptionBudgetpodsecuritypolicies               psp          policy/v1beta1                         false        PodSecurityPolicyclusterrolebindingsrbac.authorization.k8s.io/v1           false        ClusterRoleBindingclusterroles       rbac.authorization.k8s.io/v1           false        ClusterRolerolebindings       rbac.authorization.k8s.io/v1           true         RoleBindingroles              rbac.authorization.k8s.io/v1           true         Rolepriorityclasses                   pc           scheduling.k8s.io/v1                   false        PriorityClasscsidrivers         storage.k8s.io/v1                      false        CSIDrivercsinodes           storage.k8s.io/v1                      false        CSINodecsistoragecapacities                           storage.k8s.io/v1beta1                 true         CSIStorageCapacitystorageclasses                    sc           storage.k8s.io/v1                      false        StorageClassvolumeattachments  storage.k8s.io/v1                      false        VolumeAttachment

3、查看集群信息:kubectl cluster-info

[root@master ~]# kubectl cluster-info

Kubernetes control plane is running at https://192.168.159.10:6443CoreDNS is running at https://192.168.159.10:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxyTo further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

4、查看帮助信息:kubectl --help

[root@master ~]# kubectl --help

kubectl controls the Kubernetes cluster manager. Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/Basic Commands (Beginner):  create        Create a resource from a file or from stdin.  expose        使用 replication controller, service, deployment 或者 pod 并暴露它作为一个 新的 KubernetesService  run           在集群中运行一个指定的镜像  set           为 objects 设置一个指定的特征Basic Commands (Intermediate):  explain       查看资源的文档  get           显示一个或更多 resources  edit          在服务器上编辑一个资源  delete        Delete resources by filenames, stdin, resources and names, or by resources and label selectorDeploy Commands:  rollout       Manage the rollout of a resource  scale         Set a new size for a Deployment, ReplicaSet or Replication Controller  autoscale     Auto-scale a Deployment, ReplicaSet, StatefulSet, or ReplicationControllerCluster Management Commands:  certificate   修改 certificate 资源.  cluster-info  显示集群信息  top           显示 Resource (CPU/Memory) 使用.  cordon        标记 node 为 unschedulable  uncordon      标记 node 为 schedulable  drain         Drain node in preparation for maintenance  taint         更新一个或者多个 node 上的 taintsTroubleshooting and Debugging Commands:  describe      显示一个指定 resource 或者 group 的 resources 详情  logs          输出容器在 pod 中的日志  attach        Attach 到一个运行中的 container  exec          在一个 container 中执行一个命令  port-forward  Forward one or more local ports to a pod  proxy         运行一个 proxy 到 Kubernetes API server  cp            复制 files 和 directories 到 containers 和从容器中复制 files 和 directories.  auth          Inspect authorization  debug         Create debugging sessions for troubleshooting workloads and nodesAdvanced Commands:  diff          Diff live version against would-be applied version  apply         通过文件名或标准输入流(stdin)对资源进行配置  patch         Update field(s) of a resource  replace       通过 filename 或者 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         更新在这个资源上的 labels  annotate      更新一个资源的注解  completion    Output shell completion code for the specified shell (bash or zsh)Other Commands:  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       输出 client 和 server 的版本信息Usage:  kubectl [flags] [options]Use "kubectl  --help" for more information about a given command.Use "kubectl options" for a list of global command-line options (applies to all commands).

官方帮助信息:Kubernetes kubectl 命令表 _ Kubernetes(K8S)中文文档_Kubernetes中文社区http://docs.kubernetes.org.cn/683.html

5、node节点日志查看:journalctl -u kubelet -f

-- Logs begin at 三 2022-11-02 02:24:50 CST. --11月 01 19:51:04 master kubelet[13641]: I1101 19:51:04.415651   13641 pod_container_deletor.go:79] "Container not found in pod's containers" containerID="041b38b5bb2ff0161170bea161fd70e9175cc27fdc98877944d899ebe7b90d2f"11月 01 19:51:06 master kubelet[13641]: I1101 19:51:06.428325   13641 reconciler.go:196] "operationExecutor.UnmountVolume started for volume \"kubeconfig\" (UniqueName: \"kubernetes.io/host-path/771ef2517500c43b40e7df4c76198cac-kubeconfig\") pod \"771ef2517500c43b40e7df4c76198cac\" (UID: \"771ef2517500c43b40e7df4c76198cac\") "11月 01 19:51:06 master kubelet[13641]: I1101 19:51:06.428370   13641 operation_generator.go:829] UnmountVolume.TearDown succeeded for volume "kubernetes.io/host-path/771ef2517500c43b40e7df4c76198cac-kubeconfig" (OuterVolumeSpecName: "kubeconfig") pod "771ef2517500c43b40e7df4c76198cac" (UID: "771ef2517500c43b40e7df4c76198cac"). InnerVolumeSpecName "kubeconfig". PluginName "kubernetes.io/host-path", VolumeGidValue ""11月 01 19:51:06 master kubelet[13641]: I1101 19:51:06.529163   13641 reconciler.go:319] "Volume detached for volume \"kubeconfig\" (UniqueName: \"kubernetes.io/host-path/771ef2517500c43b40e7df4c76198cac-kubeconfig\") on node \"master\" DevicePath \"\""11月 01 19:51:07 master kubelet[13641]: I1101 19:51:07.282148   13641 kubelet_getters.go:300] "Path does not exist" path="/var/lib/kubelet/pods/771ef2517500c43b40e7df4c76198cac/volumes"11月 01 19:51:10 master kubelet[13641]: I1101 19:51:10.913108   13641 topology_manager.go:187] "Topology Admit Handler"11月 01 19:51:11 master kubelet[13641]: I1101 19:51:11.079185   13641 reconciler.go:224] "operationExecutor.VerifyControllerAttachedVolume started for volume \"kubeconfig\" (UniqueName: \"kubernetes.io/host-path/5e72c0f5a18f84d50f027106c98ab6b1-kubeconfig\") pod \"kube-scheduler-master\" (UID: \"5e72c0f5a18f84d50f027106c98ab6b1\") "11月 01 19:51:15 master kubelet[13641]: E1101 19:51:15.849398   13641 cadvisor_stats_provider.go:151] "Unable to fetch pod etc hosts stats" err="failed to get stats failed command 'du' ($ nice -n 19 du -x -s -B 1) on path /var/lib/kubelet/pods/771ef2517500c43b40e7df4c76198cac/etc-hosts with error exit status 1" pod="kube-system/kube-scheduler-master"11月 01 19:51:25 master kubelet[13641]: E1101 19:51:25.874999   13641 cadvisor_stats_provider.go:151] "Unable to fetch pod etc hosts stats" err="failed to get stats failed command 'du' ($ nice -n 19 du -x -s -B 1) on path /var/lib/kubelet/pods/771ef2517500c43b40e7df4c76198cac/etc-hosts with error exit status 1" pod="kube-system/kube-scheduler-master"11月 01 19:51:31 master kubelet[13641]: I1101 19:51:31.

k8s中查看核心组件日志怎么看

①、通过kubeadm部署的:kubectl logs -f pod_组件名 -n namespace

                                             或者 journalctl -u kubelet -f

②、二进制部署的:journalctl -u kubelet -f

6、获取一个或多个资源信息:kubectl get 

语法格式:

kubectl get [-o wide | json| yaml] [-n namespace]

注释:

resource:可以是具体资源名称

-o :指定输出格式

-n :指定名称空间

6.1、查看所有命名空间运行的pod信息: kubectl get pods -A

[root@master ~]# kubectl get pods -A

NAMESPACE      NAME READY   STATUS    RESTARTS   AGEkube-flannel   kube-flannel-ds-7clld            1/1     Running   0          5h35mkube-flannel   kube-flannel-ds-psgvb            1/1     Running   0          5h35mkube-flannel   kube-flannel-ds-xxncr            1/1     Running   0          5h35mkube-system    coredns-6f6b8cc4f6-lbvl5         1/1     Running   0          5h45mkube-system    coredns-6f6b8cc4f6-m6brz         1/1     Running   0          5h45mkube-system    etcd-master                      1/1     Running   0          5h45mkube-system    kube-apiserver-master            1/1     Running   0          5h45mkube-system    kube-controller-manager-master   1/1     Running   0          5h11mkube-system    kube-proxy-jwpnz                 1/1     Running   0          5h40mkube-system    kube-proxy-xqcqm                 1/1     Running   0          5h41mkube-system    kube-proxy-z6rhl                 1/1     Running   0          5h45mkube-system    kube-scheduler-master            1/1     Running   0          5h11m

6.2、查看所有命名空间运行的pod详细信息: kubectl get pods -A -o wide

[root@master ~]# kubectl get pods -A -o wide

NAMESPACE      NAME READY   STATUS    RESTARTS   AGE     IP               NODE     NOMINATED NODE   READINESS GATESkube-flannel   kube-flannel-ds-7clld            1/1     Running   0          5h39m   192.168.159.13   node02              kube-flannel   kube-flannel-ds-psgvb            1/1     Running   0          5h39m   192.168.159.11   node01              kube-flannel   kube-flannel-ds-xxncr            1/1     Running   0          5h39m   192.168.159.10   master              kube-system    coredns-6f6b8cc4f6-lbvl5         1/1     Running   0          5h49m   10.150.2.2       node02              kube-system    coredns-6f6b8cc4f6-m6brz         1/1     Running   0          5h49m   10.150.1.2       node01              kube-system    etcd-master                      1/1     Running   0          5h49m   192.168.159.10   master              kube-system    kube-apiserver-master            1/1     Running   0          5h49m   192.168.159.10   master              kube-system    kube-controller-manager-master   1/1     Running   0          5h16m   192.168.159.10   master              kube-system    kube-proxy-jwpnz                 1/1     Running   0          5h45m   192.168.159.13   node02              kube-system    kube-proxy-xqcqm                 1/1     Running   0          5h45m   192.168.159.11   node01              kube-system    kube-proxy-z6rhl                 1/1     Running   0          5h49m   192.168.159.10   master              kube-system    kube-scheduler-master            1/1     Running   0          5h15m   192.168.159.10   master              

6.3、 查看所有资源对象:kubectl get all -A

[root@master ~]# kubectl get all -A

NAMESPACE      NAME     READY   STATUS    RESTARTS   AGEkube-flannel   pod/kube-flannel-ds-7whbw            1/1     Running   0          5h14mkube-flannel   pod/kube-flannel-ds-nj4vl            1/1     Running   0          5h14mkube-flannel   pod/kube-flannel-ds-w55x5            1/1     Running   0          5h14mkube-system    pod/coredns-6f6b8cc4f6-lg6hc         1/1     Running   0          5h20mkube-system    pod/coredns-6f6b8cc4f6-tdwhx         1/1     Running   0          5h20mkube-system    pod/etcd-master                      1/1     Running   0          5h20mkube-system    pod/kube-apiserver-master            1/1     Running   0          5h20mkube-system    pod/kube-controller-manager-master   1/1     Running   0          5h13mkube-system    pod/kube-proxy-gv58r                 1/1     Running   0          5h20mkube-system    pod/kube-proxy-xd4lz                 1/1     Running   0          5h18mkube-system    pod/kube-proxy-zzs2s                 1/1     Running   0          5h18mkube-system    pod/kube-scheduler-master            1/1     Running   0          5h12mNAMESPACE     NAME                    TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                  AGEdefault       service/kubernetes      ClusterIP   10.125.0.1             443/TCP                  5h20mdefault       service/nginx-service   NodePort    10.125.18.84           80:32476/TCP             5h3mkube-system   service/kube-dns        ClusterIP   10.125.0.10            53/UDP,53/TCP,9153/TCP   5h20mNAMESPACE      NAME DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR            AGEkube-flannel   daemonset.apps/kube-flannel-ds   3         3         3       3            3                              5h14mkube-system    daemonset.apps/kube-proxy        3         3         3       3            3           kubernetes.io/os=linux   5h20mNAMESPACE     NAME                      READY   UP-TO-DATE   AVAILABLE   AGEkube-system   deployment.apps/coredns   2/2     2            2           5h20mNAMESPACE     NAME     DESIRED   CURRENT   READY   AGEkube-system   replicaset.apps/coredns-6f6b8cc4f6   2         2         2       5h20m

6.4、查看node节点上的标签:kube get nodes --show-labels 

[root@master ~]# kubectl get nodes --show-labels

NAME     STATUS   ROLES                  AGE     VERSION   LABELSmaster   Ready    control-plane,master   5h58m   v1.21.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=node01   Ready    node                   5h53m   v1.21.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node01,kubernetes.io/os=linux,node-role.kubernetes.io/node=nodenode02   Ready    node                   5h53m   v1.21.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node02,kubernetes.io/os=linux,node-role.kubernetes.io/node=node

6.5、查看pod节点上的标签:kubectl get pods --show-labels -A

[root@master ~]# kubectl get pods --show-labels -A

[root@master ~]# kubectl get pods --show-labels -ANAMESPACE      NAME READY   STATUS    RESTARTS   AGE     LABELSkube-flannel   kube-flannel-ds-7clld            1/1     Running   0          5h51m   app=flannel,controller-revision-hash=5b775b5b5c,pod-template-generation=1,tier=nodekube-flannel   kube-flannel-ds-psgvb            1/1     Running   0          5h51m   app=flannel,controller-revision-hash=5b775b5b5c,pod-template-generation=1,tier=nodekube-flannel   kube-flannel-ds-xxncr            1/1     Running   0          5h51m   app=flannel,controller-revision-hash=5b775b5b5c,pod-template-generation=1,tier=nodekube-system    coredns-6f6b8cc4f6-lbvl5         1/1     Running   0          6h1m    k8s-app=kube-dns,pod-template-hash=6f6b8cc4f6kube-system    coredns-6f6b8cc4f6-m6brz         1/1     Running   0          6h1m    k8s-app=kube-dns,pod-template-hash=6f6b8cc4f6kube-system    etcd-master                      1/1     Running   0          6h1m    component=etcd,tier=control-planekube-system    kube-apiserver-master            1/1     Running   0          6h1m    component=kube-apiserver,tier=control-planekube-system    kube-controller-manager-master   1/1     Running   0          5h27m   component=kube-controller-manager,tier=control-planekube-system    kube-proxy-jwpnz                 1/1     Running   0          5h56m   controller-revision-hash=6b87fcb57c,k8s-app=kube-proxy,pod-template-generation=1kube-system    kube-proxy-xqcqm                 1/1     Running   0          5h56m   controller-revision-hash=6b87fcb57c,k8s-app=kube-proxy,pod-template-generation=1kube-system    kube-proxy-z6rhl                 1/1     Running   0          6h1m    controller-revision-hash=6b87fcb57c,k8s-app=kube-proxy,pod-template-generation=1kube-system    kube-scheduler-master            1/1     Running   0          5h26m   component=kube-scheduler,tier=control-plane

6.6、查看节点状态信息:kubectl get cs

[root@master ~]# kubectl get cs

Warning: v1 ComponentStatus is deprecated in v1.19+NAME                 STATUS    MESSAGE             ERRORscheduler            Healthy   ok                  controller-manager   Healthy   ok                  etcd-0               Healthy   {"health":"true"}  

6.7、查看命名空间:kubectl get namespaces

或者使用缩写:[root@master ~]# kubectl get ns

[root@master ~]# kubectl  get namespaceNAME              STATUS   AGEdefault           Active   6h8mkube-flannel      Active   5h58mkube-node-lease   Active   6h8mkube-public       Active   6h8mkube-system       Active   6h8m

7、创建命名空间 :kubectl create ns app

[root@master ~]# kubectl create ns ceshinamespace/ceshi created

 

8、删除命名空间:kubectl delete ns ceshi

[root@master ~]# kubectl delete ns ceshinamespace "ceshi" deleted

9、在命名空间kube-public创建无状态控制器deployment来启动pod,暴露80端口,副本集为3

在kube-public命名空间创建一个nginx

[root@master ~]# kubectl create deployment nginx --image=nginx:1.15 --port=80 --replicas=3 -n kube-public

[root@master ~]# kubectl create deployment nginx --image=nginx:1.15 --port=80 --replicas=3 -n kube-publicdeployment.apps/nginx created

11、暴露发布pod中的服务供用户访问

[root@master ~]# kubectl expose deployment nginx --port=80 --target-port=80 --name=nginx-service --type=NodePort -n kube-public

访问:

 

 

12、删除pod中的nginx服务及service

[root@master ~]# kubectl delete deployment nginx -n kube-public

[root@master ~]# kubectl delete svc -n kube-public nginx-service 

 

13、查看endpoint的信息

[root@master ~]# kubectl get endpoints

14、修改/更新(镜像、参数......) kubectl set 

例如:查看nginx的版本号:

需求:修改这个运行中的nginx的版本号

[root@master ~]# kubectl set image deployment/nginx nginx=nginx:1.11

 

过程中,他会先下载新的镜像进行创建,启动后删除老版本的容器 

 15、调整副本集的数量:kubectl scale

[root@master ~]# kubectl scale deployment nginx --replicas=5 -n default
deployment.apps/nginx scaled       ## -n 指定namespace,本服务就创建在默认namespace中

16、查看详细信息:kubectl describe

16.1、显示所有的nodes的详细信息:kubectl describe nodes

[root@master ~]# kubectl describe nodes

16.2、查看某个node的详细信息

 [root@master ~]# kubectl describe nodes node01

Name:               node01Roles:              nodeLabels:             beta.kubernetes.io/arch=amd64                    beta.kubernetes.io/os=linux                    kubernetes.io/arch=amd64                    kubernetes.io/hostname=node01                    kubernetes.io/os=linux                    node-role.kubernetes.io/node=nodeAnnotations:        flannel.alpha.coreos.com/backend-data: {"VNI":1,"VtepMAC":"c2:b4:d2:1b:fa:c2"}                    flannel.alpha.coreos.com/backend-type: vxlan                    flannel.alpha.coreos.com/kube-subnet-manager: true                    flannel.alpha.coreos.com/public-ip: 192.168.159.13                    kubeadm.alpha.kubernetes.io/cri-socket: /var/run/dockershim.sock                    node.alpha.kubernetes.io/ttl: 0                    volumes.kubernetes.io/controller-managed-attach-detach: trueCreationTimestamp:  Sat, 05 Nov 2022 09:26:33 +0800Taints:             Unschedulable:      falseLease:  HolderIdentity:  node01  AcquireTime:       RenewTime:       Sat, 05 Nov 2022 17:10:49 +0800Conditions:  Type                 Status  LastHeartbeatTime                 LastTransitionTime                Reason                       Message  ----                 ------  -----------------                 ------------------                ------                       -------  NetworkUnavailable   False   Sat, 05 Nov 2022 09:31:06 +0800   Sat, 05 Nov 2022 09:31:06 +0800   FlannelIsUp                  Flannel is running on this node  MemoryPressure       False   Sat, 05 Nov 2022 17:07:16 +0800   Sat, 05 Nov 2022 09:26:33 +0800   KubeletHasSufficientMemory   kubelet has sufficient memory available  DiskPressure         False   Sat, 05 Nov 2022 17:07:16 +0800   Sat, 05 Nov 2022 09:26:33 +0800   KubeletHasNoDiskPressure     kubelet has no disk pressure  PIDPressure          False   Sat, 05 Nov 2022 17:07:16 +0800   Sat, 05 Nov 2022 09:26:33 +0800   KubeletHasSufficientPID      kubelet has sufficient PID available  Ready                True    Sat, 05 Nov 2022 17:07:16 +0800   Sat, 05 Nov 2022 09:31:14 +0800   KubeletReady                 kubelet is posting ready statusAddresses:  InternalIP:  192.168.159.13  Hostname:    node01Capacity:  cpu:                2  ephemeral-storage:  15349Mi  hugepages-1Gi:      0  hugepages-2Mi:      0  memory:             3861508Ki  pods:               110Allocatable:  cpu:                2  ephemeral-storage:  14485133698  hugepages-1Gi:      0  hugepages-2Mi:      0  memory:             3759108Ki  pods:               110System Info:  Machine ID:                 737b63dadf104cdaa76db981253e1baa  System UUID:                F0114D56-06E7-3FC5-4619-BAD443CE9F72  Boot ID:                    ca48a336-8e26-4d35-bd9b-2f403926c2b5  Kernel Version:             3.10.0-957.el7.x86_64  OS Image:                   CentOS Linux 7 (Core)  Operating System:           linux  Architecture:               amd64  Container Runtime Version:  docker://20.10.21  Kubelet Version:            v1.21.3  Kube-Proxy Version:         v1.21.3PodCIDR:                      10.150.2.0/24PodCIDRs:                     10.150.2.0/24Non-terminated Pods:          (6 in total)  Namespace                   Name                        CPU Requests  CPU Limits  Memory Requests  Memory Limits  Age  ---------                   ----                        ------------  ----------  ---------------  -------------  ---  default                     nginx-6fc77dcb7c-dq86p      0 (0%)        0 (0%)      0 (0%)           0 (0%)         45m  default                     nginx-6fc77dcb7c-gv9qq      0 (0%)        0 (0%)      0 (0%)           0 (0%)         45m  default                     nginx-6fc77dcb7c-hkcbb      0 (0%)        0 (0%)      0 (0%)           0 (0%)         49m  kube-flannel                kube-flannel-ds-nj4vl       100m (5%)     100m (5%)   50Mi (1%)        50Mi (1%)      7h40m  kube-system                 coredns-6f6b8cc4f6-lg6hc    100m (5%)     0 (0%)      70Mi (1%)        170Mi (4%)     7h46m  kube-system                 kube-proxy-xd4lz            0 (0%)        0 (0%)      0 (0%)           0 (0%)         7h44mAllocated resources:  (Total limits may be over 100 percent, i.e., overcommitted.)  Resource           Requests    Limits  --------           --------    ------  cpu                200m (10%)  100m (5%)  memory             120Mi (3%)  220Mi (5%)  ephemeral-storage  0 (0%)      0 (0%)  hugepages-1Gi      0 (0%)      0 (0%)  hugepages-2Mi      0 (0%)      0 (0%)Events:              

16.3、显示所有Pod的详细信息:kubectl describe pods

[root@master ~]# kubectl describe pods

16.4、显示一个pod的详细信息 :kubectl describe deploy/nginx

[root@master ~]# kubectl describe deploy/nginx
[root@master ~]# kubectl describe deploy/nginxName:                   nginxNamespace:              defaultCreationTimestamp:      Sat, 05 Nov 2022 16:06:47 +0800Labels:                 app=nginxAnnotations:            deployment.kubernetes.io/revision: 3Selector:               app=nginxReplicas:               5 desired | 5 updated | 5 total | 5 available | 0 unavailableStrategyType:           RollingUpdateMinReadySeconds:        0RollingUpdateStrategy:  25% max unavailable, 25% max surgePod Template:  Labels:  app=nginx  Containers:   nginx:    Image:        nginx:1.21    Port:         80/TCP    Host Port:    0/TCP    Environment:      Mounts:         Volumes:        Conditions:  Type           Status  Reason  ----           ------  ------  Progressing    True    NewReplicaSetAvailable  Available      True    MinimumReplicasAvailableOldReplicaSets:  NewReplicaSet:   nginx-6fc77dcb7c (5/5 replicas created)Events:  Type    Reason             Age                From                   Message  ----    ------             ----               ----                   -------  Normal  ScalingReplicaSet  56m                deployment-controller  Scaled up replica set nginx-6fc77dcb7c to 1  Normal  ScalingReplicaSet  56m                deployment-controller  Scaled down replica set nginx-897f8f586 to 2  Normal  ScalingReplicaSet  52m (x5 over 56m)  deployment-controller  (combined from similar events): Scaled up replica set nginx-6fc77dcb7c to 5

16.5、显示所有svc详细信息:kubectl describe svc

[root@master ~]# kubectl describe svc

[root@master ~]# kubectl describe svcName:              kubernetesNamespace:         defaultLabels:            component=apiserver                   provider=kubernetesAnnotations:       Selector:          Type:              ClusterIPIP Family Policy:  SingleStackIP Families:       IPv4IP:                10.125.0.1IPs:               10.125.0.1Port:              https  443/TCPTargetPort:        6443/TCPEndpoints:         192.168.159.12:6443Session Affinity:  NoneEvents:            Name:                     nginx-serviceNamespace:                defaultLabels:                   app=nginxAnnotations:              Selector:                 app=nginxType:                     NodePortIP Family Policy:         SingleStackIP Families:              IPv4IP:                       10.125.204.159IPs:                      10.125.204.159Port:                       80/TCPTargetPort:               80/TCPNodePort:                   30307/TCPEndpoints:                10.150.1.10:80,10.150.1.11:80,10.150.2.11:80 + 2 more...Session Affinity:         NoneExternal Traffic Policy:  ClusterEvents:                   

16.6、显示指定svc的详细信息:kubectl describe svc nginx-service

[root@master ~]# kubectl describe svc nginx-service

[root@master ~]# kubectl describe svc nginx-serviceName:                     nginx-serviceNamespace:                defaultLabels:                   app=nginxAnnotations:              Selector:                 app=nginxType:                     NodePortIP Family Policy:         SingleStackIP Families:              IPv4IP:                       10.125.204.159IPs:                      10.125.204.159Port:                       80/TCPTargetPort:               80/TCPNodePort:                   30307/TCPEndpoints:                10.150.1.10:80,10.150.1.11:80,10.150.2.11:80 + 2 more...Session Affinity:         NoneExternal Traffic Policy:  ClusterEvents:                   

16.7、显示所有namespace的详细信息:kubectl describe ns

[root@master ~]# kubectl describe ns

[root@master ~]# kubectl describe nsName:         ceshiLabels:       kubernetes.io/metadata.name=ceshiAnnotations:  Status:       ActiveNo resource quota.No LimitRange resource.Name:         defaultLabels:       kubernetes.io/metadata.name=defaultAnnotations:  Status:       ActiveNo resource quota.No LimitRange resource.Name:         kube-flannelLabels:       kubernetes.io/metadata.name=kube-flannel              pod-security.kubernetes.io/enforce=privilegedAnnotations:  Status:       ActiveNo resource quota.No LimitRange resource.Name:         kube-node-leaseLabels:       kubernetes.io/metadata.name=kube-node-leaseAnnotations:  Status:       ActiveNo resource quota.No LimitRange resource.Name:         kube-publicLabels:       kubernetes.io/metadata.name=kube-publicAnnotations:  Status:       ActiveNo resource quota.No LimitRange resource.Name:         kube-systemLabels:       kubernetes.io/metadata.name=kube-systemAnnotations:  Status:       ActiveNo resource quota.No LimitRange resource.

16.8、显示指定namespace的详细信息:kubectl describe ns kube-public

[root@master ~]# kubectl describe ns kube-public

[root@master ~]# kubectl describe ns publicError from server (NotFound): namespaces "public" not found[root@master ~]# kubectl describe ns kube-publicName:         kube-publicLabels:       kubernetes.io/metadata.name=kube-publicAnnotations:  Status:       ActiveNo resource quota.No LimitRange resource.

故障现象:节点3个node 1个master1、现在创建一个pod,发现结果是pod无法创建

kubectl describe po pod_name-n namespaces

events:

调度器正常完成工作正常退出pod 创建失败,可能原因之一:资源现在问题

 

来源地址:https://blog.csdn.net/m0_62948770/article/details/127643882

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

云原生之kubectl命令详解

下载Word文档到电脑,方便收藏和打印~

下载Word文档

猜你喜欢

K8S 中 kubectl 命令详解

这篇文章主要介绍了K8S 中 kubectl 命令,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
2022-11-13

Linux 命令之rsync命令详解

rsync命令rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速
2022-06-04

linux命令之crontab详解

crontab 提交和管理用户的需要周期性执行的任务 更多linux命令详解:linux命令在线工具 Linux 教程:VitePress编写的Linux教程 补充说明 crontab命令 被用来提交和管理用户的需要周期性执行的任务,与wi
2023-08-18

shell命令之 IFS详解

IFS是Shell中的一个环境变量,用于指定字段分隔符。字段分隔符用于指定如何将一行文本拆分成多个字段。在Shell中,默认的字段分隔符是空格、制表符和换行符。当我们使用一些命令(如read命令)从文本中读取数据时,Shell会根据字段分隔
2023-09-15

Linux之split命令详解

命令功能:切割文件,将文件以行为单位或以字节为单位进行切割 命令语法: split [?help][?version] split [-<行数>][-l <行数>] [要切割的文件][输出文件名] ,以行数为单位 split [-b <字
2022-06-04

go命令生成功能详解

go 提供了 "go generate" 命令,它允许根据自定义模板生成代码。该命令接收可选的正则表达式参数 "-run" 和要应用模板的文件列表。模板使用标记 {{.fieldname}} 来指定代码占位符,并可以通过自定义函数(例如 p
go命令生成功能详解
2024-04-08

django之常用命令详解

Django 基本命令 本节主要是为了让您了解一些django最基本的命令,请尝试着记住它们,并且多多练习下 1. 新建一个 django projectdjango-admin.py startproject project-name 一
2022-06-04

linux命令详解之useradd命令使用方法

Linux 系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统。用户的账号一方面可以帮助系统管理员对使用系统的用户进行跟踪,并控制他们对系统资源的访问;另一方
2022-06-04

Linux常用命令之grep命令用法详解

1.官方简介 grep是linux的常用命令,用于对文件和文本执行重复搜索任务的Unix工具,可以通过grep命令指定特定搜索条件来搜索文件及其内容以获取有用的信息。Usage: grep [OPTION]... PATTERN [FILE
2022-06-03

linux命令详解之chkconfig命令使用方法

使用语法:chkconfig[--add][--del][--list][系统服务] 或chkconfig[--level < 等级代号>][系统服务][on/off/reset] chkconfig在没有参数运行时,显示用法。如果加上服务
2022-06-04

【Linux学习】Linux必备命令之cp命令详解

cp命令是Linux中的一个常用命令,用于将文件或目录复制到指定位置。下面是关于cp命令的一些详细解释和用法:1. 命令格式:cp [选项] 源文件 目标文件- 选项说明:- -r:复制目录及其子目录内容- -i:询问是否覆盖已存在的目标文
2023-09-09

Linux命令之mkdir 和 touch详解

目录mkdir什么是 mkdir 命令?如何使用 mkdir 命令?举例说明常用选项参数例子touch什么是 touch 命令?如何使用 touch 命令?举例说明常用选项参数总结mkdir 什么是 mkdir 命令? mkdir 是 ma
2023-06-07

Linux基础命令之mktemp详解

mktemp 创建临时文件或者目录,这样的创建方式是安全的。此命令的适用范围:RedHat、RHEL、Ubuntu、CentOS、SUSE、openSUSE、Fedora。 1、语法 mktemp [选项] [TE
2022-06-04

Linux学习之mkdir命令详解

目录前言1. 文件概念基础知识2. mkdir命令前言 最近几天越来越对linux很感兴趣了, 想再见塔克斯的心情也是越来越浓烈, 自从它给我留了110110110之后,就没了联系。 到现在我也是一头雾水, 我又找不到它而且又没有联
2022-06-04

spring之Bean的生命周期详解

Bean的生命周期:Bean的定义——Bean的初始化——Bean的使用——Bean的销毁Bean的定义Bean 是 spring 装配的组件模型,一切实体类都可以配置成一个 Bean ,进而就可以在任何其他的 Bean 中使用,一个 Be
2023-05-31

网络管理常用命令之Pathping命令使用详解

Pathping命令是Windows系统中的网络管理工具,它结合了Tracert和Ping命令的功能,可以提供更详细的网络路径信息。Pathping命令的使用格式如下:```pathping [-g ] [-h ] [-i ] [-n] [
2023-09-13

编程热搜

  • Python 学习之路 - Python
    一、安装Python34Windows在Python官网(https://www.python.org/downloads/)下载安装包并安装。Python的默认安装路径是:C:\Python34配置环境变量:【右键计算机】--》【属性】-
    Python 学习之路 - Python
  • chatgpt的中文全称是什么
    chatgpt的中文全称是生成型预训练变换模型。ChatGPT是什么ChatGPT是美国人工智能研究实验室OpenAI开发的一种全新聊天机器人模型,它能够通过学习和理解人类的语言来进行对话,还能根据聊天的上下文进行互动,并协助人类完成一系列
    chatgpt的中文全称是什么
  • C/C++中extern函数使用详解
  • C/C++可变参数的使用
    可变参数的使用方法远远不止以下几种,不过在C,C++中使用可变参数时要小心,在使用printf()等函数时传入的参数个数一定不能比前面的格式化字符串中的’%’符号个数少,否则会产生访问越界,运气不好的话还会导致程序崩溃
    C/C++可变参数的使用
  • css样式文件该放在哪里
  • php中数组下标必须是连续的吗
  • Python 3 教程
    Python 3 教程 Python 的 3.0 版本,常被称为 Python 3000,或简称 Py3k。相对于 Python 的早期版本,这是一个较大的升级。为了不带入过多的累赘,Python 3.0 在设计的时候没有考虑向下兼容。 Python
    Python 3 教程
  • Python pip包管理
    一、前言    在Python中, 安装第三方模块是通过 setuptools 这个工具完成的。 Python有两个封装了 setuptools的包管理工具: easy_install  和  pip , 目前官方推荐使用 pip。    
    Python pip包管理
  • ubuntu如何重新编译内核
  • 改善Java代码之慎用java动态编译

目录