Because of the immense flexibility we have with 2 2
Kubernetes, there's lots of styles for how you 3 3
do your work in the Kubernetes cluster system. 4 4
You've heard me talk about that before, right. 5 5
I've told you that there are different ways to fit your 6 6
workflow, and that you can use different commands in 7 7
different ways. So, that can be challenging when you're 8 8
trying to teach it because people really want to know how 9 9
they're supposed to be using it. Like what's the best way? 10 10
What's the best practice? That's hard because 11 11
it really depends on what you want out of it. 12 12
It's it it's an unappealing dated system. 13 13
So, there's lots of different ways to do stuff. 14 14
This is where I try to break that down. 15 15
So, this one's a key important one. 16 16
But, this is all just slide learning. 17 17
This is going to be me talking at you for a few minutes to 18 18
try to break down these concepts and apply what they mean 19 19
to Kubernetes specifically. 20 20
Before we get to recommended approaches, and 21 21
there's basically going to end up being three recommended 22 22
strategies for how to use Kubernetes. 23 23
This is where I need to break down some of these terms so 24 24
you know what that next lecture is. 25 25
You'll see this in a lot of blog posts and in 26 26
documentation. Imperative versus Declarative. 27 27
The best way that describes it, in relation 28 28
to Kubernetes, is to back up and just say 29 29
what it means in terms of software. 30 30
When we're writing software imperatively, we're focused 31 31
on the how of that program operating, 32 32
each step, in order, one after the other. 33 33
When we talk about a declarative approach to software, 34 34
we're describing more about what it should accomplish. 35 35
What are its end goals? 36 36
We don't care so much how it gets there just that it 37 37
does get there. Let's try an analogy. 38 38
Imagine you want a cup of coffee. 39 39
When I wake up every morning, I'm looking for that first 40 40
cup. If I'm going to make it myself, I'm going 41 41
to have to do it imperatively. 42 42
I'm going to have to think about boiling some water. 43 43
What's the next step? Well, I need some coffee beans. 44 44
Then I've got to grind those beans. How much exactly do I 45 45
need? How much water do I pour over those beans to get 46 46
the perfect cup of coffee, etc, right. 47 47
I'm going through each step, and I'm thinking about how 48 48
it's done, step by step. 49 49
That's imperative. When you look at Kubernetes, we'll talk 50 50
about that in a second. 51 51
Declarative mode. Think of it as more of 52 52
maybe I'm at a coffee shop and I'm talking to a barista. 53 53
I'm simply asking them. 54 54
I'd like a cup of coffee. 55 55
My only job is to ask for the cup of coffee 56 56
and it's up to them to figure out, hey. 57 57
Do I need to make it from scratch? Is there one already 58 58
made in the pot? Or is the pot too old? 59 59
They can do...they can...maybe have to retry 60 60
three times to make the cup of coffee because they messed 61 61
it up. That's fine. 62 62
I'm not asking how they make it. 63 63
I'm asking for them to produce me a cup of coffee. 64 64
Let's apply this to the commands you're going to be using 65 65
in Kubernetes. Let's focus on imperative, 66 66
which is what you've largely been learning so far because 67 67
it's the way to start learning any tool is 68 68
imperatively. You're going to be doing these things in the 69 69
order they need to be done, and you're giving it explicit 70 70
instructions on how to do it. 71 71
These commands, like kubectl run, and create deployment, 72 72
and update, which we haven't yet covered, or edit. 73 73
These are the different types of commands that will have 74 74
you starting in a state that you know. 75 75
When you use a create deployment command, you 76 76
know that the deployment isn't yet created. 77 77
Thus, you are using the create, right. 78 78
If you want to update, or edit something, you're explicitly 79 79
using that command to do that. 80 80
The nice thing here for command line and typing into the 81 81
CLI is that different commands are used for different types 82 82
of actions, whether it's create, or update, or delete. 83 83
That's easier for our brains to remember because we know 84 84
the action we're taking. 85 85
Just remember that imperative is easier when you already 86 86
know the state of the object or the resource, and 87 87
you're just getting started with using the tools. 88 88
So, you want to think explicitly, OK, I need to create a 89 89
service and that service needs to be a load balancer. 90 90
You know that kind of thing. Then, it's easier for us in 91 91
our human minds to type these things at the CLI. 92 92
It's unfortunately not easy 93 93
to automate. That's one of the biggest drawbacks to this 94 94
type of work is that it's really hard to get 95 95
an automated script, or tool, to 96 96
automatically handle the, Oh is this an update that 97 97
you want? Or is this a create? 98 98
Or is this a delete? Like you know, what am I supposed to 99 99
do here? It's hard for us to automate that type 100 100
of work. It's easy in our heads. 101 101
Easy for us to type at the CLI. Hard for us to automate. 102 102
Remember, declarative means we don't really know the state. 103 103
Remember the coffee. I don't know whether they have coffee 104 104
or not. They may already have some made. They may have to 105 105
make some from scratch. 106 106
I don't know the state. 107 107
I just know the end result that I want. 108 108
That's really fitting for a declarative model. 109 109
Some of the benefits here for us, and for automation, are 110 110
that the same command, usually you are going to be using 111 111
the apply command, kubectl apply. 112 112
That same command can be used regardless of whether we're 113 113
creating, or updating, or even possibly deleting, 114 114
resources. If you started looking at YAML, which we're 115 115
going to get into here in a minute. 116 116
That YAML can either be in a single file. 117 117
You can bundle lots of different types of resources into a 118 118
single YAML. Or, you can break them out into a single 119 119
object in each YAML file in a single directory, and you can 120 120
use one command to apply the entire directory. 121 121
There's lots of options there. 122 122
The reasons we don't get started using this method 123 123
is that one, we don't always need. 124 124
It it adds a little bit of complexity because you have to 125 125
create this file, and all of the stuff in, it before you 126 126
can even do it. 127 127
You may not have those from scratch, and you don't 128 128
necessarily want to create them just to create a pod or 129 129
something, right. So, getting started, the CLI makes sense. 130 130
Using the standard imperative commands makes sense. 131 131
But, as you start to think about production, and 132 132
automation, and doing this repetitive update process 133 133
over and over, like you're going to, then you start to 134 134
think about, OK. I need a more declarative approach. 135 135
The challenge there is you have to know all those YAML keys 136 136
and settings, and you kind of just have to start from 137 137
scratch and build up your expertise over time into all 138 138
the ways and things that you need to put in this YAML. 139 139
The great thing here is that your 140 140
investment is not wasted. While you're getting some 141 141
basic things done in Kubernetes now by typing the CLI's 142 142
like kubectl run or create. 143 143
Over that time, you're looking at YAML with the 144 144
output command, remember the -o yaml. 145 145
You can look at the output of the YAML. 146 146
There's other ways we can look at YAML. 147 147
We start building up our knowledge of what that YAML is 148 148
supposed to look like so that once you get to the world 149 149
in production where you're basically doing YAML all day, 150 150
you're going to be a little bit more familiar with the way 151 151
it's broken out. This YAML way is the 152 152
easiest way to automate your orchestration. 153 153
This is also the easiest way to get-ops 154 154
happiness. We won't talk a lot about get-ops in this 155 155
course. I recommend you go Google that because 156 156
it is a type of doing DevOps 157 157
work that uses the get tool as 158 158
your source of truth. 159 159
Basically, every time you want to make a change to your 160 160
environment, you edit some YAML. 161 161
You do the apply command. 162 162
You do a get-commit. Now you have this get-log of every 163 163
single thing you've done. That's one of the attributes of 164 164
the get-ops model of operations and doing 165 165
this with declarative is how you get there. 166 166
Let's look at the three ways that the Kubernetes teams 167 167
think we should be using the Kubernetes command line.