Pro DevOps with Cloud Platform With Docker, Jenkins, and

Pierluigi Riti Pro DevOps with : With Docker, Jenkins, and Kubernetes Pierluigi Riti Mullingar, Westmeath, Ireland

ISBN-13 (pbk): 978-1-4842-3896-7 ISBN-13 (electronic): 978-1-4842-3897-4 https://doi.org/10.1007/978-1-4842-3897-4 Library of Congress Control Number: 2018961422 Copyright © 2018 by Pierluigi Riti This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image, we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the author nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Steve Anglin Development Editor: Matthew Moodie Coordinating Editor: Mark Powers Cover designed by eStudioCalamar Cover image designed by Freepik (www.freepik.com) Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer- sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science+Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected]; for reprint, paperback, or audio rights, please e-mail [email protected]. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress.com/9781484238967. For more detailed information, please visit www.apress.com/source-code. Printed on acid-free paper To Mara, Nicole, and Mattia. My life is empty without you. Table of Contents

About the Author ����������������������������������������������������������������������������������������������������� xi About the Technical Reviewer ������������������������������������������������������������������������������� xiii Acknowledgments ���������������������������������������������������������������������������������������������������xv Introduction �����������������������������������������������������������������������������������������������������������xvii

Chapter 1: Introduction to DevOps ��������������������������������������������������������������������������� 1 What Is DevOps? ��������������������������������������������������������������������������������������������������������������������������� 1 The DevOps Engineer �������������������������������������������������������������������������������������������������������������������� 2 Adopting DevOps ��������������������������������������������������������������������������������������������������������������������������� 3 The Manager Must Promote the Change ��������������������������������������������������������������������������������� 3 The Developer Must Be Responsible for the Software ������������������������������������������������������������ 4 The Operational People Must Be Treated As “First-Class Citizens” ����������������������������������������� 4 Continuous Integration and Continuous Delivery Policies Must Be Built ��������������������������������� 5 Barriers to the IT Department Must Be Removed �������������������������������������������������������������������� 6 The Release Process Must Be Automated ������������������������������������������������������������������������������� 6 Agile Practices Must Be Promoted Across the Entire Company ���������������������������������������������� 7 Reasons for Adopting DevOps ������������������������������������������������������������������������������������������������������� 7 What and Who Are Involved in DevOps? ���������������������������������������������������������������������������������� 8 Changing the Coordination ������������������������������������������������������������������������������������������������������ 9 The DevOps Chain ����������������������������������������������������������������������������������������������������������������� 11 Defining the Development Pipeline ��������������������������������������������������������������������������������������������� 13 Centralizing the Building Server �������������������������������������������������������������������������������������������� 15 Monitoring Best Practices ����������������������������������������������������������������������������������������������������� 16 Best Practices for Operations ������������������������������������������������������������������������������������������������ 17 Conclusion ���������������������������������������������������������������������������������������������������������������������������������� 18

v Table of Contents

Chapter 2: Introduction to GCP ������������������������������������������������������������������������������� 19 Introduction to Cloud Computing ������������������������������������������������������������������������������������������������ 19 Cloud Computing Service Model ������������������������������������������������������������������������������������������� 20 The Deployment Models �������������������������������������������������������������������������������������������������������� 22 Why Use the Cloud? ��������������������������������������������������������������������������������������������������������������� 23 Introduction to Google Cloud Platform ���������������������������������������������������������������������������������������� 23 Starting with GCP ������������������������������������������������������������������������������������������������������������������������ 24 Understanding Billing in GCP ������������������������������������������������������������������������������������������������� 28 GCP Resources ���������������������������������������������������������������������������������������������������������������������� 30 Google SDK ��������������������������������������������������������������������������������������������������������������������������������� 32 Conclusion ���������������������������������������������������������������������������������������������������������������������������������� 35

Chapter 3: Introduction to Continuous Integration and Delivery ���������������������������� 37 Definition of Continuous Integration ������������������������������������������������������������������������������������������� 37 What Is Build in a Continuous Integration Scenario? ������������������������������������������������������������ 38 The Code Repository Server �������������������������������������������������������������������������������������������������� 40 The Continuous Integration Server ���������������������������������������������������������������������������������������� 40 Continuous Delivery �������������������������������������������������������������������������������������������������������������������� 42 Differences Between Continuous Integration and Continuous Delivery �������������������������������� 44 Strategies for Continuous Delivery ���������������������������������������������������������������������������������������� 44 Benefits of Continuous Integration and Continuous Delivery ������������������������������������������������������ 47 Designing a Continuous Integration and Continuous Delivery System ��������������������������������������� 49 Building Continuous Integration and Continuous Delivery Pipelines ������������������������������������� 51 Continuous Database Integration ������������������������������������������������������������������������������������������ 53 Continuous Testing and Inspection ���������������������������������������������������������������������������������������� 54 Preparing the Build for Release �������������������������������������������������������������������������������������������������� 56 Identifying the Code in the Repository ���������������������������������������������������������������������������������� 57 Creating Build Reports ����������������������������������������������������������������������������������������������������������� 57 Putting the Build in a Shared Location ���������������������������������������������������������������������������������� 58 Releasing the Build ���������������������������������������������������������������������������������������������������������������� 58 Conclusion ���������������������������������������������������������������������������������������������������������������������������������� 62 vi Table of Contents

Chapter 4: Containerization with Docker and Kubernetes ������������������������������������� 63 Introduction to Docker ���������������������������������������������������������������������������������������������������������������� 63 Why Use Docker? ������������������������������������������������������������������������������������������������������������������ 65 Using Docker in Google Cloud Platform �������������������������������������������������������������������������������������� 66 Introduction to �������������������������������������������������������������������������������� 66 Creating a Compute Engine Instance ������������������������������������������������������������������������������������ 67 Instance Group ���������������������������������������������������������������������������������������������������������������������� 74 Container Application in Google Cloud Platform ������������������������������������������������������������������������� 75 What Is Kubernetes? ������������������������������������������������������������������������������������������������������������������� 79 Using Kubernetes Engine to Deploy an Application �������������������������������������������������������������������� 82 Deploying Our First Kubernetes App �������������������������������������������������������������������������������������� 85 Conclusion ���������������������������������������������������������������������������������������������������������������������������������� 96

Chapter 5: Continuous Delivery with GCP and Jenkins ������������������������������������������ 99 An Introduction to Jenkins ���������������������������������������������������������������������������������������������������������� 99 Continuous Integration and Delivery with Jenkins ������������������������������������������������������������������� 100 Code ������������������������������������������������������������������������������������������������������������������������������������ 101 Unit Test ������������������������������������������������������������������������������������������������������������������������������� 101 Code Integration ������������������������������������������������������������������������������������������������������������������ 101 System Testing �������������������������������������������������������������������������������������������������������������������� 102 Stage Release ���������������������������������������������������������������������������������������������������������������������� 102 User Acceptance ������������������������������������������������������������������������������������������������������������������ 102 Production Release �������������������������������������������������������������������������������������������������������������� 102 Designing a Good Branching Strategy �������������������������������������������������������������������������������������� 103 Using Jenkins in GCP ���������������������������������������������������������������������������������������������������������������� 104 Configuring Jenkins on Kubernetes ������������������������������������������������������������������������������������ 105 Designing the Jenkins Architecture ������������������������������������������������������������������������������������� 110 Namespaces, Pods, Services, Quotas, and Deployments in Kubernetes ����������������������������� 112 Create Jenkins Service �������������������������������������������������������������������������������������������������������� 117 Deploying Jenkins on Kubernetes ��������������������������������������������������������������������������������������� 124

vii Table of Contents

Creating a Continuous Delivery Project ������������������������������������������������������������������������������������� 130 Creating the Repository ������������������������������������������������������������������������������������������������������� 135 Creating Jenkins Pipelines �������������������������������������������������������������������������������������������������� 138 Creating the Jenkinsfile ������������������������������������������������������������������������������������������������������������ 142 Conclusion �������������������������������������������������������������������������������������������������������������������������������� 146

Chapter 6: Microservices Architecture with GCP ������������������������������������������������� 147 Introduction to Microservices Architecture ������������������������������������������������������������������������������� 147 Implementing a Microservices Architecture ������������������������������������������������������������������������ 149 Microservices Architecture with Docker and Kubernetes ��������������������������������������������������� 150 Creating the Microservices Architecture in GCP ������������������������������������������������������������������ 154 Creating the Services ���������������������������������������������������������������������������������������������������������������� 155 Publishing the Image in the Registry ����������������������������������������������������������������������������������� 157 Creating the Kubernetes Cluster ����������������������������������������������������������������������������������������������� 161 Conclusion �������������������������������������������������������������������������������������������������������������������������������� 163

Chapter 7: Monitoring in GCP ������������������������������������������������������������������������������� 165 What Is a Monitoring System? �������������������������������������������������������������������������������������������������� 165 Factors Involved in the Monitoring System ������������������������������������������������������������������������� 167 Why Monitoring Is Important ����������������������������������������������������������������������������������������������� 169 White-Box and Black-Box Monitoring ��������������������������������������������������������������������������������������� 171 Latency �������������������������������������������������������������������������������������������������������������������������������� 172 Traffic ���������������������������������������������������������������������������������������������������������������������������������� 172 Error ������������������������������������������������������������������������������������������������������������������������������������� 172 Saturation ���������������������������������������������������������������������������������������������������������������������������� 172 Building a Monitoring System ��������������������������������������������������������������������������������������������� 173 Configuring Stackdriver on GCP ������������������������������������������������������������������������������������������������ 174 Creating the Application ������������������������������������������������������������������������������������������������������ 178 Log Analysis with Stackdriver ��������������������������������������������������������������������������������������������� 181 Alerts in Stackdriver ������������������������������������������������������������������������������������������������������������ 183 Policy Alert Configuration ���������������������������������������������������������������������������������������������������� 185

viii Table of Contents

Creating a Dashboard ���������������������������������������������������������������������������������������������������������� 187 Testing the Dashboard ��������������������������������������������������������������������������������������������������������� 189 Conclusion �������������������������������������������������������������������������������������������������������������������������������� 190

Chapter 8: Creating and Managing Infrastructure in GCP ������������������������������������ 191 Infrastructure As Code �������������������������������������������������������������������������������������������������������������� 191 Infrastructure as Code Principles ���������������������������������������������������������������������������������������� 192 Architecture for Infrastructure As Code ������������������������������������������������������������������������������� 194 Infrastructure As Code in Google Cloud Platform ���������������������������������������������������������������������� 195 Configuration ����������������������������������������������������������������������������������������������������������������������� 196 Templates ���������������������������������������������������������������������������������������������������������������������������� 197 Resource ����������������������������������������������������������������������������������������������������������������������������� 201 Types ����������������������������������������������������������������������������������������������������������������������������������� 201 Manifest ������������������������������������������������������������������������������������������������������������������������������� 201 Deployment ������������������������������������������������������������������������������������������������������������������������� 204 Starting with Google Cloud Deployment Manager �������������������������������������������������������������������� 205 Upgrading Our Infrastructure ���������������������������������������������������������������������������������������������������� 209 Expanding and Customizing Our Deployment ��������������������������������������������������������������������� 212 Creating a Template for Our Deployment ���������������������������������������������������������������������������� 216 Defining the Template with the Environment Variable ��������������������������������������������������������� 219 Conclusion �������������������������������������������������������������������������������������������������������������������������������� 222

Chapter 9: Identity and Access Management with Google Cloud Platform ���������� 223 What Is Identity and Access Management? ������������������������������������������������������������������������������ 223 Defining a Digital Entity ������������������������������������������������������������������������������������������������������� 224 The Importance of Digital Identity ��������������������������������������������������������������������������������������� 225 IAM and Digital Entity ���������������������������������������������������������������������������������������������������������������� 226 Authentication ��������������������������������������������������������������������������������������������������������������������� 228 Authorization ����������������������������������������������������������������������������������������������������������������������� 228 Roles ������������������������������������������������������������������������������������������������������������������������������������ 228 IAM in Google Cloud Platform ��������������������������������������������������������������������������������������������������� 228 Google IAM Policy ��������������������������������������������������������������������������������������������������������������������� 232

ix Table of Contents

Creating and Managing the IAM Policy ������������������������������������������������������������������������������������� 234 Creating a JSON File ������������������������������������������������������������������������������������������������������������ 235 Using the API via REST �������������������������������������������������������������������������������������������������������� 239 Creating the API via Java Code �������������������������������������������������������������������������������������������� 240 Using the Console ���������������������������������������������������������������������������������������������������������������� 242 Conclusion �������������������������������������������������������������������������������������������������������������������������������� 244

Chapter 10: Network Configuration and Management in GCP ������������������������������ 245 Fundamentals of Networking in GCP ���������������������������������������������������������������������������������������� 245 Introduction to the Virtual Private Cloud ����������������������������������������������������������������������������������� 247 Network and Subnets ���������������������������������������������������������������������������������������������������������� 249 Defining the IP Range for the Subnet ���������������������������������������������������������������������������������� 250 Routes and Firewalls ����������������������������������������������������������������������������������������������������������������� 252 Tagging Network ����������������������������������������������������������������������������������������������������������������� 254 Implementing a VPC Network ���������������������������������������������������������������������������������������������� 255 Creating and Maintaining Firewall Rules ����������������������������������������������������������������������������� 262 Creating and Maintaining Routes in GCP ���������������������������������������������������������������������������������� 265 Conclusion �������������������������������������������������������������������������������������������������������������������������������� 269

Index ��������������������������������������������������������������������������������������������������������������������� 271

x About the Author

Pierluigi Riti has more than 20 years of extensive experience in the design and development of different scale applications, particularly in the telecommunications and financial industries. He is a freelance consultant, with specialization in DevOps, the cloud, and security. He has quality-development skills in the latest technologies, including Java, J2EE, C#, F#, .NET, Spring .NET, EF, WPF, WF, WinForm, WebAPI, MVC, Nunit, Scala, Spring, JSP, EJB, Struts, Struts2, SOAP, REST, C, C++, Hibernate, NHibernate, Weblogic, XML, XSLT, Unix script, Ruby, and Python. He is the author of Practical Scala DSL, also published by Apress. He can be contacted via his LinkedIn profile page at ­www.linkedin.com/in/pierluigi-riti/.

xi About the Technical Reviewer

David Gonzalez is a DevOps engineer who has written three books about DevOps and microservices. He works as a consultant, helping large companies to advance their systems development, by tweaking related software processes and tools. David is also a Google Developer Expert (https://developers.google.com/experts/people/david-­ gonzalez-­gonzalez) in Kubernetes (Google Container Engine) and a member of the Node.js foundation, working on security in third-party npm packages. In his free time, he enjoys cycling and walking with his dogs in the green fields of Ireland.

xiii Acknowledgments

My thanks go to David Gonzalez, for the amazing feedback he provided during the technical review of the text. Another big thank you to Steve, Matthew, and Mark, for their support and help in completing this book.

xv Introduction

Cloud technology is increasingly present in our daily lives. The intent of this book is to introduce Google Cloud Platform, of cloud computing services, with an eye to DevOps practices. DevOps is currently a hot commodity in the information technology (IT) field, and every day, a new company begins to adopt its practice. The cloud is a natural environment for DevOps, because in the cloud environment, such practices as infrastructure as code and continuous delivery can be adopted more efficiently. Thus, the cloud is the natural environment in which DevOps can grow and be increasingly effective. In 2003, Google created Site Reliability Engineering (SRE). This represented a new approach to the continuous large-scale release of product features. SRE shares many concepts with DevOps. I use Google Cloud Platform, because it is one of the most highly recommended by Gartner, particularly its use in infrastructure development. As with the Gartner Magic Quadrant, strong network management, and with the integration of Kubernetes, it is ideal for developing container and infrastructure solutions. By adding code, Google Cloud Platform offers a complete solution for creating the deployment management for release of infrastructure. In addition, Google Cloud Platform has one of the highest levels of application programming interface (API) available in the cloud environment. All the code in this book can be accessed from the related GitHub site. Please feel free to read that code and alter it for your personal use.

xvii