linux cpu占用率如何看
244
2022-09-10
#云原生征文#深入Kubernetes(k8s)概念
深入Kubernetes(k8s)概念
一、基础概念理解
集群
masterworker
Node
Pod
应用最终以Pod为一个基本单位部署
Label
很多资源都可以打标签
Deployment
应用部署用它,deployment最终会产生Pod
Service
负载均衡机制
二、kubernetes Objects(k8s对象)
1、什么是k8s对象
官方文档地址: 理解 Kubernetes 对象 | Kubernetes
集群中运行了哪些容器化应用程序(以及在哪个节点上运行)集群中对应用程序可用的资源(网络,存储等)应用程序相关的策略定义,例如,重启策略、升级策略、容错策略其他Kubernetes管理应用程序时所需要的信息 scheduler先计算应该去哪个节点部署
对象的spec和status每一个 Kubernetes 对象都包含了两个重要的字段:spec 必须由您来提供,描述了您对该对象所期望的 目标状态status 只能由 Kubernetes 系统来修改,描述了该对象在 Kubernetes 系统中的 实际状态Kubernetes通过对应的 控制器,不断地使实际状态趋向于您期望的目标状态### kubectl create deployment my-nginx --image=nginx apiVersion: apps/v1 kind: Deployment metadata: annotations: deployment.kubernetes.io/revision: "1" creationTimestamp: "2022-05-27T11:37:59Z" generation: 1 labels: app: my-nginx name: my-nginx namespace: default resourceVersion: "376418" uid: 5a47e879-e2e9-40d1-8b02-180031903e8a spec: ###期望状态 progressDeadlineSeconds: 600 replicas: 1 ### 副本数量 revisionHistoryLimit: 10 selector: matchLabels: app: my-nginx strategy: rollingUpdate: maxSurge: 25% maxUnavailable: 25% type: RollingUpdate template: metadata: creationTimestamp: null labels: app: my-nginx spec: containers: - image: nginx ###使用这个镜像创建容器 imagePullPolicy: Always name: nginx resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: File dnsPolicy: ClusterFirst restartPolicy: Always schedulerName: default-scheduler securityContext: {} terminationGracePeriodSeconds: 30 status: ###当前状态 availableReplicas: 1 ## 当前集群可用的 conditions: - lastTransitionTime: "2022-05-27T11:38:17Z" lastUpdateTime: "2022-05-27T11:38:17Z" message: Deployment has minimum availability. reason: MinimumReplicasAvailable status: "True" type: Available - lastTransitionTime: "2022-05-27T11:37:59Z" lastUpdateTime: "2022-05-27T11:38:17Z" message: ReplicaSet "my-nginx-6b74b79f57" has successfully progressed. reason: NewReplicaSetAvailable status: "True" type: Progressing observedGeneration: 1 readyReplicas: 1 replicas: 1 updatedReplicas: 1 ### 最终一致。 ## etcd保存的创建资源期望的状态和最终这个资源的状态要是一致的;spec和status要最终一致 ## 1、kubectl create deployment my-nginx --image=nginx ## 2、api-server保存etcd,controller-manager最终解析数据,知道集群要my-nginx一份,保存到etcd ## 3、kubelet就做一件事情,spec状态和最终状态一致 while(true){ if(my-nginx.replicas != spec.replicas) { kubelet.startPod(); } }
2、描述k8s对象
自己编写任意资源的yaml都可以创建出他如何会写任意资源的yaml,比如Pod编写yaml的黑科技 kubectl run my-nginx666 --image=nginx #启动一个Pod 1、kubectl get pod my-nginx666 -oyaml 集群中挑一个同类资源,获取出他的yaml。 2、kubectl run my-tomcat --image=tomcat --dry-run -oyaml 干跑一遍
kind: Pod #资源类型 kubectl api-resources:可以获取到所有资源apiVersion: v1 #同一个资源有可能有多个版本。看 kubectl api-resources提示的。metadata: #每一个资源定义一些元数据信息 labels: run: my-tomcat name: my-tomcatspec: #资源的规格(镜像名、镜像的环境变量信息等等) containers: - image: tomcat name: my-tomcat resources: {} dnsPolicy: ClusterFirst restartPolicy: Always
当你在 Kubernetes 中创建一个对象时,必须提供
该对象的 spec 字段,通过该字段描述您期望的目标状态该对象的一些基本信息,例如名字
可以使用 kubectl 命令行创建对象,也可以编写 .yaml 格式的文件进行创建
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: selector: matchLabels: app: nginx replicas: 2 # 运行 2 个容器化应用程序副本 template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.7.9 ports: - containerPort: 80
#1、部署kubectl apply -f deployment.yaml#2、移除kubectl delete -f deployment.yaml
3、k8s对象yaml的结构
必填字段
在上述的 .yaml 文件中,如下字段是必须填写的:
apiVersion 用来创建对象时所使用的Kubernetes API版本kind 被创建对象的类型metadata 用于唯一确定该对象的元数据:包括 name 和 namespace,如果 namespace 为空,则默认值为 defaultspec 描述您对该对象的期望状态
不同类型的 Kubernetes,其 spec 对象的格式不同(含有不同的内嵌字段),通过API 手册可以查看 Kubernetes 对象的字段和描述。例如,假设您想了解 Pod 的 spec 定义,可以在这里找到,Deployment 的 spec 定义可以在这里找到
Kubernetes API Reference Docs 这就是我们以后要完全参照的文档。
4、管理k8s对象
同一个Kubernetes对象应该只使用一种方式管理,否则可能会出现不可预期的结果
5、对象名称
Kubernetes REST API 中,所有的对象都是通过 name 和 UID 唯一性确定
可以通过 namespace + name 唯一性地确定一个 RESTFUL 对象,例如:
/api/v1/namespaces/{namespace}/pods/{name}
Names
同一个名称空间下,同一个类型的对象,可以通过 name 唯一性确定。如果删除该对象之后,可以再重新创建一个同名对象。
依据命名规则,Kubernetes对象的名字应该:
最长不超过 253个字符必须由小写字母、数字、减号-、小数点 . 组成某些资源类型有更具体的要求
例如,下面的配置文件定义了一个 name 为 nginx-demo 的 Pod,该 Pod 包含一个 name 为 nginx 的容器:
apiVersion: v1 kind: Pod metadata: name: nginx-demo ##pod的名字 spec: containers: - name: nginx ##容器的名字 image: nginx:1.7.9 ports: - containerPort: 80 UIDs UID
UID 是由 Kubernetes 系统生成的,唯一标识某个 Kubernetes 对象的字符串。
Kubernetes集群中,每创建一个对象,都有一个唯一的 UID。用于区分多次创建的同名对象(如前所述,按照名字删除对象后,重新再创建同名对象时,两次创建的对象 name 相同,但是 UID 不同。)
6、名称空间
kubectl get namespaceskubectl describe namespaces
Kubernetes 安装成功后,默认有初始化了三个名称空间:
default 默认名称空间,如果 Kubernetes 对象中不定义 metadata.namespace 字段,该对象将放在此名称空间下kube-system Kubernetes系统创建的对象放在此名称空间下kube-public 此名称空间自动在安装集群是自动创建,并且所有用户都是可以读取的(即使是那些未登录的用户)。主要是为集群预留的,例如,某些情况下,某些Kubernetes对象应该被所有集群用户看到。
名称空间未来如何隔离1)、基于环境隔离(prod,test)prod:部署的所有应用test:部署的所有应用2)、基于产品线的名称空间(商城,android,ios,backend);3)、基于团队隔访问其他名称空间的东西?(名称空间资源隔离,网络不隔离)1)、配置直接拿来用。不行2)、网络访问,可以。Pod-Pod;serviceName来访问,找本名称空间的Service负载均衡serviceName.名称空间,可以访问别的名称空间的
创建名称空间
apiVersion: v1 kind: Namespace metadata: name: <名称空间的名字>apiVersion: v1 kind: Namespace metadata: creationTimestamp: null name: k8s-03 spec: {} status: {}
kubectl create -f ./my-namespace.yaml
#直接用命令 kubectl create namespace <名称空间的名字>#删除 kubectl delete namespaces <名称空间的名字>
名称空间的名字必须与 DNS 兼容:
不能带小数点.不能带下划线_使用数字、小写字母和减号- 组成的字符串
默认情况下,安装Kubernetes集群时,会初始化一个 default 名称空间,用来将承载那些未指定名称空间的 Pod、Service、Deployment等对象
为请求设置命名空间
#要为当前请求设置命名空间,请使用 --namespace 参数。kubectl run nginx --image=nginx --namespace=
#在对象yaml中使用命名空间 apiVersion: v1 kind: Pod metadata: name: nginx-demo ##pod的名字 namespace: default #不写就是default spec: containers: - name: nginx ##容器的名字 image: nginx:1.7.9 ports: - containerPort: 80
当您创建一个 Service 时,Kubernetes 会创建一个相应的DNS条目。
该条目的形式是
# 创建Pod kind:Pod k8s底层最小的部署单元是Pod。Service,Deploy,ReplicaSet # Deploy:直接指定Pod模板() kind: Deploy
并非所有对象都在命名空间中
大多数 kubernetes 资源(例如 Pod、Service、副本控制器等)都位于某些命名空间中。但是命名空间资源本身并不在命名空间中。而且底层资源,例如nodes和持久化卷不属于任何命名空间。
查看哪些 Kubernetes 资源在命名空间中,哪些不在命名空间中:
# In a namespacekubectl api-resources --namespaced=true# Not in a namespacekubectl api-resources --namespaced=false
7、标签和选择器
标签(Label)是附加在Kubernetes对象上的一组名值对,其意图是按照对用户有意义的方式来标识Kubernetes对象,同时,又不对Kubernetes的核心逻辑产生影响。标签可以用来组织和选择一组Kubernetes对象。您可以在创建Kubernetes对象时为其添加标签,也可以在创建以后再为其添加标签。每个Kubernetes对象可以有多个标签,同一个对象的标签的 Key 必须唯一,例如:
metadata: labels: key1: value1 key2: value2
使用标签(Label)可以高效地查询和监听Kubernetes对象,在Kubernetes界面工具(如 Kubenetes Dashboard 或 Kuboard)和 kubectl 中,标签的使用非常普遍。那些非标识性的信息应该记录在 注解(annotation)
为什么要使用标签
使用标签,用户可以按照自己期望的形式组织 Kubernetes 对象之间的结构,而无需对 Kubernetes 有任何修改。
应用程序的部署或者批处理程序的部署通常都是多维度的(例如,多个高可用分区、多个程序版本、多个微服务分层)。管理这些对象时,很多时候要针对某一个维度的条件做整体操作,例如,将某个版本的程序整体删除,这种情况下,如果用户能够事先规划好标签的使用,再通过标签进行选择,就会非常地便捷。
标签的例子有:
release: stable、release: canaryenvironment: dev、environment: qa、environment: productiontier: frontend、tier: backend、tier: cachepartition: customerA、partition: customerBtrack: daily、track: weekly
上面只是一些使用比较普遍的标签,您可以根据您自己的情况建立合适的使用标签的约定。
句法和字符集
标签是一组名值对(key/value pair)。标签的 key 可以有两个部分:可选的前缀和标签名,通过 / 分隔。
标签名:
标签名部分是必须的不能多于 63 个字符必须由字母、数字开始和结尾可以包含字母、数字、减号-、下划线_、小数点.
标签前缀:
标签前缀部分是可选的如果指定,必须是一个DNS的子域名,例如:k8s.eip.work不能多于 253 个字符使用/ 和标签名分隔
如果省略标签前缀,则标签的 key 将被认为是专属于用户的。Kubernetes的系统组件(例如,kube-scheduler、kube-controller-manager、kube-apiserver、kubectl 或其他第三方组件)向用户的Kubernetes对象添加标签时,必须指定一个前缀。kubernetes.io/ 和 k8s.io/ 这两个前缀是 Kubernetes 核心组件预留的。
标签的 value 必须:
不能多于 63 个字符可以为空字符串如果不为空,则
必须由字母、数字开始和结尾可以包含字母、数字、减号-、下划线_、小数点.
apiVersion: v1 kind: Pod metadata: name: label-demo labels: environment: production app: nginx spec: containers: - name: nginx image: nginx:1.7.9 ports: - containerPort: 80
标签选择器
通常来讲,会有多个Kubernetes对象包含相同的标签。通过使用标签选择器(label selector),用户/客户端可以选择一组对象。标签选择器(label selector)是 Kubernetes 中最主要的分类和筛选手段。
Kubernetes api server支持两种形式的标签选择器,equality-based 基于等式的 和 set-based 基于集合的。标签选择器可以包含多个条件,并使用逗号分隔,此时只有满足所有条件的 Kubernetes 对象才会被选中
使用基于等式的选择方式,可以使用三种操作符 =、==、!=。前两个操作符含义是一样的,都代表相等,后一个操作符代表不相等
kubectl get pods -l environment=production,tier=frontend# 选择了标签名为 `environment` 且 标签值为 `production` 的Kubernetes对象environment = production# 选择了标签名为 `tier` 且标签值不等于 `frontend` 的对象,以及不包含标签 `tier` 的对象tier != frontend
使用基于集合的选择方式
Set-based 标签选择器可以根据标签名的一组值进行筛选。支持的操作符有三种:in、notin、exists。例如# 选择所有的包含 `environment` 标签且值为 `production` 或 `qa` 的对象environment in (production, qa)# 选择所有的 `tier` 标签不为 `frontend` 和 `backend`的对象,或不含 `tier` 标签的对象tier notin (frontend, backend)# 选择所有包含 `partition` 标签的对象partition# 选择所有不包含 `partition` 标签的对象!partition# 选择包含 `partition` 标签(不检查标签值)且 `environment` 不是 `qa` 的对象partition,environment notin (qa)kubectl get pods -l 'environment in (production),tier in (frontend)'
#Job、Deployment、ReplicaSet 和 DaemonSet 同时支持基于等式的选择方式和基于集合的选择方式。例如: selector: matchLabels: component: redis matchExpressions: - {key: tier, operator: In, values: [cache]} - {key: environment, operator: NotIn, values: [dev]}# matchLabels 是一个 {key,value} 组成的 map。map 中的一个 {key,value} 条目相当于 matchExpressions 中的一个元素,其 key 为 map 的 key,operator 为 In, values 数组则只包含 value 一个元素。matchExpression 等价于基于集合的选择方式,支持的 operator 有 In、NotIn、Exists 和 DoesNotExist。当 operator 为 In 或 NotIn 时,values 数组不能为空。所有的选择条件都以 AND 的形式合并计算,即所有的条件都满足才可以算是匹配
#添加或者修改标签kubectl label --help# Update pod 'foo' with the label 'unhealthy' and the value 'true'.kubectl label pods foo unhealthy=true# Update pod 'foo' with the label 'status' and the value 'unhealthy', overwriting any existing value.kubectl label --overwrite pods foo status=unhealthy# Update all pods in the namespacekubectl label pods --all status=unhealthy# Update a pod identified by the type and name in "pod.json"kubectl label -f pod.json status=unhealthy# Update pod 'foo' only if the resource is unchanged from version 1.kubectl label pods foo status=unhealthy --resource-version=1# Update pod 'foo' by removing a label named 'bar' if it exists.# Does not require the --overwrite flag.kubectl label pods foo bar-
8、注解annotation
注解(annotation)可以用来向 Kubernetes 对象的 metadata.annotations 字段添加任意的信息。Kubernetes 的客户端或者自动化工具可以存取这些信息以实现其自定义的逻辑。
metadata: annotations: key1: value1 key2: value2
9、字段选择器
字段选择器(Field selectors)允许您根据一个或多个资源字段的值筛选 Kubernetes 资源。 下面是一些使用字段选择器查询的例子:
metadata.name=my-servicemetadata.namespace!=defaultstatus.phase=Pending
kubectl get pods --field-selector status.phase=Running
三个命令玩转所有的yaml写法‘kubectl get xxx -oyamlkubectl create deploy xxxxx --dry-run-client -oyamlkubectl explain pod.spec.xx
写完yaml kubectl apply -f 即可
10、给vscode安装插件
搜索kubernetes ,安装 yaml和kubernetes template插件即可
idea也有kubernetes插件
11、认识kubectl和kubelet
kubeadm安装的集群。二进制后来就是 yum install etcd api-server
认识核心文件夹 /etc/kubernetes . 以Pod方式安装的核心组件。
etcd,api-server,scheduler。(安装k8s的时候,yum kubeadmkubelet kubectl)
回顾集群安装的时候,为什么只有master节点的kubectl可以操作集群kubelet额外参数配置/etc/sysconfig/kubelet;kubelet配置位置 /var/lib/kubelet/config.yaml
kubectl的所有命令参考:
命令参考:Kubectl Reference Docs
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 newKubernetes Service run Run a particular image on the cluster set Set specific features on objectsBasic Commands (Intermediate): 基础命令 explain Documentation of resources get Display one or many resources edit Edit a resource on the server delete Delete resources by filenames, stdin, resources and names, or by resources and labelselectorDeploy 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 ReplicationControllerCluster Management Commands: #集群管理的命令 certificate Modify certificate resources. cluster-info Display cluster info 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 nodesTroubleshooting and Debugging Commands: # debug的命令 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 nodesAdvanced Commands: # 高阶命令 diff Diff live version against would-be applied version apply Apply a configuration to a resource by filename or stdin patch Update field(s) of a resource replace Replace a resource by filename 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 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 Modify kubeconfig files plugin Provides utilities for interacting with plugins. version Print the client and server version information
12、自动补全
官方文档地址:
# 安装yum install bash-completion# 自动补全echo 'source <(kubectl completion bash)' >>~/.bashrckubectl completion bash >/etc/bash_completion.d/kubectlsource /usr/share/bash-completion/bash_completion
【本文正在参加云原生有奖征文活动】,活动链接:https://ost./posts/12598
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~