k8s的声明式资源管理(yaml文件)
发布时间:2024年01月02日
1、声明式管理的特点
(1)适合对资源的修改操作
(2)声明式管理依赖于yaml文件,所有的内容都在yaml文件当中
(3)编辑好的yaml文件,还是要依靠陈述式的命令发布到k8s集群当中
kubectl create | 只能创建,不能更新,从指定的yaml文件中读取配置,创建服务,不能更新 |
kubectl apply -f (使用最多) | 既可以创建资源对象,也可以更新资源对象,如果yaml文件更改了,apply可以直接更新资源对象 |
kubectl delete -f | 删除yaml文件中声明的资源对象(声明deployment、pod、service) |
2、生成yaml文件
(1)手动输入
(2)可以根据已有的资源直接生成
修改yaml文件:kubectl edit deployment nginx |
查看yaml文件:kubectl get deployments.apps nginx -o yaml |
导出yaml文件:kubectl get deployments.apps nginx -o yaml > /opt/test.yaml  |
3、修改yaml文件(基于已有的资源生成yaml)
(1)基于已有的资源生成yaml:kubectl get deployments.apps nginx -o yaml > /opt/test.yaml

(2)修改,重新创建更新资源对象:kubectl apply -f test.yaml --force

4、常见的yaml文件
(1)deployment的yaml文件(daemonset、statefulset)
(2)service的yaml文件
(3)不基于控制器的pod的yaml文件
5、k8s中支持两种声明式的资源管理方式
(1)yaml格式:用于配置和管理资源对象
(2)json格式:主要用于在api接口之间消息的传递
6、配置deployment的yaml文件(daemonset、statefulset)
(1)kubectl explain deployment


(2)kubectl explain service/kubectl explain pod


(3)配置deployment的yaml文件
①创建deployment的yaml文件

②kubectl apply -f nginx.yaml


7、基于yaml文件创建service


8、创建pod的yaml文件




9、command、args:用于指定容器启动时执行的命令和参数
定义容器运行的参数,类似于docker的CMD和entrypoint(重) |
command | 类似于docker中的entrypoint |
args | 可以理解为docker中的CMD,可以给command的传参 |
command和args都会覆盖原容器的标准输出(cmd和entrypoint) |
①command的和args的命令格式相同。 ②command和args不要同时出现,除非要传参,都是容器的标准输出(类似CMD、entrypoint) |
(1)args传参给command

① kubectl logs centos1 -n hyde

(2)覆盖容器的标准输出

(3)指定输出内容
①格式一


②格式二


查看内容:kubectl exec -it centos2 -n hyde -- cat /opt/test1

③格式三

10、总结
(1)三种发布方式(蓝绿发布、灰度发布(重点,基于deployment的滚动发布模式,使用了pause机制,resume继续。暂停时不能回滚,只有全部都升级完才可以回滚)、滚动发布)
(2)三种yaml文件的模式
①deployment
②service
③pod
文章来源:https://blog.csdn.net/weixin_48145965/article/details/135342909
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:chenni525@qq.com进行投诉反馈,一经查实,立即删除!