Now that we've talked about the four types of Kubernetes 2 2
services, let's create some from the command line. 3 3
First, we're going to need to create a Deployment 4 4
that we can point these services to. Then we'll create 5 5
different types of services to point at this Deployment. 6 6
In this demo, we're going to create two different shells, 7 7
or different windows, into your Kubernetes environment 8 8
so we can see what's happening with a watch 9 9
command while we're doing it with the other commands. 10 10
All right. In my case, I have two windows, one on top of 11 11
the other. It doesn't really matter how you do that. 12 12
It's just for easier use for you to see what I'm doing. 13 13
We're going to use the kubectl get pods. 14 14
We're going to add a -w. 15 15
This is like a watch command where it will keep 16 16
running and show us over time as things change. 17 17
You'll notice that this is an option on multiple, different 18 18
kubectl commands. We'll do that here just to see what's 19 19
happening in the background. 20 20
get pods with -w. 21 21
Then in my other window, we're going to create a 22 22
Deployment. 23 23
create deployment. We're going to call this httpenv. 24 24
What this is using is a very simple HTTP 25 25
Web server that essentially when you hit it with a cURL, 26 26
it just spits back all the environment variables it's 27 27
getting at the command line. 28 28
It's a very simple, rudimentary way for us to just validate 29 29
that it's working, but also to get back interesting values 30 30
like the name of the pod. 31 31
Stuff like that. 32 32
Now that we've created that, you'll see at the top that 33 33
it's creating containers. 34 34
Think of it more like a log where each time something 35 35
changes state, or the status changes, or something like 36 36
that, you'll see it as a new line printing down 37 37
the page like a log file. 38 38
You'll see in that last line, it now shows that we have 39 39
that pod running in the running state there. 40 40
Let's spin up more of these pods so that we can 41 41
have more than one of these containers running 42 42
behind our service that we'll create a second. 43 43
The scale command is useful with a Deployment because 44 44
it allows us to change the number of replicas in a single, 45 45
friendly command. Of course, with everything in Kubernetes, 46 46
there are multiple ways to do this, but this is the easiest 47 47
way in our situation here. 48 48
All right. Now you'll see that it's created four more pods 49 49
and it's showing those there as running. 50 50
Now, we're going to create that service. 51 51
For that, we use the expose command. 52 52
In this case, we're telling it which Deployment to expose 53 53
and the port that that Deployment is listening on. 54 54
You'll see that this didn't change any of the pods because 55 55
this is creating an additional feature that's not 56 56
related to the pods themselves. 57 57
This is actually going to create a cluster IP by default. 58 58
That's the cluster IP type of service. 59 59
It will create that in front of the 60 60
Deployment. Let's check out that service we just created. 61 61
You should see two here. 62 62
The first one will be the Kubernetes endpoint. 63 63
That's for the API. 64 64
Then you'll see our service we just created and its type is 65 65
cluster IP. Remember, cluster IP is only usable 66 66
inside the cluster for nodes and other pods 67 67
to be able to access it. 68 68
This IP address we can reach, but it depends 69 69
on where we are and whether or not we can reach it. 70 70
Since that IP address is only accessible from either the 71 71
nodes themselves or other pods 72 72
inside the cluster. 73 73
When you're on Docker Desktop, if you remember that this is 74 74
not running on the host OS of Mac and Windows. 75 75
This is technically running in a small Linux VM. 76 76
We need to run, in this case, another 77 77
pod that's running on that cluster so that we can cURL 78 78
it. We don't want to run a Deployment with a replica set. 79 79
In this case, we really just want to run one, single pod. 80 80
The easiest way to do that is with kubectl run. 81 81
We're going to use an alternate generator. 82 82
You might remember that from the error earlier. 83 83
We'll talk about what generators are in a little bit. 84 84
There's a lot going on in this command, so let's break it 85 85
down. The kubectl run you've seen before. 86 86
The generator is the type of template we want the 87 87
run command to use. 88 88
We'll talk a little bit more about that in a future lecture. 89 89
We're picking the pod template because we only want to run 90 90
a pod. We don't want a full Deployment with replica sets or 91 91
anything. We're going to call it tmp-shell. 92 92
You'll notice that some of these 93 93
options are similar to docker run. That's on purpose. 94 94
One of the future goals of the 95 95
kubectl run command is to act more like a docker run 96 96
command. In fact, we'll talk later about how it's 97 97
going to eventually, by default, just create a pod for you 98 98
and you won't have to use this generator option. 99 99
We're going to use the --rm here which means once the 100 100
pod stops, go ahead and remove it. 101 101
The -it gives us a shell into that pod 102 102
once we hit the enter command. Then --image is 103 103
telling us what image we prefer. 104 104
In this case, we're using my netshoot image, which 105 105
is a whole bunch of different networking and Linux 106 106
utilities in it. One of which is cURL. 107 107
Then with the run command, we can change 108 108
the cmd, or the thing that's going to run 109 109
inside the container. 110 110
We do that with a space -- space. 111 111
That tells the run command to stop looking at options 112 112
and that anything after that double dashes is the command 113 113
to run when I execute this container. 114 114
You'll see up at the top that the temp shell pod 115 115
is showing up and now it's a status of running, and we're 116 116
in the pod with our bash prompt. 117 117
It's something we'll talk about in a little bit is that the 118 118
service name that we created becomes part 119 119
of the DNS name for this service. 120 120
We can use...the very similar to what we just did in Docker 121 121
and Swarm, we can use the service name to get it with DNS. 122 122
Remember, it's running on Port 8888. 123 123
When we do that, we get back the response that I would 124 124
expect. This httpenv utility 125 125
simply dumps all the environment variables on 126 126
that machine which is inside that unique 127 127
pod, right. You'll see the pod name is the hostname and 128 128
other Kubernetes defaults out-of-the-box. 129 129
That method will work for any of you, on 130 130
any cluster, as long as your kubectl command 131 131
line can talk to the API, that command will get you 132 132
into a new pod running that image 133 133
inside a shell. That's great. 134 134
But, if you're on Linux, there's an easier way. 135 135
Because the way that Kubernetes works is that all nodes 136 136
and containers, by default, can talk to each other 137 137
out-of-the-box. The node that you're on, assuming that 138 138
you're running Kubernetes on that local machine, has access 139 139
to all of these private IP addresses. 140 140
You should just be able to cURL the service IP. 141 141
Let me get out of my shell and do a kubectl 142 142
get service again. 143 143
That IP address for the cluster IP should be resolvable 144 144
from your host. Your host won't know the 145 145
DNS name like we did with inside the pod because 146 146
that's exclusive to being inside the cluster. 147 147
But, it will know the IP address and be able to access it. 148 148
You can cURL the IP address 149 149
:8888 just like you would for another machine on 150 150
your network. The reason that doesn't work from Docker 151 151
Desktop is because you're sitting on Mac or Windows when 152 152
you're typing those commands, and they are not 153 153
on the same cluster of Kubernetes 154 154
as that Linux VM running your Kubernetes for you. 155 155
All right. Let's go ahead and leave that stuff running for 156 156
the next lecture where we are going to create more 157 157
services.