当前位置:   article > 正文

云原生之kubectl命令详解_kubectl get pod

kubectl get pod

目录

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

  1. [root@master ~]# kubectl version
  2. 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"}
  3. 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"}
  4. [root@master ~]# kubectl

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

[root@master ~]# kubectl api-resources

  1. 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"}
  2. 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"}
  3. [root@master ~]# kubectl api-resources
  4. NAME SHORTNAMES APIVERSION NAMESPACED KIND
  5. bindings v1 true Binding
  6. componentstatuses cs v1 false ComponentStatus
  7. configmaps cm v1 true ConfigMap
  8. endpoints ep v1 true Endpoints
  9. events ev v1 true Event
  10. limitranges limits v1 true LimitRange
  11. namespaces ns v1 false Namespace
  12. nodes no v1 false Node
  13. persistentvolumeclaims pvc v1 true PersistentVolumeClaim
  14. persistentvolumes pv v1 false PersistentVolume
  15. pods po v1 true Pod
  16. podtemplates v1 true PodTemplate
  17. replicationcontrollers rc v1 true ReplicationController
  18. resourcequotas quota v1 true ResourceQuota
  19. secrets v1 true Secret
  20. serviceaccounts sa v1 true ServiceAccount
  21. services svc v1 true Service
  22. mutatingwebhookconfigurations admissionregistration.k8s.io/v1 false MutatingWebhookConfiguration
  23. validatingwebhookconfigurations admissionregistration.k8s.io/v1 false ValidatingWebhookConfiguration
  24. customresourcedefinitions crd,crds apiextensions.k8s.io/v1 false CustomResourceDefinition
  25. apiservices apiregistration.k8s.io/v1 false APIService
  26. controllerrevisions apps/v1 true ControllerRevision
  27. daemonsets ds apps/v1 true DaemonSet
  28. deployments deploy apps/v1 true Deployment
  29. replicasets rs apps/v1 true ReplicaSet
  30. statefulsets sts apps/v1 true StatefulSet
  31. tokenreviews authentication.k8s.io/v1 false TokenReview
  32. localsubjectaccessreviews authorization.k8s.io/v1 true LocalSubjectAccessReview
  33. selfsubjectaccessreviews authorization.k8s.io/v1 false SelfSubjectAccessReview
  34. selfsubjectrulesreviews authorization.k8s.io/v1 false SelfSubjectRulesReview
  35. subjectaccessreviews authorization.k8s.io/v1 false SubjectAccessReview
  36. horizontalpodautoscalers hpa autoscaling/v1 true HorizontalPodAutoscaler
  37. cronjobs cj batch/v1 true CronJob
  38. jobs batch/v1 true Job
  39. certificatesigningrequests csr certificates.k8s.io/v1 false CertificateSigningRequest
  40. leases coordination.k8s.io/v1 true Lease
  41. endpointslices discovery.k8s.io/v1 true EndpointSlice
  42. events ev events.k8s.io/v1 true Event
  43. ingresses ing extensions/v1beta1 true Ingress
  44. flowschemas flowcontrol.apiserver.k8s.io/v1beta1 false FlowSchema
  45. prioritylevelconfigurations flowcontrol.apiserver.k8s.io/v1beta1 false PriorityLevelConfiguration
  46. ingressclasses networking.k8s.io/v1 false IngressClass
  47. ingresses ing networking.k8s.io/v1 true Ingress
  48. networkpolicies netpol networking.k8s.io/v1 true NetworkPolicy
  49. runtimeclasses node.k8s.io/v1 false RuntimeClass
  50. poddisruptionbudgets pdb policy/v1 true PodDisruptionBudget
  51. podsecuritypolicies psp policy/v1beta1 false PodSecurityPolicy
  52. clusterrolebindings rbac.authorization.k8s.io/v1 false ClusterRoleBinding
  53. clusterroles rbac.authorization.k8s.io/v1 false ClusterRole
  54. rolebindings rbac.authorization.k8s.io/v1 true RoleBinding
  55. roles rbac.authorization.k8s.io/v1 true Role
  56. priorityclasses pc scheduling.k8s.io/v1 false PriorityClass
  57. csidrivers storage.k8s.io/v1 false CSIDriver
  58. csinodes storage.k8s.io/v1 false CSINode
  59. csistoragecapacities storage.k8s.io/v1beta1 true CSIStorageCapacity
  60. storageclasses sc storage.k8s.io/v1 false StorageClass
  61. volumeattachments storage.k8s.io/v1 false VolumeAttachment

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

[root@master ~]# kubectl cluster-info

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

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

[root@master ~]# kubectl --help

  1. kubectl controls the Kubernetes cluster manager.
  2. Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/
  3. Basic Commands (Beginner):
  4. create Create a resource from a file or from stdin.
  5. expose 使用 replication controller, service, deployment 或者 pod 并暴露它作为一个 新的 Kubernetes
  6. Service
  7. run 在集群中运行一个指定的镜像
  8. set 为 objects 设置一个指定的特征
  9. Basic Commands (Intermediate):
  10. explain 查看资源的文档
  11. get 显示一个或更多 resources
  12. edit 在服务器上编辑一个资源
  13. delete Delete resources by filenames, stdin, resources and names, or by resources and label selector
  14. Deploy Commands:
  15. rollout Manage the rollout of a resource
  16. scale Set a new size for a Deployment, ReplicaSet or Replication Controller
  17. autoscale Auto-scale a Deployment, ReplicaSet, StatefulSet, or ReplicationController
  18. Cluster Management Commands:
  19. certificate 修改 certificate 资源.
  20. cluster-info 显示集群信息
  21. top 显示 Resource (CPU/Memory) 使用.
  22. cordon 标记 node 为 unschedulable
  23. uncordon 标记 node 为 schedulable
  24. drain Drain node in preparation for maintenance
  25. taint 更新一个或者多个 node 上的 taints
  26. Troubleshooting and Debugging Commands:
  27. describe 显示一个指定 resource 或者 group 的 resources 详情
  28. logs 输出容器在 pod 中的日志
  29. attach Attach 到一个运行中的 container
  30. exec 在一个 container 中执行一个命令
  31. port-forward Forward one or more local ports to a pod
  32. proxy 运行一个 proxy 到 Kubernetes API server
  33. cp 复制 files 和 directories 到 containers 和从容器中复制 files 和 directories.
  34. auth Inspect authorization
  35. debug Create debugging sessions for troubleshooting workloads and nodes
  36. Advanced Commands:
  37. diff Diff live version against would-be applied version
  38. apply 通过文件名或标准输入流(stdin)对资源进行配置
  39. patch Update field(s) of a resource
  40. replace 通过 filename 或者 stdin替换一个资源
  41. wait Experimental: Wait for a specific condition on one or many resources.
  42. kustomize Build a kustomization target from a directory or URL.
  43. Settings Commands:
  44. label 更新在这个资源上的 labels
  45. annotate 更新一个资源的注解
  46. completion Output shell completion code for the specified shell (bash or zsh)
  47. Other Commands:
  48. api-resources Print the supported API resources on the server
  49. api-versions Print the supported API versions on the server, in the form of "group/version"
  50. config 修改 kubeconfig 文件
  51. plugin Provides utilities for interacting with plugins.
  52. version 输出 client 和 server 的版本信息
  53. Usage:
  54. kubectl [flags] [options]
  55. Use "kubectl <command> --help" for more information about a given command.
  56. 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

  1. -- Logs begin at 三 2022-11-02 02:24:50 CST. --
  2. 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"
  3. 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\") "
  4. 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 ""
  5. 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 \"\""
  6. 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"
  7. 11月 01 19:51:10 master kubelet[13641]: I1101 19:51:10.913108 13641 topology_manager.go:187] "Topology Admit Handler"
  8. 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\") "
  9. 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"
  10. 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. 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 <resource> [-o wide | json| yaml] [-n namespace]

注释:

resource:可以是具体资源名称

-o :指定输出格式

-n :指定名称空间

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

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

  1. NAMESPACE NAME READY STATUS RESTARTS AGE
  2. kube-flannel kube-flannel-ds-7clld 1/1 Running 0 5h35m
  3. kube-flannel kube-flannel-ds-psgvb 1/1 Running 0 5h35m
  4. kube-flannel kube-flannel-ds-xxncr 1/1 Running 0 5h35m
  5. kube-system coredns-6f6b8cc4f6-lbvl5 1/1 Running 0 5h45m
  6. kube-system coredns-6f6b8cc4f6-m6brz 1/1 Running 0 5h45m
  7. kube-system etcd-master 1/1 Running 0 5h45m
  8. kube-system kube-apiserver-master 1/1 Running 0 5h45m
  9. kube-system kube-controller-manager-master 1/1 Running 0 5h11m
  10. kube-system kube-proxy-jwpnz 1/1 Running 0 5h40m
  11. kube-system kube-proxy-xqcqm 1/1 Running 0 5h41m
  12. kube-system kube-proxy-z6rhl 1/1 Running 0 5h45m
  13. kube-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

  1. NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  2. kube-flannel kube-flannel-ds-7clld 1/1 Running 0 5h39m 192.168.159.13 node02 <none> <none>
  3. kube-flannel kube-flannel-ds-psgvb 1/1 Running 0 5h39m 192.168.159.11 node01 <none> <none>
  4. kube-flannel kube-flannel-ds-xxncr 1/1 Running 0 5h39m 192.168.159.10 master <none> <none>
  5. kube-system coredns-6f6b8cc4f6-lbvl5 1/1 Running 0 5h49m 10.150.2.2 node02 <none> <none>
  6. kube-system coredns-6f6b8cc4f6-m6brz 1/1 Running 0 5h49m 10.150.1.2 node01 <none> <none>
  7. kube-system etcd-master 1/1 Running 0 5h49m 192.168.159.10 master <none> <none>
  8. kube-system kube-apiserver-master 1/1 Running 0 5h49m 192.168.159.10 master <none> <none>
  9. kube-system kube-controller-manager-master 1/1 Running 0 5h16m 192.168.159.10 master <none> <none>
  10. kube-system kube-proxy-jwpnz 1/1 Running 0 5h45m 192.168.159.13 node02 <none> <none>
  11. kube-system kube-proxy-xqcqm 1/1 Running 0 5h45m 192.168.159.11 node01 <none> <none>
  12. kube-system kube-proxy-z6rhl 1/1 Running 0 5h49m 192.168.159.10 master <none> <none>
  13. kube-system kube-scheduler-master 1/1 Running 0 5h15m 192.168.159.10 master <none> <none>

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

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

  1. NAMESPACE NAME READY STATUS RESTARTS AGE
  2. kube-flannel pod/kube-flannel-ds-7whbw 1/1 Running 0 5h14m
  3. kube-flannel pod/kube-flannel-ds-nj4vl 1/1 Running 0 5h14m
  4. kube-flannel pod/kube-flannel-ds-w55x5 1/1 Running 0 5h14m
  5. kube-system pod/coredns-6f6b8cc4f6-lg6hc 1/1 Running 0 5h20m
  6. kube-system pod/coredns-6f6b8cc4f6-tdwhx 1/1 Running 0 5h20m
  7. kube-system pod/etcd-master 1/1 Running 0 5h20m
  8. kube-system pod/kube-apiserver-master 1/1 Running 0 5h20m
  9. kube-system pod/kube-controller-manager-master 1/1 Running 0 5h13m
  10. kube-system pod/kube-proxy-gv58r 1/1 Running 0 5h20m
  11. kube-system pod/kube-proxy-xd4lz 1/1 Running 0 5h18m
  12. kube-system pod/kube-proxy-zzs2s 1/1 Running 0 5h18m
  13. kube-system pod/kube-scheduler-master 1/1 Running 0 5h12m
  14. NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  15. default service/kubernetes ClusterIP 10.125.0.1 <none> 443/TCP 5h20m
  16. default service/nginx-service NodePort 10.125.18.84 <none> 80:32476/TCP 5h3m
  17. kube-system service/kube-dns ClusterIP 10.125.0.10 <none> 53/UDP,53/TCP,9153/TCP 5h20m
  18. NAMESPACE NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
  19. kube-flannel daemonset.apps/kube-flannel-ds 3 3 3 3 3 <none> 5h14m
  20. kube-system daemonset.apps/kube-proxy 3 3 3 3 3 kubernetes.io/os=linux 5h20m
  21. NAMESPACE NAME READY UP-TO-DATE AVAILABLE AGE
  22. kube-system deployment.apps/coredns 2/2 2 2 5h20m
  23. NAMESPACE NAME DESIRED CURRENT READY AGE
  24. kube-system replicaset.apps/coredns-6f6b8cc4f6 2 2 2 5h20m

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

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

  1. NAME STATUS ROLES AGE VERSION LABELS
  2. master 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=
  3. 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=node
  4. node02 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

  1. [root@master ~]# kubectl get pods --show-labels -A
  2. NAMESPACE NAME READY STATUS RESTARTS AGE LABELS
  3. kube-flannel kube-flannel-ds-7clld 1/1 Running 0 5h51m app=flannel,controller-revision-hash=5b775b5b5c,pod-template-generation=1,tier=node
  4. kube-flannel kube-flannel-ds-psgvb 1/1 Running 0 5h51m app=flannel,controller-revision-hash=5b775b5b5c,pod-template-generation=1,tier=node
  5. kube-flannel kube-flannel-ds-xxncr 1/1 Running 0 5h51m app=flannel,controller-revision-hash=5b775b5b5c,pod-template-generation=1,tier=node
  6. kube-system coredns-6f6b8cc4f6-lbvl5 1/1 Running 0 6h1m k8s-app=kube-dns,pod-template-hash=6f6b8cc4f6
  7. kube-system coredns-6f6b8cc4f6-m6brz 1/1 Running 0 6h1m k8s-app=kube-dns,pod-template-hash=6f6b8cc4f6
  8. kube-system etcd-master 1/1 Running 0 6h1m component=etcd,tier=control-plane
  9. kube-system kube-apiserver-master 1/1 Running 0 6h1m component=kube-apiserver,tier=control-plane
  10. kube-system kube-controller-manager-master 1/1 Running 0 5h27m component=kube-controller-manager,tier=control-plane
  11. kube-system kube-proxy-jwpnz 1/1 Running 0 5h56m controller-revision-hash=6b87fcb57c,k8s-app=kube-proxy,pod-template-generation=1
  12. kube-system kube-proxy-xqcqm 1/1 Running 0 5h56m controller-revision-hash=6b87fcb57c,k8s-app=kube-proxy,pod-template-generation=1
  13. kube-system kube-proxy-z6rhl 1/1 Running 0 6h1m controller-revision-hash=6b87fcb57c,k8s-app=kube-proxy,pod-template-generation=1
  14. kube-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

  1. Warning: v1 ComponentStatus is deprecated in v1.19+
  2. NAME                 STATUS    MESSAGE             ERROR
  3. scheduler            Healthy   ok                  
  4. controller-manager   Healthy   ok                  
  5. etcd-0               Healthy   {"health":"true"}  

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

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

  1. [root@master ~]# kubectl get namespace
  2. NAME STATUS AGE
  3. default Active 6h8m
  4. kube-flannel Active 5h58m
  5. kube-node-lease Active 6h8m
  6. kube-public Active 6h8m
  7. kube-system Active 6h8m

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

  1. [root@master ~]# kubectl create ns ceshi
  2. namespace/ceshi created

 

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

  1. [root@master ~]# kubectl delete ns ceshi
  2. namespace "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

  1. [root@master ~]# kubectl create deployment nginx --image=nginx:1.15 --port=80 --replicas=3 -n kube-public
  2. deployment.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

  1. Name: node01
  2. Roles: node
  3. Labels: beta.kubernetes.io/arch=amd64
  4. beta.kubernetes.io/os=linux
  5. kubernetes.io/arch=amd64
  6. kubernetes.io/hostname=node01
  7. kubernetes.io/os=linux
  8. node-role.kubernetes.io/node=node
  9. Annotations: flannel.alpha.coreos.com/backend-data: {"VNI":1,"VtepMAC":"c2:b4:d2:1b:fa:c2"}
  10. flannel.alpha.coreos.com/backend-type: vxlan
  11. flannel.alpha.coreos.com/kube-subnet-manager: true
  12. flannel.alpha.coreos.com/public-ip: 192.168.159.13
  13. kubeadm.alpha.kubernetes.io/cri-socket: /var/run/dockershim.sock
  14. node.alpha.kubernetes.io/ttl: 0
  15. volumes.kubernetes.io/controller-managed-attach-detach: true
  16. CreationTimestamp: Sat, 05 Nov 2022 09:26:33 +0800
  17. Taints: <none>
  18. Unschedulable: false
  19. Lease:
  20. HolderIdentity: node01
  21. AcquireTime: <unset>
  22. RenewTime: Sat, 05 Nov 2022 17:10:49 +0800
  23. Conditions:
  24. Type Status LastHeartbeatTime LastTransitionTime Reason Message
  25. ---- ------ ----------------- ------------------ ------ -------
  26. 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
  27. MemoryPressure False Sat, 05 Nov 2022 17:07:16 +0800 Sat, 05 Nov 2022 09:26:33 +0800 KubeletHasSufficientMemory kubelet has sufficient memory available
  28. DiskPressure False Sat, 05 Nov 2022 17:07:16 +0800 Sat, 05 Nov 2022 09:26:33 +0800 KubeletHasNoDiskPressure kubelet has no disk pressure
  29. PIDPressure False Sat, 05 Nov 2022 17:07:16 +0800 Sat, 05 Nov 2022 09:26:33 +0800 KubeletHasSufficientPID kubelet has sufficient PID available
  30. Ready True Sat, 05 Nov 2022 17:07:16 +0800 Sat, 05 Nov 2022 09:31:14 +0800 KubeletReady kubelet is posting ready status
  31. Addresses:
  32. InternalIP: 192.168.159.13
  33. Hostname: node01
  34. Capacity:
  35. cpu: 2
  36. ephemeral-storage: 15349Mi
  37. hugepages-1Gi: 0
  38. hugepages-2Mi: 0
  39. memory: 3861508Ki
  40. pods: 110
  41. Allocatable:
  42. cpu: 2
  43. ephemeral-storage: 14485133698
  44. hugepages-1Gi: 0
  45. hugepages-2Mi: 0
  46. memory: 3759108Ki
  47. pods: 110
  48. System Info:
  49. Machine ID: 737b63dadf104cdaa76db981253e1baa
  50. System UUID: F0114D56-06E7-3FC5-4619-BAD443CE9F72
  51. Boot ID: ca48a336-8e26-4d35-bd9b-2f403926c2b5
  52. Kernel Version: 3.10.0-957.el7.x86_64
  53. OS Image: CentOS Linux 7 (Core)
  54. Operating System: linux
  55. Architecture: amd64
  56. Container Runtime Version: docker://20.10.21
  57. Kubelet Version: v1.21.3
  58. Kube-Proxy Version: v1.21.3
  59. PodCIDR: 10.150.2.0/24
  60. PodCIDRs: 10.150.2.0/24
  61. Non-terminated Pods: (6 in total)
  62. Namespace Name CPU Requests CPU Limits Memory Requests Memory Limits Age
  63. --------- ---- ------------ ---------- --------------- ------------- ---
  64. default nginx-6fc77dcb7c-dq86p 0 (0%) 0 (0%) 0 (0%) 0 (0%) 45m
  65. default nginx-6fc77dcb7c-gv9qq 0 (0%) 0 (0%) 0 (0%) 0 (0%) 45m
  66. default nginx-6fc77dcb7c-hkcbb 0 (0%) 0 (0%) 0 (0%) 0 (0%) 49m
  67. kube-flannel kube-flannel-ds-nj4vl 100m (5%) 100m (5%) 50Mi (1%) 50Mi (1%) 7h40m
  68. kube-system coredns-6f6b8cc4f6-lg6hc 100m (5%) 0 (0%) 70Mi (1%) 170Mi (4%) 7h46m
  69. kube-system kube-proxy-xd4lz 0 (0%) 0 (0%) 0 (0%) 0 (0%) 7h44m
  70. Allocated resources:
  71. (Total limits may be over 100 percent, i.e., overcommitted.)
  72. Resource Requests Limits
  73. -------- -------- ------
  74. cpu 200m (10%) 100m (5%)
  75. memory 120Mi (3%) 220Mi (5%)
  76. ephemeral-storage 0 (0%) 0 (0%)
  77. hugepages-1Gi 0 (0%) 0 (0%)
  78. hugepages-2Mi 0 (0%) 0 (0%)
  79. Events: <none>

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

[root@master ~]# kubectl describe pods

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

[root@master ~]# kubectl describe deploy/nginx
  1. [root@master ~]# kubectl describe deploy/nginx
  2. Name: nginx
  3. Namespace: default
  4. CreationTimestamp: Sat, 05 Nov 2022 16:06:47 +0800
  5. Labels: app=nginx
  6. Annotations: deployment.kubernetes.io/revision: 3
  7. Selector: app=nginx
  8. Replicas: 5 desired | 5 updated | 5 total | 5 available | 0 unavailable
  9. StrategyType: RollingUpdate
  10. MinReadySeconds: 0
  11. RollingUpdateStrategy: 25% max unavailable, 25% max surge
  12. Pod Template:
  13. Labels: app=nginx
  14. Containers:
  15. nginx:
  16. Image: nginx:1.21
  17. Port: 80/TCP
  18. Host Port: 0/TCP
  19. Environment: <none>
  20. Mounts: <none>
  21. Volumes: <none>
  22. Conditions:
  23. Type Status Reason
  24. ---- ------ ------
  25. Progressing True NewReplicaSetAvailable
  26. Available True MinimumReplicasAvailable
  27. OldReplicaSets: <none>
  28. NewReplicaSet: nginx-6fc77dcb7c (5/5 replicas created)
  29. Events:
  30. Type Reason Age From Message
  31. ---- ------ ---- ---- -------
  32. Normal ScalingReplicaSet 56m deployment-controller Scaled up replica set nginx-6fc77dcb7c to 1
  33. Normal ScalingReplicaSet 56m deployment-controller Scaled down replica set nginx-897f8f586 to 2
  34. 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

  1. [root@master ~]# kubectl describe svc
  2. Name: kubernetes
  3. Namespace: default
  4. Labels: component=apiserver
  5. provider=kubernetes
  6. Annotations: <none>
  7. Selector: <none>
  8. Type: ClusterIP
  9. IP Family Policy: SingleStack
  10. IP Families: IPv4
  11. IP: 10.125.0.1
  12. IPs: 10.125.0.1
  13. Port: https 443/TCP
  14. TargetPort: 6443/TCP
  15. Endpoints: 192.168.159.12:6443
  16. Session Affinity: None
  17. Events: <none>
  18. Name: nginx-service
  19. Namespace: default
  20. Labels: app=nginx
  21. Annotations: <none>
  22. Selector: app=nginx
  23. Type: NodePort
  24. IP Family Policy: SingleStack
  25. IP Families: IPv4
  26. IP: 10.125.204.159
  27. IPs: 10.125.204.159
  28. Port: <unset> 80/TCP
  29. TargetPort: 80/TCP
  30. NodePort: <unset> 30307/TCP
  31. Endpoints: 10.150.1.10:80,10.150.1.11:80,10.150.2.11:80 + 2 more...
  32. Session Affinity: None
  33. External Traffic Policy: Cluster
  34. Events: <none>

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

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

  1. [root@master ~]# kubectl describe svc nginx-service
  2. Name: nginx-service
  3. Namespace: default
  4. Labels: app=nginx
  5. Annotations: <none>
  6. Selector: app=nginx
  7. Type: NodePort
  8. IP Family Policy: SingleStack
  9. IP Families: IPv4
  10. IP: 10.125.204.159
  11. IPs: 10.125.204.159
  12. Port: <unset> 80/TCP
  13. TargetPort: 80/TCP
  14. NodePort: <unset> 30307/TCP
  15. Endpoints: 10.150.1.10:80,10.150.1.11:80,10.150.2.11:80 + 2 more...
  16. Session Affinity: None
  17. External Traffic Policy: Cluster
  18. Events: <none>

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

[root@master ~]# kubectl describe ns

  1. [root@master ~]# kubectl describe ns
  2. Name: ceshi
  3. Labels: kubernetes.io/metadata.name=ceshi
  4. Annotations: <none>
  5. Status: Active
  6. No resource quota.
  7. No LimitRange resource.
  8. Name: default
  9. Labels: kubernetes.io/metadata.name=default
  10. Annotations: <none>
  11. Status: Active
  12. No resource quota.
  13. No LimitRange resource.
  14. Name: kube-flannel
  15. Labels: kubernetes.io/metadata.name=kube-flannel
  16. pod-security.kubernetes.io/enforce=privileged
  17. Annotations: <none>
  18. Status: Active
  19. No resource quota.
  20. No LimitRange resource.
  21. Name: kube-node-lease
  22. Labels: kubernetes.io/metadata.name=kube-node-lease
  23. Annotations: <none>
  24. Status: Active
  25. No resource quota.
  26. No LimitRange resource.
  27. Name: kube-public
  28. Labels: kubernetes.io/metadata.name=kube-public
  29. Annotations: <none>
  30. Status: Active
  31. No resource quota.
  32. No LimitRange resource.
  33. Name: kube-system
  34. Labels: kubernetes.io/metadata.name=kube-system
  35. Annotations: <none>
  36. Status: Active
  37. No resource quota.
  38. No LimitRange resource.

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

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

  1. [root@master ~]# kubectl describe ns public
  2. Error from server (NotFound): namespaces "public" not found
  3. [root@master ~]# kubectl describe ns kube-public
  4. Name: kube-public
  5. Labels: kubernetes.io/metadata.name=kube-public
  6. Annotations: <none>
  7. Status: Active
  8. No resource quota.
  9. No LimitRange resource.

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

kubectl describe po pod_name-n namespaces

events:

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

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号