Quick Start Kubernetes Nigel Poulton Thisbookisforsaleathttp://leanpub.com/quickstartkubernetes Thisversionwaspublishedon2022-01-19 ThisisaLeanpubbook.Leanpubempowersauthorsandpublisherswiththe LeanPublishingprocess.LeanPublishingistheactofpublishinganin-progress ebookusinglightweighttoolsandmanyiterationstogetreaderfeedback,pivot untilyouhavetherightbookandbuildtractiononceyoudo. ©2021-2022NigelPoulton Contents Abouttheauthor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 NigelPoulton(@nigelpoulton) . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Aboutthebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Whoisthebookfor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Whatdoesthebookcover . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 WillthebookmakeyouaKubernetesexpert . . . . . . . . . . . . . . . . . . 2 Willyouknowwhatyou’retalkingaboutwhenyoufinishthebook . . . 3 Editions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Terminologyandresponsiblelanguage . . . . . . . . . . . . . . . . . . . . . . 3 Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Thesampleapp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1:WhatisKubernetes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Whataremicroservices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Whatiscloud-native. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Whatisanorchestrator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 OtherusefulKubernetesstufftoknow . . . . . . . . . . . . . . . . . . . . . . 12 Chaptersummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2:WhyweneedKubernetes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 WhytechcompaniesneedKubernetes . . . . . . . . . . . . . . . . . . . . . . 15 WhytheusercommunityneedsKubernetes . . . . . . . . . . . . . . . . . . . 17 ChapterSummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 3:WhatdoesKuberneteslooklike . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Controlplanenodesandworkernodes . . . . . . . . . . . . . . . . . . . . . . 20 Controlplanenodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Workernodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 CONTENTS HostedKubernetes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 ManagingKuberneteswiththekubectlcommandlinetool . . . . . . . . . 26 Chaptersummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 4:GettingKubernetes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 KubernetesonyourlaptopwithDockerDesktop . . . . . . . . . . . . . . . 27 KubernetesinthecloudwithLinodeKubernetesEngine(LKE) . . . . . . 29 Chaptersummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 5:Creatingacontainerizedapp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Pre-requisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Gettheapplicationcode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Buildthecontainerimage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Hosttheimageonaregistry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Chaptersummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 6:RunninganapponKubernetes . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 VerifyyourKubernetescluster . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 DeploytheapptoKubernetes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Connecttotheapp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Clean-up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Chaptersummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 7:Addingself-healing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 IntrotoKubernetesDeployments . . . . . . . . . . . . . . . . . . . . . . . . . 59 Self-healfromaPodfailure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Self-healfromaworkernodefailure . . . . . . . . . . . . . . . . . . . . . . . 64 Chaptersummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 8:Scalingtheapp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Pre-requisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Scaleanapplicationup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Scaleanapplicationdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Anotherwordonlabels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Importantclean-up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Chaptersummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 9:Performingarollingupdate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Pre-requisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Updatetheapp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 CONTENTS Clean-up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Chaptersummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 10:Whatnext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Otherbooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Videocourses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Let’sconnect. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Showsomelove. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 AppendixA:Labcode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Chapter5:Creatingacontainerizedapp . . . . . . . . . . . . . . . . . . . . . 85 Chapter6:RunninganapponKubernetes . . . . . . . . . . . . . . . . . . . . 86 Chapter7:Addingself-healing . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Chapter8:Scalinganapp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Chapter9:Performingarollingupdate . . . . . . . . . . . . . . . . . . . . . . 91 Terminology. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 About the author Nigel Poulton (@nigelpoulton) Hi, I’m Nigel. I live in the UK and I’m a techoholic. In fact, working with technologieslikethecloudandcontainersislivingthedreamforme! My early career was massively influenced by a book called Mastering Windows Server2000byMarkMinasi.Eversincereadingit,Iwantedtowritemyownbooks so I could influence people’s lives and careers the way Mark’s book influenced mine.Sincethen,I’vebeenfortunateenoughtoauthorseveralbest-sellingbooks, including Data Storage Networking, Docker Deep Dive, and The Kubernetes Book. I feel so privileged to have reached so many people and genuinely appreciate the feedbackIreceive. I’m also the author of best-selling video training courses on Docker and Kuber- netes.Myvideosarealwaysentertaining,andoccasionallylaugh-out-loudfunny (notmywords). Seeafulllistofmyvideosathttps://nigelpoulton.com/video-courses At my website, nigelpoulton.com, you’ll also find all my books, my blog, weekly newsletter,andotherthingsthatI’mdoing. WhenI’mnotworkingwithcontainersandKubernetes,I’mdreamingaboutthem. WhenI’mnotdreamingaboutthem,I’mspendingtimewithmyfamily.Ialsolike Americanmusclecars,playinggolf,readingsci-fi,andIsupportSunderlandAFC (thegreatestfootball/soccerteamintheworld). I’mhappytoconnectandI’malwayslookingforwaystoimprovemybooksand videos.Thesearesomeoftheeasiestwaystogetconnected. • twitter.com/nigelpoulton • linkedin.com/in/nigelpoulton/ • nigelpoulton.com • [email protected] About the book As the title states, this is a quick start guide to Kubernetes. It doesn’t cover everything, just the important bits. And it covers them as clearly as possible in anengagingway.It’salsoagreatmixoftheoryandoptionalhands-on. ThegoalistodemystifyKubernetesandgetyousomehands-onexperience. Who is the book for Iwrotethebookforanyoneandeveryonethatneedstogetup-to-speedwiththe fundamentalsofKubernetesandlikestolearnbydoing. Theentirebook,theoryandlabs,havebeenhand-craftedovermanyyearssothat technicalandnon-technicalreaderswillbenefitequally.So,whetheryouworkin marketing,management,architecture,oroneofthemanyhands-onroles,you’re goingtolovethebook. What does the book cover You’lllearnwhywehaveKubernetes,whatitis,andwhereit’sgoing. Onthe theory front,you’lllearn about microservices,orchestration,whyKuber- netesistheOSofthecloud,andthearchitectureofaKubernetescluster.Onthe hands-on front, you’ll have the opportunity to build a cluster, containerize an app,deployit,breakit,seeKubernetesfixit,scaleit,andperformanapplication update. Andasthisisaquickstartguide,you’llbeup-to-speedinnotimeatall. Will the book make you a Kubernetes expert No,butitwillstartyouonyourjourneytobecominganexpert. Aboutthebook 3 Will you know what you’re talking about when you finish the book Yes, you’ll be able to hold your own in conversations about Kubernetes at the coffeemachineandwithyourpeers. Editions ThefollowingEnglishlanguageeditionsareavailableinasmanyAmazonterrito- riesaspossible: • Hardback • Paperback • Kindle EbookcopiesarealsoavailablefromLeanpub.com ThefollowingtranslationsareavailableonAmazonandLeanpub(orwillbevery soon). • French, German, Hindi, Italian, Portuguese, Russian, Simplified Chinese, Spanish. Terminology and responsible language ThroughoutthebookIcapitalizeKubernetesAPIobjects.Wow,wehaven’teven startedandI’mthrowingjargonaround:-D Put more simply, Kubernetes features such as Pods and Services are spelled with a capital letter. This helps you know when I’m talking about a Kubernetes “Pod” andnota“pod”ofwhales. ThebookalsofollowsguidelinesfromtheInclusiveNamingInitiative(inclusive- naming.org) which promotes responsible language. As an example, the Kuber- netes project has replaced the potentially harmful term “master” with “control plane node”. This book does the same and attempts to follow all guidance from theinclusivenaminginitiative. Aboutthebook 4 Feedback Ifyoulikethebookandit’shelpedyourcareer,sharethelovebyrecommending ittoafriendandleavingareviewonAmazon. ModernbooksliveanddiebyAmazonreviewsandstars,soI’dbegratefulifyou couldwriteone.YoucansometimesleaveanAmazonreviewevenifyougotthe bookfromsomewhereelse. [email protected]. The sample app Asthisisahands-onbook,ithasasampleapp. It’sasimpleNode.jsappavailableonGitHubat: https://github.com/nigelpoulton/qsk-book/ Don’t stress about the app and GitHub if you’re not a developer. The focus of thebookisKubernetes,nottheapp.Plus,weexplaineverythinginlaymanterms whentalkingabouttheapp,andyoudon’thavetoknowhowtouseGitHub. If you’re interested, the code for the app is in the App folder and comprises the followingfiles. • app.js:Thisisthemainapplicationfile.It’saNode.jswebapp. • bootstrap.css:Thisisadesigntemplatefortheapplication’swebpage. • package.json:Thisfilelistsanydependenciestheapphas. • views:Thisisafolderforthecontentsoftheapplication’swebpage. • Dockerfile:ThisfiletellsDockerhowtobuildtheappintoacontainer. If want to download the app now, run the following command. You’ll need git installedonyourmachine.It’sOKifyoudon’thavegit,weshowyouhowtoget itanddownloadtheapplaterinthebook. $ git clone https://github.com/nigelpoulton/qsk-book.git $ cd qsk-book Finally,theappischeckedatleastonceperyearforpackageupdatesandknown vulnerabilities.