Managing Kubernetes from Python using Kube
CC Attribution - NonCommercial - ShareAlike 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this
Charles, David
David Charles - Managing Kubernetes from Python using Kube Kubernetes is the Google Borg inspired control plane for Docker containers. It has a great API but needs a load of HTTP client code and JSON processing to use it from Python. This talk introduces Kube, a Python wrapper around the Kubernetes API that enables you to manage your Kubernetes cluster in a pythonic way while avoiding any Kubernetes API peculiarities. Programmers and operations folk who are interested in interacting with the Kubernetes API using Python. - Docker has had a transformative influence on the way we deploy software and Kubernetes, the Google Borg inspired control plane for Docker-container- hosting-clusters, is gaining similar momentum. Being able to easily interact with this technology from Python will become an increasingly important capability in many organisations. I'll discuss what the motivations behind writing Kube. We'll dive into Kube using the Python interactive interpreter, getting connected to the API, and simple viewing and label update operations. Finally I'll discuss more advanced resource management activities like Kube's 'watch' API capability. ## Objectives Attendees will learn about the key concepts in getting resource information out of their Kubernetes cluster using Kube. ## Outline 1. Setting the scene (3 minutes) 1. Other Python kubernetes wrappers (2 minutes) 1. Kubernetes concepts quick recap (5 minutes) 1. Dive into Kube in the Python interactive interpreter (10 minutes) * Outline prerequisites * The entry point - a Cluster instance * Views and Items - two important Kube concepts * Item meta data: labels and versions 1. More Kube features (5 minutes) * Creating and deleting resources * Using Kube's Watch API support * The cluster proxy attribute for when you need to get at the actual API. 1. Q&A
 Okay, hello, how are you? Hi, my name's Dave. I work at Koby. Koby IO. Today I'm going to be talking to you a bit about managing humans' clusters from Python using the open source project we wrote with Q. First I want to share with you my journey here briefly. And in some ways mitigate the, well inadequate and poorly prepared presentation that I'm going to give you today. I submitted my talk to URIFY about four weeks ago. And I was about here when I saw the email saying my talk had been accepted. I was like, hey URIFY just pulled my bluff. And I'm prepared to talk. I was like, oh it's okay. I'll work on it while I'm here. I went to the drama company. I'm here. I'm here. I'm here. I'm here. I'm here. I'm here. I'm here. I'm here. I'm here. I'm here. I'm here. I'm here. It was interesting to learn that this was an ambition for Portland. Nice. It was also quite distressing to see what the speed was. So that. So, okay Dave, don't be too objective. Don't be too upset. Why don't you go on a little drive? I'll go from Portland to New York. So I hired myself one of these and I started here. By the way, if any of you have seen the 1985 film The Boonies, early Steven Spielberg film, the partner who's been there, it's a really cool place. And I saw a lot of that on the way. I saw really cool things like that. And I saw some not so cool things like that. I do want to just have a picture of the cell site for how it feels. So I only saw you, so basically, I managed to avoid her. So after that, any miles, I got to my destination and I was really, really happy. And then I remembered that I should be doing this. So in my hotel room, I arrived in Gorbao yesterday, in my hotel room, I was doing my company, gets inspiration and start to develop a good reputation for the film. So I went out and I looked at that which was really cool. Did anyone go to the town square last month to see that? Really cool music, Catholic music, really, really cool. And I was a bit techish, so I had one of those. And I had one of those. And I had one of those as well, and I really, really, really, really, really full up. I thought, no, I'm going to get a picture of you. The fact is that you're not going to get a picture of me. And it had my name coinciding now and I didn't hear it. So we ride monitors at a time and let me go. I started to work on a project called the system that monitored monitor systems, which was a bit of a silly idea and it worked out very well. So we had a slight pivot and I... We took the aging technology from our monitor, the monitors, and turned that into a general purpose monitoring solution. We built up the product over a decade and it was, to be honest, it was never that great because there's a general problem with monitoring and it's all about like, the employer's a software as a service. Now we chose Google Compute Engine and GKE. And GKE is the hosted Kubernetes implementation that Google provide. And we were kind of quite keen on dog fooding and we wanted to use our own solution to monitor our stuff and also we needed to provision monitoring instances for customers who were signing up to the SAS so that they could actually, you know, click a button and a monitoring instance be deployed on Kubernetes. So we needed to interact with the Kubernetes API. We're all Python people so we had to do that with Python. So that's why we wrote Q. Now it's important to say that there are other Python API wrappers out there. And I'd strongly encourage you to go off and look at them so you can see how rubbish they are and how good ours is. No, seriously. Go off and have a look. They might be better fit for you. And also if you do end up choosing one of these instead of Parkube, it'd be really great if you could come back and tell us what you thought was better because that would be interesting to know and we could try and fix anything we've got going on in ours. But we wrote ours, we wrote Q in the way we wrote Q because we wanted to abstract away from what's a somewhat moving target as far as the Kubernetes API goes. It's also got some idiosyncrasies that you don't necessarily need to be exposed to when you're interacting with Kubernetes Firebase API. So we wanted to kind of like create an opinionated version of the API that made things a little bit more palatable for the user of that API. We also wanted a clean watch interface. Watch interface allows you to get notifications of changes to resources within Kubernetes and some of the other offerings that are about at the time, some have come and gone since we started. But the ones that were about didn't do that very well. We wanted something with Python and we didn't want to use code generation swagger and that's a theme in some of these alternatives. But you guys take a look and take your choice. So I hate it when speakers say, oh, hands up. Do you know this? Do you know that? But just a very quick show of hands is who has kind of been exposed to Kubernetes? Okay. So third, okay. I'll try and railroad through this because time is a premium. So Kubernetes is about orchestrating Docker containers, essentially. But not just Docker containers. Docker came out of Truity file systems and if any of you have been exposed to Solaris and Solaris zones, similar kind of concept. What it gives you is basically an immutable deployment component. It's easy to author. There's a runtime that runs on many platforms and it allows you to develop an immutable deployment component that underpins DevOps practices and continuous deployment. Across multiple nodes, it's hard to manage Docker containers in the raw. So especially for scale and resilience. So that's why control planes like Kubernetes and Docker swarm came about. And I mean, Google already doing this. Google had a system called Borg which uses LXC containers and it manages those across their enterprise. It allowed Google to scale developer productivity and the number of services they're offering to their customers internally and externally without the corresponding increase in operational overhead. So there was obviously, you know, it was a useful technology. Kubernetes has had an amazing amount of momentum behind it. It's interesting how many, in quotes, competitors have actually got behind Kubernetes where initially they thought to position themselves as direct competitors to Kubernetes. They kind of seem to acknowledge that they have a particular sweet spot. Maybe it's in cluster management or managing containers on very, very large scales. And they seem to have all sought to accommodate Kubernetes in their offering and in their space. So, you know, the only sort of like, sort of offering that hasn't really done that, I suppose is Docker swarm because that is, you know, trying to do exactly the same thing. So how does it work? There you go. Happy with that? Excellent. Yeah, it's not very helpful, that kind of diagram, you know, and it just takes forever. I haven't got time. So I just want to go through some key concepts. So in Kubernetes we have the idea of a cluster which is a single homogenous cluster of nodes, compute resource. Watch out for a thing called Ubinities, which is kind of an attempt to federate multiple Kubernetes clusters so you can have basically multiple clusters with different kinds of shapes of resource running inside it. A node is some resource where pods are scheduled. And pods are the smallest unit of scheduling that runs the actual container. So Docker, but not exclusively Docker, there's also support coming for rocket containers. And the Docker containers run inside the pods and those are the things that the Kubernetes system schedules. There's the concept of replica sets. And what they do is they, it's a specification that defines the pods and how many replicas of those pods there need to be for scale and resilience, amongst other things. There's also services. Services target pods and expose their capabilities at the edge of the Kubernetes cluster. So you can think about the actual Docker containers, I suppose, you know, as nano services and then the actual pods or microservices and then the service definitions provide actual services for a consumer. Labels are an interesting thing and we'll be looking at those very, very quickly. And they're key value pairs that are associated with resources within Kubernetes, but they will be a scheduler to organize the objects within it. And there's lots of other stuff that we could talk about, but that's probably enough to get us going through the next part of the talk, something, something 30 minutes. So some other, some key concepts for Q. We need to get the terminology straight really. Right at the beginning is principally the API, the Kubernetes API defines, defines kinds and defines resources. So a kind is the name of an object schema, essentially it's a resource type. And a resource is a representation of a system entity that's center retrieved from the API by Jason over HTTP. And resort, there's two types of resources, collections and elements. So these are kind of Kubernetes terms I'm using, but they do map quite nicely into, into Qube. So for example, a pod is a pod resource, whereas nodes is a node list resource and that's a collection of nodes. So try and bear that in mind. Additionally, it's really important to understand the separation of specification and status in Kubernetes. When an API update is made, the specification of the resource you're updating is made. And that's available immediately. So that's almost like an atomic operation. But over time, Kubernetes will work to bring the status of the resource whose specification has changed up towards that specification. So the system will drive towards the most recent spec. And that makes the behavior of Kubernetes level-based, not edge-based, which is quite a nice feature. So, okay, so now the tricky bit. I'm going to open a terminal window. Bear with me for a moment. I'm going to try and mirror this display. Yay! That works. Okay, cool. Okay, so what I've got running here is, um, yes, sorry. Oh, I could. Yeah, I should have tried that before. Say when. Okay. So I've got a single node, Kubernetes cluster, running on by MacBuckair. By the way, if anyone's interested, it was really easy to do. If anyone's interested in knowing how to do that, just come and see me at the stand and we can have a chat about it. It's quite cool. Right. So, um, what I'm going to do, okay, so, Python, cube. Here we go. So what you do is just import cube, spelling it probably right. Remember that America thing where I was like really jet lagged and was up till three o'clock in the morning trying to write this stuff. So, be nice. So, import cube. So the key entry point in the cube API is a cluster. So we can say something like cluster equals cube.cluster and we create an instance of one of those things. So that gives us a cluster object. So there's, if you want, if you're, okay, so one thing I forgot to mention was, um, when you're interacting with the Kubernetes API, the preferred approach is to run a cube control proxy. And what that does is it proxies the cube control API from wherever it's running to local host on your machine. So what I've got here is quite simply cube control proxy. You can just go out and see it down the screen. So, if you're running your Python code using cube, you know, in a container, then what you normally do is have a sidecar container inside your pod. So one's running the proxy and one's running your Python code. Okay. So the other thing I can do here is I can specify a URL if the proxy is running on a non-standard endpoint or port. So we can just say local host, port number 8001 slash API, something like that. And then we get, obviously, cluster instance. Can use context managers as well. So, yeah. So you can say something like cube with cube.cluster as, let's say, k.k.notes. We'll talk about this in a sec. So the k.nodes is returned a node for you and that will become clearer in a minute. So there's a few ways to actually create your entry point as a cluster. So do you remember I was talking earlier about collections and elements? Well, they're represented here as views and items. So this kind of nodes thing here, because it's plural, you can see that's actually a node list. So that's a collection. And I can iterate over that to get actual view items out. So let's have a look at a few of these. So we've got cluster.nodes. The cluster object has a few of these things, many of these things. You can look at the documentation, it's all on read the docs. It's a working progress, but there's some essentials in there. And we can see things like the clusters, replicasets, and namespaces, and that kind of stuff. So, okay. So we want to get a resource item out of here. So I can say something like RS because I'm going to get a replica set from my cluster.replicasets and I can do a.fetch and I need to specify the name. Now, something I prepared earlier, I have... This is just a Qt control on the line. And I can get the replica sets and I know I've got one called service demo. So I'm going to say get me service......demo. But I have to specify the namespace. So I have to say namespace equals default. It's the namespace that that's running in. You see how bad my typing is? Shouldn't be allowed in the alive demos. And then we look at RS. We've got a replica set item now. So this is an actual element as opposed to a collection. And it's got some attributes associated with it. So I can say look at some metadata and I can see what the name is and, lo and behold, it's service demo. So I actually got to give them the right thing. And I can see what namespace that came from. And I can also see what labels are associated with that particular replica set. So... Sorry, again a bit close to the bottom of the screen. Okay. So what's important to remember about resources is their version. Kubernetes versions, all of the resources it returns across the API. And if you remember when I was talking about the separation of spec and status, when you get given a resource item back, it's versioned. So, you know, we can see here that RS.meta.version is version 1561. So one thing I forgot to mention was I think I did mention briefly is that these collections are actually iterated. So I can do cool things like a list of comprehension. So I can say RS for RS in cluster.replica sets. And you get... If there's anyone, you get a list of those things back. So I can build a list, say for example, saying node.meta.version. For node in cluster.nodes. So that's going to give me a list of all the versions for all the nodes in the cluster. So there's anyone, and at the moment it's that version. If you keep doing this, eventually you will see a different version coming out. So that means the state of the node resource has changed because something's changed about the node. It's used a slightly different amount of CPU and that's been reported and actually whatever. So when you're interacting through Qube, you need to make sure you've got the latest version of the object. Otherwise, you know, you could be looking at stuff that's wrong or out of date. Okay. So back to labels. So let's have a look at our replica set object that we had and it's got some labels associated with it. It's got one. It's actually a dictionary. So I can do stuff like run the run attribute and get the value service demo. It is however immutable, so you can't mess up that way. And that was kind of a design decision of ours when we were writing Qube that we wanted every operation to be done by a set call. So you can update the label using a set command. So you could say something like, let's add a new one, foo, and we'll set it to label.set foo and we'll give it a value of a bar. That's not predictable at all, is it? Okay. So we get one back. Okay. Let's have a look at RS. See if anyone knows what's going on here. RS.meta. There it was. And it's not there. That's really annoying. So that's because I'm actually looking at the old version of RS because that's the one that got returned from an earlier call, that version. So what I can do is this. I'm going to be fancy and use a list comprehension because I know what I'm expecting. RS for RS in cluster.replicasets. And I'll get the first value out of that and I should get the second value out of that. So let's have a look at RS.version. Lo and behold, it's a slightly different version. Yay. Okay. So now let's have a look at RS.meta.labels. And we can see that our foo attribute is on there now. So that's really nice. And so updating a label is kind of the same as creating a label. So I can do that. It's returned by a new replica set, which I didn't assign to a variable. So I'll do that. I'll look at RS.version. We've got a new version. Let's have a look at the labels and we've got Baz set on there. So cool. So to go on about labels a lot, one, it was kind of easy to do in this. And the other thing is they're a really good way of managing your Kubernetes cluster. If you want to manage the way your resources, you know, pods, services, etc., are managed, then, you know, setting and resetting labels is a good way of doing that. Okay. So we can also delete labels. So we can say RS.meta.labels.delete and we can say we want to delete foo. I'm not going to be caught out a second time. I'm actually going to assign that to RS and then look at RS.meta.labels and foo is gone. Okay. So that's kind of the end of the, that bit, the live code demo bit. So I'm going to go back to Arrangement. Okay. Thank you very much. Okay. So briefly talk about some of the features that I haven't got time to demo. And in the latest version of Kube, we've got creating and deleting resources, which actually makes it quite useful. So you can actually go in, create pods, delete pods, replic sets, services, namespaces, all that kind of stuff. It's just a simple create call. You pass in a JSON specification and it does sense a call to Kubernetes. We've also got a watch API implementation, which, you know, say, come by the booth and let us show you because it's really cool. And my colleague who wrote that bit actually wrote a blog about how it was tricky. And he's done all the great service because he's insulated it from all the horrors of how to do watch support using Python and over H2P. And yeah, it's neat. There's also, if you remember the fetch command that I used to get resource items from collections, there's actually a filter capability. So you can filter the return results on label values, which is also really nitty and really cool. And I didn't get a chance to show you. Finally, the cluster instance, which is your entry point, has a proxy to the Kubernetes API. So if all else fails and you want to get to the actual API in Python while you're using Qube, you just use cluster.proxy to do that. Okay. So time for questions. It's on Bitbucket. And I'm really interested to hear if you all think Bitbucket sucks and you want, you should be on Git or if it doesn't really matter to people. I've had a sharp intake of breath from some audiences when I've asked them about that. And anyway, that's where it is. Happy to move it. Check us out on Qobi.io. I'm Qobi CTO. Follow me on Twitter because I'm funny. And yeah, so I'll take questions if we've got time. Oh, no. Thank you. Hello. So I have a kind of question. So I'm just going to condescend into what, which days are you here? Are you here all of today and tomorrow and throughout? Okay. So the question was, are we here or we can, can you come and see us? And yes, we're in the vendor area. We've got like one of those little booze on the green and yellow carpets. You'll see that fancy graphic should be up on a monitor. And we can show you, you know, where we've got, still in beta, but we can show you where we've got and you can have a chat with us. And my colleague, by the way, is one of the developers on Pi.test. So some of you may have heard of him anyway. So, yes. What was the inversion of the event from Kubernetes? Okay. So the question was, what version of Kubernetes are we working with here? It wasn't. What does this version of the event, you say it changes when something happens? Okay. So it's just an opaque number. And it just represents a version of the resource compared to the last time the resource changed. Now, depending on the resource type, the kind, that could be anything. So for example, for a node, it could be because some of the nodes attributes have changed. It could be because a label has been updated on a replica set. So that's when the version number is incremented. Not just when you make the call, but when Kubernetes itself changes the resource. No. So what I should have said was, don't rely on the version numbers. Just always get the latest version of the object. So I showed you the version numbers. I was kind of advised, don't show you people the version numbers. But I just thought it was an interesting thing. So I did that. Yeah. Any other questions? I think you had a call. Okay. I think I can give a call. So I'm using type for testing of tests in Kubernetes. Okay. I was wondering how do you guys represent namespaces? Okay. Come to the booth, ask my colleague. And because, yeah, and he'll give you a really good answer. I'll give you an average answer. He'll give you a really good answer. So thanks, guys.
 Okay, hello, how are you?  Hi, my name's Dave.  I work at Koby.  Koby IO.  Today I'm going to be talking to you a bit about  managing humans' clusters from Python using  the open source project we wrote with Q.  First I want to share with you my journey here briefly.  And in some ways mitigate the, well inadequate  and poorly prepared presentation that I'm going to  give you today.  I submitted my talk to URIFY about four weeks ago.  And I was about here when I saw the email saying my talk  had been accepted.  I was like, hey URIFY just pulled my bluff.  And I'm prepared to talk.  I was like, oh it's okay.  I'll work on it while I'm here.  I went to the drama company.  I'm here.  I'm here.  I'm here.  I'm here.  I'm here.  I'm here.  I'm here.  I'm here.  I'm here.  I'm here.  I'm here.  I'm here.  It was interesting to learn that this was an ambition for  Portland.  Nice.  It was also quite distressing to see what the speed was.  So that.  So, okay Dave, don't be too objective.  Don't be too upset.  Why don't you go on a little drive?  I'll go from Portland to New York.  So I hired myself one of these and I started here.  By the way, if any of you have seen the 1985 film The Boonies, early Steven Spielberg film,  the partner who's been there, it's a really cool place.  And I saw a lot of that on the way.  I saw really cool things like that.  And I saw some not so cool things like that.  I do want to just have a picture of the cell site for how it feels.  So I only saw you, so basically, I managed to avoid her.  So after that, any miles, I got to my destination and I was really, really happy.  And then I remembered that I should be doing this.  So in my hotel room, I arrived in Gorbao yesterday,  in my hotel room, I was doing my company, gets inspiration and start to develop a good reputation for the film.  So I went out and I looked at that which was really cool.  Did anyone go to the town square last month to see that?  Really cool music, Catholic music, really, really cool.  And I was a bit techish, so I had one of those.  And I had one of those.  And I had one of those as well, and I really, really, really, really, really full up.  I thought, no, I'm going to get a picture of you.  The fact is that you're not going to get a picture of me.  And it had my name coinciding now and I didn't hear it.  So we ride monitors at a time and let me go.  I started to work on a project called the system that monitored monitor systems,  which was a bit of a silly idea and it worked out very well.  So we had a slight pivot and I...  We took the aging technology from our monitor, the monitors, and turned that into a general  purpose monitoring solution. We built up the product over a decade and it was, to be  honest, it was never that great because there's a general problem with monitoring and it's  all about like, the employer's a software as a service. Now we chose Google Compute  Engine and GKE. And GKE is the hosted Kubernetes implementation that Google provide. And we  were kind of quite keen on dog fooding and we wanted to use our own solution to monitor  our stuff and also we needed to provision monitoring instances for customers who were  signing up to the SAS so that they could actually, you know, click a button and a  monitoring instance be deployed on Kubernetes. So we needed to interact with the  Kubernetes API. We're all Python people so we had to do that with Python. So that's why  we wrote Q. Now it's important to say that there are other Python API wrappers out there.  And I'd strongly encourage you to go off and look at them so you can see how rubbish  they are and how good ours is. No, seriously. Go off and have a look. They might be  better fit for you. And also if you do end up choosing one of these instead of  Parkube, it'd be really great if you could come back and tell us what you thought was  better because that would be interesting to know and we could try and fix anything  we've got going on in ours. But we wrote ours, we wrote Q in the way we wrote Q  because we wanted to abstract away from what's a somewhat moving target as far as  the Kubernetes API goes. It's also got some idiosyncrasies that you don't  necessarily need to be exposed to when you're interacting with Kubernetes  Firebase API. So we wanted to kind of like create an opinionated version of the  API that made things a little bit more palatable for the user of that API. We also  wanted a clean watch interface. Watch interface allows you to get notifications  of changes to resources within Kubernetes and some of the other offerings that are  about at the time, some have come and gone since we started. But the ones that  were about didn't do that very well. We wanted something with Python and we didn't  want to use code generation swagger and that's a theme in some of these alternatives.  But you guys take a look and take your choice. So I hate it when speakers say,  oh, hands up. Do you know this? Do you know that? But just a very quick show of  hands is who has kind of been exposed to Kubernetes? Okay. So third, okay. I'll try  and railroad through this because time is a premium. So Kubernetes is about  orchestrating Docker containers, essentially. But not just Docker containers.  Docker came out of Truity file systems and if any of you have been exposed to  Solaris and Solaris zones, similar kind of concept. What it gives you is basically  an immutable deployment component. It's easy to author. There's a runtime that  runs on many platforms and it allows you to develop an immutable deployment  component that underpins DevOps practices and continuous deployment. Across  multiple nodes, it's hard to manage Docker containers in the raw. So especially  for scale and resilience. So that's why control planes like Kubernetes and  Docker swarm came about. And I mean, Google already doing this. Google had a  system called Borg which uses LXC containers and it manages those across  their enterprise. It allowed Google to scale developer productivity and the  number of services they're offering to their customers internally and externally  without the corresponding increase in operational overhead. So there was  obviously, you know, it was a useful technology. Kubernetes has had an amazing  amount of momentum behind it. It's interesting how many, in quotes,  competitors have actually got behind Kubernetes where initially they thought  to position themselves as direct competitors to Kubernetes. They kind of  seem to acknowledge that they have a particular sweet spot. Maybe it's in  cluster management or managing containers on very, very large scales. And  they seem to have all sought to accommodate Kubernetes in their offering  and in their space. So, you know, the only sort of like, sort of offering that  hasn't really done that, I suppose is Docker swarm because that is, you know,  trying to do exactly the same thing. So how does it work? There you go.  Happy with that? Excellent. Yeah, it's not very helpful, that kind of  diagram, you know, and it just takes forever. I haven't got time. So I just  want to go through some key concepts. So in Kubernetes we have the idea of a  cluster which is a single homogenous cluster of nodes, compute resource.  Watch out for a thing called Ubinities, which is kind of an attempt to  federate multiple Kubernetes clusters so you can have basically multiple  clusters with different kinds of shapes of resource running inside it.  A node is some resource where pods are scheduled. And pods are the smallest  unit of scheduling that runs the actual container. So Docker, but not  exclusively Docker, there's also support coming for rocket containers.  And the Docker containers run inside the pods and those are the things that  the Kubernetes system schedules. There's the concept of replica sets.  And what they do is they, it's a specification that defines the pods  and how many replicas of those pods there need to be for scale and  resilience, amongst other things. There's also services. Services target pods  and expose their capabilities at the edge of the Kubernetes cluster.  So you can think about the actual Docker containers, I suppose, you know, as  nano services and then the actual pods or microservices and then the  service definitions provide actual services for a consumer.  Labels are an interesting thing and we'll be looking at those very, very  quickly. And they're key value pairs that are associated with resources  within Kubernetes, but they will be a scheduler to organize the objects  within it. And there's lots of other stuff that we could talk about, but  that's probably enough to get us going through the next part of the talk,  something, something 30 minutes. So some other, some key concepts for Q.  We need to get the terminology straight really. Right at the beginning is  principally the API, the Kubernetes API defines, defines kinds and defines  resources. So a kind is the name of an object schema, essentially it's a resource  type. And a resource is a representation of a system entity that's  center retrieved from the API by Jason over HTTP. And resort, there's two  types of resources, collections and elements. So these are kind of  Kubernetes terms I'm using, but they do map quite nicely into, into Qube.  So for example, a pod is a pod resource, whereas nodes is a node list resource  and that's a collection of nodes. So try and bear that in mind.  Additionally, it's really important to understand the separation of  specification and status in Kubernetes. When an API update is made, the  specification of the resource you're updating is made. And that's available  immediately. So that's almost like an atomic operation. But over time,  Kubernetes will work to bring the status of the resource whose specification  has changed up towards that specification. So the system will drive  towards the most recent spec. And that makes the behavior of Kubernetes  level-based, not edge-based, which is quite a nice feature.  So, okay, so now the tricky bit. I'm going to open a terminal window. Bear  with me for a moment. I'm going to try and mirror this display.  Yay! That works. Okay, cool. Okay, so what I've got running here is, um, yes,  sorry. Oh, I could. Yeah, I should have tried that before.  Say when.  Okay. So I've got a single node, Kubernetes cluster, running on by  MacBuckair. By the way, if anyone's interested, it was really easy to do.  If anyone's interested in knowing how to do that, just come and see me at the  stand and we can have a chat about it. It's quite cool. Right. So, um, what  I'm going to do, okay, so, Python, cube. Here we go. So what you do is just  import cube, spelling it probably right. Remember that America thing where I was  like really jet lagged and was up till three o'clock in the morning trying to  write this stuff. So, be nice. So, import cube. So the key entry point in the  cube API is a cluster. So we can say something like cluster equals cube.cluster  and we create an instance of one of those things. So that gives us a cluster object.  So there's, if you want, if you're, okay, so one thing I forgot to mention was,  um, when you're interacting with the Kubernetes API, the preferred approach is  to run a cube control proxy. And what that does is it proxies the cube control  API from wherever it's running to local host on your machine. So what I've got  here is quite simply cube control proxy. You can just go out and see it down the  screen. So, if you're running your Python code using cube, you know, in a  container, then what you normally do is have a sidecar container inside your  pod. So one's running the proxy and one's running your Python code. Okay. So the  other thing I can do here is I can specify a URL if the proxy is running on  a non-standard endpoint or port. So we can just say local host, port number  8001 slash API, something like that. And then we get, obviously, cluster instance.  Can use context managers as well. So, yeah. So you can say something like  cube with cube.cluster as, let's say, k.k.notes. We'll talk about this in a  sec. So the k.nodes is returned a node for you and that will become clearer in a  minute. So there's a few ways to actually create your entry point as a cluster.  So do you remember I was talking earlier about collections and elements? Well,  they're represented here as views and items. So this kind of nodes thing here,  because it's plural, you can see that's actually a node list. So that's a  collection. And I can iterate over that to get actual view items out. So let's  have a look at a few of these. So we've got cluster.nodes. The cluster object has  a few of these things, many of these things. You can look at the documentation,  it's all on read the docs. It's a working progress, but there's some essentials  in there. And we can see things like the clusters, replicasets, and namespaces,  and that kind of stuff. So, okay. So we want to get a resource item out of  here. So I can say something like RS because I'm going to get a replica set  from my cluster.replicasets and I can do a.fetch and I need to specify the name.  Now, something I prepared earlier, I have... This is just a Qt control on the  line. And I can get the replica sets and I know I've got one called service demo.  So I'm going to say get me service... ...demo. But I have to specify the namespace. So I have to say namespace equals  default. It's the namespace that that's running in. You see how bad my typing is?  Shouldn't be allowed in the alive demos. And then we look at RS. We've got a  replica set item now. So this is an actual element as opposed to a collection.  And it's got some attributes associated with it. So I can say look at some  metadata and I can see what the name is and, lo and behold, it's service demo.  So I actually got to give them the right thing. And I can see what namespace  that came from. And I can also see what labels are associated with that particular  replica set. So... Sorry, again a bit close to the bottom of the screen.  Okay. So what's important to remember about resources is their version.  Kubernetes versions, all of the resources it returns across the API.  And if you remember when I was talking about the separation of spec and status,  when you get given a resource item back, it's versioned. So, you know, we can see  here that RS.meta.version is version 1561. So one thing I forgot to mention was  I think I did mention briefly is that these collections are actually iterated.  So I can do cool things like a list of comprehension. So I can say RS for RS in  cluster.replica sets. And you get... If there's anyone, you get a list of those  things back. So I can build a list, say for example, saying node.meta.version.  For node in cluster.nodes. So that's going to give me a list of all the versions  for all the nodes in the cluster. So there's anyone, and at the moment it's  that version. If you keep doing this, eventually you will see a different  version coming out. So that means the state of the node resource has changed  because something's changed about the node. It's used a slightly different  amount of CPU and that's been reported and actually whatever. So when you're  interacting through Qube, you need to make sure you've got the latest version  of the object. Otherwise, you know, you could be looking at stuff that's wrong  or out of date. Okay. So back to labels. So let's have a look at our replica set  object that we had and it's got some labels associated with it. It's got one.  It's actually a dictionary. So I can do stuff like run the run attribute and get  the value service demo. It is however immutable, so you can't mess up that  way. And that was kind of a design decision of ours when we were writing Qube  that we wanted every operation to be done by a set call. So you can update the  label using a set command. So you could say something like, let's add a new one,  foo, and we'll set it to label.set foo and we'll give it a value of a bar.  That's not predictable at all, is it? Okay. So we get one back. Okay. Let's have a  look at RS. See if anyone knows what's going on here. RS.meta.  There it was. And it's not there. That's really annoying. So that's because I'm  actually looking at the old version of RS because that's the one that got returned  from an earlier call, that version. So what I can do is this. I'm going to be  fancy and use a list comprehension because I know what I'm expecting. RS for RS  in cluster.replicasets. And I'll get the first value out of that and I should get  the second value out of that. So let's have a look at RS.version. Lo and behold,  it's a slightly different version. Yay. Okay. So now let's have a look at RS.meta.labels.  And we can see that our foo attribute is on there now. So that's really nice.  And so updating a label is kind of the same as creating a label. So I can do that.  It's returned by a new replica set, which I didn't assign to a variable. So I'll do  that. I'll look at RS.version. We've got a new version. Let's have a look at the  labels and we've got Baz set on there. So cool. So to go on about labels a lot,  one, it was kind of easy to do in this. And the other thing is they're a really good  way of managing your Kubernetes cluster. If you want to manage the way your  resources, you know, pods, services, etc., are managed, then, you know, setting and  resetting labels is a good way of doing that. Okay. So we can also delete labels.  So we can say RS.meta.labels.delete and we can say we want to delete foo.  I'm not going to be caught out a second time. I'm actually going to assign that to RS  and then look at RS.meta.labels and foo is gone. Okay. So that's kind of the end of  the, that bit, the live code demo bit. So I'm going to go back to Arrangement.  Okay. Thank you very much. Okay. So briefly talk about some of the features that I  haven't got time to demo. And in the latest version of Kube, we've got creating and  deleting resources, which actually makes it quite useful. So you can actually go in,  create pods, delete pods, replic sets, services, namespaces, all that kind of stuff.  It's just a simple create call. You pass in a JSON specification and it does sense  a call to Kubernetes. We've also got a watch API implementation, which, you know,  say, come by the booth and let us show you because it's really cool. And my colleague  who wrote that bit actually wrote a blog about how it was tricky. And he's done all  the great service because he's insulated it from all the horrors of how to do watch  support using Python and over H2P. And yeah, it's neat. There's also, if you remember  the fetch command that I used to get resource items from collections, there's actually a  filter capability. So you can filter the return results on label values, which is also  really nitty and really cool. And I didn't get a chance to show you. Finally, the cluster  instance, which is your entry point, has a proxy to the Kubernetes API. So if all else  fails and you want to get to the actual API in Python while you're using Qube, you just  use cluster.proxy to do that. Okay. So time for questions. It's on Bitbucket. And I'm  really interested to hear if you all think Bitbucket sucks and you want, you should be  on Git or if it doesn't really matter to people. I've had a sharp intake of breath from  some audiences when I've asked them about that. And anyway, that's where it is. Happy to  move it. Check us out on Qobi.io. I'm Qobi CTO. Follow me on Twitter because I'm funny.  And yeah, so I'll take questions if we've got time. Oh, no. Thank you. Hello.  So I have a kind of question. So I'm just going to condescend into what, which days are you  here? Are you here all of today and tomorrow and throughout?  Okay. So the question was, are we here or we can, can you come and see us? And yes, we're  in the vendor area. We've got like one of those little booze on the green and yellow  carpets. You'll see that fancy graphic should be up on a monitor. And we can show you, you  know, where we've got, still in beta, but we can show you where we've got and you can  have a chat with us. And my colleague, by the way, is one of the developers on Pi.test.  So some of you may have heard of him anyway. So, yes.  What was the inversion of the event from Kubernetes?  Okay. So the question was, what version of Kubernetes are we working with here? It wasn't.  What does this version of the event, you say it changes when something happens?  Okay. So it's just an opaque number. And it just represents a version of the resource  compared to the last time the resource changed. Now, depending on the resource type, the kind,  that could be anything. So for example, for a node, it could be because some of the nodes  attributes have changed. It could be because a label has been updated on a replica set.  So that's when the version number is incremented. Not just when you make the call, but when  Kubernetes itself changes the resource.  No. So what I should have said was, don't rely on the version numbers. Just always get the  latest version of the object. So I showed you the version numbers. I was kind of advised,  don't show you people the version numbers. But I just thought it was an interesting thing.  So I did that. Yeah.  Any other questions?  I think you had a call.  Okay. I think I can give a call. So I'm using type for testing of tests in Kubernetes.  Okay.  I was wondering how do you guys represent namespaces?  Okay. Come to the booth, ask my colleague. And because, yeah, and he'll give you a really  good answer. I'll give you an average answer. He'll give you a really good answer. So thanks,  guys.
David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_0_107.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_108_779.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_780_2291.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_2292_3227.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_3228_12815.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_12816_13055.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_13056_21107.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_21108_21179.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_21180_21323.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_21324_25727.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_25728_26543.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_26544_27347.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_27348_27911.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_27912_28907.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_28908_29507.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_29508_29603.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_29604_29663.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_29664_33239.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_33240_33287.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_33288_33527.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_33528_33887.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_33888_34139.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_34140_35855.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_35856_38231.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_38232_39491.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_39492_40643.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_40644_40895.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_40896_41231.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_41232_41579.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_41580_41711.png David_Charles_-_Managing_Kubernetes_from_Python_using_Kube_41712_43010.png