Kubernetes--深入理解Pod资源管理

kubectl --help

kubectl 是 Kubernetes 命令行工具,允许用户与 Kubernetes 集群进行交互。通过 kubectl,用户可以创建、查看、更新、删除集群中的各种资源(如 Pod、Service、Deployment 等),并且可以调试和管理 Kubernetes 集群。

root@k8s-master01:~# kubectl --help
kubectl controls the Kubernetes cluster manager.

 Find more information at: https://kubernetes.io/docs/reference/kubectl/

Basic Commands (Beginner):
  create          Create a resource from a file or from stdin
  expose          Take a replication controller, service, deployment or pod and expose it as a new Kubernetes service
  run             Run a particular image on the cluster
  set             Set specific features on objects

Basic Commands (Intermediate):
  explain         Get documentation for a resource
  get             Display one or many resources
  edit            Edit a resource on the server
  delete          Delete resources by file names, 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, replica set, or replication controller
  autoscale       Auto-scale a deployment, replica set, stateful set, or replication controller

Cluster Management Commands:
  certificate     Modify certificate resources.
  cluster-info    Display cluster information
  top             Display resource (CPU/memory) usage
  cordon          Mark node as unschedulable
  uncordon        Mark node as schedulable
  drain           Drain node in preparation for maintenance
  taint           Update the taints on one or more nodes

Troubleshooting and Debugging Commands:
  describe        Show details of a specific resource or group of resources
  logs            Print the logs for a container in a pod
  attach          Attach to a running container
  exec            Execute a command in a container
  port-forward    Forward one or more local ports to a pod
  proxy           Run a proxy to the Kubernetes API server
  cp              Copy files and directories to and from containers
  auth            Inspect authorization
  debug           Create debugging sessions for troubleshooting workloads and nodes
  events          List events

Advanced Commands:
  diff            Diff the live version against a would-be applied version
  apply           Apply a configuration to a resource by file name or stdin
  patch           Update fields of a resource
  replace         Replace a resource by file name or 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           Update the labels on a resource
  annotate        Update the annotations on a resource
  completion      Output shell completion code for the specified shell (bash, zsh, fish, or powershell)

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          Modify kubeconfig files
  plugin          Provides utilities for interacting with plugins
  version         Print the client and server version information

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).

api-resources

kubectl api-resources 命令用于列出 Kubernetes API 中的所有资源及其对应的组、版本、类别等详细信息。在 Kubernetes中,资源是指各种对象类型,例如Pods、Services、Deployments 等,API是这些资源的入口点。通过 kubectl api-resources,你可以查看集群中支持的所有资源,并了解如何使用它们。

kubectl api-resources

以下是kubectl api-resources的部分截图
在这里插入图片描述
api-resources常见列项解释:

常见列项解释:
  1.NAME:列出了资源的名称。例如 pods 表示 Pod 资源。
  
  2.SHORTNAMES:资源名称的简写形式。例如 po是pods的简写,svc是services的简写。
  
  3.APIGROUP:资源的API组,Kubernetes 将API资源按组分类。核心资源(如 pods 和 services)没有API组,因此该列为空。
      3.1.核心API组的资源不需要在使用时指定API组。例如,kubectl get pods可以直接访问Pod资源。
      3.2.其他API组的资源需要通过 kubectl 指定 API 组,例如 kubectl get deployments.apps。
      
  4.NAMESPACED:如果为true,表示该资源是基于命名空间的,资源只能在特定命名空间内创建和管理。如果为 false,表示该资源在集群范围内全局可用。
  
  5.KIND:资源的类型或类。例如 Pod、Deployment、Services等。

api-versions

kubectl api-versions 是一个 Kubernetes 命令,用于列出集群当前支持的所有 API 版本。在 Kubernetes 中,API 是与集群交互的核心,API 版本管理允许 Kubernetes 保持向后兼容性,并引入新功能而不破坏现有的功能。

kubectl api-versions
常见的API版本包括:
  1. v1:核心API组中常见的版本,是最稳定的。
  2. apps/v1、batch/v1等:这些是非核心API组,包含不同功能模块的API版本。
  3. Alpha、Beta和Stable阶段的API版本,表示功能的成熟度。

在这里插入图片描述

输出的解释
  1.apps/v1:表示apps API组中的版本v1,用于管理应用相关资源(如 Deployment、StatefulSet 等)。
  2.batch/v1:表示batch API组中的版本v1,用于管理批处理作业(如 Job 和 CronJob)。
  3.autoscaling/v1:表示autoscaling API组中的版本v1,用于管理自动扩缩容功能(如   HorizontalPodAutoscaler)。
  4.v1:表示核心API组中的版本 v1,用于管理核心资源(如 Pod、Service、Node、Namespace 等)。

API 版本的阶段

Kubernetes中的API版本分为以下几个阶段:
  1.Alpha:
    可能有重大变更,不建议在生产环境中使用。
    默认情况下不启用,通常需要使用特定的配置来启用。
    例如:batch/v2alpha1

  2.Beta:
    较为稳定,且默认启用。
    可能仍然会有变更,但使用在生产环境中是可接受的。
    例如:apps/v1beta1。
  
  3.Stable:
    已经经过长期测试,并被认为是安全且稳定的。
    推荐在生产环境中使用。
    例如:apps/v1。

kubectl explain …

kubectl explain 是 Kubernetes 中用于查看资源的详细信息及其字段解释的命令。它帮助用户了解 Kubernetes 资源的结构和字段意义,特别是当你编写 YAML 文件或者需要修改资源时,kubectl explain 是一个非常有用的工具。

kubectl explain Deployment

打印Deployment的说明文档
在这里插入图片描述

kubectl explain Deployment.spec
root@k8s-master01:~# kubectl explain Deployment.spec
GROUP:      apps
KIND:       Deployment
VERSION:    v1

FIELD: spec <DeploymentSpec>

DESCRIPTION:
    Specification of the desired behavior of the Deployment.
    DeploymentSpec is the specification of the desired behavior of the
    Deployment.
    
FIELDS:
  minReadySeconds	<integer>
    Minimum number of seconds for which a newly created pod should be ready
    without any of its container crashing, for it to be considered available.
    Defaults to 0 (pod will be considered available as soon as it is ready)

  paused	<boolean>
    Indicates that the deployment is paused.

  progressDeadlineSeconds	<integer>
    The maximum time in seconds for a deployment to make progress before it is
    considered to be failed. The deployment controller will continue to process
    failed deployments and a condition with a ProgressDeadlineExceeded reason
    will be surfaced in the deployment status. Note that progress will not be
    estimated during the time a deployment is paused. Defaults to 600s.

  replicas	<integer>
    Number of desired pods. This is a pointer to distinguish between explicit
    zero and not specified. Defaults to 1.

  revisionHistoryLimit	<integer>
    The number of old ReplicaSets to retain to allow rollback. This is a pointer
    to distinguish between explicit zero and not specified. Defaults to 10.

  selector	<LabelSelector> -required-
    Label selector for pods. Existing ReplicaSets whose pods are selected by
    this will be the ones affected by this deployment. It must match the pod
    template's labels.

  strategy	<DeploymentStrategy>
    The deployment strategy to use to replace existing pods with new ones.

  template	<PodTemplateSpec> -required-
    Template describes the pods that will be created. The only allowed
    template.spec.restartPolicy value is "Always".

在这里插入图片描述

API资源

Kubernetes API 中的资源包括不同的对象类型,如 Pod、Service、Deployment 等。每个资源都可以使用 YAML 或 JSON 格式来定义,并通过 API server 进行管理。不同资源代表 Kubernetes 中不同的组件和配置。

Kubernetes 中的 API 资源是与 Kubernetes 对象和操作交互的核心。Kubernetes 的 API 提供了访问集群中所有资源的途径,无论是用 kubectl 命令行工具,还是通过编程接口进行操作。

API: RESTful API
  Resource
    Deployment --> demoapp --> Controller执行
    Service

    资源类型 --> 对象 --> 实体
       对象:用户期望的状态 
       实体:实际状态 

   小汽车:资源类型
     发动机:
     变速箱

资源规范

Pod

Pod:最基本的计算单元,容纳一个或多个容器,具有共享的网络和存储。

 Pod:
   资源规范:
     apiVersion: GROUP/VERSION
       ~# kubectl api-versions
     kind:KIND
       ~# kubectl api-resources
     metadata:
       name: <NAME>
         名称空间级别的资源:同一名称空间下,同一类型中,必须惟一;
       namespace: <NAMESPACE>
       labels: 
         key1: val1
         key2: val2
       annotations:
         key1: val1
         ...
       spec: 
         ~# kubectl explain KIND.spec
apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: mycontainer
    image: nginx

Service

Service:定义一组 Pod 的访问方式,通过固定 IP 或 DNS 名称对外暴露服务。

apiVersion: v1
kind: Service
metadata:
  name: myservice
spec:
  selector:
    app: myapp
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

ConfigMap

ConfigMap:用于存储配置信息的键值对。

apiVersion: v1
kind: ConfigMap
metadata:
  name: myconfigmap
data:
  key: value

Secret

Secret:用于存储敏感数据(如密码、token)。

apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
data:
  password: cGFzc3dvcmQ=

Namespace

Namespace名称空间是Kubernetes集群提供对内部资源进行“软隔离”的机制,以方便用户管理和组织集群资源。可以将其想像成虚拟的“子集群”

Kubernetes 中的 Namespace 是一种用于在同一个集群中划分多个虚拟集群的机制。通过使用命名空间,Kubernetes 可以将资源隔离开来,使多个团队或应用能够在同一个物理集群上安全、独立地工作。它主要用于资源的组织、权限的隔离以及对不同用户或应用组的资源配额管理。

Namespace 的作用

Namespace 的作用
  1.资源隔离:
      不同命名空间下的资源(如 Pods、Services、ConfigMaps 等)是相互隔离的。
      同名的资源可以存在于不同的命名空间中,不会产生冲突。
  
  2.权限管理:
      结合 Kubernetes 的 RBAC(基于角色的访问控制),可以在命名空间的级别上限制用户的权限。
      例如,一个用户只可以管理特定命名空间中的资源。
  
  3.资源配额:
      通过配置资源配额(Resource Quotas),
      可以限制某个命名空间可以使用的 CPU、内存等资源,防止一个团队或应用过度占用集群资源。
  
  4.简化多租户管理:
      对于有多个团队或项目共用同一个 Kubernetes 集群的情况,可以通过 Namespace 实现资源隔离,
      保证每个团队或项目有独立的工作空间。

Namespace 的使用场景

Namespace 的使用场景
  1.'开发、测试和生产环境的隔离':
      在实际工作中,可以为开发、测试和生产环境创建不同的命名空间,
      例如 dev、test 和 prod。
      这样即便是在同一个物理集群上,也可以确保不同环境中的应用相互独立,避免开发和测试中的错误影响到生产环境。

  2.'多租户系统':
      当多个团队或项目共享同一个 Kubernetes 集群时,可以为每个团队或项目分配独立的命名空间。
      每个团队只需要管理自己的命名空间,集群管理员可以通过资源配额和 RBAC 实现对资源使用的管理和限制。

  3.'微服务架构中的应用隔离':
       在微服务架构中,不同的服务可以部署在不同的命名空间中。
       例如,一个命名空间可以专门用于业务服务,另一个命名空间可以用于基础设施服务,如数据库、消息队列等。

常用的 Namespace 命令

kubectl get namespaces                     列出所有命名空间

kubectl create namespace <namespace-name>  创建命名空间

kubectl delete namespace <namespace-name>  删除命名空间

kubectl get pods -n <namespace-name>       查看某个命名空间中的资源

kubectl config set-context --current --namespace=<namespace-name>   切换命名空间的上下文

实际应用举例

示例 1:隔离开发、测试和生产环境

假设一个公司正在使用 Kubernetes 进行微服务部署,应用的生命周期分为开发(dev)、测试(test)和生产(prod)三个阶段。可以为每个阶段创建单独的命名空间:

kubectl create namespace dev
kubectl create namespace 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值