My scrapbook about almost anything I stumble upon in my tech world. If you find anything useful don't forget to give thumbs-up :)

Breaking

Sunday, April 12, 2020

Convert Openshift Object Yaml file into Template Object


It's very crucial to understand Template object of OCP platform if you are planning to play around with OCP object very frequently.

What is Template Object in respect of OCP? - In layman's terms, Template is a pre-defined and re-usable object yaml file which can consist of single or multiple OCP object, and it is a necessity if you want other to use your OCP definition of any application. Template is customize-able at run time as per the parameters passed to it.

Now, How to generate or create template? It's very easy if you have any existing OCP object yaml file or you can export the yaml from any running application. Let's learn how can we achieve this task?

To Do: Convert Yaml definition to  Template Object

Steps:
1. First decide, when you process your template, which OCP object need to be created. In this example, I am picking a Deployment and Service Object.

Deployment Yaml file:
apiVersion: apps/v1 
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2 
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

Service Yaml file:
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376  

2. Decide which fields you want to be as a parameter, In this example, I am taking targetPort and replica to be parameterized.

3. Follow the Template of Template Object as below -
apiVersion: v1
kind: Template
metadata:
  name: appname-template
  annotations:
    description: "Description"
    tags: "application,tags"
objects:
- obj1 definition
- obj2 definition 
- obj3 definition
- .....
- .....
- ..... 
parameters:
  - name: param1
    description: "description of paramters"
    value: value1

4. We are almost done, we just need to tweak Template's template as our requirement which is having 2 objects - Deployment and Service with 2 parameters - targetPort and replica

5. Our Template file will look like below, after putting the OCP object def in Template's template
apiVersion: v1
kind: Template
metadata:
  name: ngnix-template
  annotations:
    description: "This template will deploy ngnix application with service"
    tags: "ngnix,linux,loadbalancer"
objects:
- apiVersion: apps/v1 
  kind: Deployment
  metadata:
    name: nginx-deployment
  spec:
    selector:
      matchLabels:
        app: nginx
    replicas: ${REPLICA_COUNT} 
    template:
      metadata:
        labels:
          app: nginx
      spec:
        containers:
        - name: nginx
          image: nginx:1.14.2
          ports:
          - containerPort: 80
- apiVersion: v1
  kind: Service
  metadata:
    name: nginx-service
  spec:
    selector:
      app: nginx
    ports:
      - protocol: TCP
        port: 80
        targetPort: ${TARGET_PORT} 
parameters:
  - name: REPLICA_COUNT
    description: "No of Pods"
    value: 2
  - name: TARGET_PORT
    description: "Exposed Port"
    value: 9376

The only thing which you need to take care is indentation of definition, this way you can create any template for your application from running OCP object. Having a template will set a standard for you application when reuses by developers/prod teams and provide you ease when spinning off multiple containers on different places for same application.

In Next post, we will learn about how to process the Template to deploy the OCP object, till then.....Happy Learning !!

Like the below page to get the update  
Facebook Page      Facebook Group      Twitter Feed      Telegram Group

No comments:

Post a Comment

Disclaimer

The postings on this site are my own and don't necessarily represent IBM's or other companies positions, strategies or opinions. All content provided on this blog is for informational purposes and knowledge sharing only.
The owner of this blog makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site. The owner will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of his information.