Deployments是在Kubernetes 1.2版本增加的特性,作为对于pod的控制,Deployments实现了声明式的应用发布和升级,保证了应用的高可用性和快速回滚的能力。在Kubernetes 1.8时相关API移至apps/v1beta2,在Kubernetes 1.9中整合至apps/v1,随着环境中的Kubernetes的升级,原有API的废弃导致Deployment的相应yaml文件需要修改,这篇文章以旧版的Prometheus的Deployment的修改为例,介绍Deployments修改需要注意的事项。

事前准备

本文示例中准备了Kubernetes 1.17的环境,环境的准备方法可参考如下Easyapck下的Ansible一键脚本。

  • 单机版本或者集群版本环境搭建
[root@host131 ansible]# kubectl version
Client Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.0", GitCommit:"70132b0f130acc0bed193d9ba59dd186f0e634cf", GitTreeState:"clean", BuildDate:"2019-12-07T21:20:10Z", GoVersion:"go1.13.4", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.0", GitCommit:"70132b0f130acc0bed193d9ba59dd186f0e634cf", GitTreeState:"clean", BuildDate:"2019-12-07T21:12:17Z", GoVersion:"go1.13.4", Compiler:"gc", Platform:"linux/amd64"}
[root@host131 ansible]#
[root@host131 ansible]# kubectl get nodes -o wide
NAME              STATUS   ROLES    AGE   VERSION   INTERNAL-IP       EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION          CONTAINER-RUNTIME
192.168.163.131   Ready    <none>   18m   v1.17.0   192.168.163.131   <none>        CentOS Linux 7 (Core)   3.10.0-957.el7.x86_64   docker://18.9.7
[root@host131 ansible]#

Deployment示例准备

如下是Deployment在beta1阶段的Yaml文件示例

[root@host131 prometheus]# cat prometheus-deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:name: prometheus-deploymentnamespace: monitoring
spec:replicas: 1template:metadata:labels:app: prometheus-serverspec:containers:- name: prometheusimage: prom/prometheus:v2.12.0args:- "--config.file=/etc/prometheus/prometheus.yml"- "--storage.tsdb.path=/prometheus/"ports:- containerPort: 9090volumeMounts:- name: prometheus-config-volumemountPath: /etc/prometheus/- name: prometheus-storage-volumemountPath: /prometheus/volumes:- name: prometheus-config-volumeconfigMap:defaultMode: 420name: prometheus-server-conf- name: prometheus-storage-volumeemptyDir: {}
[root@host131 prometheus]#

Yaml文件中所需要的命名空间ConfigMap等其他准备也事前已经准备:

[root@host131 prometheus]# kubectl get ns |grep monitoring
monitoring        Active   3m18s
[root@host131 prometheus]# kubectl get clusterrole |grep prometheus
prometheus                                                             3m1s
[root@host131 prometheus]# kubectl get clusterrolebindings |grep prometheus
prometheus                                             3m7s
[root@host131 prometheus]# kubectl get cm -n monitoring
NAME                     DATA   AGE
prometheus-server-conf   2      93s
[root@host131 prometheus]#

API变化所需要的对应事项

从上述extensions/v1beta1到apps/v1,Deployment需要进行如下修改:

对应事项1: 修改apiVersion

在Kubernetes 1.17下直接使用使用上述Yaml文件进行Deployment的创建,首先会提示如下错误信息:

[root@host131 prometheus]# kubectl create -f prometheus-deployment.yaml
error: unable to recognize "prometheus-deployment.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
[root@host131 prometheus]#

原因很简单,在这个版本中,extensions/v1beta1中已经没有Deployment了,已经被移除了,只能使用apps/v1了,然后做如下修改

[root@host131 prometheus]# cp prometheus-deployment.yaml prometheus-deployment.yaml.org
[root@host131 prometheus]# vi prometheus-deployment.yaml
[root@host131 prometheus]# diff prometheus-deployment.yaml prometheus-deployment.yaml.org
1c1
< apiVersion: apps/v1
---
> apiVersion: extensions/v1beta1
[root@host131 prometheus]#

对应事项2: Deployment.spec中添加selector

修改apiVersion之后,再次执行kubectl create命令进行Deployment的创建,会得到如下错误信息提示:

[root@host131 prometheus]# kubectl create -f prometheus-deployment.yaml
error: error validating "prometheus-deployment.yaml": error validating data: ValidationError(Deployment.spec): missing required field "selector" in io.k8s.api.apps.v1.DeploymentSpec; if you choose to ignore these errors, turn validation off with --validate=false
[root@host131 prometheus]#

原因也非常清晰,Deployment.spec中需要添加selector选择器,使用Kubernetes自带的帮助文档进行确认Deployment.spec.selector的写法

[root@host131 prometheus]# kubectl explain Deployment.spec.selector
KIND:     Deployment
VERSION:  apps/v1RESOURCE: selector <Object>DESCRIPTION:Label selector for pods. Existing ReplicaSets whose pods are selected bythis will be the ones affected by this deployment. It must match the podtemplate's labels.A label selector is a label query over a set of resources. The result ofmatchLabels and matchExpressions are ANDed. An empty label selector matchesall objects. A null label selector matches no objects.FIELDS:matchExpressions   <[]Object>matchExpressions is a list of label selector requirements. The requirementsare ANDed.matchLabels    <map[string]string>matchLabels is a map of {key,value} pairs. A single {key,value} in thematchLabels map is equivalent to an element of matchExpressions, whose keyfield is "key", the operator is "In", and the values array contains only"value". The requirements are ANDed.[root@host131 prometheus]#

对配置文件进行如下修改

[root@host131 prometheus]# vi prometheus-deployment.yaml
[root@host131 prometheus]# diff prometheus-deployment.yaml prometheus-deployment.yaml.org
1c1
< apiVersion: apps/v1
---
> apiVersion: extensions/v1beta1
8,10d7
<   selector:
<     matchLabels:
<       app: prometheus-server
[root@host131 prometheus]# cat prometheus-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: prometheus-deploymentnamespace: monitoring
spec:replicas: 1selector:matchLabels:app: prometheus-servertemplate:metadata:labels:app: prometheus-serverspec:containers:- name: prometheusimage: prom/prometheus:v2.12.0args:- "--config.file=/etc/prometheus/prometheus.yml"- "--storage.tsdb.path=/prometheus/"ports:- containerPort: 9090volumeMounts:- name: prometheus-config-volumemountPath: /etc/prometheus/- name: prometheus-storage-volumemountPath: /prometheus/volumes:- name: prometheus-config-volumeconfigMap:defaultMode: 420name: prometheus-server-conf- name: prometheus-storage-volumeemptyDir: {}
[root@host131 prometheus]#

既然是matchLabels,当然就需要设定相应的Label,这里添加了Deployment.spec.selector,实际上此Label需要和template里面的Label一致,这里之所以定义为app: prometheus-server也是因为为了和template已经存在的Label保持一致,这样就可以成功的进行创建了

[root@host131 prometheus]# kubectl create -f prometheus-deployment.yaml
deployment.apps/prometheus-deployment created
[root@host131 prometheus]# kubectl get pods -n monitoring
NAME                                     READY   STATUS    RESTARTS   AGE
prometheus-deployment-5c4f4f5779-vvr6h   1/1     Running   0          9m17s
[root@host131 prometheus]# kubectl get deployment -n monitoring
NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
prometheus-deployment   1/1     1            1           13m
[root@host131 prometheus]#

如果两者不一致的情况下,也会提示错误信息,比如这里我们特意将Label改成不一致

[root@host131 prometheus]# kubectl delete -f prometheus-deployment.yaml
deployment.apps "prometheus-deployment" deleted
[root@host131 prometheus]# cp prometheus-deployment.yaml prometheus-deployment.yaml.test
[root@host131 prometheus]# vi prometheus-deployment.yaml.test
[root@host131 prometheus]# diff prometheus-deployment.yaml prometheus-deployment.yaml.test
10c10
<       app: prometheus-server
---
>       app: prometheus-test
[root@host131 prometheus]#

不一致的情况下就会明确的提示,Deployment的selector和spec.template.metadata.labels不一致。

[root@host131 prometheus]# kubectl create -f prometheus-deployment.yaml.test
The Deployment "prometheus-deployment" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"app":"prometheus-server"}: `selector` does not match template `labels`
[root@host131 prometheus]#

Kubernetes基础:Deployments从beta版本到v1的变化对应方法相关推荐

  1. Kubernets集群管理-升级 kubernetes 集群版本到v1.21.14

    Kubernets集群管理-kubernets集群升级 文章目录 Kubernets集群管理-kubernets集群升级 前言 一.检查kubernets各组件当前版本 二.升级控制节点平面kuber ...

  2. 2345浏览器,2011年4月14号的V1.1, beta版本又这些新功能!

    系列文章目录 第二章 :2345浏览器,2011年4月14号的V1.1, beta版本又这些新功能! 文章目录 系列文章目录 前言 2011-04-14 V1.1 beta版 新增功能 新增改进 BU ...

  3. Kubernetes基础3

    主题 1.运行机制介绍 2.flanel 网络介绍 3.Nginx+tomcat+NFS 实现动静分离 1.运行机制介绍 1.1 master 运行机制 1.1.1 kube-apiserver k8 ...

  4. Kubernetes基础入门(完整版)

    简介 Kubernetes这个名字源于希腊语,意为"舵手"或"飞行员".k8s这个缩写是因为k和s之间有八个字符.Google在 2014年开源了Kuberne ...

  5. Tekton 流水线发布首个官方 Beta 版本

    这是 Tekton 流水线 的第一个官方 Beta 版本. 如果你已经在使用上一个 release 候选版本,那么,自 RC4 之后并没有代码的变更.唯一需要注意的是,在你的集群上部署最新的 Tekt ...

  6. kubernetes基础介绍及kubectl常用命令

    kubernetes基础介绍及kubectl常用命令 k8s的pod分类 自主式pod 控制器管理的pod 核心主键 HPA service 网络模型 同节点Pod之间的通信 不同节点上的Pod通信 ...

  7. 开源版本_3个月5次版本迭代,旷视开源框架天元Beta版本有哪些主要特性?

    深度学习框架是现代人工智能算法开发和应用的基本支撑框架.目前,国外主流深度学习框架基于开源开放的生态环境,已经构建起了较为完整的产业链. 要推动国内人工智能更好更快的发展,需要加大资源投入力度.旷视作 ...

  8. iBATIS.NET DataMapper V1.3 Beta and DataAccess V1.7 Beta发布了

    IBATIS.NET 团队发布了DataMapper V1.3 和 DataAccess V1.7  框架的β测试版本! 虽然这是主要为bug修改和文档更新, 有一些重要变动: : Enabling ...

  9. 云原生生态周报 Vol. 17 | Helm 3 发布首个 beta 版本

    本周作者 | 墨封.衷源.元毅.有济.心水 业界要闻 1. Helm 3 首个 beta 版本 v3.0.0-beta.1 发布 该版本的重点是完成最后的修改和重构,以及移植其他 Helm 2 特性. ...

最新文章

  1. 查看本地计算机已安装HOTFIX 几种方法
  2. WordCount代码详解
  3. WebService怎样在IIS上部署
  4. html三列布局源码,HTML三列布局 - 黄柳淞的个人页面 - OSCHINA - 中文开源技术交流社区...
  5. [JavaWeb-Servlet]Servlet3.0注解配置
  6. 带有ActiveMQ的JMS
  7. java不要无限循环_java – 看似无限循环终止,除非使用System.out.println
  8. vs 2015 oracle,VS2015连接oracle11g出现异常
  9. avg最多用多少列 mysql_使用MySQL中的AVG函数求平均值的教程
  10. android 中如何监听按键的长按事件
  11. 对Map集合排序,先对value降序,value相同的情况下,key升序
  12. Sybase迁移Oracle字符集问题,Sybase数据库迁移数据到Oracle(未改进)
  13. 程序员被空姐骗到香港做传销!
  14. 大数据求中位数(插值计算)
  15. Stripe 银行卡支付功能初步指南(Java)
  16. 如何深入学习 Android Framework
  17. Vue 设置图片不转为base64
  18. java 加密方式_Get史上最优雅的加密方式!没有之一!
  19. 企业微信朋友圈和个人微信朋友圈有哪些区别?
  20. jquery.min.js:2 Uncaught Error: Syntax error, unrecognized expression: unsupported pseudo: blank

热门文章

  1. mvdbos php spider,Scrapy-Redis分布式爬取自如网(一)
  2. 拼音翻译成阿拉伯数字_华为SO挑战赛2015年8月
  3. 智联招聘python岗位_python智联招聘爬虫
  4. (信贷风控九)行为评分卡模型python实现
  5. MAC设置允许任何来源
  6. Pixhawk串口名称与硬件接口对应关系
  7. react-router v6
  8. python判断数是整数还是小数
  9. 美国标准的网络安全体系架构
  10. java实现智能拼图,JAVA实现拼图游戏