1.template的定义
官方对template的定义是
A template describes a set of objects that can be parameterized and processed to produce a list of objects for creation by OKD. A template can be processed to create anything you have permission to create within a project, for example services, build configurations, and deployment configurations. A template may also define a set of labels to apply to every object defined in the template.
也就是Template是一组对象的集合,包括Service,BC,DC等,template可以针对里面定义的一组对象指定标签,便于批量操作。
2.template的使用
创建一个template
ericdeMacBook-Pro:template ericnie$ cat deployment-template.yaml apiVersion: v1 kind: Template metadata: creationTimestamp: null name: deployment-example # 模板配置部分 objects: # 资源配置部分 - apiVersion: v1 kind: ImageStream metadata: annotations: openshift.io/generated-by: OpenShiftNewApp creationTimestamp: null labels: app: deployment-example name: deployment-example spec: lookupPolicy: local: false tags: - annotations: openshift.io/imported-from: s2itomcat:latest from: kind: DockerImage name: s2itomcat:latest generation: null importPolicy: {} name: latest referencePolicy: type: "" status: dockerImageRepository: "" - apiVersion: v1 kind: DeploymentConfig metadata: annotations: openshift.io/generated-by: OpenShiftNewApp creationTimestamp: null labels: app: deployment-example name: deployment-example spec: replicas: 1 selector: app: deployment-example deploymentconfig: deployment-example strategy: resources: {} template: metadata: annotations: openshift.io/generated-by: OpenShiftNewApp creationTimestamp: null labels: app: deployment-example deploymentconfig: deployment-example spec: containers: - image: openshift/deployment-example:latest name: deployment-example ports: - containerPort: 8080 protocol: TCP resources: {} test: false triggers: - type: ConfigChange - imageChangeParams: automatic: true containerNames: - deployment-example from: kind: ImageStreamTag name: s2itomcat: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: deployment-example name: deployment-example spec: ports: - name: 8080-tcp port: 8080 protocol: TCP targetPort: 8080 selector: app: deployment-example deploymentconfig: deployment-example status: loadBalancer: {}
ericdeMacBook-Pro:template ericnie$ oc process deployment-example | oc create -f - imagestream "deployment-example" created deploymentconfig "deployment-example" created service "deployment-example" created
查看这些对象
ericdeMacBook-Pro:template ericnie$ oc get dc NAME REVISION DESIRED CURRENT TRIGGERED BY deployment-example 1 1 1 config,image(s2itomcat:latest) s2itomcat 7 1 1 config,image(s2itomcat:latest) ericdeMacBook-Pro:template ericnie$ oc get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE deployment-example ClusterIP 172.30.160.162 <none> 8080/TCP 11m s2itomcat ClusterIP 172.30.145.161 <none> 8080/TCP 5d ericdeMacBook-Pro:template ericnie$ oc get imagestream NAME DOCKER REPO TAGS UPDATED deployment-example 172.30.1.1:5000/s2i-tomcat/deployment-example latest mytomcat 172.30.1.1:5000/s2i-tomcat/mytomcat latest 5 days ago s2itomcat 172.30.1.1:5000/s2i-tomcat/s2itomcat latest 2 hours ago
在界面上看到这些对象已经创建
3.参数化
如果一个模版对应多个微服务,可以创建参数化模版并赋值。修改如下
ericdeMacBook-Pro:template ericnie$ cat deployment-template.yaml apiVersion: v1 kind: Template metadata: creationTimestamp: null name: deployment-example # 模板配置部分 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: s2itomcat:latest from: kind: DockerImage name: s2itomcat:latest generation: null importPolicy: {} name: latest referencePolicy: type: "" status: dockerImageRepository: "" - 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: s2itomcat:latest name: ${APP_NAME} ports: - containerPort: 8080 protocol: TCP resources: {} test: false triggers: - type: ConfigChange - imageChangeParams: automatic: true containerNames: - ${APP_NAME} from: kind: ImageStreamTag name: s2itomcat: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: - name: APP_NAME displayname: application name value: myapp
创建
ericdeMacBook-Pro:template ericnie$ oc new-app deployment-template.yaml --> Deploying template "s2i-tomcat/deployment-example" for "deployment-template.yaml" to project s2i-tomcat * With parameters: * APP_NAME=myapp --> Creating resources ... imagestream "myapp" created deploymentconfig "myapp" created service "myapp" created --> Success Application is not exposed. You can expose services to the outside world by executing one or more of the commands below: 'oc expose svc/myapp' Run 'oc status' to view your app.
4.批量删除对象
ericdeMacBook-Pro:template ericnie$ oc delete all -l app=deployment-example deploymentconfig "deployment-example" deleted imagestream "deployment-example" deleted route "deployment-example" deleted pod "deployment-example-1-wmz26" deleted service "deployment-example" deleted
5.导出现有应用到模版
ericdeMacBook-Pro:template ericnie$ oc export deploymentconfig,buildconfig,svc,route --as-template=mytomcat > mytomcat.yaml
3.11版本
oc get -o yaml --export deploymentconfig > openshift-tomcat.yaml
ericdeMacBook-Pro:template ericnie$ cat mytomcat.yaml apiVersion: v1 kind: Template metadata: creationTimestamp: null name: mytomcat objects: - apiVersion: v1 kind: DeploymentConfig metadata: annotations: openshift.io/generated-by: OpenShiftWebConsole creationTimestamp: null generation: 1 labels: app: s2itomcat name: s2itomcat spec: replicas: 1 selector: deploymentconfig: s2itomcat strategy: activeDeadlineSeconds: 21600 resources: {} rollingParams: intervalSeconds: 1 maxSurge: 25% maxUnavailable: 25% timeoutSeconds: 600 updatePeriodSeconds: 1 type: Rolling template: metadata: creationTimestamp: null labels: app: s2itomcat deploymentconfig: s2itomcat spec: containers: - image: 172.30.1.1:5000/s2i-tomcat/s2itomcat@sha256:9c192795180367f2a689b1b395b4156e746fdc266c9b1249a97651b1caa002d6 imagePullPolicy: Always name: s2itomcat ports: - containerPort: 8080 protocol: TCP resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: File dnsPolicy: ClusterFirst restartPolicy: Always schedulerName: default-scheduler securityContext: {} terminationGracePeriodSeconds: 30 test: false triggers: - imageChangeParams: automatic: true containerNames: - s2itomcat from: kind: ImageStreamTag name: s2itomcat:latest namespace: s2i-tomcat type: ImageChange - type: ConfigChange status: availableReplicas: 0 latestVersion: 0 observedGeneration: 0 replicas: 0 unavailableReplicas: 0 updatedReplicas: 0 - apiVersion: v1 kind: BuildConfig metadata: annotations: openshift.io/generated-by: OpenShiftWebConsole creationTimestamp: null labels: app: s2itomcat name: s2itomcat spec: nodeSelector: null output: to: kind: ImageStreamTag name: s2itomcat:latest postCommit: {} resources: {} runPolicy: Serial source: git: ref: master uri: https://github.com/ericnie2015/openshift-tomcat.git type: Git strategy: sourceStrategy: from: kind: ImageStreamTag name: s2i-tomcat:latest namespace: openshift type: Source triggers: - imageChange: {} type: ImageChange - type: ConfigChange - generic: secret: 0e2a7f575472e635 type: Generic - github: secret: 2c06c9b430310ec1 type: GitHub status: lastVersion: 0 - apiVersion: v1 kind: Service metadata: annotations: openshift.io/generated-by: OpenShiftWebConsole creationTimestamp: null labels: app: s2itomcat name: s2itomcat spec: ports: - name: 8080-tcp port: 8080 protocol: TCP targetPort: 8080 selector: deploymentconfig: s2itomcat sessionAffinity: None type: ClusterIP status: loadBalancer: {} - apiVersion: v1 kind: Route metadata: annotations: openshift.io/generated-by: OpenShiftWebConsole openshift.io/host.generated: "true" creationTimestamp: null labels: app: s2itomcat name: s2itomcat spec: host: s2itomcat-s2i-tomcat.192.168.99.100.nip.io port: targetPort: 8080-tcp to: kind: Service name: s2itomcat weight: 100 wildcardPolicy: None status: ingress: - conditions: - lastTransitionTime: 2018-09-26T13:33:16Z status: "True" type: Admitted host: s2itomcat-s2i-tomcat.192.168.99.100.nip.io routerName: router wildcardPolicy: None