Openshift 基于Template部署应用

Template 是什么?

Template是一组对象的集合objects, 包括BC,DC,Service,route等,template可以针对里面定义的一组对象指定标签,便于批量操作。
可以基于已有通过s2i创建的实例导出 BC、DC、Service 将这些资源组合一起根据需求修改

Template 部署应用(附:常用配置说明)

场景:个人理解,直接使用S2I构建与使用Template是有区别,但是核心思想不变,最终都是提供PAAS平台能力,S2I默认提供了Version、Application Name、Git Repository,对于仅仅部署应用程序,这些参数已经可以满足了,如果你的应用逻辑比较复杂,甚至需要关联多个容器的情况下建议使用Template。(便于大家理解,这里以Template部署简单应用为例。可以根据实际情况或用户使用习惯选择适合的方案)

查看模板配置:oc get template template-s2i -o yaml

apiVersion: template.openshift.io/v1
kind: Template # 模板配置
labels:
  template: test
metadata:
  annotations:
    description: |-
      JDK S2I Build

      WARNING: Any data stored will be lost upon pod destruction. Only use this template for testing
    iconClass: icon-openjdk
    openshift.io/display-name: JDK S2I (Template)
    openshift.io/provider-display-name: PH, Inc.
    tags: java
  creationTimestamp: 2019-01-29T04:02:51Z
  name: template-s2i
  namespace: devops
  resourceVersion: "5220582"
  selfLink: /apis/template.openshift.io/v1/namespaces/devops/templates/template-s2i
  uid: bf753d0e-237a-11e9-a45e-525400002fbd
objects: # 资源配置
- apiVersion: v1
  kind: ImageStream
  metadata:
    annotations:
      openshift.io/generated-by: OpenShiftNewApp
    creationTimestamp: null
    labels:
      app: ${APP_NAME}
    name: ${APP_NAME}
  spec:
    lookupPolicy:
      local: false
    tags:
    - annotations:
        openshift.io/imported-from: registry.xxxxxx.com/openshift/s2i:jdk8
      from:
        kind: DockerImage
        name: registry.xxxxxx.com/openshift/s2i:jdk8
      generation: null
      importPolicy: {}
      name: latest
      referencePolicy:
        type: ""
  status:
    dockerImageRepository: ""
- apiVersion: v1
  kind: BuildConfig
  metadata:
    annotations: null
    name: ${APP_NAME}
  spec:
    output:
      to:
        kind: ImageStreamTag
        name: ${APP_NAME}:latest
    source:
      contextDir: ${CONTEXT_DIR}
      git:
        ref: ${SOURCE_REPOSITORY_REF}
        uri: ${SOURCE_REPOSITORY_URL}
      sourceSecret:
        name: ${SOURCE_SECRET}
      type: Git
    strategy:
      sourceStrategy:
        env:
        - name: mvn_args
          value: ${MVN_ARGS}
        - name: code_subdir
          value: ${CODE_SUBDIR}
        from:
          kind: ImageStreamTag
          name: s2i:${S2I_VERSION}
          namespace: ${NAMESPACE}
      type: Source
    triggers:
    - type: ImageChange
    - type: ConfigChange
    - github:
        secret: ${GITHUB_WEBHOOK_SECRET}
      type: GitHub
- apiVersion: v1
  kind: DeploymentConfig
  metadata:
    annotations:
      openshift.io/generated-by: OpenShiftNewApp
    creationTimestamp: null
    labels:
      app: ${APP_NAME}
    name: ${APP_NAME}
  spec:
    replicas: 1
    selector:
      app: ${APP_NAME}
      deploymentconfig: ${APP_NAME}
    strategy:
      resources: {}
    template:
      metadata:
        annotations:
          openshift.io/generated-by: OpenShiftNewApp
        creationTimestamp: null
        labels:
          app: ${APP_NAME}
          deploymentconfig: ${APP_NAME}
      spec:
        containers:
        - image: registry.xxxxxx.com/openshift/s2i:jdk8
          name: ${APP_NAME}
          ports:
          - containerPort: 8080
            protocol: TCP
          resources: {}
    test: false
    triggers:
    - type: ConfigChange
    - imageChangeParams:
        automatic: true
        containerNames:
        - ${APP_NAME}
        from:
          kind: ImageStreamTag
          name: ${APP_NAME}:latest
      type: ImageChange
  status:
    availableReplicas: 0
    latestVersion: 0
    observedGeneration: 0
    replicas: 0
    unavailableReplicas: 0
    updatedReplicas: 0
- apiVersion: v1
  kind: Service
  metadata:
    annotations:
      openshift.io/generated-by: OpenShiftNewApp
    creationTimestamp: null
    labels:
      app: ${APP_NAME}
    name: ${APP_NAME}
  spec:
    ports:
    - name: 8080-tcp
      port: 8080
      protocol: TCP
      targetPort: 8080
    selector:
      app: ${APP_NAME}
      deploymentconfig: ${APP_NAME}
  status:
    loadBalancer: {}
parameters: # 用户自定义参数配置,最终会在web console显示
- description: application name.
  name: APP_NAME
  required: true
  value: appname
- description: The OpenShift Namespace where the ImageStream resides.
  displayName: Namespace
  name: NAMESPACE
  required: true
  value: openshift
- description: The URL of the repository with your application source code.
  displayName: Git Repository URL
  name: SOURCE_REPOSITORY_URL
  required: true
  value: http://gitlab.xxxx.cn/appname.git
- description: Set this to a branch name, tag or other ref of your repository if you
    are not using the default branch.
  displayName: Git Reference
  name: SOURCE_REPOSITORY_REF
  value: master
- description: Set this to the relative path to your project if it is not in the root
    of your repository.
  displayName: Context Directory
  name: CONTEXT_DIR
- description: maven args.
  name: MVN_ARGS
  required: true
  value: mvn clean install -Dmaven.test.skip=true
- description: code subdir.
  name: CODE_SUBDIR
  value: appname
- description: S2I version.
  name: S2I_VERSION
  value: "8.0"
- description: source secret name, you should create it, before.
  name: SOURCE_SECRET
  value: qq_dao
- description: Github trigger secret.  A difficult to guess string encoded as part
    of the webhook URL.  Not encrypted.
  displayName: GitHub Webhook Secret
  from: '[a-zA-Z0-9]{40}'
  generate: expression
  name: GITHUB_WEBHOOK_SECRET

模板配置说明:
description 描述
tags 会出现在Browse Catalog Java 分类下
openshift.io/display-name: web console中显示的名字
openshift.io/provider-display-name: Template提供者
iconClass参考 https://github.com/openshift/origin-web-console/blob/master/app/scripts/constants.js
parameters:参数化配置。(web console中由用户输入参数)

资源配置说明:
ImageStream、BuildConfig、DeploymentConfig、Service等资源配置集合

创建Template,并在web console查看

oc create -f template-s2i.yaml
在这里插入图片描述
在这里插入图片描述

通过label查看资源情况

oc get all -l app=test

通过label删除所有资源

oc delete all -l app=test

参考文档:
https://docs.okd.io/3.11/dev_guide/templates.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值