Skip to main content

Building a Pod on OpenShift

Hands-On Lab

 

Photo of joel

joel

Training Architect

Length

01:00:00

Difficulty

Intermediate

Building a Pod on OpenStack In this scenario, we have been asked to configure an s2i application using the json template included in our instructions. Once the pod build completes, create a new route using Origin Clients, then confirm that the route is accessible by accessing it with elinks.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

First create a new project called gem.

oc new-project gem

Use the s2i-application-template.json template found under /class-templates/ in the content-openshift-on-openstack GitHub repository to create the ruby application. This can either be done by copy/pasting the .json into a new file on the master, or by using the raw hyperlink directly from GitHub.

$ oc new-app -f s2i-application-template.json

Once the app build completes, expose a new route to the service using the subdomain configured in the cluster.

oc get services
oc expose svc frontend

Optionally, visit the exposed service using elinks to confirm functionality.

elinks http://apps.10.0.2.203.xip.io/gem/frontend

Application Template

{
  "kind": "Template",
  "apiVersion": "v1",
  "metadata": {
    "name": "ruby-helloworld-sample",
    "annotations": {
      "description": "This example shows how to create a simple ruby application in openshift origin v3",
      "iconClass": "icon-ruby",
      "tags": "instant-app,ruby,mysql"
    }
  },
  "objects": [
    {
      "kind": "Secret",
      "apiVersion": "v1",
      "metadata": {
        "name": "dbsecret"
      },
      "stringData" : {
        "mysql-user" : "${MYSQL_USER}",
        "mysql-password" : "${MYSQL_PASSWORD}"
      }
    },
    {
      "kind": "Service",
      "apiVersion": "v1",
      "metadata": {
        "name": "frontend"
      },
      "spec": {
        "ports": [
          {
            "name": "web",
            "protocol": "TCP",
            "port": 5432,
            "targetPort": 8080,
            "nodePort": 0
          }
        ],
        "selector": {
          "name": "frontend"
        },
        "type": "ClusterIP",
        "sessionAffinity": "None"
      },
      "status": {
        "loadBalancer": {}
      }
    },
    {
      "kind": "Route",
      "apiVersion": "v1",
      "metadata": {
        "name": "route-edge",
        "annotations": {
          "template.openshift.io/expose-uri": "http://{.spec.host}{.spec.path}"
        }
     },
      "spec": {
        "host": "www.example.com",
        "to": {
          "kind": "Service",
          "name": "frontend"
        },
        "tls": {
          "termination": "edge"
        }
      },
      "status": {}
    },
    {
      "kind": "ImageStream",
      "apiVersion": "v1",
      "metadata": {
        "name": "origin-ruby-sample"
      },
      "spec": {},
      "status": {
        "dockerImageRepository": ""
      }
    },
    {
      "kind": "ImageStream",
      "apiVersion": "v1",
      "metadata": {
        "name": "ruby-22-centos7"
      },
      "spec": {
        "dockerImageRepository": "centos/ruby-22-centos7"
      },
      "status": {
        "dockerImageRepository": ""
      }
    },
    {
      "kind": "BuildConfig",
      "apiVersion": "v1",
      "metadata": {
        "name": "ruby-sample-build",
        "labels": {
          "name": "ruby-sample-build"
        },
        "annotations": {
          "template.alpha.openshift.io/wait-for-ready": "true"
        }
      },
      "spec": {
        "triggers": [
          {
            "type": "GitHub",
            "github": {
              "secret": "secret101"
            }
          },
          {
            "type": "Generic",
            "generic": {
              "secret": "secret101",
              "allowEnv": true
            }
          },
          {
            "type": "ImageChange",
            "imageChange": {}
          },
          {
            "type": "ConfigChange"
          }
        ],
        "source": {
          "type": "Git",
          "git": {
            "uri": "https://github.com/openshift/ruby-hello-world.git"
          }
        },
        "strategy": {
          "type": "Source",
          "sourceStrategy": {
            "from": {
              "kind": "ImageStreamTag",
              "name": "ruby-22-centos7:latest"
            },
            "env": [
              {
                "name": "EXAMPLE",
                "value": "sample-app"
              }
            ]
          }
        },
        "output": {
          "to": {
            "kind": "ImageStreamTag",
            "name": "origin-ruby-sample:latest"
          }
        },
        "postCommit": {
          "args": ["bundle", "exec", "rake", "test"]
        },
        "resources": {}
      },
      "status": {
        "lastVersion": 0
      }
    },
    {
      "kind": "DeploymentConfig",
      "apiVersion": "v1",
      "metadata": {
        "name": "frontend",
        "annotations": {
          "template.alpha.openshift.io/wait-for-ready": "true"
        }
      },
      "spec": {
        "strategy": {
          "type": "Rolling",
          "rollingParams": {
            "updatePeriodSeconds": 1,
            "intervalSeconds": 1,
            "timeoutSeconds": 120,
            "pre": {
              "failurePolicy": "Abort",
              "execNewPod": {
                "command": [
                  "/bin/true"
                ],
                "env": [
                  {
                    "name": "CUSTOM_VAR1",
                    "value": "custom_value1"
                  }
                ],
                "containerName": "ruby-helloworld"
              }
            },
            "post": {
              "failurePolicy": "Ignore",
              "execNewPod": {
                "command": [
                  "/bin/true"
                ],
                "env": [
                  {
                    "name": "CUSTOM_VAR2",
                    "value": "custom_value2"
                  }
                ],
                "containerName": "ruby-helloworld"
              }
            }
          },
          "resources": {}
        },
        "triggers": [
          {
            "type": "ImageChange",
            "imageChangeParams": {
              "automatic": true,
              "containerNames": [
                "ruby-helloworld"
              ],
              "from": {
                "kind": "ImageStreamTag",
                "name": "origin-ruby-sample:latest"
              }
            }
          },
          {
            "type": "ConfigChange"
          }
        ],
        "replicas": 2,
        "selector": {
          "name": "frontend"
        },
        "template": {
          "metadata": {
            "labels": {
              "name": "frontend"
            }
          },
          "spec": {
            "containers": [
              {
                "name": "ruby-helloworld",
                "image": "origin-ruby-sample",
                "ports": [
                  {
                    "containerPort": 8080,
                    "protocol": "TCP"
                  }
                ],
                "env": [
                  {
                    "name": "MYSQL_USER",
                    "valueFrom": {
                      "secretKeyRef" : {
                        "name" : "dbsecret",
                        "key" : "mysql-user"
                      }
                    }
                  },
                  {
                    "name": "MYSQL_PASSWORD",
                    "valueFrom": {
                      "secretKeyRef" : {
                        "name" : "dbsecret",
                        "key" : "mysql-password"
                      }
                    }
                  },
                  {
                    "name": "MYSQL_DATABASE",
                    "value": "${MYSQL_DATABASE}"
                  }
                ],
                "resources": {},
                "terminationMessagePath": "/dev/termination-log",
                "imagePullPolicy": "IfNotPresent",
                "securityContext": {
                  "capabilities": {},
                  "privileged": false
                }
              }
            ],
            "restartPolicy": "Always",
            "dnsPolicy": "ClusterFirst"
          }
        }
      },
      "status": {}
    },
    {
      "kind": "Service",
      "apiVersion": "v1",
      "metadata": {
        "name": "database"
      },
      "spec": {
        "ports": [
          {
            "name": "db",
            "protocol": "TCP",
            "port": 5434,
            "targetPort": 3306,
            "nodePort": 0
          }
        ],
        "selector": {
          "name": "database"
        },
        "type": "ClusterIP",
        "sessionAffinity": "None"
      },
      "status": {
        "loadBalancer": {}
      }
    },
    {
      "kind": "DeploymentConfig",
      "apiVersion": "v1",
      "metadata": {
        "name": "database",
        "annotations": {
          "template.alpha.openshift.io/wait-for-ready": "true"
        }
      },
      "spec": {
        "strategy": {
          "type": "Recreate",
          "recreateParams": {
            "pre": {
              "failurePolicy": "Abort",
              "execNewPod": {
                "command": [
                  "/bin/true"
                ],
                "env": [
                  {
                    "name": "CUSTOM_VAR1",
                    "value": "custom_value1"
                  }
                ],
                "containerName": "ruby-helloworld-database",
                "volumes": ["ruby-helloworld-data"]
              }
            },
            "mid": {
              "failurePolicy": "Abort",
              "execNewPod": {
                "command": [
                  "/bin/true"
                ],
                "env": [
                  {
                    "name": "CUSTOM_VAR2",
                    "value": "custom_value2"
                  }
                ],
                "containerName": "ruby-helloworld-database",
                "volumes": ["ruby-helloworld-data"]
              }
            },
            "post": {
              "failurePolicy": "Ignore",
              "execNewPod": {
                "command": [
                  "/bin/true"
                ],
                "env": [
                  {
                    "name": "CUSTOM_VAR2",
                    "value": "custom_value2"
                  }
                ],
                "containerName": "ruby-helloworld-database",
                "volumes": ["ruby-helloworld-data"]
              }
            }
          },
          "resources": {}
        },
        "triggers": [
          {
            "type": "ConfigChange"
          }
        ],
        "replicas": 1,
        "selector": {
          "name": "database"
        },
        "template": {
          "metadata": {
            "labels": {
              "name": "database"
            }
          },
          "spec": {
            "containers": [
              {
                "name": "ruby-helloworld-database",
                "image": "centos/mysql-57-centos7:latest",
                "ports": [
                  {
                    "containerPort": 3306,
                    "protocol": "TCP"
                  }
                ],
                "env": [
                  {
                    "name": "MYSQL_USER",
                    "valueFrom": {
                      "secretKeyRef" : {
                        "name" : "dbsecret",
                        "key" : "mysql-user"
                      }
                    }
                  },
                  {
                    "name": "MYSQL_PASSWORD",
                    "valueFrom": {
                      "secretKeyRef" : {
                        "name" : "dbsecret",
                        "key" : "mysql-password"
                      }
                    }
                  },
                  {
                    "name": "MYSQL_DATABASE",
                    "value": "${MYSQL_DATABASE}"
                  }
                ],
                "resources": {},
                "volumeMounts": [
                  {
                    "name": "ruby-helloworld-data",
                    "mountPath": "/var/lib/mysql/data"
                  }
                ],
                "terminationMessagePath": "/dev/termination-log",
                "imagePullPolicy": "Always",
                "securityContext": {
                  "capabilities": {},
                  "privileged": false
                }
              }
            ],
            "volumes": [
              {
                "name": "ruby-helloworld-data",
                "emptyDir": {
                  "medium": ""
                }
              }
            ],
            "restartPolicy": "Always",
            "dnsPolicy": "ClusterFirst"
          }
        }
      },
      "status": {}
    }
  ],
  "parameters": [
    {
      "name": "MYSQL_USER",
      "description": "database username",
      "generate": "expression",
      "from": "user[A-Z0-9]{3}",
      "required": true
    },
    {
      "name": "MYSQL_PASSWORD",
      "description": "database password",
      "generate": "expression",
      "from": "[a-zA-Z0-9]{8}",
      "required": true
    },
    {
      "name": "MYSQL_DATABASE",
      "description": "database name",
      "value": "root",
      "required": true
    }
  ],
  "labels": {
    "template": "application-template-stibuild"
  }
}