k8s命令式对象管理、命令式对象配置、声明式对象配置管理资源介绍

本文主要是介绍k8s命令式对象管理、命令式对象配置、声明式对象配置管理资源介绍,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

一.kubernetes资源管理简介

二.三种资源管理方式优缺点比较

三.命令式对象管理介绍

1.kubectl命令语法格式

2.资源类型

(1)通过“kubectl api-resources”来查看所有的资源

(2)每列含义

(3)常用资源介绍 ​编辑

3.kubectl命令支持命令

(1)获取命令帮助

(2)常用命令介绍 ​编辑

4.常用命令示例

四.命令式对象配置介绍

五.声明式对象配置

1.kubectl apply

2.操作多个文件内的资源


一.kubernetes资源管理简介

1.实际上kubernetes是一个集群管理系统,管理者在这个系统中运行容器、将程序运行在容器中,以此来部署服务。

2.在前面介绍k8s的工作流程来看,kubernetes的最小管理单元是pod,需要将容器放在pod中,再通过pod控制器来管理pod。

3.考虑到pod服务的相关管理,kubernetes支持service服务管理、便于数据持久化的存储系统等。其中,pod控制器、service管理、数据存储是关键技能。

二.三种资源管理方式优缺点比较

管理方式描述操作对象和使用环境优点缺点示例代码
命令式对象管理使用命令直接操作资源操作”对象“,适用于测试环境简易上手不支持审计跟踪
命令式对象配置使用命令配置和文件配置操作资源操作“文件”,适用于开发环境支持审计跟踪大项目配置文件繁琐时,很麻烦
声明式对象配置apply命令和配置文件操作资源操作“目录或文件”,适用于开发环境支持对目录的操作发生意外情况,资源将变得难以调试管理

三.命令式对象管理介绍

直接使用命令行

1.kubectl命令语法格式

kubectl 命令 资源类型 资源名称 (其他参数)
类别可选项
命令要对资源进行的操作,create、get等
资源类型pod、node、deployment等,具体看下文
资源名称此项对大小写很敏感,像pod名称等

2.资源类型

(1)通过“kubectl api-resources”来查看所有的资源

[root@k8s-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
selfsubjectreviews                             authentication.k8s.io/v1               false        SelfSubjectReview
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/v2                         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
bgpconfigurations                              crd.projectcalico.org/v1               false        BGPConfiguration
bgppeers                                       crd.projectcalico.org/v1               false        BGPPeer
blockaffinities                                crd.projectcalico.org/v1               false        BlockAffinity
caliconodestatuses                             crd.projectcalico.org/v1               false        CalicoNodeStatus
clusterinformations                            crd.projectcalico.org/v1               false        ClusterInformation
felixconfigurations                            crd.projectcalico.org/v1               false        FelixConfiguration
globalnetworkpolicies                          crd.projectcalico.org/v1               false        GlobalNetworkPolicy
globalnetworksets                              crd.projectcalico.org/v1               false        GlobalNetworkSet
hostendpoints                                  crd.projectcalico.org/v1               false        HostEndpoint
ipamblocks                                     crd.projectcalico.org/v1               false        IPAMBlock
ipamconfigs                                    crd.projectcalico.org/v1               false        IPAMConfig
ipamhandles                                    crd.projectcalico.org/v1               false        IPAMHandle
ippools                                        crd.projectcalico.org/v1               false        IPPool
ipreservations                                 crd.projectcalico.org/v1               false        IPReservation
kubecontrollersconfigurations                  crd.projectcalico.org/v1               false        KubeControllersConfiguration
networkpolicies                                crd.projectcalico.org/v1               true         NetworkPolicy
networksets                                    crd.projectcalico.org/v1               true         NetworkSet
endpointslices                                 discovery.k8s.io/v1                    true         EndpointSlice
events                            ev           events.k8s.io/v1                       true         Event
flowschemas                                    flowcontrol.apiserver.k8s.io/v1beta3   false        FlowSchema
prioritylevelconfigurations                    flowcontrol.apiserver.k8s.io/v1beta3   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
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/v1                      true         CSIStorageCapacity
storageclasses                    sc           storage.k8s.io/v1                      false        StorageClass
volumeattachments                              storage.k8s.io/v1                      false        VolumeAttachment

(2)每列含义

NAME表示资源名称

SHORTNAMES表示该资源的缩写

APIVERSION表示API版本

NAMESPACED表示是否使用命名空间隔离

KIND表示API类型

(3)常用资源介绍 

3.kubectl命令支持命令

(1)获取命令帮助

[root@k8s-master ~]# 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 stdinexpose          Take a replication controller, service, deployment or pod and expose it as a new Kubernetes servicerun             Run a particular image on the clusterset             Set specific features on objects
​
Basic Commands (Intermediate):explain         Get documentation for a resourceget             Display one or many resourcesedit            Edit a resource on the serverdelete          Delete resources by file names, stdin, resources and names, or by resources and label selector
​
Deploy Commands:rollout         Manage the rollout of a resourcescale           Set a new size for a deployment, replica set, or replication controllerautoscale       Auto-scale a deployment, replica set, stateful set, or replication controller
​
Cluster Management Commands:certificate     Modify certificate resourcescluster-info    Display cluster informationtop             Display resource (CPU/memory) usagecordon          Mark node as unschedulableuncordon        Mark node as schedulabledrain           Drain node in preparation for maintenancetaint           Update the taints on one or more nodes
​
Troubleshooting and Debugging Commands:describe        Show details of a specific resource or group of resourceslogs            Print the logs for a container in a podattach          Attach to a running containerexec            Execute a command in a containerport-forward    Forward one or more local ports to a podproxy           Run a proxy to the Kubernetes API servercp              Copy files and directories to and from containersauth            Inspect authorizationdebug           Create debugging sessions for troubleshooting workloads and nodesevents          List events
​
Advanced Commands:diff            Diff the live version against a would-be applied versionapply           Apply a configuration to a resource by file name or stdinpatch           Update fields of a resourcereplace         Replace a resource by file name or stdinwait            Experimental: Wait for a specific condition on one or many resourceskustomize       Build a kustomization target from a directory or URL
​
Settings Commands:label           Update the labels on a resourceannotate        Update the annotations on a resourcecompletion      Output shell completion code for the specified shell (bash, zsh, fish, or powershell)
​
Other Commands:api-resources   Print the supported API resources on the serverapi-versions    Print the supported API versions on the server, in the form of "group/version"config          Modify kubeconfig filesplugin          Provides utilities for interacting with pluginsversion         Print the client and server version information
​
Usage:kubectl [flags] [options]

(2)常用命令介绍 

4.常用命令示例

(1)查看所有的pod或指定名称空间下的pod

[root@k8s-master ~]# kubectl get pods -A
​
[root@k8s-master ~]# kubectl get pods -n kube-system

(2)查看所有节点和名称空间

[root@k8s-master ~]# kubectl get nodes
NAME         STATUS   ROLES           AGE   VERSION
k8s-master   Ready    control-plane   23m   v1.28.2
k8s-node1    Ready    <none>          22m   v1.28.2
k8s-node2    Ready    <none>          22m   v1.28.2
[root@k8s-master ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   23m
kube-node-lease   Active   23m
kube-public       Active   23m
kube-system       Active   23m

(3)查看资源的具体文档

[root@k8s-master ~]# kubectl explain pod
KIND:       Pod
VERSION:    v1
​
DESCRIPTION:Pod is a collection of containers that can run on a host. This resource iscreated 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, andmay 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 objectrepresents. Servers may infer this from the endpoint the client submitsrequests to. Cannot be updated. In CamelCase. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
​metadata  <ObjectMeta>Standard object's metadata. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
​spec  <PodSpec>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    <PodStatus>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
​
​
[root@k8s-master ~]# kubectl explain pod.spec #以这种形式指定查看具体某个部分的文档,一层一层

(4)显示资源内部详细信息,多用于节点排错分析

需要在pod后指定pod名称,-n指定该pod位于的名称空间

[root@k8s-master ~]# kubectl describe pod calico-node-58k7l -n kube-system

(5)创建名称空间并在其中运行pod

[root@k8s-master ~]# kubectl create ns myns
namespace/myns created
[root@k8s-master ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   38m
kube-node-lease   Active   38m
kube-public       Active   38m
kube-system       Active   38m
myns              Active   3s
[root@k8s-master ~]# kubectl run pod --image=nginx -n myns
pod/pod created
[root@k8s-master ~]# kubectl get pods -n myns
NAME   READY   STATUS    RESTARTS   AGE
pod    1/1     Running   0          22s

(6)删除pod和namespace

[root@k8s-master ~]# kubectl get pods -n myns
NAME   READY   STATUS    RESTARTS   AGE
pod    1/1     Running   0          22s
[root@k8s-master ~]# kubectl delete pod pod -n myns
pod "pod" deleted
[root@k8s-master ~]# kubectl delete ns myns
namespace "myns" deleted
​
[root@k8s-master ~]# kubectl get pods -n myns
No resources found in myns namespace.

(6)以宽格式显示

[root@k8s-master ~]# kubectl get nodes -o wide
NAME         STATUS   ROLES           AGE     VERSION   INTERNAL-IP     EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION           CONTAINER-RUNTIME
k8s-master   Ready    control-plane   9m21s   v1.28.2   192.168.2.150   <none>        CentOS Linux 7 (Core)   3.10.0-1160.el7.x86_64   containerd://1.6.24
k8s-node1    Ready    <none>          9m3s    v1.28.2   192.168.2.151   <none>        CentOS Linux 7 (Core)   3.10.0-1160.el7.x86_64   containerd://1.6.24
k8s-node2    Ready    <none>          9m1s    v1.28.2   192.168.2.152   <none>        CentOS Linux 7 (Core)   3.10.0-1160.el7.x86_64   containerd://1.6.24

四.命令式对象配置介绍

使用命令和文件进行操作

示例:通过kubectl命令+yaml文件,创建myns名称空间,再在其中运行nginx的pod

[root@k8s-master ~]# cat nginxpod.yaml 
apiVersion: v1
kind: Namespace        #指定类型为namespace
metadata:             #创建名称空间name: myns
​
---
​
apiVersion: v1
kind: Pod
metadata:name: nginxpodnamespace: myns
spec:containers:                  #使用nginx镜像进行运行pod- name: nginx-containersimage: nginx
​
[root@k8s-master ~]# kubectl create -f nginxpod.yaml   #指定文件进行创建
namespace/myns created
pod/nginxpod created
[root@k8s-master ~]# kubectl get -f nginxpod.yaml     #获取文件中的所有资源的状态
NAME             STATUS   AGE
namespace/myns   Active   6s
​
NAME           READY   STATUS    RESTARTS   AGE
pod/nginxpod   1/1     Running   0          6s
​
[root@k8s-master ~]# kubectl delete -f  nginxpod.yaml    #删除文件中所有资源,这里包括namespace和pod
namespace "myns" deleted
pod "nginxpod" deleted

五.声明式对象配置

基本上同命令式对象配置,但只有apply命令

1.kubectl apply

apply -f 指定配置资源文件,资源存在时重复执行后会报资源未发生改变(相当于kubectl patch更新),在声明式对象配置中仍然是可以进行文件内资源的get和delete

[root@k8s-master ~]# kubectl apply -f nginxpod.yaml 
namespace/myns created
pod/nginxpod created
[root@k8s-master ~]# kubectl apply -f nginxpod.yaml 
namespace/myns unchanged
pod/nginxpod unchanged
[root@k8s-master ~]# kubectl get -f nginxpod.yaml 
NAME             STATUS   AGE
namespace/myns   Active   16s
​
NAME           READY   STATUS    RESTARTS   AGE
pod/nginxpod   1/1     Running   0          16s
[root@k8s-master ~]# kubectl delete -f nginxpod.yaml 
namespace "myns" deleted
pod "nginxpod" deleted

2.操作多个文件内的资源

[root@k8s-master nginx]# ll
total 8
-rw-r--r-- 1 root root 197 Oct 29 20:19 nginxpod1.yaml
-rw-r--r-- 1 root root 197 Oct 29 20:20 nginxpod2.yaml
[root@k8s-master nginx]# kubectl apply -f ./
namespace/myns1 created
pod/nginxpod created
namespace/myns2 created
pod/nginxpod created
[root@k8s-master nginx]# kubectl get -f ./
NAME              STATUS   AGE
namespace/myns1   Active   15s
​
NAME           READY   STATUS    RESTARTS   AGE
pod/nginxpod   1/1     Running   0          15s
​
NAME              STATUS   AGE
namespace/myns2   Active   15s
​
NAME           READY   STATUS              RESTARTS   AGE
pod/nginxpod   0/1     ContainerCreating   0          15s
[root@k8s-master nginx]# kubectl delete -f ./
namespace "myns1" deleted
pod "nginxpod" deleted
namespace "myns2" deleted
pod "nginxpod" deleted

这篇关于k8s命令式对象管理、命令式对象配置、声明式对象配置管理资源介绍的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/303709

相关文章

Zookeeper安装和配置说明

一、Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及伪集群模式。 ■ 单机模式:Zookeeper只运行在一台服务器上,适合测试环境; ■ 伪集群模式:就是在一台物理机上运行多个Zookeeper 实例; ■ 集群模式:Zookeeper运行于一个集群上,适合生产环境,这个计算机集群被称为一个“集合体”(ensemble) Zookeeper通过复制来实现

CentOS7安装配置mysql5.7 tar免安装版

一、CentOS7.4系统自带mariadb # 查看系统自带的Mariadb[root@localhost~]# rpm -qa|grep mariadbmariadb-libs-5.5.44-2.el7.centos.x86_64# 卸载系统自带的Mariadb[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7

性能测试介绍

性能测试是一种测试方法,旨在评估系统、应用程序或组件在现实场景中的性能表现和可靠性。它通常用于衡量系统在不同负载条件下的响应时间、吞吐量、资源利用率、稳定性和可扩展性等关键指标。 为什么要进行性能测试 通过性能测试,可以确定系统是否能够满足预期的性能要求,找出性能瓶颈和潜在的问题,并进行优化和调整。 发现性能瓶颈:性能测试可以帮助发现系统的性能瓶颈,即系统在高负载或高并发情况下可能出现的问题

水位雨量在线监测系统概述及应用介绍

在当今社会,随着科技的飞速发展,各种智能监测系统已成为保障公共安全、促进资源管理和环境保护的重要工具。其中,水位雨量在线监测系统作为自然灾害预警、水资源管理及水利工程运行的关键技术,其重要性不言而喻。 一、水位雨量在线监测系统的基本原理 水位雨量在线监测系统主要由数据采集单元、数据传输网络、数据处理中心及用户终端四大部分构成,形成了一个完整的闭环系统。 数据采集单元:这是系统的“眼睛”,

hadoop开启回收站配置

开启回收站功能,可以将删除的文件在不超时的情况下,恢复原数据,起到防止误删除、备份等作用。 开启回收站功能参数说明 (1)默认值fs.trash.interval = 0,0表示禁用回收站;其他值表示设置文件的存活时间。 (2)默认值fs.trash.checkpoint.interval = 0,检查回收站的间隔时间。如果该值为0,则该值设置和fs.trash.interval的参数值相等。

NameNode内存生产配置

Hadoop2.x 系列,配置 NameNode 内存 NameNode 内存默认 2000m ,如果服务器内存 4G , NameNode 内存可以配置 3g 。在 hadoop-env.sh 文件中配置如下。 HADOOP_NAMENODE_OPTS=-Xmx3072m Hadoop3.x 系列,配置 Nam

Hadoop数据压缩使用介绍

一、压缩原则 (1)运算密集型的Job,少用压缩 (2)IO密集型的Job,多用压缩 二、压缩算法比较 三、压缩位置选择 四、压缩参数配置 1)为了支持多种压缩/解压缩算法,Hadoop引入了编码/解码器 2)要在Hadoop中启用压缩,可以配置如下参数

wolfSSL参数设置或配置项解释

1. wolfCrypt Only 解释:wolfCrypt是一个开源的、轻量级的、可移植的加密库,支持多种加密算法和协议。选择“wolfCrypt Only”意味着系统或应用将仅使用wolfCrypt库进行加密操作,而不依赖其他加密库。 2. DTLS Support 解释:DTLS(Datagram Transport Layer Security)是一种基于UDP的安全协议,提供类似于

综合安防管理平台LntonAIServer视频监控汇聚抖动检测算法优势

LntonAIServer视频质量诊断功能中的抖动检测是一个专门针对视频稳定性进行分析的功能。抖动通常是指视频帧之间的不必要运动,这种运动可能是由于摄像机的移动、传输中的错误或编解码问题导致的。抖动检测对于确保视频内容的平滑性和观看体验至关重要。 优势 1. 提高图像质量 - 清晰度提升:减少抖动,提高图像的清晰度和细节表现力,使得监控画面更加真实可信。 - 细节增强:在低光条件下,抖

【Python编程】Linux创建虚拟环境并配置与notebook相连接

1.创建 使用 venv 创建虚拟环境。例如,在当前目录下创建一个名为 myenv 的虚拟环境: python3 -m venv myenv 2.激活 激活虚拟环境使其成为当前终端会话的活动环境。运行: source myenv/bin/activate 3.与notebook连接 在虚拟环境中,使用 pip 安装 Jupyter 和 ipykernel: pip instal