一、K8S声明式的管理方式:
1、适合对资源的修改操作
2、声明式管理依赖于yaml文件,所有的内容都在yaml文件中声明
3、编辑好的yml文件还是要靠陈述式命令发布到K8S集群中
二、K8S中支持三种声明式的资源管理方式:
1、deployment格式:用于配置和管理资源对象
apiversion:定义api版本
kind:定义资源的类型deployment\job\ingress\service\deamonset
metadata:定义创建资源的元数据信息、资源的名称--pod的名称、命名空间的名称、pod标签
name:nginx1
namespaces:命名空间可写可不写一般是注释掉的
labels:定义标签
app:nginx1
spec:配置deployment资源对象需要的参数属性,副本数,匹配的标签以及容器的重启策略
replicas:3定义该资源pod数量也就是副本数
selector:定义标签的选择器
matchLabels:定义匹配的标签,这里得和全局保持一致
app:nginx1
template:定义业务模板,上面定义了三个副本,所有pod的属性都会和template的设置保持一致
metedata:元数据
lables:指定标签,也要和全局保持一致
app:nginx1
以上为pod的内容,以下为pod内容器内容
spec:定义pod内容器
containers:定义镜像
- name: nginx
image:nginx:1.22
ports:暴漏端口
- containerPort: 8080
如果nginx端口是默认的80,ports可以不加,因为写了也无法改变容器的端口,容器的端口是由dockerfile决定的;如果默认端口不是80就要ports做为声明,声明非默认端口。
pod和容器是分开的也是合并的,镜像定义容器,pod不能改变镜像。
kubectl create deployment nginx1 --image=nginx:1.22 --replicas=3 -n xy102


配置修改过后apply -f就进行更改了
删除yaml文件就是相当于删除了所有资源对象

2.services类型书写
kubectl expose deployment nginx1 --port=80 --target-port=80 --name=nginx11 -n xy102



3.pod模式:



三、容器的重启策略和传参
1.restartPolicy:
Always:只要启动失败就会一直重启容器
Never:从不重启
OnFailure:只有容器异常退出才会重启,返回码非零时才会对容器进行重启
Always:




Never


Onfailure:


2.pod内的传参:
command和args(属于容器内容):在yaml文件中二者不需要传参的情况下只能有一个,args可以给command传参。
command和args与docker中CMD和ENTRYPOINT意义相同
调用bin/bash

两种写法




执行多个命令
-c:输出目录的格式 /bin/bash

传参:


不论是command还是args都会覆盖容器的标准输出

四、如何制作yaml文件模板
