配置清单示例

资源清单是 yml 格式,api-server 会自动转成 json 格式

每个 API 对象都有 3 大类属性:元数据 metadata、规范 spec 和状态 status

spec 是期望状态,status 是实际状态,如果实际状态和期望状态有出入,控制器(通常是 deployment 控制器)的控制循环就会监控到差异,然后将需要做出的更改提交到 apiserver,调度器 scheduler 监控到 apiserver 中有未执行的操作,就会去找适合执行操作的 node,然后提交到 apiserver,kubelet 监控到 apiserver 中有关于自己节点的操作,就会执行操作,将执行结果返回给 apiserver,apiserver 再更新实际状态

deployment

1

HorizontalPodAutoscaler

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
kind: HorizontalPodAutoscaler
apiVersion: autoscaling/v2beta1
metadata:
namespace: linux42
name: tomcat-app1-podautoscaler
labels:
app: tomcat-app1 #自定义app标签
version: v2beta1 #自定义version标签
spec: # 对象具体信息
scaleTargetRef: #定义水平伸缩的目标对象:Deployment、ReplicationController/ReplicaSet
kind: Deployment #目标对象类型为deployment
apiVersion: apps/v1 #API版本
name: tomcat-app1-deployment #deployment的名称
minReplicas: 2 #最小pod数
maxReplicas: 5 #最大pod数
metrics: #需要安装metrics server
- type: Resource # 类型为资源
resource: # 定义资源
name: cpu
targetAverageUtilization: 80 #CPU使用率,超过80%就增加pod
- type: Resource
resource:
name: memory
targetAverageValue: 1024Mi # 内存使用量,超过1024Mi就增加pod

namespace

1
2
3
4
apiVersion: v1
kind: Namespace
metadata:
name: test

Nginx 业务 yaml 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
kind: Deployment
apiVersion: extensions/v1beta1 # API版本
metadata: # deployment元数据
name: nginx-deployment # deployment名称,创建后的pdo名称是这个名称加随机字符串
namespace: test # pod的namespace,默认是default
labels: # 自定义deployment标签
app: nginx-deployment-label
spec: # deployment的详细信息
replicas: 1 # 创建出的pod的副本数,即多少个pod,默认值为1
selector: # Deployment如何查找要管理的Pods,它必须与pod模板的标签相匹配
matchLabels: # 定义匹配Pod的标签,pod模板的标签相匹配
app: nginx-selector # 就是下面的 template.metadata.labels.app
template: # 定义模板,必须定义,用于描述要创建的pod
metadata: # pod元数据
labels: # 自定义pod的标签,主要用于service匹配
app: nginx-selector # 自定义app标签
spec: # pod详细信息
containers: # pod中容器列表,可以多个,至少一个,绝大部分情况是一个,pod不能动态增减容器
- name: nginx-container # 容器名称
image:
harbor.magedu.net/test/nginx-web1:v1 # 镜像地址
# command: ["/apps/tomcat/bin/run_tomcat.sh"] # 容器启动执行的命令或脚本
# imagePullPolicy: IfNotPresent
imagePullPolicy: Always # 拉取镜像策略
ports: # 定义容器端口列表
- name: http # 端口名称
containerPort: 80
protocol: TCP
- name: https # 端口名称
containerPort: 443
protocol: TCP
env: # 配置环境变量
- name: "password" # 变量名称。必须要用引号引起来
value: "123456"
- name: "age"
value: "18"
resources: # 对资源的请求设置和限制设置
limits: # 资源限制设置上限
cpu: 2 # cpu的限制,单位为core数,可以写0.5或者500m等CPU压缩值
memory: 2Gi # 内存限制,单位可以为Mib/Gib,将用于docker run --memory参数
requests: # 资源请求的设置
cpu: 1 # cpu请求数,容器启动的初始可用数量,可以写0.5或者500m等CPU压缩值
memory: 512Mi # 内存请求大小,容器启动的初始可用数量,用于调度pod时候使用
---
kind: Service
apiVersion: v1 # API版本
metadata: # service元数据
name: nginx-spec # service的名称,此名称会被DNS解析
namespace: test # 该service隶属于的namespaces名称,即把service创建到哪个namespace里面
labels: # 自定义service标签
app: nginx
spec: # service的详细信息
type: NodePort # service的类型,定义服务的访问方式,默认为ClusterIP
ports: # 定义访问端口
- name: http # 定义一个端口名称
port: 80 # service 80端口
protocol: TCP # 协议类型
targetPort: 80 # 目标pod的端口
nodePort: 30001 # node节点暴露的端口
- name: https
port: 443
protocol: TCP
targetPort: 443
nodePort: 30043
selector: # service的标签选择器,匹配Pod的标签
app: nginx-selector # 匹配定义了app标签且值为nginx-selector的Pod