其他分享
首页 > 其他分享> > 在OpenShift上导出应用YAML配置并重新部署应用

在OpenShift上导出应用YAML配置并重新部署应用

作者:互联网

文章目录

在OpenShift上导出应用YAML配置并重新部署应用

文章目录

前言

在之前的文章中介绍了如何在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中多余的字段

删除多余的字段:

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

说明:

去掉Build Config的triggers(可选)

去掉以下triggers:

保留一下triggers:

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

说明:

修改DeploymentConfig

spec.spec.containers.image的值设置为' '

删除ImageChange trigger的from.namespacelastTriggeredImage

注意:不要修改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,object,oc,YAML,应用,yq,OpenShift,delete
来源: https://blog.csdn.net/nklinsirui/article/details/110817206