Kubernetes for the Spring Developer - Meaghan Kjelland

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
my name is Maegan I work at Google although I work at Google I don't work on the kubernetes team itself but I work on the team with pivotal and VMware that works on Cloud Foundry container runtime so I started on this team about a year ago when the team started and since then I've learned a lot about kubernetes and in my previous job I worked with a spring web app so I thought I would talk a little bit about how you as a spring developer could use kubernetes and I'm mostly going to focus on how kubernetes works and then I'll do a demo showing spring boot an application deployed on kubernetes cool so this is an overview and as a warning I made a lot of these diagrams myself so they look like this the people are people if you can't guess so this is kind of an example cluster view we have a controller node that controls the work that's going on on worker nodes we have three worker nodes I have three in the example I'm going to show later also and then you deploy the operator of the cluster would use cube control which is the CLI for kubernetes to tell the controller node they want to deploy a workload like this spring boot application and then the controller node will handle scheduling that on the workers then if you have an application user they can talk to your workload the spring boot app directly on the worker node so kubernetes uses declarative descriptions of state which means you just say this is what I want and they'll handle dealing with the scheduling and provisioning of resources kubernetes just make sure the actual state matches the desired state and that provides a lot of things so that means you can scale really easily because all you have to do is say I actually want four of those instead of three you can do updates because you just have to say use this image instead of this one and you can get monitoring and self-healing of processes and this can run on the cloud or in the cloud or on-premise how does it all work this is kind of a high level overview although it's really not that complicated first we have an API server which handles all of the communication within the cluster so everything talks through the API server but the API server relies on at CD for storage and then we have a controller manager process that just runs a loop making sure that we have what we want to have so that's if you so they whatever the operator told us that it wanted the controller manager make sure that that's there and if it's not we use the scheduler to schedule anything that's missing and those processes all make up what we call the control plane and they usually run on the controller node that we saw in the previous slide and then we have all of our worker nodes which run a cubelet process and the cubelet monitors the workloads running on it to make sure that they're healthy and reports that back to the api server and then we also have a cube proxy which handles requests coming in from the internet and make sure that they find the right place in the cluster when I say workload usually that's called a pod in kubernetes kubernetes has a bunch of different objects and pod is kind of the most basic one it's a group of containers that are tightly coupled and they share an IP address each pod will get its own IP address and they share a port space and they also share volumes and life cycles they live and die together and usually we're talking about docker containers when we say containers one pod is typically one instance of an application and they're not considered durable so if something starts going wrong in a pod it'll just get deleted and an identical one will be created in its place so before we go on I just want to show you how you could deploy a really simple pot on a kubernetes cluster so I have a cluster here deployed that I used gke to deploy so it's actually managed and it's running on GC P and if I want to create a really simple pod I can just say Q control create and then this engine X spec that I have and the spec just says like this is a pod that's the kind of resource I want I want it to be named Engine X it's in an engine X namespace and namespaces are a way of kind of dividing workloads and allowing some people to access certain ones and not others and then I'm also adding a label app engine X you can pretty much label any object in kubernetes and some other objects will use the labels to to find pods for example and then I just have one container which is Engine X I use this engine X image and I'm opening up port 80 on that container so let's see what we got so I have a pod I can get more information about it so you can see this is the IP address that was allocated to this pod so if I just curl that IP address I get the sample like the nginx welcome page so that's a really simple example another thing you can do is you can select based on the label so since this has app equals nginx it'll show up if I ask for pods with that label cool so some other things you probably want from your cluster if you have three of your pods running and one of them dies unexpectedly you'd want it to come back without you having to do anything if you as this blue operator guy decides you want four pods instead of three the controller node should handle that for you as well you shouldn't have to worry about actually creating the pod and this all is facilitated using another object called deployments so instead of just two pulling one pod you can ask for a deployment and then you can say I want one replicas or three and it'll make sure that you always have that many this facilitates scaling because all you have to do is update the number of replicas if you want more or less it also lets you change the image really easily which we'll see in the demo later and you can do rolling updates and rollback of versions of deployments as well and the way it works is using a replica set which is another object but it's not that important but you might have noticed in this picture it's kind of weird because the application user is directly accessing one of the pods and earlier I told you they're not durable so that pod might die and be rescheduled somewhere else or I might have a different name so how does the application user know where to get it and also there are three so it could access any of those three they should be identical what we really want is something that looks like this where we have our application user talking to a load balancer and then the load bouncer will decide which of the three pods to use and for that we use services services allow access to a dynamic set of pods so no matter what the pods are named or where they're running you can access them the same way it uses labels to find the pods so when we said app equals engine X we can tell the service just send it to any pod that's named App Engine X of its labeled App Engine X and at load balances traffic across the nodes there are four types of services the first one cluster IP that's used for if you want to access your workload but only within the cluster itself so other pods can access it node port I actually think this one's really cool so if you use node port then it will allocate a high numbered port on every single worker node so I think it's like above 30,000 or something it'll allocate that on every worker node and even if a pod is not running on that worker node that is for the service it'll reroute you to one that is so it just handles it for you it's pretty cool and that's actually how the next one works load balancer this one actually provisions you as an is specific load balancer so if you're running on TCP you'll get one of the l7 load balancers provisioned for you and then it'll send traffic to the worker nodes on their node port so that's actually really powerful because it also creates firewall rules for you so you don't really have to handle any of the networking for your applications and then external name is not one that's as useful but you can use that to access services that aren't running in your cluster as if they are running in your cluster and volumes are another thing that's cool I think for spring developers you're probably not using a lot of persistence in your applications themselves but if you want to run things that do you alongside your application you can use volumes to get persistence with kubernetes host path lets you talk to it mounts the pod on to a directory on the worker node so you can access things that if you know they're on the worker nodes you can access files for example the empty dirt one is for scratch space so it gives you an empty directory and then it deletes it when that pot is gone and the coolest one I think is persistent disks because if you ask for a persistent disk you can actually get a provision the persistent disk and then it will always be attached to the node that your pod is running on so even if your pod dies and gets rescheduled somewhere else the disk will be unattached and reattached to the correct worker node and then secrets are used for sensitive information to get access to them in your pot cool so most of this is just going to be a demo in the demo I'm going to deploy a spring boot application just one pod then I will put a load balancer in front of it so we can access the application then I will make the application depend on Postgres for and the Postgres we will also deploy in kubernetes and we'll use a persistent volume so the data doesn't get lost when the pods get lost and then we'll scale it so we have three copies cool so first I'm going to create one of the pods and let's look at that file so this is just saying I want to name the deployment this it's in the default namespace and then I only want one replica for now these environment variables we're going to ignore for now but I'll show you later and then I'm using this image which is I built this image out of one of the string sample applications and I'm also going to show you how you can build an image for yourself when we modify it so now we can see we have one pod running and I'm going to expose that which creates a service and I'm going to use type equals load balancer because the default is cluster IP and I'll put it on port 8080 and the load balancer takes like a couple minutes to provision everything in GCPD so you can see here it says pending that's because the external IP is not created yet so it's going to create me an external IP a load balancer and all the firewall rules to allow my notes to be accessed on this node port oh yeah up over 30,000 it's 31 to 6-4 so it does take a couple minutes but yeah we do have this cluster IP though so Oh what I was going to say is we had to open up port 8080 because that's the port that we opened on the container so there we go we have an IP address we did have pods yes okay so we this is just like a app that stores messages and it stores them in memory right now so if I say hello as a message that will get saved but then if I delete this pod we can see what happens so now kubernetes already rescheduled oh that's the same pod it didn't delete it Oh cuz I spelled that wrong that's smart okay well it does really recreate them really fast so you can see this one's running already and this one's still terminating so it can start them up faster than I can stop them I guess but then once we refresh this we can see we don't have any messages because we didn't process the data so now we can deploy Postgres and Postgres will use a persistent disk for persisting the data and we can see how that works so I'm gonna create this Postgres instance and what this is doing this is how you get the persistent disk it's actually doing a claim which means it's going to create one instead of using one that existed and then up here in my deployments back I say this deployment uses this persistent volume claim and then when I create my container I just mount the volume onto or I'm out the pod onto this directory in the persistent disk when the if the pods die it will reattach the disk somewhere else so it still knows that that claim but there's already a VM or sorry there's already a disk for that claim so the claim won't get recreated I believe I would use a different claim yeah we can check right and then for Postgres we have to tell it this PG data environment variable so it knows to mount on to this directory instead of the root directory of that disk and the other thing I did is I already created a config map earlier that had the user and password in it you can also use secrets for the password but we are using a config map here and so it can grab out of that config map these keys a config map is just a way of it's a key value store and kubernetes for configuration information cool so we created that deployment I'm also going to create a service so I can access it but the service is just going to be a cluster IP because we don't need to access it outside of the cluster so we have this cluster IP and then I'm actually going to update the config map to include that also because the the application that I'm going to deploy is going to depend on that as well and then we can look at how to build a docker image for our spring boot app so I have all of the code here cool I already modified it to use Postgres because that wasn't the point of the talk and I figured you probably already know how to do that but so we're just gonna build the code that I modified and the only difference is that it uses Postgres instead of an in-memory store so first we'll build it so we have a jar file I guess it takes a second once we've built it we can build a docker image for it and then push the docker image to docker hub and then we can reference it in our spec for the deployment or actually we can just update the image in the current deployment so I have a docker file here and what this does is it it starts out from this like default open JDK 8 image which contains Java and then we're copying the target file that we just built from the current directory into that image and then it'll be at AB slash App jar and we opened up port 8080 which we saw earlier and then when you start it you just run Java jar and then that jar file so we can do docker build let's see what it's called spring sample webui I think and then you have to pass in the current directory Oh Oh / yes thank you you already know how to do this and then I will push it cool and then I can update the image on my deployment directly and it'll update it for me it'll actually create new pods and delete the old ones what was the name of my deployment and then you have to pass you have to say this is the container I want to change which one's named the same thing and then we can pass the image we just created cool then we can see if we can catch it in time yeah we can see this one is being terminated and this one is being created and now it's running we should be able to use the exact same service that we created the for which it is the same URL here cool so now we have the exact same application and if we create a message now if we go back we'll see the message and I can actually delete both of these pods and it should persist ah I keep doing that cook so this Postgres one actually will take a second because it's probably being scheduled on a different node so it's going to have to unattach the disk and reattach it to a new node you can set a flag in your in your deployment to say you want them to be on the same node usually but you don't have to and I did not so it'll take a second while that's happening we can also scale up our application so I said we were gonna have three pods by the end of this so we can do that using I spelled it right that time scale and you just pass in how many replicas you want oh I'm typing everything wrong today cool then let's look at our pods and now we have three one of them still creating now we have three and let's see if our data persisted yes we still have our message cool that was all I had thank you so much for coming [Applause]
Info
Channel: SpringDeveloper
Views: 22,907
Rating: 4.9697733 out of 5
Keywords: Web Development (Interest), spring, pivotal, Web Application (Industry) Web Application Framework (Software Genre), Java (Programming Language), Spring Framework, Software Developer (Project Role), Java (Software), Weblogic, IBM WebSphere Application Server (Software), IBM WebSphere (Software), WildFly (Software), JBoss (Venture Funded Company), cloud foundry, spring boot, kubernetes
Id: OsWXtVbTnv0
Channel Id: undefined
Length: 20min 42sec (1242 seconds)
Published: Thu Dec 14 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.