Deploying to Openshift a GUIDE for BUSY DEVELOPERS
Total Page:16
File Type:pdf, Size:1020Kb
Compliments of Deploying to Graham Dumpleton OpenShift A GUIDE FOR BUSY DEVELOPERS Deploying to OpenShift A Guide for Busy Developers Graham Dumpleton Beijing Boston Farnham Sebastopol Tokyo Deploying to OpenShift 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 promotional use. Online editions are also available for most titles (http://oreilly.com/safari). For more information, contact our corporate/insti‐ tutional sales department: 800-998-9938 or [email protected]. Editors: Virginia Wilson and Nikki McDonald Indexer: Judy McConville Production Editor: Melanie Yarbrough Interior Designer: David Futato Copyeditor: Dwight Ramsey Cover Designer: Karen Montgomery Proofreader: Rachel Head Illustrator: Rebecca Demarest April 2018: First Edition Revision History for the First Edition 2018-03-05: First Release This work is part of a collaboration between O’Reilly and Red Hat. See our statement of editorial independ‐ ence. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Deploying to OpenShift, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-492-03248-9 [LSI] Table of Contents Preface. ix 1. The OpenShift Container Platform. 1 The Role of Containers 2 Orchestrating at Scale 3 Containers as a Service 4 Platform as a Service 4 Deploying Your Application 5 2. Running an OpenShift Cluster. 7 Using OpenShift Online 7 Installing OpenShift Origin 8 Launching Using Minishift 8 Running oc cluster up 10 Summary 11 3. Accessing the OpenShift Cluster. 13 Using the Web Console 13 Using the Command Line 14 Using the OpenShift REST API 17 Summary 18 4. Adding Applications to a Project. 19 The Role of a Project 19 Creating a Project 20 Adding a Collaborator 22 Deploying Applications 23 Deploying from the Catalog 24 iii Deploying an Image 26 Deploying a Set of Resources 27 Summary 28 5. Deploying Applications from Images. 29 Deploying Your 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 Pushing to the Registry 37 Images and Security 38 Summary 38 6. Building and Deploying from Source. 41 The Source Build Strategy 42 Deploying from Source 42 Creating a Separate Build 43 Triggering a New Build 45 Building from a Local Source 46 Binary Input Builds 46 Testing the Container Image 47 Build and Runtime Configuration 48 Summary 49 7. Building an Image from a Dockerfile. 51 The Docker Build Strategy 51 Security and Docker Builds 52 Creating the Build 52 Deploying the Image 53 Build and Runtime Configuration 53 Using an Inline Dockerfile 55 Summary 56 8. Understanding Source-to-Image Builders. 57 The Source-to-Image Project 57 Building the Application Image 58 Assembling the Source Code 59 Creating an S2I Builder Image 60 Building the S2I Builder Image 62 Using the S2I Builder with OpenShift 62 iv | Table of Contents 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 Incremental and Chained Builds. 73 Faster Builds Using Caching 73 Using Incremental Builds 74 Saving Artifacts from a Build 75 Restoring the Build Artifacts 75 Enabling Incremental Builds 76 Using Chained Builds 77 Summary 78 11. Webhooks and Build Automation. 79 Using a Hosted Git Repository 79 Accessing a Private Git Repository 80 Adding a Repository Webhook 82 Customized Build Triggers 83 Summary 83 12. Configuration and Secrets. 85 Passing Environment Variables 85 Working with Configuration Files 87 Handling of Secret Information 89 Deleting Configuration and Secrets 91 Summary 92 13. Services, Networking, and Routing. 93 Containers and Pods 93 Services and Endpoints 94 Connecting Between Projects 96 Creating External Routes 96 Using Secure Connections 98 Internal and External Ports 99 Exposing Non-HTTP Services 100 Table of Contents | v 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 107 Deleting a Persistent Volume 107 Copying Data to a Volume 108 Summary 108 15. Resource Quotas and Limits. 109 What Is Managed by Quotas 109 Quotas versus Limit Ranges 111 Requests Versus Limits 112 Resource Requirements 113 Overriding Build Resources 114 Summary 114 16. Monitoring Application Health. 115 The Role of a Readiness Probe 115 The Role of a Liveness Probe 116 Using an HTTP Request 116 Using a Container Command 117 Using a Socket Connection 118 Probe Frequency and Timeouts 118 Summary 120 17. Application Lifecycle Management. 121 Deployment Strategies 121 Rolling Deployment 122 Recreate Deployment 123 Custom Deployments 124 Container Runtime Hooks 125 Init Containers 126 Summary 127 18. Logging, Monitoring, and Debugging. 129 Viewing the Build Logs 129 vi | Table of Contents 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. 139 Table of Contents | vii Preface OpenShift 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. You can deploy OpenShift in your own infrastructure or on public clouds, or you can use OpenShift Online, Red Hat’s cloud-based hosting service. The latest version of OpenShift uses the industry-standard Kubernetes platform from the Cloud Native Computing Foundation (CNCF) for managing and running appli‐ cations within containers at scale. The ability to run any application image is ensured through adherence to image and runtime specifications from the Open Container Initiative (OCI). OpenShift offers you the ability to easily deploy your web application code directly using a library of predefined image builders, or you can bring your own container images. With support in OpenShift for features such as persistent volumes, you are not limited to just running stateless 12-factor or cloud-native applications. Using OpenShift, you can also deploy databases and many legacy applications that you otherwise would not be able to run on a traditional Platform as a Service (PaaS) offer‐ ing. OpenShift is a complete container application platform. It is a modern take on the traditional PaaS that you can use with your existing applications, but that also pro‐ vides the power and flexibility to meet future needs. Who Should Read This Book This book is intended for developers who are evaluating OpenShift, or have already decided to use it, and who seek a more in-depth knowledge of the core features of OpenShift that are used to deploy applications. It will also be of interest to adminis‐ trators who are managing an OpenShift cluster and who need to provide assistance to developers using the platform. ix The book is the third in a series of books from Red Hat about the latest version of OpenShift. The prior books in the series are: • OpenShift for Developers: A Guide for Impatient Beginners • DevOps with OpenShift: Cloud Deployments Made Easy Why I Wrote This Book The first book, OpenShift for Developers, which I coauthored with Grant Shipley, aimed to get you started as quickly as possible by skipping many of the details. Those details are important, though, when you want to make the most of OpenShift. In this book I wanted to fill in some of those gaps to give you that broader understanding of OpenShift and how it can make your job of deploying applications to the cloud easier. The topics I have chosen for the book are based on my experience of having to answer many a question on public community forums (such as Stack Overflow and Google Groups) and at conferences, in my role at Red Hat working as a Developer Advocate for OpenShift. The aim is that this book will act as a quick reference guide you can keep coming back to in order to refresh your memory on commonly used patterns, or to learn more about additional topics as you continue to use OpenShift. Online Resources As with many new technologies, OpenShift is still evolving as it adapts to the wide range of use cases it is applied to. When you come to read this book, some informa‐ tion may not provide the most up-to-date picture. That’s why we encourage you to check online resources for the latest details on OpenShift and how to use it. The OpenShift documentation is a great place to start when you’re looking for infor‐ mation about OpenShift, from OpenShift Online to Red Hat’s enterprise products. To learn more about OpenShift by working through online training exercises, you can use the interactive learning portal for OpenShift, implemented on top of Kata‐ coda.