文章目录
在OpenShift上导出应用YAML配置并重新部署应用
文章目录
前言
在之前的文章中介绍了如何在OpenShift上部署Spring Boot应用:
- 从零开始在OpenShift上开发和部署Spring Boot应用
- 配置GitHub push自动触发OpenShift自动构建和部署
- 在OpenShift上动态更新Spring Boot应用的配置
- 在OpenShift中对Spring Boot应用作健康检查
本文描述了如何在OpenShift中导出应用的YAML配置,并重新部署应用。通过本文也展示了OpenShift (或Kubernetes)的Infrastructure as Code的特点。
示例项目:https://github.com/cookcodeblog/spring-db-demo
导出OpenShift YAML
在spring-db-demo项目下创建templates
目录。
在templates/
目录下执行:
# use william-spring-demo project
oc project william-spring-demo
# 列出项目的资源
oc get all -o name
# 列出不属于项目但是项目需要用到的资源,比如configmap, secret, volume, pvc, rolebinding等
oc get configmap
oc get secret
oc get pvc
oc get rolebinding
导出OpenShift YAML文件:
# spring-db-demo
oc get imagestream/spring-db-demo -o yaml > image-stream.yaml
oc get buildconfig/spring-db-demo -o yaml > build-config.yaml
oc get deploymentconfig/spring-db-demo -o yaml > deployment-config.yaml
oc get route/spring-db-demo -o yaml > route.yaml
oc get service/spring-db-demo -o yaml > service.yaml
oc get configmap/spring-db-demo -o yaml > config-map.yaml
oc get rolebinding/view -o yaml > role-binding-view.yaml
# mysql
oc get deploymentconfig/mysql -o yaml > deployment-config-mysql.yaml
oc get service/mysql -o yaml > service-mysql.yaml
oc get secret/mysql -o yaml > secret-mysql.yaml
删除导出的Yaml中多余的字段
删除多余的字段:
- OpenShift自动生成的字段
-
namespace
支持在任意namespace
上部署 -
host
不硬编码route的Host -
clusterIP
不硬编码service的clusterIP
for object in $(find . -name '*.yaml')
do
yq delete -i $object metadata.managedFields
yq delete -i $object metadata.creationTimestamp
yq delete -i $object metadata.resourceVersion
yq delete -i $object metadata.selfLink
yq delete -i $object metadata.uid
yq delete -i $object metadata.generation
yq delete -i $object 'metadata.annotations."app.openshift.io/vcs-ref"'
yq delete -i $object 'metadata.annotations."app.openshift.io/vcs-uri"'
yq delete -i $object 'metadata.annotations."openshift.io/generated-by"'
yq delete -i $object 'metadata.annotations."openshift.io/host.generated"'
yq delete -i $object 'metadata.labels."template.openshift.io/template-instance-owner"'
yq delete -i $object 'metadata.annotations."template.openshift.io/expose-*"'
yq delete -i $object metadata.namespace
yq delete -i $object status
yq delete -i $object spec.host
yq delete -i $object spec.clusterIP
yq delete -i $object 'subjects[0].namespace'
done
或执行脚本:
bash ../scripts/clean_openshift_yaml.sh
说明:
- 需要本地安装
yq
Yaml命令行工具,Mac OS上可以用brew install yq
来安装。
去掉Build Config的triggers(可选)
去掉以下triggers:
generic
github
imageChange
保留一下triggers:
configChange
for object in $(find . -name 'build-config*.yaml')
do
yq delete -i $object 'spec.triggers.(type==Generic)'
yq delete -i $object 'spec.triggers.(type==GitHub)'
yq delete -i $object 'spec.triggers.(type==ImageChange)'
done
或执行脚本:
bash ../scripts/clean_openshift_build_config.sh
说明:
- 去掉GitHub trigger后,需要手工构建,或配置CICD pipeline来在代码变更时自动触发构建。
修改DeploymentConfig
将spec.spec.containers.image
的值设置为' '
。
删除ImageChange
trigger的from.namespace
和lastTriggeredImage
。
注意:不要修改MySQL数据库的DeploymentConfig,不然会导致数据库无法启动。
for object in $(find . -name 'deployment-config.yaml')
do
yq write -i $object 'spec.template.spec.containers[0].image' ' '
yq delete -i $object 'spec.triggers.(type==ImageChange).imageChangeParams.from.namespace'
yq delete -i $object 'spec.triggers.(type==ImageChange).imageChangeParams.lastTriggeredImage'
done
或执行脚本:
bash ../scripts/clean_openshift_deployment_config.sh
在一个新的项目中测试
在templates/
目录下:
# Create a new openshift project and switch to it
oc new-project william-spring-temp
oc project
# Apply YAML files to create OpenShift resources
oc apply -f .
打开OpenShift web console,查看重新部署后的应用程序和数据库是否工作正常。
参考从零开始在OpenShift上开发和部署Spring Boot应用,在数据库上创建表结构和初始化数据,然后进行测试。
参考文档
- Yaml语法参考:Yaml Syntax
-
yq
Yaml Path 表达式参考:yq Path Expressions