云原生之kubectl命令详解

这篇具有很好参考价值的文章主要介绍了云原生之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 version
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 

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-resources
NAME                              SHORTNAMES   APIVERSION                             NAMESPACED   KIND
bindings                                       v1                                     true         Binding
componentstatuses                 cs           v1                                     false        ComponentStatus
configmaps                        cm           v1                                     true         ConfigMap
endpoints                         ep           v1                                     true         Endpoints
events                            ev           v1                                     true         Event
limitranges                       limits       v1                                     true         LimitRange
namespaces                        ns           v1                                     false        Namespace
nodes                             no           v1                                     false        Node
persistentvolumeclaims            pvc          v1                                     true         PersistentVolumeClaim
persistentvolumes                 pv           v1                                     false        PersistentVolume
pods                              po           v1                                     true         Pod
podtemplates                                   v1                                     true         PodTemplate
replicationcontrollers            rc           v1                                     true         ReplicationController
resourcequotas                    quota        v1                                     true         ResourceQuota
secrets                                        v1                                     true         Secret
serviceaccounts                   sa           v1                                     true         ServiceAccount
services                          svc          v1                                     true         Service
mutatingwebhookconfigurations                  admissionregistration.k8s.io/v1        false        MutatingWebhookConfiguration
validatingwebhookconfigurations                admissionregistration.k8s.io/v1        false        ValidatingWebhookConfiguration
customresourcedefinitions         crd,crds     apiextensions.k8s.io/v1                false        CustomResourceDefinition
apiservices                                    apiregistration.k8s.io/v1              false        APIService
controllerrevisions                            apps/v1                                true         ControllerRevision
daemonsets                        ds           apps/v1                                true         DaemonSet
deployments                       deploy       apps/v1                                true         Deployment
replicasets                       rs           apps/v1                                true         ReplicaSet
statefulsets                      sts          apps/v1                                true         StatefulSet
tokenreviews                                   authentication.k8s.io/v1               false        TokenReview
localsubjectaccessreviews                      authorization.k8s.io/v1                true         LocalSubjectAccessReview
selfsubjectaccessreviews                       authorization.k8s.io/v1                false        SelfSubjectAccessReview
selfsubjectrulesreviews                        authorization.k8s.io/v1                false        SelfSubjectRulesReview
subjectaccessreviews                           authorization.k8s.io/v1                false        SubjectAccessReview
horizontalpodautoscalers          hpa          autoscaling/v1                         true         HorizontalPodAutoscaler
cronjobs                          cj           batch/v1                               true         CronJob
jobs                                           batch/v1                               true         Job
certificatesigningrequests        csr          certificates.k8s.io/v1                 false        CertificateSigningRequest
leases                                         coordination.k8s.io/v1                 true         Lease
endpointslices                                 discovery.k8s.io/v1                    true         EndpointSlice
events                            ev           events.k8s.io/v1                       true         Event
ingresses                         ing          extensions/v1beta1                     true         Ingress
flowschemas                                    flowcontrol.apiserver.k8s.io/v1beta1   false        FlowSchema
prioritylevelconfigurations                    flowcontrol.apiserver.k8s.io/v1beta1   false        PriorityLevelConfiguration
ingressclasses                                 networking.k8s.io/v1                   false        IngressClass
ingresses                         ing          networking.k8s.io/v1                   true         Ingress
networkpolicies                   netpol       networking.k8s.io/v1                   true         NetworkPolicy
runtimeclasses                                 node.k8s.io/v1                         false        RuntimeClass
poddisruptionbudgets              pdb          policy/v1                              true         PodDisruptionBudget
podsecuritypolicies               psp          policy/v1beta1                         false        PodSecurityPolicy
clusterrolebindings                            rbac.authorization.k8s.io/v1           false        ClusterRoleBinding
clusterroles                                   rbac.authorization.k8s.io/v1           false        ClusterRole
rolebindings                                   rbac.authorization.k8s.io/v1           true         RoleBinding
roles                                          rbac.authorization.k8s.io/v1           true         Role
priorityclasses                   pc           scheduling.k8s.io/v1                   false        PriorityClass
csidrivers                                     storage.k8s.io/v1                      false        CSIDriver
csinodes                                       storage.k8s.io/v1                      false        CSINode
csistoragecapacities                           storage.k8s.io/v1beta1                 true         CSIStorageCapacity
storageclasses                    sc           storage.k8s.io/v1                      false        StorageClass
volumeattachments                              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:6443
CoreDNS is running at https://192.168.159.10:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To 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 并暴露它作为一个 新的 Kubernetes
Service
  run           在集群中运行一个指定的镜像
  set           为 objects 设置一个指定的特征

Basic Commands (Intermediate):
  explain       查看资源的文档
  get           显示一个或更多 resources
  edit          在服务器上编辑一个资源
  delete        Delete resources by filenames, stdin, resources and names, or by resources and label selector

Deploy 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 ReplicationController

Cluster 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 上的 taints

Troubleshooting 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 nodes

Advanced 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 <command> --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 <resource> [-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   AGE
kube-flannel   kube-flannel-ds-7clld            1/1     Running   0          5h35m
kube-flannel   kube-flannel-ds-psgvb            1/1     Running   0          5h35m
kube-flannel   kube-flannel-ds-xxncr            1/1     Running   0          5h35m
kube-system    coredns-6f6b8cc4f6-lbvl5         1/1     Running   0          5h45m
kube-system    coredns-6f6b8cc4f6-m6brz         1/1     Running   0          5h45m
kube-system    etcd-master                      1/1     Running   0          5h45m
kube-system    kube-apiserver-master            1/1     Running   0          5h45m
kube-system    kube-controller-manager-master   1/1     Running   0          5h11m
kube-system    kube-proxy-jwpnz                 1/1     Running   0          5h40m
kube-system    kube-proxy-xqcqm                 1/1     Running   0          5h41m
kube-system    kube-proxy-z6rhl                 1/1     Running   0          5h45m
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

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

kubectl get pod,k8s,运维,kubernetes,docker,java

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

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

NAMESPACE      NAME                                 READY   STATUS    RESTARTS   AGE
kube-flannel   pod/kube-flannel-ds-7whbw            1/1     Running   0          5h14m
kube-flannel   pod/kube-flannel-ds-nj4vl            1/1     Running   0          5h14m
kube-flannel   pod/kube-flannel-ds-w55x5            1/1     Running   0          5h14m
kube-system    pod/coredns-6f6b8cc4f6-lg6hc         1/1     Running   0          5h20m
kube-system    pod/coredns-6f6b8cc4f6-tdwhx         1/1     Running   0          5h20m
kube-system    pod/etcd-master                      1/1     Running   0          5h20m
kube-system    pod/kube-apiserver-master            1/1     Running   0          5h20m
kube-system    pod/kube-controller-manager-master   1/1     Running   0          5h13m
kube-system    pod/kube-proxy-gv58r                 1/1     Running   0          5h20m
kube-system    pod/kube-proxy-xd4lz                 1/1     Running   0          5h18m
kube-system    pod/kube-proxy-zzs2s                 1/1     Running   0          5h18m
kube-system    pod/kube-scheduler-master            1/1     Running   0          5h12m

NAMESPACE     NAME                    TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                  AGE
default       service/kubernetes      ClusterIP   10.125.0.1     <none>        443/TCP                  5h20m
default       service/nginx-service   NodePort    10.125.18.84   <none>        80:32476/TCP             5h3m
kube-system   service/kube-dns        ClusterIP   10.125.0.10    <none>        53/UDP,53/TCP,9153/TCP   5h20m

NAMESPACE      NAME                             DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR            AGE
kube-flannel   daemonset.apps/kube-flannel-ds   3         3         3       3            3           <none>                   5h14m
kube-system    daemonset.apps/kube-proxy        3         3         3       3            3           kubernetes.io/os=linux   5h20m

NAMESPACE     NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
kube-system   deployment.apps/coredns   2/2     2            2           5h20m

NAMESPACE     NAME                                 DESIRED   CURRENT   READY   AGE
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

NAME     STATUS   ROLES                  AGE     VERSION   LABELS
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=
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
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

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

Warning: v1 ComponentStatus is deprecated in v1.19+
NAME                 STATUS    MESSAGE             ERROR
scheduler            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 namespace
NAME              STATUS   AGE
default           Active   6h8m
kube-flannel      Active   5h58m
kube-node-lease   Active   6h8m
kube-public       Active   6h8m
kube-system       Active   6h8m

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

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

kubectl get pod,k8s,运维,kubernetes,docker,java

 文章来源地址https://www.toymoban.com/news/detail-788737.html

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

[root@master ~]# kubectl delete ns ceshi
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


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

kubectl get pod,k8s,运维,kubernetes,docker,java

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

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

kubectl get pod,k8s,运维,kubernetes,docker,java

访问:

kubectl get pod,k8s,运维,kubernetes,docker,java 

kubectl get pod,k8s,运维,kubernetes,docker,java 

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

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

kubectl get pod,k8s,运维,kubernetes,docker,java

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

kubectl get pod,k8s,运维,kubernetes,docker,java

 

13、查看endpoint的信息

[root@master ~]# kubectl get endpoints

kubectl get pod,k8s,运维,kubernetes,docker,java

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

例如:查看nginx的版本号:

kubectl get pod,k8s,运维,kubernetes,docker,java

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

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

kubectl get pod,k8s,运维,kubernetes,docker,java

kubectl get pod,k8s,运维,kubernetes,docker,java 

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

kubectl get pod,k8s,运维,kubernetes,docker,java

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

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

kubectl get pod,k8s,运维,kubernetes,docker,java

16、查看详细信息:kubectl describe

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

[root@master ~]# kubectl describe nodes

kubectl get pod,k8s,运维,kubernetes,docker,java

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

 [root@master ~]# kubectl describe nodes node01

Name:               node01
Roles:              node
Labels:             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
Annotations:        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: true
CreationTimestamp:  Sat, 05 Nov 2022 09:26:33 +0800
Taints:             <none>
Unschedulable:      false
Lease:
  HolderIdentity:  node01
  AcquireTime:     <unset>
  RenewTime:       Sat, 05 Nov 2022 17:10:49 +0800
Conditions:
  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 status
Addresses:
  InternalIP:  192.168.159.13
  Hostname:    node01
Capacity:
  cpu:                2
  ephemeral-storage:  15349Mi
  hugepages-1Gi:      0
  hugepages-2Mi:      0
  memory:             3861508Ki
  pods:               110
Allocatable:
  cpu:                2
  ephemeral-storage:  14485133698
  hugepages-1Gi:      0
  hugepages-2Mi:      0
  memory:             3759108Ki
  pods:               110
System 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.3
PodCIDR:                      10.150.2.0/24
PodCIDRs:                     10.150.2.0/24
Non-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%)         7h44m
Allocated 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:              <none>

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

[root@master ~]# kubectl describe pods

kubectl get pod,k8s,运维,kubernetes,docker,java

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

[root@master ~]# kubectl describe deploy/nginx
[root@master ~]# kubectl describe deploy/nginx
Name:                   nginx
Namespace:              default
CreationTimestamp:      Sat, 05 Nov 2022 16:06:47 +0800
Labels:                 app=nginx
Annotations:            deployment.kubernetes.io/revision: 3
Selector:               app=nginx
Replicas:               5 desired | 5 updated | 5 total | 5 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.21
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Progressing    True    NewReplicaSetAvailable
  Available      True    MinimumReplicasAvailable
OldReplicaSets:  <none>
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 svc
Name:              kubernetes
Namespace:         default
Labels:            component=apiserver
                   provider=kubernetes
Annotations:       <none>
Selector:          <none>
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.125.0.1
IPs:               10.125.0.1
Port:              https  443/TCP
TargetPort:        6443/TCP
Endpoints:         192.168.159.12:6443
Session Affinity:  None
Events:            <none>


Name:                     nginx-service
Namespace:                default
Labels:                   app=nginx
Annotations:              <none>
Selector:                 app=nginx
Type:                     NodePort
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.125.204.159
IPs:                      10.125.204.159
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  30307/TCP
Endpoints:                10.150.1.10:80,10.150.1.11:80,10.150.2.11:80 + 2 more...
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

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

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

[root@master ~]# kubectl describe svc nginx-service
Name:                     nginx-service
Namespace:                default
Labels:                   app=nginx
Annotations:              <none>
Selector:                 app=nginx
Type:                     NodePort
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.125.204.159
IPs:                      10.125.204.159
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  30307/TCP
Endpoints:                10.150.1.10:80,10.150.1.11:80,10.150.2.11:80 + 2 more...
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

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

[root@master ~]# kubectl describe ns

[root@master ~]# kubectl describe ns
Name:         ceshi
Labels:       kubernetes.io/metadata.name=ceshi
Annotations:  <none>
Status:       Active

No resource quota.

No LimitRange resource.


Name:         default
Labels:       kubernetes.io/metadata.name=default
Annotations:  <none>
Status:       Active

No resource quota.

No LimitRange resource.


Name:         kube-flannel
Labels:       kubernetes.io/metadata.name=kube-flannel
              pod-security.kubernetes.io/enforce=privileged
Annotations:  <none>
Status:       Active

No resource quota.

No LimitRange resource.


Name:         kube-node-lease
Labels:       kubernetes.io/metadata.name=kube-node-lease
Annotations:  <none>
Status:       Active

No resource quota.

No LimitRange resource.


Name:         kube-public
Labels:       kubernetes.io/metadata.name=kube-public
Annotations:  <none>
Status:       Active

No resource quota.

No LimitRange resource.


Name:         kube-system
Labels:       kubernetes.io/metadata.name=kube-system
Annotations:  <none>
Status:       Active

No 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 public
Error from server (NotFound): namespaces "public" not found
[root@master ~]# kubectl describe ns kube-public
Name:         kube-public
Labels:       kubernetes.io/metadata.name=kube-public
Annotations:  <none>
Status:       Active

No resource quota.

No LimitRange resource.

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

kubectl describe po pod_name-n namespaces

events:

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

kubectl get pod,k8s,运维,kubernetes,docker,java

 

到了这里,关于云原生之kubectl命令详解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 玩转k8s:kubectl命令行工具用法详解

    Kubectl 是管理kubernetes的命令行工具,kubectl在$HOME/.kube目录下查找config配置文件。可以通过设置KUBECONFIG环境变量或设置--kubeconfig参数指定其他kubeconfig文件。 本文介绍kubectl语法和命令操作描述,并提供常见示例。包括支持的参数和子命令。 kubectl使用以下语法,在终端运行命令

    2024年02月07日
    浏览(51)
  • 五、Kubernetes(K8S):Kubectl常用命令详解

    注意:后续技术分享,第一时间更新,以及更多更及时的技术资讯和学习技术资料 ,将在公众号 CTO Plus 发布,请关注公众号: CTO Plus   Kubectl 是Kubernetes命令行工具,用于管理Kubernetes集群资源和应用程序的部署、升级和扩展等。其作用包括但不限于: 1. 创建、更新和删除应

    2024年02月04日
    浏览(45)
  • [云原生] K8s之pod控制器详解

    Pod 是 Kubernetes 集群中能够被创建和管理的最小部署单元。所以需要有工具去操作和管理它们的生命周期,这里就需要用到控制器了。 Pod 控制器由 master 的 kube-controller-manager 组件提供,常见的此类控制器有 Replication Controller、ReplicaSet、Deployment、DaemonSet、StatefulSet、Job 和 CronJo

    2024年03月11日
    浏览(57)
  • k8s之Pod常用命令详解、镜像拉取策略(imagePullPolicy)

    imagePullPolicy 有三个取值: Always 每次都下载最新镜像 Never 不会尝试获取镜像,如果镜像已经以某种方式存在本地,kubelet 会尝试启动容器;否则,会启动失败 IfNotPresent 只有当镜像在本地不存在时才会拉取 默认镜像拉取策略: 当你(或控制器)向 API 服务器提交一个新的 Po

    2024年02月04日
    浏览(57)
  • 【云原生】K8s管理工具--Kubectl(一)

    1、陈述式管理方式 kubernetes 集群管理集群资源的唯一入口是通过相应的方法调用 apiserver 的接口 kubectl 是官方的 CLI 命令行工具,用于与 apiserver 进行通信,将用户在命令行输入的命令,组织并转化为 apiserver 能识别的信息,进而实现管理 k8s 各种资源的一种有效途径 kubectl 的

    2024年02月05日
    浏览(42)
  • 【云原生】k8s中kubectl陈述式资源管理

    陈述式资源管理方法 --主要依赖命令行工具kubectl进行管理 优点 可以满足90%以上的使用场景 对资源的增、删、查操作比较容易 缺点 命令冗长,复杂,难以记忆 特定场景下,无法实现管理需求 对资源的修改麻烦,需要patch来使用json串更改。 声明式资源管理方法 主要依赖统一

    2024年02月12日
    浏览(61)
  • k8s kubectl常用命令

    kubectl 是 Kubernetes 的一个命令行管理工具,可用于 Kubernetes 上的应用部署和日常管理。本文列举了 9 个常见的 kubectl 命令,并总结了一些使用技巧,希望可以帮助系统管理员简化管理工作。 对于刚开始使用命令行工具的开发者,最保险的方法是提出问题(读取操作),而不是

    2024年02月11日
    浏览(42)
  • k8s集群node节点运行kubectl命令

            在容器化项目部署中,某些应用需要部署到指定的机器上(涉及机器信息收集,然后生成license,机器授权等),所以需要在k8s集群的node节点上,手动执行kubectl命令。         具体的操作步骤如下: (1)在node节点执行任意kubectl命令,例如:kubectl get nodes,可以看到

    2024年02月14日
    浏览(55)
  • 【云原生】k8s的pod基础(上)

    pod是kubernetes中最小的资源管理组件 ,Pod也是最小化运行容器化应用的资源对象。一 个Pod代表着集群中运行的一个进程 。kubernetes中其他大多数组件都是用绕着Pod来进行支撑和扩展Pod功能的,例如,用于管理pod运行的StatefulSet和Deploment等控制器对象,用于暴露pod应用的service和

    2024年02月11日
    浏览(45)
  • 【云原生】k8s之pod控制器

    Pod 是 Kubernetes 集群中能够被创建和管理的最小部署单元。所以需要有工具去操作和管理它们的生命周期,这里就需要用到控制器了。 Pod 控制器由 master 的 kube-controller-manager 组件提供,常见的此类控制器有 Replication Controller、ReplicaSet、Deployment、DaemonSet、StatefulSet、Job 和 CronJo

    2024年02月13日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包