Bootstrap FreeKB - OpenShift - Getting Started with Templates
OpenShift - Getting Started with Templates

Updated:   |  OpenShift articles

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
VERSION:  v1

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

FIELDS:
   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:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

   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:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

   metadata     <Object>
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

   spec <Object>
     Specification of the desired behavior of the pod. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

   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:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

 

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
metadata:
  name: my-pod
  namespace: foo
spec:
    containers:
      - 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
  metadata:
    annotations:
      description: Exposes the database server
    name: ${DATABASE_SERVICE_NAME}
  spec:
    ports:
    - name: mysql
      port: 3306
      targetPort: 3306
    selector:
      name: ${DATABASE_SERVICE_NAME}

 

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
apiVersion: project.openshift.io/v1
kind: Project
metadata:
  annotations:
    openshift.io/description: ""
    openshift.io/display-name: ""
    openshift.io/requester: john.doe
    openshift.io/sa.scc.mcs: s0:c29,c4
    openshift.io/sa.scc.supplemental-groups: 1000820000/10000
    openshift.io/sa.scc.uid-range: 1000820000/10000
  creationTimestamp: "2021-12-29T05:49:11Z"
  labels:
    kubernetes.io/metadata.name: foo
  name: foo
  resourceVersion: "411411327"
  uid: 74fe0011-b0d4-4378-827b-971556803367
spec:
  finalizers:
  - kubernetes
status:
  phase: Active

 




Did you find this article helpful?

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



Comments


Add a Comment


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