text stringlengths 0 2.72k |
|---|
Graham Dumpleton Deploying to Open Shift A GUIDE FOR BUSY DEVELOPERS Compliments of |
Graham Dumpleton Deploying to Open Shift A Guide for Busy Developers Boston Farnham Sebastopol Tokyo Beijing Boston Farnham Sebastopol Tokyo Beijing |
978-1-492-03248-9 [LSI]Deploying to Open Shift by Graham Dumpleton Copyright © 2018 Red Hat, Inc. All rights reserved. Printed in the United States of America. Published by O'Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O'Reilly books may be purchased for educational, business, or sales pro... |
Table of Contents Preface....................................................................... ix 1. The Open Shift Container Platform............................................. 1 The Role of Containers 2 Orchestrating at Scale 3 Containers as a Service 4 Platform as a Service 4 Deploying Y our Application 5 2. Run... |
Deploying an Image 26 Deploying a Set of Resources 27 Summary 28 5. Deploying Applications from Images.......................................... 29 Deploying Y our First Image 29 Scaling Up the Application 33 Runtime Configuration 33 Deleting the Application 34 Deploying Using the Web Console 34 Importing an Image 36 P... |
Adding an S2I Builder to the Catalog 63 Summary 64 9. Customizing Source-to-Image Builds.......................................... 65 Using Environment Variables 65 Overriding the Builder Scripts 66 Read-Only Code Repositories 68 Overriding the Runtime Image 68 Updating the Image Metadata 69 Summary 71 10. Using Increm... |
Local Port Forwarding 100 Summary 101 14. Working with Persistent Storage............................................ 103 Types of Persistent Storage 103 Claiming a Persistent Volume 105 Unmounting a Persistent Volume 106 Reusing a Persistent Volume Claim 106 Sharing Between Applications 106 Sharing Between Containers ... |
Viewing Application Logs 130 Monitoring Resource Objects 131 Monitoring System Events 132 Viewing Container Metrics 132 Running an Interactive Shell 133 Debugging Startup Failures 133 Summary 134 Afterword................................................................... 137 Index......................................... |
Preface Open Shift implements a polyglot platform for the deployment of web applications and services. It uses containers in conjunction with a Security-Enhanced Linux (SELi‐ nux) environment to implement a secure multitenant environment suitable for the enterprise. Y ou can deploy Open Shift in your own infrastructure... |
The book is the third in a series of books from Red Hat about the latest version of Open Shift. The prior books in the series are: Open Shift for Developers: A Guide for Impatient Beginners Dev Ops with Open Shift: Cloud Deployments Made Easy Why I Wrote This Book The first book, Open Shift for Developers, which I coau... |
tures, including experimental features, with support being provided by the Open Shift community. Y ou are warmly invited to clone the Open Shift Origin project code, send in your contributions, or open an issue to report any problems you find. The Open Shift product releases are created as regular snapshots of the Open... |
Constant width Used for program listings, as well as within paragraphs to refer to program ele‐ ments such as variable or function names, databases, data types, environment variables, statements, and keywords. Constant width bold Shows commands or other text that should be typed literally by the user. Constant width it... |
O'Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, CA 95472 800-998-9938 (in the United States or Canada) 707-829-0515 (international or local) 707-829-0104 (fax) To comment or ask technical questions about this book, send email to bookques‐ tions@oreilly. com. For more information about our books, courses... |
CHAPTER 1 The Open Shift Container Platform The Open Shift platform was launched in May 2011. The source code was made avail‐ able through an open source project, with anyone being able to download it and use it. Red Hat also offered a supported version of Open Shift for use in enterprise deploy‐ ments, and a hosted se... |
The Role of Containers The National Institute of Standards and Technology (NIST) defines as part of their definition of cloud computing three standard service models for the provision of cloud computing services: Software as a Service (Saa S) The capability provided to the consumer is to use the provider's applications... |
In 2013 a company called dot Cloud, a Paa S provider, announced in a lightning talk at Py Con US a tool called Docker. This tool was an outgrowth of the proprietary tech‐ nology that dot Cloud used to help run applications in containers; it provided a wrap‐ per for making it easier to launch applications in containers ... |
Kubernetes was subsequently released as 1. 0 in July 2015, with the project being donated to the Cloud Native Computing Foundation (CNCF). It has since become the de facto standard for container orchestration. Containers as a Service Kubernetes does not fit any of the existing service model classifications for cloud co... |
To provide the Paa S capability of being able to take source code and deploy it, Open‐ Shift adds automation for performing builds on top of Kubernetes. Open Shift sup‐ ports two main strategies for building from source code. In the style of a traditional Paa S, Open Shift can take your application source code and, usi... |
Y ou will also learn how to integrate your application into the Open Shift platform, how to configure it through Open Shift, how to mount persistent volumes, how to make it public so users can access it, and how to monitor and debug your application. To interact with Open Shift you can use either a web console or a com... |
CHAPTER 2 Running an Open Shift Cluster The easiest way to get started with Open Shift is to get access to a hosted version, such as Open Shift Online from Red Hat. To install and run Open Shift yourself, you can download the source from the upstream Open Shift Origin project. For quickly running Open Shift on your own... |
Installing Open Shift Origin If you prefer to install and run Open Shift yourself, you can install Open Shift Origin. The recommended installation method for Open Shift Origin uses a set of Ansible playbooks. Provide details about your environment and the machine nodes you have available, run Ansible, and the cluster w... |
... Open Shift server started. The server is accessible via web console at: https://192. 168. 99. 128:8443 You are logged in as: User: developer Password: developer To login as administrator: oc login-u system:admin By default Minishift attempts to use the virtualization driver native to the operating system. To use a ... |
Shift cluster. Y ou can override the default values for these resources, as well as the VM driver used, using the minishift config command. Minishift uses Open Shift Origin. For a version of Minishift that uses Open Shift Container Platform from Red Hat, see the Red Hat Container Development Kit. Running oc cluster up ... |
To find the URL for the Open Shift web console, you can run oc whoami--show-server. To shut down the Open Shift cluster, run oc cluster down. The oc cluster up command is intended for local development and testing, not for production use. By default, when you run oc cluster up, anything you do within the Open Shift clu... |
CHAPTER 3 Accessing the Open Shift Cluster At this point, you should have a basic understanding of the Open Shift platform and what it can be used for. To deploy your own applications to an Open Shift cluster, you can use either the Open Shift web console, or the oc command-line client. Before you can do anything, thou... |
Figure 3-1. Web console login In the case of an external Open ID Connect or OAuth authentication service provider being used, it will be necessary to log in through the external service ( Figure 3-2 ). Figure 3-2. Web console login via Red Hat For a new user, once you have logged in, you should be presented with the “W... |
thing you may want to do, especially in a cluster or project administrator role, can be done through the web console. Y ou will therefore also need to be familiar with using the Open Shift command-line tool, oc. Always use a version of the oc command-line tool that matches the version of the Open Shift environment you ... |
The form of the login command, with the token, is: oc login https://api. starter-us-east-1. openshift. com--token=Sbqw....T3UU The token used when you log in will periodically expire, and you will need to log in again when it does. When this occurs, you can run oc login with no options: $ oc login Login failed (401 Una... |
oc new-project <projectname> For help on the specific options accepted by oc login, or any other command, run the command but with the--help option: $ oc login--help To get general information about the oc command, run it with no options. To get a list of all the commands that oc accepts, run it with the help command:... |
curl-H "Authorization: Bearer $TOKEN" $URL As when logging in using oc login, this token will expire and will need to be renewed. The REST API can be used to manage end-user applications, the cluster, and the users of the cluster. This book will not delve into how to use the REST API; for fur‐ ther information, see the... |
CHAPTER 4 Adding Applications to a Project Now that you know how to access your Open Shift cluster and can log in, you are almost ready to start deploying your first application. Applications can be deployed from an existing container image that you have built outside the Open Shift cluster, or one that is supplied by ... |
tions running in the same project, unless you choose to make it public outside the Open Shift cluster. Y ou can deploy more than one application into a single project. Y ou would usually do this if they have tight coupling. Or, you could instead choose to always create a sepa‐ rate project for each application and sele... |
Figure 4-2. Creating a new project When you specify a name for a project, it will need to satisfy a couple of require‐ ments. The first requirement is that the name you choose must be unique across the whole Open Shift cluster. This means you cannot use a project name that is already in use by another user. The second ... |
Projects can be created from the command line too, by using the oc new-project command: $ oc new-project myproject--display-name 'My Project' Already on project "myproject" on server "https://localhost:8443". You can add applications to this project with the 'new-app' command. For example, try: oc new-app centos/ruby-2... |
admin A project manager. The user will have rights to view any resource in the project and modify any resource in the project except for quotas. A user with this role for a project will be able to delete the project. edit A user that can modify most objects in a project, but does not have the power to view or modify ro... |
From image source code in a Git repository hosting service. The image source code would be built into an image inside Open Shift using instructions provided in a Dockerfile. From application source code pushed into Open Shift from a local filesystem using the command-line oc client. The application source code would be... |
Clicking on “ Add to Project” will bring you to a catalog of application templates and S2I builders, as shown in Figure 4-5, that have been preinstalled into the Open Shift cluster. Y ou can also get to this page by clicking “ Add to Project” in the top banner of any project and selecting Browse Catalog. Figure 4-5. Br... |
To get a list of the images available, use the command oc get imagestreams. The--namespace openshift option should again be supplied to list those in the open shift project. When you run this command, not all images listed may correspond to a builder image. This is because an image is also constructed for the applicati... |
To use an image that is hosted on an image registry outside the Open Shift cluster, select Image Name and enter the name of the image, including the hostname of the image registry if using an image registry other than Docker Hub. If using the command line, images hosted on any image registry can be deployed using the o... |
Summary Projects provide a space into which you can deploy your applications. Y ou can choose to do everything within a single project, or use multiple projects and selectively enable access between projects so that different application components can commu‐ nicate with each other. The same features that provide isola... |
CHAPTER 5 Deploying Applications from Images Now that you have created a project, you can move on to deploying an application. In this chapter you will start out by deploying an application from a pre-existing con‐ tainer image hosted on an external image registry. Y ou would use this method if you created the image fo... |
To deploy the container image use the oc new-app command, providing it with the location of the image. The--name option is to set the name for the deployed applica‐ tion. If a name is not supplied, it will default to the last part of the image name. We'll use the name blog : $ oc new-app openshiftkatacoda/blog-django-p... |
This will create a resource object called a route. With the application deployed and visible, you can check on the status of the overall project using the oc status command: $ oc status In project My Project (myproject) on server https://127. 0. 0. 1:8443 http://blog-myproject. 127. 0. 0. 1. nip. io to pod port 8080-tc... |
The Open Shift web console, shown in Figure 5-1, can also be used to view the appli‐ cation. Y ou will find a summary for the deployed application in the project overview. Figure 5-1. Blog site overview with route Visit the URL displayed in the application summary with a web browser and you will be presented with the h... |
Scaling Up the Application When oc new-app is used to deploy an application from a container image, only one instance of the application will be started. If you need to run more than once instance in order to handle the expected traffic, you can scale up the number of instances by running the oc scale command against t... |
Deleting the Application When you no longer need the application, you can delete it using the oc delete command. When doing this, you need to be selective about which resource objects you delete to ensure you delete only those for that particular application. This can be achieved by using the label that was applied by ... |
Figure 5-3. Deploy application image Click on the Deploy Image tab. Select Image Name to use an image stored on an external image registry. Enter openshiftkatacoda/blog-django-py as the value for Image Name. Press Enter, or click on the query icon to pull down details of the image. Change the name for the application t... |
Figure 5-4. The Create Route link Services, networking, and routes will be covered in more detail in Chapter 13. The overview page also provides the ability to scale up or down the number of instan‐ ces of the deployed web application. Use the up and down arrows to the right of the circle showing the number and status ... |
In the web console, instead of the Image Name option on the Deploy Image page, you would use the Image Stream Tag option. Because the image is imported prior to deploying an application, it is not tagged with the app label of a specific deployment. When you delete any of the applications using the label, you will not d... |
Images and Security When your application is deployed to Open Shift the default security model will enforce that it is run using an assigned Unix user ID unique to the project you are deploying it into. This behavior is implemented as part of the multitenant capabilities of Open Shift, but is also done to prevent image... |
you can set up an image stream definition such that, when deploying the image, it will be automatically pulled into and cached in the internal image registry. When deploying an image from the web console or command line, Open Shift will automati‐ cally set up this image stream for you. Hosting of an image registry inte... |
CHAPTER 6 Building and Deploying from Source When you deploy a pre-existing container image, if this is your own image, it means you needed to have separate tooling available to construct that image. Y ou also had to upload the image to an image registry from which Open Shift could pull it down, or push the image into ... |
Details of how to use the Docker build strategy will be covered in Chapter 7. How‐ ever, this book will not go into the last two build strategies. See the Open Shift docu‐ mentation for more details on the Pipeline and Custom build strategies. The Source Build Strategy The Source build strategy uses the Source-to-Image... |
The result of running this command is:--> Found image 956e2bd (5 days old) in image stream "openshift/python" under tag "3. 5" for "python" Python 3. 5 ---------- Platform for building and running Python 3. 5 applications Tags: builder, python, python35, rh-python35 * A source build using source code from https://githu... |
Y ou can perform the build step separately by running the oc new-build command instead of the oc new-app command: $ oc new-build--name blog \ python:3. 5~https://github. com/openshift-katacoda/blog-django-py --> Found image 956e2bd (5 days old) in image stream "openshift/python" under tag "3. 5" for "python" Python 3. ... |
--> Success Run 'oc status' to view your app. The service once again can be exposed using oc expose. Triggering a New Build In the event that the source files used as input to the Source build strategy have changed, a new build can be triggered using the oc start-build command: $ oc get bc NAME TYPE FROM LATEST blog So... |
code trigger will then ensure the latest code is pulled down and the S2I build process run against it to generate the updated application image, with the application also being redeployed as a result. The topic of automating builds when source code changes are made will be covered further in Chapter 11. Building from a... |
Tags: builder, python, python35, rh-python35 * A source build using binary input will be created * The resulting image will be pushed to image stream "blog:latest" * A binary build was created, use 'start-build--from-dir' to trigger a new build--> Creating resources with label build=blog... imagestream "blog" created ... |
entrypoint. If--is used, arguments to pass to the original image command can be supplied. To remove a build hook, the--remove option can be used. When running unit tests from a post-commit hook, you should avoid contacting other services, as the container will be run in the same project as your deployed appli‐ cation. ... |
$ oc set env bc/blog--list # buildconfigs blog UPGRADE_PIP_TO_LATEST=1 Although the environment variables are specified in the build configuration, they will also be set in the image created and visible to the application when it is deployed. Summary The ability to deploy an application from source code, with Open Shif... |
CHAPTER 7 Building an Image from a Dockerfile Using an S2I builder to create an application image simplifies the build process, as the builder does all the hard work for you. To make it simple, the author of the S2I builder will have made some decisions in advance about what application server stack is used, how it is ... |
image will therefore trigger the subsequent builds and deployments, automating your workflow. To illustrate how to set up a build from a Dockerfile, we are going to build the same image we used in Chapter 5 to deploy our blog site, but do it inside Open Shift. We will then do a quick recap on how to deploy the image. T... |
--> Creating resources with label build=blog... imagestream "python-35-centos7" created imagestream "blog" created buildconfig "blog" created --> Success Build configuration "blog" created and build triggered. Run 'oc logs-f bc/blog' to stream the build progress. The build, when run, will use the Dockerfile in the rep... |
to oc new-app. This can be done when deploying directly from source code, or when using oc new-app to deploy the image created by a distinct build step. If it is necessary to set environment variables for the build step and you are deploying directly from source code, use the--build-env option to oc new-app : $ oc new-... |
from the image registry, you will be able to see the value associated with a build argu‐ ment using docker history or docker inspect. Using an Inline Dockerfile Images can be customized by creating a new image that derives from an existing image. Additional layers are then added, which include further software packages... |
When the build has completed, the customized image can be used in place of the original S2I builder image by using the name of the imagestream created in place of the original builder name. Summary As an extension to the traditional Paa S functionality of being able to build from application source code with the platfo... |
CHAPTER 8 Understanding Source-to-Image Builders The Docker build strategy provides the most control over how to build an image. Because of the potential security risks with allowing Docker builds within a shared Open Shift cluster, the ability to use the Docker build strategy would usually be restricted to trusted dev... |
To illustrate how the S2I build process works, we will use the s2i command-line tool directly. Building the Application Image To build an application image using the s2i command-line tool, you need two inputs. The first is your application source code. The second is an S2I builder image that sup‐ ports the programming ... |
Although the process of generating the application image may appear to be magic, the resulting image isn't special. Y ou can build images outside Open Shift using s2i and run them using a local container service, share them with others by pushing them to an image registry, or deploy them to Open Shift. Using the S2I su... |
Creating an S2I Builder Image As an S2I builder is a container image, you can create your own custom builder images. This is done by building an image from a Dockerfile. The Open Shift project provides a convenient base image you can use as a starting point for a custom S2I builder. To illustrate a simple S2I builder, ... |
instructions on using the S2I builder image see: * https://github. com/openshift-katacoda/simple-http-server EOF The usage script is provided as a way for users to find information on how to use your builder image. The main work in preparing the image is done by the assemble script, which looks like this: #!/bin/bash e... |
ID 1 of the container, allowing the application to receive any signals sent to the con‐ tainer to trigger a shutdown. With both the assemble and run scripts, if any step in the scripts fails, the script as a whole should fail immediately. The easiest way of ensuring this, without needing to check the result of every co... |
The final option is to build the S2I builder image in Open Shift. The source files for this example can be found on Git Hub. To build it in Open Shift you would run: $ oc new-build--name simple-http-server--strategy =docker \ --code https://github. com/openshift-katacoda/simple-http-server $ oc start-build simple-http-... |
In the catalog of the web console, you can now search for simple-http-server and create your application using the builder from your browser. Additional annotations can be added to the image stream definition to provide a dis‐ play name, description, and category. For addition information, see the Open Shift documentat... |
CHAPTER 9 Customizing Source-to-Image Builds Rather than creating an S2I builder from scratch, it is more typical that you will require only minor customizations to the behavior of the S2I builder image for a spe‐ cific application. A well-designed S2I builder image should provide the ability to customize the behav‐ io... |
Configuration for a builder can be supplied using environment variables. Y ou saw in Chapter 6 how environment variables can be set for the deployment of an application, or when the application is being built using an S2I builder. In the prior examples, the environment variables were set as part of the build and deploy... |
set-eo pipefail # Set environment variables. #... # Execute original assemble script. /usr/libexec/s2i/assemble # Run additional build steps. #... Ensure the script is executable. Being a shell script, you can include any shell code in it, including code that dynamically sets environment variables. Remember that it is ... |
Read-Only Code Repositories Adding assemble and run scripts to the application source files can only be done if you own the original source code, or if the application source code is using a hosted Git repository and you have forked the original code repository. If you can't add to the original source files, or don't w... |
$ oc create configmap blog-run-script--from-file =run Next, mount the config map into the container: $ oc set volume dc/blog--add--type =configmap \ --configmap-name=blog-run-script \ --mount-path=/opt/app-root/scripts Then update the deployment configuration to execute this script when starting the container: $ oc pat... |
#!/bin/bash set-eo pipefail # Move assemble/run scripts to new location. mkdir /opt/app-root/s2i mv /tmp/src/* /opt/app-root/s2i # Override image metadata for builder image. mkdir-p /tmp/. s2i cat > /tmp/. s2i/image_metadata. json << EOF { "labels": [ { "io. openshift. s2i. scripts-url": "image:///opt/app-root/s2i" } ]... |
The only restriction is that the build runs as a non-root user, and as such it is not pos‐ sible to install additional operating system packages. Summary A Source-to-Image builder can typically be configured by passing environment vari‐ ables through to the build or subsequent deployment of the application image cre‐ a... |
CHAPTER 10 Using Incremental and Chained Builds When a Source-to-Image builder is used, the build phase is performed in a single step by the assemble script. One issue with this is that each build is distinct. This means that build artifacts generated by one build cannot be used for a subsequent build. As everything ne... |
know that certain code files do not need to be recompiled as the code change won't affect them. When using an S2I builder, however, everything that may be required to build the application has to be done every time. This is because each build is run in a new con‐ tainer, starting with only the builder base image. In th... |
Saving Artifacts from a Build In order to extract build artifacts from the previous image produced by a build, the S2I builder must provide a save-artifacts script. This should be placed in the same directory as the assemble and run scripts. An example of a generic save-artifacts script is as follows: #!/bin/bash mkdir... |
Enabling Incremental Builds To enable incremental builds for an existing build configuration, you must set the spec. strategy. source Strategy. incremental property of the build configuration to true. To enable incremental builds on the sample blog site application from Chapter 6, you can use the oc patch command: $ oc... |
Using Chained Builds Chained builds are similar to incremental builds in that files are copied across from an existing image. There are, however, two key differences when using chained builds. The first is that instead of the files being copied from a previous build of the same image, they are copied from a separate im... |
To create the chained build we need to use oc new-build to first create the build for the application. The--source-image option specifies the name of the image contain‐ ing the prebuilt artifacts. The--source-image-path option indicates which directory in the source image should be copied into the application image whe... |
CHAPTER 11 Webhooks and Build Automation The support in Open Shift for the Docker and Source build strategies simplifies the build and deployment process, as Open Shift worries about the details of running the steps for you. When the source code is contained in a hosted Git repository, Open Shift can also automatically... |
Y ou also now how to manually trigger a new build of the application, from the description of the build configuration in the web console or by running the oc start-build command from the command line. Except in the case of a binary input source build being used, the application source code will be pulled down from a ma... |
$ oc secrets new-sshauth github-blog-sshauth \ --ssh-privatekey=$HOME/. ssh/github-blog-sshauth Grant the builder service account access to the secret, so it can be used when pulling down the source code to build the application: $ oc secrets link builder github-blog-sshauth Finally, add an annotation to the secret to ... |
Adding a Repository Webhook A webhook (also known as a user-defined HTTP callback) is a way for an application to notify another application of a change. All major Git repository hosting services support generating a callback via a webhook when a new set of source code changes is pushed to the hosted Git repository. Op... |
the Git hosting service you are using, you can run the command oc set triggers with the build configuration name as an argument, and pass the option--from-github,--from-gitlab, or--from-bitbucket as appropriate. Rerun oc describe on the build configuration to view the new webhook URL. Use the URL when configuring the G... |
When building from application source code, the files can be pulled from a hosted Git repository. This may be a public or private Git repository. Input for builds can also be uploaded for a single build from a local directory on your own system. When using a hosted Git repository, you can configure a webhook callback t... |
End of preview. Expand in Data Studio
README.md exists but content is empty.
- Downloads last month
- 4