首页 > 其他分享 >k8s常用命令

k8s常用命令

时间:2023-01-11 21:34:15浏览次数:40  
标签:node kubectl filehttp Running 常用命令 pod k8s metadata

查看node状态

$ kubectl get nodes
NAME         STATUS   ROLES           AGE     VERSION
centos7906   Ready    <none>          3h42m   v1.25.2
centos7907   Ready    <none>          3h42m   v1.25.2
centos7908   Ready    <none>          3h42m   v1.25.2
node         Ready    control-plane   3h43m   v1.25.2

查看服务状态

$ kubectl get componentstatuses 
Warning: v1 ComponentStatus is deprecated in v1.19+
NAME                 STATUS    MESSAGE                         ERROR
controller-manager   Healthy   ok                              
scheduler            Healthy   ok                              
etcd-0               Healthy   {"health":"true","reason":""}

查看pod

#查看默认命名空间的pod
$ kubectl get pod
NAME       READY   STATUS    RESTARTS   AGE
filehttp   1/1     Running   0          8m26s
#查看详细的
$ kubectl get pod -o wide 
NAME       READY   STATUS    RESTARTS   AGE     IP              NODE         NOMINATED NODE   READINESS GATES
filehttp   1/1     Running   0          8m31s   10.244.86.130   centos7907   <none>           <none>
#查看指定命名空间的详细的
$ kubectl get pod -o wide -n kube-system 
NAME                                       READY   STATUS    RESTARTS   AGE     IP               NODE         NOMINATED NODE   READINESS GATES
calico-kube-controllers-58dbc876ff-68bhz   1/1     Running   0          3h43m   10.244.167.130   node         <none>           <none>
calico-node-bjg58                          1/1     Running   0          3h43m   10.0.0.8         centos7908   <none>           <none>
calico-node-bn48b                          1/1     Running   0          3h43m   10.0.0.7         centos7907   <none>           <none>
calico-node-gbqpz                          1/1     Running   0          3h43m   10.50.34.106     node         <none>           <none>
calico-node-j2l5m                          1/1     Running   0          3h43m   10.0.0.6         centos7906   <none>           <none>
coredns-c676cc86f-27nl4                    1/1     Running   0          3h45m   10.244.167.129   node         <none>           <none>
coredns-c676cc86f-5gmfm                    1/1     Running   0          3h45m   10.244.167.131   node         <none>           <none>
etcd-node                                  1/1     Running   0          3h45m   10.50.34.106     node         <none>           <none>
kube-apiserver-node                        1/1     Running   0          3h45m   10.50.34.106     node         <none>           <none>
kube-controller-manager-node               1/1     Running   0          3h45m   10.50.34.106     node         <none>           <none>
kube-proxy-fz2ll                           1/1     Running   0          3h44m   10.0.0.7         centos7907   <none>           <none>
kube-proxy-grqvz                           1/1     Running   0          3h43m   10.0.0.8         centos7908   <none>           <none>
kube-proxy-wmrvd                           1/1     Running   0          3h44m   10.0.0.6         centos7906   <none>           <none>
kube-proxy-wxcpv                           1/1     Running   0          3h45m   10.50.34.106     node         <none>           <none>
kube-scheduler-node                        1/1     Running   0          3h45m   10.50.34.106     node         <none>           <none>

#查看指定标签的
$ kubectl get pod -o wide -l app=http
NAME       READY   STATUS    RESTARTS   AGE   IP              NODE         NOMINATED NODE   READINESS GATES
filehttp   1/1     Running   0          10m   10.244.86.130   centos7907   <none>           <none>

查看pod详细信息

$ kubectl describe pod filehttp 
Name:             filehttp
Namespace:        default
Priority:         0
Service Account:  default
Node:             centos7907/10.0.0.7
Start Time:       Thu, 13 Oct 2022 04:26:08 +0800
Labels:           app=http
Annotations:      cni.projectcalico.org/containerID: bd3a03d6a2cf0a89675e912be783fa1470a129be6059641a5d82c0f0ac51d1c5
                  cni.projectcalico.org/podIP: 10.244.86.130/32
                  cni.projectcalico.org/podIPs: 10.244.86.130/32
Status:           Running
IP:               10.244.86.130
IPs:
  IP:  10.244.86.130
Containers:
  http1:
    Container ID:   containerd://da80b41fe55aac4a16518ff1531f3ec32e28b331ccc7a9e9d67b606d3748a4dd
    Image:          httpd:latest
    Image ID:       docker.io/library/httpd@sha256:4400fb49c9d7d218d3c8109ef721e0ec1f3897028a3004b098af587d565f4ae5
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Thu, 13 Oct 2022 04:26:43 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-lxz78 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-lxz78:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  11m   default-scheduler  Successfully assigned default/filehttp to centos7907
  Normal  Pulling    11m   kubelet            Pulling image "httpd:latest"
  Normal  Pulled     10m   kubelet            Successfully pulled image "httpd:latest" in 33.820714165s
  Normal  Created    10m   kubelet            Created container http1
  Normal  Started    10m   kubelet            Started container http1

查看所有命名空间

$ kubectl get namespaces 
NAME              STATUS   AGE
default           Active   3h49m
kube-node-lease   Active   3h49m
kube-public       Active   3h49m
kube-system       Active   3h49m

查看对应控制器的字段

查询pod中metadata中的字段

$ kubectl explain pod.metadata
KIND:     Pod
VERSION:  v1

RESOURCE: metadata <Object>

DESCRIPTION:
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

     ObjectMeta is metadata that all persisted resources must have, which
     includes all objects users must create.

FIELDS:
   annotations	<map[string]string>
     Annotations is an unstructured key value map stored with a resource that
     may be set by external tools to store and retrieve arbitrary metadata. They
     are not queryable and should be preserved when modifying objects. More
     info: http://kubernetes.io/docs/user-guide/annotations

.............................................

   uid	<string>
     UID is the unique in time and space value for this object. It is typically
     generated by the server on successful creation of a resource and is not
     allowed to change on PUT operations.

     Populated by the system. Read-only. More info:
     http://kubernetes.io/docs/user-guide/identifiers#uids

进入pod

filehttp是对应的pod名 http1是pod中对应的容器名

#进入pod中,执行命令bash
$ kubectl exec -it filehttp -- bash

#进入pod中指定的容器,并执行命令bash
$ kubectl exec -it filehttp -c http1 -- bash

查看pod的日志

$ kubectl logs filehttp

删除资源

这里以pod为例

直接删除

$ kubectl delete pod filehttp

通过配置文件删除

$ kubectl delete -f  1.yaml

查看pod具有哪些标签

$ kubectl get pod filehttp --show-labels 
NAME       READY   STATUS    RESTARTS   AGE   LABELS
filehttp   1/1     Running   0          21s   app=http
$ kubectl get pod --show-labels 
NAME       READY   STATUS    RESTARTS   AGE   LABELS
filehttp   1/1     Running   0          28s   app=http

强制删除pod

带上--force --grace-period 0 表示强制删除,并且不等待

$ kubectl delete -f 2.yaml --force --grace-period 0

补充编写资源文件

可以通过kubectl explain ...来查询字段

例如

查询pod的字段

$ kubectl explain pod
KIND:     Pod
VERSION:  v1

DESCRIPTION:
     Pod is a collection of containers that can run on a host. This resource is
     created by clients and scheduled onto hosts.

FIELDS:
   apiVersion	<string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

   kind	<string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

   metadata	<Object>
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

   spec	<Object>
     Specification of the desired behavior of the pod. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

   status	<Object>
     Most recently observed status of the pod. This data may not be up to date.
     Populated by the system. Read-only. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

查询pod中metadata中的字段

$ kubectl explain pod.metadata
KIND:     Pod
VERSION:  v1

RESOURCE: metadata <Object>

DESCRIPTION:
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

     ObjectMeta is metadata that all persisted resources must have, which
     includes all objects users must create.

FIELDS:
   annotations	<map[string]string>
     Annotations is an unstructured key value map stored with a resource that
     may be set by external tools to store and retrieve arbitrary metadata. They
     are not queryable and should be preserved when modifying objects. More
     info: http://kubernetes.io/docs/user-guide/annotations

.............................................

   uid	<string>
     UID is the unique in time and space value for this object. It is typically
     generated by the server on successful creation of a resource and is not
     allowed to change on PUT operations.

     Populated by the system. Read-only. More info:
     http://kubernetes.io/docs/user-guide/identifiers#uids

一个简单的pod配置文件

---
apiVersion: v1
kind: Pod
metadata:
  name: filehttp
  labels:
    app: http
spec:
  containers:
    - name: http1
      ports:
        - containerPort: 80
      image: httpd:latest
      imagePullPolicy: IfNotPresent

注意:

  • 如果字段后面带有 -required- 标记,则表示该字段为必需有的字段

标签:node,kubectl,filehttp,Running,常用命令,pod,k8s,metadata
From: https://www.cnblogs.com/guangdelw/p/17044947.html

相关文章

  • Kubernetes(k8s) kubectl 命令行工具的使用
    kubectl是Kubernetes集群的命令行工具,通过kubectl能够对集群本身进行管理,并且能够在集群上进行容器化应用的安装与部署。本文主要介绍Kubernetes(k8s)kubectl命令......
  • inux常用命令
    ls-la查看当前文件夹的文件mrdir目录名称[创建目录,创建文件夹]关机,重启shutdown-hnow关机shutdown-h1[1分钟后关机]shutdown-rnow重启系统halt关机......
  • k8s-新增master节点
    在当前唯一的master节点上运行如下命令第一步:kubeadminitphaseupload-certs--upload-certs执行结果如下:1#kubeadminitphaseupload-certs--upload-cert......
  • K8s常用命令
    #部署应用kubectlapply-fapp.yaml#查看deploymentkubectlgetdeployment#查看podkubectlgetpod-owide#查看pod详情kubectldescribepodpod-name#......
  • K8s创建pod yaml文件详解
    apiVersion:v1#指定api版本,此值必须在kubectlapiversion中kind:Pod#指定创建资源的角色/类型metadata:#资源的元数据/属性name:web04-pod#资源的名字,......
  • 搭建k8s集群初始化master节点 kubeadm init 遇到问题解决
    搭建k8s集群时遇到的问题一记,自己找了很久解决方案,也看到有些人提出类似问题后不了了之,于是发出来给网络做一次贡献kubeadminit报错”unknownserviceruntime.v1al......
  • Redis基础之常用命令说明(二)
      Redis是Key-Value类型缓存型数据库,Redis为了存储不同类型的数据,提供了五种常用数据类型,如下所示:string(字符串)hash(哈希散列)list(列表)set(集合)zset(sortedset:有......
  • Centos7.6部署k8s v1.16.4高可用集群(主备模式)
    原文:https://zhuanlan.zhihu.com/p/465647563一、部署环境主机列表:共有7台服务器,3台controlplane,3台work,1台client。k8s版本:二、高可用架构本文采用kub......
  • 在 K8S Volume 中使用 subPath
    使用subPath有时,在单个Pod中共享卷以供多方使用是很有用的。volumeMounts.subPath属性可用于指定所引用的卷内的子路径,而不是其根路径。下面是一个使用同一共享卷的......
  • 在 K8S Volume 中使用 subPath
    使用subPath有时,在单个Pod中共享卷以供多方使用是很有用的。volumeMounts.subPath属性可用于指定所引用的卷内的子路径,而不是其根路径。下面是一个使用同一共享卷的......