一、Kubernetes常用资源
K8s 中所有的内容都抽象为资源, 资源实例化之后, 叫做对象 工作负载型资源( workload ): Pod、 ReplicaSet、 Deployment、 StatefulSet、 DaemonSet、 Job、CronJob ( ReplicationController 在 v1.11 版本被废弃 )
服务发现及负载均衡型资源( ServiceDiscovery LoadBalance ): Service、 Ingress、 ...
配置与存储型资源: Volume( 存储卷 )、 CSI( 容器存储接口,可以扩展各种各样的第三方存储卷 )
特殊类型的存储卷: ConfigMap( 当配置中心来使用的资源类型 )、 Secret(保存敏感数据)、
DownwardAPI(把外部环境中的信息输出给容器)
集群级资源: Namespace、 Node、 Role、 ClusterRole、 RoleBinding、 ClusterRoleBinding
元数据型资源: HPA、 PodTemplate、 LimitRange
二、理解Kubernetes中的对象
在 Kubernetes 系统中,Kubernetes 对象 是持久化的条目。Kubernetes 使用这些条目去表示整个集群的状态。特别地,它们描述了如下信息:
- 什么容器化应用在运行(以及在哪个 Node 上)
- 可以被应用使用的资源
- 关于应用如何表现的策略,比如重启策略、升级策略,以及容错策略
Kubernetes 对象是 “目标性记录” —— 一旦创建对象,Kubernetes 系统将持续工作以确保对象存在。通过创建对象,可以有效地告知 Kubernetes 系统,所需要的集群工作负载看起来是什么样子的,这就是 Kubernetes 集群的 期望状态。
与 Kubernetes 对象工作 —— 是否创建、修改,或者删除 —— 需要使用 Kubernetes API。当使用 kubectl
命令行接口时,比如,CLI 会使用必要的 Kubernetes API 调用,也可以在程序中直接使用 Kubernetes API。
spec.containers <[]object> spec.containers.name <string> #pod的名称,必须字段,名称唯一且对象创建后不可以被修改 spec.containers.image <string> #镜像仓库的路径/镜像的名称:镜像的标签 spec.containers.image.imagePullPolicy <string> #镜像的下载策略。有三种:Always(总是去仓库下载) ,Never(从不去仓库下载) , IfNotPresent(如果本地没有就去仓库下载) 默认是"IfNotPresent" 但是,如果镜像的标签是latest,则总会是"Always,并且对象一旦被创建,这个字段不允许被改变 spec.containers.ports: #容器公开的端口列表。在这里公开端口可以为系统提供关于容器使用的网络连接的额外信息,但主要是提供信息。在这里不指定端口不会阻止该端口被公开。任何监听容器内默认的“0.0.0.0”地址的端口都可以从网络访问 spec.containers.ports.containerPort <integer> -required- #pod暴露的端口,此端口仅是额外的信息,对端口是否被暴露没有影响 spec.containers.ports.hostPort <integer> #主机上公开的端口 spec.containers.ports.protocol <string> #端口的协议 spec.containers.ports.hostIP <string> #指定要绑定的主机 spec.containers.command <[]string> #运行的程序,类似于docker中的entrypiont,并且这里的命令不会运行在shell中,如果没有这个字段docker镜像会运行自己entrypiont中的指令 spec.containers.args <[]string> #向docker镜像中传递参数 如果定义了这个字段,docker镜像中cmd命令不会被执行,如果引用变量使用$(VAR_NAME)格式引用,如果想使用命令引用的的方式,需要使用$$(VAR_NAME)方式来引用 #关于args和command的官方文档链接:https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/ pod.spec.containers.volumeMounts pod.spec.containers.volumeMounts.name pod.spec.containers.volumeMounts.mountPath #可以被容器挂载的存储卷的路径,路径不能包含':' 符号 pod.spec.containers.volumeMounts.subPath #可以被容器挂载的存储卷的路径,并且不会覆盖挂载点中的文件 pod.spec.containers.volumeMounts.readOnly #是否只读,默认为false pod.spec.containers.resources spec.containers.resources.limits #资源限制 spec.containers.resources.limits.cpu : CPU 上限, 可以短暂超过, 容器也不会被停止 spec.containers.resources.limits.memory : 内存上限, 不可以超过; 如果超过, 容器可能会被终止或调度到其他资源充足的机器上 spec.containers.resources.requests #资源需求 spec.containers.resources.requests.cpu : CPU 请求, 也是调度 CPU 资源的依据, 可以超过 spec.containers.resources.requests.memory : 内存请求, 也是调度内存资源的依据, 可以超过; 但如果超过, 容器可能会在 Node 内存不足时清理
三、Yaml语法
1、基本语法
k:(空格)v:表示一对键值对(空格必须有);
以空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的
属性和值也是大小写敏感;
2、值的写法
字面量:普通的值(数字,字符串,布尔)
k: v:字面直接来写;
字符串默认不用加上单引号或者双引号;
"":双引号;不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思
name: "zhangsan \n lisi":输出;zhangsan 换行 lisi
'':单引号;会转义特殊字符,特殊字符最终只是一个普通的字符串数据
name: ‘zhangsan \n lisi’:输出;zhangsan \n lisi
对象、Map(属性和值)(键值对):
k: v:在下一行来写对象的属性和值的关系;注意缩进
对象还是k: v的方式
行内写法:
数组(List、Set):
用- 值表示数组中的一个元素
行内写法
四 资源清单详解
资源清单格式
apiVersion: group/apiversion # 如果没有给定 group 名称,那么默认为 core,可以使用 kubectl api- versions # 获取当前 k8s 版本上所有的 apiVersion 版本信息( 每个版本可能不同 ) kind: #资源类别 metadata: #资源元数据 name namespace lables annotations # 主要目的是方便用户阅读查找 spec: # 期望的状态(disired state) status:# 当前状态,本字段有 Kubernetes 自身维护,用户不能去定义
资源清单的常用命令
获取 apiversion 版本信息
[root@k8s-master01 ~]# kubectl api-versions admissionregistration.k8s.io/v1beta1 apiextensions.k8s.io/v1beta1 apiregistration.k8s.io/v1 apiregistration.k8s.io/v1beta1 apps/v1 ......(以下省略)
获取资源的 apiVersion 版本信息
[root@k8s-master01 ~]# kubectl explain pod KIND: Pod VERSION: v1 .....(以下省略) [root@k8s-master01 ~]# kubectl explain Ingress KIND: Ingress VERSION: extensions/v1beta1
获取字段设置帮助文档
[root@k8s-master01 ~]# 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 <string> #表示字符串类型 metadata <Object> labels <map[string]string> #表示由k:v组成的映射 finalizers <[]string> #表示字串列表 ownerReferences <[]Object> #表示对象列表 hostPID <boolean> #布尔类型 priority <integer> #整型 name <string> -required- #如果类型后面接 -required-,表示为必填字段
通过定义清单文件创建 Pod
kubectl get pod xx.xx.xx -o yaml
<!--使用 -o 参数 加 yaml,可以将资源的配置以 yaml的格式输出出来,也可以使用json,输出为json格式-->
apiVersion: v1
kind: Pod
metadata: name: pod-demo
namespace: default
labels: app: myapp
spec: containers: - name: myapp-1 image: hub.atguigu.com/library/myapp:v1 - name: busybox-1
image: busybox:latest
command: - "/bin/sh" - "-c" - "sleep 3600"
五、使用配置清单创建自主式Pod资源
[root@k8s-master mnt]# cat pod.yaml apiVersion: v1 kind: Pod metadata: name: myapp-pod labels: app: myapp spec: containers: - name: app image: nginx [root@k8s-master mnt]#
创建Pod
[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 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/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/api-conventions.md#types-kinds metadata <Object> Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata spec <Object> Specification of the desired behavior of the pod. More info: https://git.k8s.io/community/contributors/devel/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/api-conventions.md#spec-and-status [root@k8s-master ~]# kubectl explain pod.apiVersion KIND: Pod VERSION: v1 FIELD: apiVersion <string> DESCRIPTION: 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/api-conventions.md#resources -rw-r--r-- 1 root root 134 12月 1 22:05 pod.yaml [root@k8s-master mnt]# kubectl create -f pod.yaml pod/myapp-pod created [root@k8s-master mnt]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES myapp-pod 1/1 Running 0 13s 10.244.1.5 k8s-node02 <none> <none> nginx-test-754bbf667c-q4z25 1/1 Running 0 4d23h 10.244.1.2 k8s-node02 <none> <none> nginx-test1-64f9f796d-wvvrz 0/1 ImagePullBackOff 0 3d 10.244.2.7 k8s-node01 <none> <none> nginx-test2-77b9dcf788-gxj76 1/1 Running 0 3d 10.244.1.3 k8s-node02 <none> <none> nginx-test3-d9ff9874d-ss6ft 0/1 ImagePullBackOff 0 2d23h 10.244.2.9 k8s-node01 <none> <none> nginx-test4-685dfcc98d-xx8h6 1/1 Running 0 2d23h 10.244.1.4 k8s-node02 <none> <none> nginx-test5-55fccd6c76-cxgsb 0/1 ImagePullBackOff 0 34m 10.244.2.11 k8s-node01 <none> <none> [root@k8s-master mnt]# kubectl describe myapp-pod error: the server doesn't have a resource type "myapp-pod" [root@k8s-master mnt]# kubectl describe pod myapp-pod Name: myapp-pod Namespace: default Priority: 0 Node: k8s-node02/192.168.180.103 Start Time: Sun, 01 Dec 2019 22:06:21 +0800 Labels: app=myapp Annotations: <none> Status: Running IP: 10.244.1.5 Containers: app: Container ID: docker://13279a22d067e98bda1e25ebb4ff1221412d24325ff47a2b577a17294856cba8 Image: nginx Image ID: docker-pullable://192.168.180.105:1180/topcheer/nginx@sha256:189cce606b29fb2a33ebc2fcecfa8e33b0b99740da4737133cdbcee92f3aba0a Port: <none> Host Port: <none> State: Running Started: Sun, 01 Dec 2019 22:06:32 +0800 Ready: True Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-hd24d (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: default-token-hd24d: Type: Secret (a volume populated by a Secret) SecretName: default-token-hd24d Optional: false QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 55s default-scheduler Successfully assigned default/myapp-pod to k8s-node02 Normal Pulling 51s kubelet, k8s-node02 Pulling image "nginx" Normal Pulled 44s kubelet, k8s-node02 Successfully pulled image "nginx" Normal Created 44s kubelet, k8s-node02 Created container app Normal Started 44s kubelet, k8s-node02 Started container app