【输出内容详细版】查看Kubernetes集群基本信息(初学者版)


当Kubernetes集群构建完成时,我们需要先查看集群内的一些信息来了解Kubernetes集群。这些信息包括集群节点,Kubernetes版本,Kubernetes API对象(Kubernetes中的资源类型)及其版本,Kubernetes上下文和配置等信息。

Kubernetes集群搭建的教程传送门:

1. 查看集群节点

kubectl get nodes:显示节点名,状态,角色,存活时间和版本。

root@k8s-01:~# kubectl get nodes
NAME     STATUS   ROLES                  AGE    VERSION
k8s-01   Ready    control-plane,master   160m   v1.22.1
k8s-02   Ready    <none>                 150m   v1.22.1
k8s-03   Ready    <none>                 152m   v1.22.1

字段解释:

字段NAMESTATUSROLESAGEERSION
中文节点名状态角色存活时间版本
说明k8s节点名节点状态:Ready或NotReady节点在集群中扮演的角色从节点创建后至现在的时间统计Kubernetes版本(即kubelet版本)

示例:

k8s-01节点的状态是Ready,表明节点kubelet服务正常运行,角色是控制面,master,已经创建了160分钟,kubelet版本是1.22.1。

2. 查看集群信息

kubectl cluster-info:显示主控节点和服务的地址。

root@k8s-01:~# kubectl cluster-info
Kubernetes control plane is running at https://10.117.136.98:6443
CoreDNS is running at https://10.117.136.98:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
  • Kubernetes 控制平面在 https://10.117.136.98:6443 运行。
  • CoreDNS 在 https://10.117.136.98:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy 运行。
  • 进一步调试和诊断集群问题,使用kubectl cluster-info dump将当前集群状态转储到标准输出)。

3. 查看集群版本

输出格式为字典,同时输出server版本和client版本。

root@k8s-01:~# kubectl version
Client Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.1", GitCommit:"632ed300f2c34f6d6d15ca4cef3d3c7073412212", GitTreeState:"clean", BuildDate:"2021-08-19T15:45:37Z", GoVersion:"go1.16.7", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.1", GitCommit:"632ed300f2c34f6d6d15ca4cef3d3c7073412212", GitTreeState:"clean", BuildDate:"2021-08-19T15:39:34Z", GoVersion:"go1.16.7", Compiler:"gc", Platform:"linux/amd64"}

kubectl version = kubectl version --client=false --short=false

3.1 简化输出格式

root@k8s-01:~# kubectl version  --short=true
Client Version: v1.22.1
Server Version: v1.22.1

3.2 只输出客户端版本

root@k8s-01:~# kubectl version --client=true
Client Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.1", GitCommit:"632ed300f2c34f6d6d15ca4cef3d3c7073412212", GitTreeState:"clean", BuildDate:"2021-08-19T15:45:37Z", GoVersion:"go1.16.7", Compiler:"gc", Platform:"linux/amd64"}

4. 查看可用的 API 资源

kubectl api-resources:列出所支持的全部资源类型及其简称、API 组, 是否是命名空间作用域和 Kind。

root@k8s-01:~# kubectl api-resources

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

字段解释:

字段NAMESHORTNAMESAPIVERSIONNAMESPACEDKIND
中文资源名缩写名API 版本按命名空间资源类型
说明资源名称资源的缩写每个资源都有自己的API组是否是命名空间作用域
true表示该资源的作用域是命名空间
false表示该资源为全局资源。
KIND字段的值

示例:

nodes资源的缩写是no,API组是v1,它是全局对象。所以我们在查询节点信息时,可以使用下面三种命令查询:

kubectl get nodes
kubectl get no
kubectl get node
  • 使用全称或者缩写都可以查询。

  • kubectl api-resources命令输出中,有些资源是复数形式,但在对该资源进行CRUD(增删改查)等操作时,可以使用复数形式的单词,也可以使用非复数形式。对操作没有任何影响。

  • 如果资源名中没有使用复数形式,就不可以自行添加复数形式,否则会报错。

    例如Kubernetes对象 assign在输出中不是复数,所以不可以在CRUD时擅自改为assigns

    在这里插入图片描述

下表列出所有受支持的资源类型及其缩写别名。

(以下输出可以通过 kubectl api-resources 获取,内容以 Kubernetes 1.25.0 版本为准。)

资源名缩写名API 版本按命名空间资源类型
bindingsv1trueBinding
componentstatusescsv1falseComponentStatus
configmapscmv1trueConfigMap
endpointsepv1trueEndpoints
eventsevv1trueEvent
limitrangeslimitsv1trueLimitRange
namespacesnsv1falseNamespace
nodesnov1falseNode
persistentvolumeclaimspvcv1truePersistentVolumeClaim
persistentvolumespvv1falsePersistentVolume
podspov1truePod
podtemplatesv1truePodTemplate
replicationcontrollersrcv1trueReplicationController
resourcequotasquotav1trueResourceQuota
secretsv1trueSecret
serviceaccountssav1trueServiceAccount
servicessvcv1trueService
mutatingwebhookconfigurationsadmissionregistration.k8s.io/v1falseMutatingWebhookConfiguration
validatingwebhookconfigurationsadmissionregistration.k8s.io/v1falseValidatingWebhookConfiguration
customresourcedefinitionscrd,crdsapiextensions.k8s.io/v1falseCustomResourceDefinition
apiservicesapiregistration.k8s.io/v1falseAPIService
controllerrevisionsapps/v1trueControllerRevision
daemonsetsdsapps/v1trueDaemonSet
deploymentsdeployapps/v1trueDeployment
replicasetsrsapps/v1trueReplicaSet
statefulsetsstsapps/v1trueStatefulSet
tokenreviewsauthentication.k8s.io/v1falseTokenReview
localsubjectaccessreviewsauthorization.k8s.io/v1trueLocalSubjectAccessReview
selfsubjectaccessreviewsauthorization.k8s.io/v1falseSelfSubjectAccessReview
selfsubjectrulesreviewsauthorization.k8s.io/v1falseSelfSubjectRulesReview
subjectaccessreviewsauthorization.k8s.io/v1falseSubjectAccessReview
horizontalpodautoscalershpaautoscaling/v2trueHorizontalPodAutoscaler
cronjobscjbatch/v1trueCronJob
jobsbatch/v1trueJob
certificatesigningrequestscsrcertificates.k8s.io/v1falseCertificateSigningRequest
leasescoordination.k8s.io/v1trueLease
endpointslicesdiscovery.k8s.io/v1trueEndpointSlice
eventsevevents.k8s.io/v1trueEvent
flowschemasflowcontrol.apiserver.k8s.io/v1beta2falseFlowSchema
prioritylevelconfigurationsflowcontrol.apiserver.k8s.io/v1beta2falsePriorityLevelConfiguration
ingressclassesnetworking.k8s.io/v1falseIngressClass
ingressesingnetworking.k8s.io/v1trueIngress
networkpoliciesnetpolnetworking.k8s.io/v1trueNetworkPolicy
runtimeclassesnode.k8s.io/v1falseRuntimeClass
poddisruptionbudgetspdbpolicy/v1truePodDisruptionBudget
podsecuritypoliciespsppolicy/v1beta1falsePodSecurityPolicy
clusterrolebindingsrbac.authorization.k8s.io/v1falseClusterRoleBinding
clusterrolesrbac.authorization.k8s.io/v1falseClusterRole
rolebindingsrbac.authorization.k8s.io/v1trueRoleBinding
rolesrbac.authorization.k8s.io/v1trueRole
priorityclassespcscheduling.k8s.io/v1falsePriorityClass
csidriversstorage.k8s.io/v1falseCSIDriver
csinodesstorage.k8s.io/v1falseCSINode
csistoragecapacitiesstorage.k8s.io/v1trueCSIStorageCapacity
storageclassesscstorage.k8s.io/v1falseStorageClass
volumeattachmentsstorage.k8s.io/v1falseVolumeAttachment

5. 查看可用的 API 版本

该命令可以应用于使用编程方式访问 Kubernetes API 时。

root@k8s-01:~# kubectl api-versions
admissionregistration.k8s.io/v1
apiextensions.k8s.io/v1
apiregistration.k8s.io/v1
apps/v1
authentication.k8s.io/v1
authorization.k8s.io/v1
autoscaling/v1
autoscaling/v2beta1
autoscaling/v2beta2
batch/v1
batch/v1beta1
certificates.k8s.io/v1
coordination.k8s.io/v1
crd.projectcalico.org/v1
discovery.k8s.io/v1
discovery.k8s.io/v1beta1
events.k8s.io/v1
events.k8s.io/v1beta1
flowcontrol.apiserver.k8s.io/v1beta1
networking.k8s.io/v1
node.k8s.io/v1
node.k8s.io/v1beta1
policy/v1
policy/v1beta1
rbac.authorization.k8s.io/v1
scheduling.k8s.io/v1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1

6.查看Kubectl 上下文和配置

查看kubeconfig文件,可以用来做认证文件。

root@k8s-01:~# kubectl config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://10.117.136.98:6443
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: kubernetes-admin
  name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED

7. 查看集群配置

如果用的是kubeadm创建的集群,可以使用下面命令查看集群配置

kubectl get cm -o yaml -n kube-system kubeadm-config

该命令等效于kubeadm config view(已经被弃用)

root@k8s-01:~# kubectl get cm -o yaml -n kube-system kubeadm-config
apiVersion: v1
data:
  ClusterConfiguration: |
    apiServer:
      extraArgs:
        authorization-mode: Node,RBAC
      timeoutForControlPlane: 4m0s
    apiVersion: kubeadm.k8s.io/v1beta3
    certificatesDir: /etc/kubernetes/pki
    clusterName: kubernetes
    controllerManager: {}
    dns: {}
    etcd:
      local:
        dataDir: /var/lib/etcd
    imageRepository: registry.aliyuncs.com/google_containers
    kind: ClusterConfiguration
    kubernetesVersion: v1.22.1
    networking:
      dnsDomain: cluster.local
      podSubnet: 10.244.0.0/16
      serviceSubnet: 10.96.0.0/12
    scheduler: {}
kind: ConfigMap
metadata:
  creationTimestamp: "2022-09-01T13:04:28Z"
  name: kubeadm-config
  namespace: kube-system
  resourceVersion: "211"
  uid: c519ae1b-a820-4d30-9803-c46bc84b2e0f
root@k8s-01:~#

其中,data字段的值是集群配置。从结果中可以查看

  • API服务器的鉴权模式(authorization mode)

  • 集群名

  • etc的存储位置

  • 使用的镜像仓库是什么

  • kubernetes版本

  • DNS域名

  • pod的子网网段

  • 服务的子网网段

8. 参考资料

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NOWSHUT

给点饭钱谢谢,我会努力更新!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值