Kubernetes Cookbook Learn how to automate and manage your Linux containers and improve the overall performance of your system Hideto Saito Hui-Chuan Chloe Lee Ke-Jou Carol Hsu BIRMINGHAM - MUMBAI
Kubernetes Cookbook Copyright 2016 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: June 2016 Production reference: 1270616 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78588-006-3 www.packtpub.com
Credits Authors Hideto Saito Hui-Chuan Chloe Lee Ke-Jou Carol Hsu Reviewer Matt Ma Commissioning Editor Kartikey Pandey Acquisition Editor Divya Poojari Content Development Editor Sachin Karnani Project Coordinator Nikhil Nair Proofreader SaÞ s Editing Indexer Hemangini Bari Graphics Jason Monteiro Production Coordinator Aparna Bhagat Cover Work Aparna Bhagat Technical Editor Pranav Kukreti Copy Editor Akshata Lobo
About the Authors Hideto Saito has around 20 years of experience in the computer industry. In 1998, while working for Sun Microsystems Japan, he was impressed with Solaris OS, OPENSTEP, and Sun Ultra Enterprise 10000 (AKA StarFire). Then, he decided to pursue the UNIX and MacOS X operation systems. In 2006, he relocated to Southern California as a software engineer to develop products and services running on Linux and MacOS X. He was especially renowned for his quick Objective-C code when he was drunk. He is also an enthusiast of Japanese anime, drama, and motor sports, and loves Japanese Otaku culture. There were a lot of difþ culties while writing this book. I believe it was the busiest moment in my life. But I was lucky to have two talented friends, Chloe and Carol, to support this project. I hope I have a chance to work with them again. Lastly, I appreciate my wife and children for their support. Their support and understanding brings me success and happiness.
Hui-Chuan Chloe Lee has worked in the software industry for over 5 years. She has a master's degree in CS from the National Taiwan University and is an AWS-certiÞ ed associate solution architect. Chloe is also a technology enthusiast who has extended interest and experiences in different topics, such as application development, container technology, and Continuous Delivery. In her free time, she enjoys reading, traveling, and spending time with the people she loves. This book is dedicated to the people I love. I feel so lucky enough to have you all in my life. Without your support, this would never happen. Especially, thanks to the other two amazing coauthors, Hideto and Carol, for your suggestions and guidance along the way. Ke-Jou Carol Hsu is an engineer at Trend Micro. As a developer working in the Data Center Service group, Carol helps to write programs for deploying or managing internal-facing systems. She has both a bachelor's and a master's degree from the National Tsing Hua University. While studying and doing research, Carol focused on the area of high performance computing and virtualization technology. The experience made her more and more interested in system software, especially distributed systems and cloud environments. Many thanks to my family and friends! You covered most of the house chores and job duties. Sometimes, you just bore my bad temper caused by the pressure while writing. Thanks to all of you! I am good to come back to my original life now! For the other two authors, Hideto and Chloe, you are deþ nitely the ones I truly appreciate. You guys know both the hard times and the happy hours during the writing of this book. Without your guide and support, it would have been impossible for me to Þ nish this book at such a rapid pace and still come out with this careful, creative work. Looking forward to another cooperation in a short time.
About the Reviewer Matt Ma is a multitalented and highly motivated full stack software engineer. He is a JavaScript ninja with over 8 years of experience. He has won over two dozen of CSS awards, Webby awards, and other web development awards. He is proud of being a Node.js contributor, a certiþ ed MongoDB developer, and an earlier adopter of Docker and Kubernetes. Matt Ma have over 6 years of Linux experience. He is a long-time user of Ubuntu and CentOS. He uses an open source lightweight operating system, such as CoreOS, and systemd init daemon along with its eco-system tools. He advocates the microservices architecture. When he is not working, he likes to go to the beach or a rock concert, hike, or spend time with his wife and two kids. He likes meeting new people at conferences and meetups. You can Þ nd him on Twitter (@bigmabig) or GitHub (https://github.com/mattma). Drop him a line or just say hi to him.
www.packtpub.com ebooks, discount offers, and more Did you know that Packt offers ebook versions of every book published, with PDF and epub Þ les available? You can upgrade to the ebook version at www.packtpub.com and as a print book customer, you are entitled to a discount on the ebook copy. Get in touch with us at customercare@packtpub.com for more details. At www.packtpub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and ebooks. TM https://www2.packtpub.com/books/subscription/packtlib Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books. Why Subscribe? Fully searchable across every book published by Packt Copy and paste, print, and bookmark content On demand and accessible via a web browser
Table of Contents Preface iii Chapter 1: Building Your Own Kubernetes 1 Introduction 1 Exploring architecture 1 Preparing your environment 8 Building datastore 13 Creating an overlay network 22 ConÞ guring master 33 ConÞ guring nodes 41 Run your Þ rst container in Kubernetes 49 Chapter 2: Walking through Kubernetes Concepts 57 Introduction 57 An overview of Kubernetes control 58 Working with pods 61 Working with a replication controller 67 Working with services 76 Working with volumes 87 Working with secrets 104 Working with names 109 Working with namespaces 114 Working with labels and selectors 121 Chapter 3: Playing with Containers 129 Introduction 129 Scaling your containers 129 Updating live containers 133 Forwarding container ports 140 Ensuring ß exible usage of your containers 154 Working with conþ guration Þ les 164 i
Table of Contents Chapter 4: Building a High Availability Cluster 173 Introduction 173 Clustering etcd 173 Building multiple masters 181 Chapter 5: Building a Continuous Delivery Pipeline 193 Introduction 193 Moving monolithic to microservices 193 Integrating with Jenkins 207 Working with the private Docker registry 216 Setting up the Continuous Delivery pipeline 222 Chapter 6: Building Kubernetes on AWS 235 Introduction 235 Building the Kubernetes infrastructure in AWS 236 Managing applications using AWS OpsWorks 245 Auto-deploying Kubernetes through Chef recipes 253 Using AWS CloudFormation for fast provisioning 269 Chapter 7: Advanced Cluster Administration 291 Introduction 291 Advanced settings in kubeconþ g 292 Setting resource in nodes 298 Playing with WebUI 304 Working with a RESTful API 308 Authentication and authorization 313 Chapter 8: Logging and Monitoring 321 Introduction 321 Collecting application logs 321 Working with Kubernetes logs 332 Working with etcd log 336 Monitoring master and node 340 Index 353 ii