OpenShift - Getting Started with Templates

If you are not familiar with the oc command, refer to OpenShift - Getting Started with the oc command.

template is a JSON or YAML file that contains key value pairs used to create an object, such as a config map, deployment, a project, a pod, a route, a secret, a service, et cetera. The oc explain command can be used to get the list of keys that can be used in the JSON or YAML template file.

~]# oc explain pod
KIND:     Pod

     Pod is a collection of containers that can run on a host. This resource is
     created by clients and scheduled onto hosts.

   apiVersion   <string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:

   kind <string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:

   metadata     <Object>
     Standard object's metadata. More info:

   spec <Object>
     Specification of the desired behavior of the pod. More info:

   status       <Object>
     Most recently observed status of the pod. This data may not be up to date.
     Populated by the system. Read-only. More info:


And then more details on each key can be displayed.

~]# oc explain pod.spec


For example, let's say you have a YAML file named pod.yml that contains the following markup.

apiVersion: v1
kind: Pod
  name: my-pod
  namespace: foo
      - name: helloworld
        image: hello-world


The oc get templates command can be used to list example templates in the openshift namespace.

~]$ oc get templates --namespace openshift
NAME                                                DESCRIPTION                                                                        PARAMETERS        OBJECTS
3scale-gateway                                      3scale's APIcast is an NGINX based API gateway used to integrate your interna...   17 (8 blank)      3
amq63-basic                                         Application template for JBoss A-MQ brokers. These can be deployed as standal...   11 (4 blank)      6
amq63-persistent                                    An example JBoss A-MQ application. For more information about using this temp...   13 (4 blank)      8
amq63-persistent-ssl                                An example JBoss A-MQ application. For more information about using this temp...   18 (6 blank)      12
amq63-ssl                                           An example JBoss A-MQ application. For more information about using this temp...   16 (6 blank)      10
apicurito                                           Design beautiful, functional APIs with zero coding, using a visual designer f...   7 (1 blank)       7
cache-service                                       Red Hat Data Grid is an in-memory, distributed key/value store.                    8 (1 blank)       4
cakephp-mysql-example                               An example CakePHP application with a MySQL database. For more information ab...   21 (4 blank)      8
cakephp-mysql-persistent                            An example CakePHP application with a MySQL database. For more information ab...   22 (4 blank)      9


The oc get template command with the --output yaml option can be used to show the YAML that would be used to create the example.

~]$ oc get template cakephp-mysql-example --namespace openshift --output yaml
- apiVersion: v1
  kind: Service
      description: Exposes the database server
    - name: mysql
      port: 3306
      targetPort: 3306


Then the oc apply or oc create command with the -f or --filename option can be used to create a resource using the template JSON or YAML file.

The oc replace command can be used to replace a resource using a new or updated template JSON or YAML file.

The oc edit command can be used to update a resources template YAML file.

~]$ oc create --filename pod.yml 
pod/my-pod created


The oc get command with the -o or --output JSON or YAML option can be used to show the template for a resource that has already been created. In this example, the template YAML for the project / namespace named "foo" is returned.

~]$ oc get project foo --output yaml
kind: Project
  annotations: "" "" john.doe s0:c29,c4 1000820000/10000 1000820000/10000
  creationTimestamp: "2021-12-29T05:49:11Z"
  labels: foo
  name: foo
  resourceVersion: "411411327"
  uid: 74fe0011-b0d4-4378-827b-971556803367
  - kubernetes
  phase: Active


Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee

Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter 831ef in the box below so that we can be sure you are a human.