‫All right. This lecture is starting off where the last one
‫ended. So, if you're just jumping into this one and you
‫skipped the last one, go back to that one because you'll
‫need the Deployment that we created in that one
‫to start this one.
‫When I do a kubectl get all, I
‫should see here that I have one Deployment
‫that has a replica set of five.
‫You'll see the five pods.
‫Then you'll have the Kubernetes default service,
‫which is always there. Then the httpenv
‫cluster IP service.
‫You, of course, may have different IP addresses and
‫different pod names. That's where we need to be.
‫We have this Deployment.
‫We've created an IP address inside the cluster for others
‫to access it with the friendly name of httpenv
‫because that's the service name.
‫Let's create one that's exposed externally on
‫the host IP. This would be, if you were maybe in
‫a data center or somewhere that you didn't have a load
‫balancer you could control with the load balancer
‫service, which we'll talk about a minute.
‫You just basically, in this case, want to do a NodePort
‫where you get ports somewhere in the high range
‫of your Kubernetes cluster that you can
‫use to talk from the outside in.
‫So it will be able to access it from some external
‫service, right. It's not going to be on port 80 by default.
‫The NodePorts are meant to be high ports so you're usually
‫putting something in front of that, like an external load
‫balancer or some automated service that will manage
‫that for you. In this case, I can do a
‫kubectl expose again.
‫It's going to look like the last one we did.
‫deployment httpenv.
‫That's the one we need to expose.
‫We're going to put in port 8888 again.
‫But, we're going to give it a new name because we already
‫have one called httpenv.
‫So, this one we're going to call a
‫httpenv-np for no port.
‫That way, it will work side by side with
‫the cluster IP one that's already there.
‫Then we're going to change the type to NodePort.
‫You'll notice that type of NodePort, because the default
‫type, which we can specify manually if you want to.
‫The default type is cluster IP.
‫If I go back and do a kubectl get
‫services, you'll
‫see a new one there that's type NodePort.
‫Let's talk about the ports on the right.
‫Those ports, when they have the colon in the middle, are
‫the opposite of what you would see in Docker and Swarm.
‫The port on the left, in this case, is
‫the one inside the cluster, inside the
‫container itself that's listing.
‫Then the port on the right is the port that's on
‫your nodes exposed to the outside world.
‫You might wonder where the port on the right came from.
‫That port is from a default range
‫for NodePorts that are preset inside your container
‫cluster.
‫It's a high range. The hope here is that there's
‫no conflicts, right. That there's nothing else on your
‫system, by default, running on those ports.
‫Because often the low ports, like 8888, are
‫common enough that they might be used by something else. Then any
‫ports in the really low range are
‫usually only accessible by root. Ports 1024 and below.
‫There's a few things to note here.
‫The first one is you'll see the cluster IP.
‫Even though when we created this NodePort
‫service, we didn't say cluster IP.
‫We said NodePort.
‫These services are additive.
‫What that means is it's starting with cluster IP, and
‫then going to NodePort, and then to the load balancer
‫optional service. Each one of them will create the
‫services above it in this list.
‫Which means that when I create a NodePort, it
‫creates a cluster IP endpoint for me because
‫that's how it connects. It takes the connection coming into
‫that port on the high port of the node and then it
‫redirects it to the cluster IP for that service.
‫Of course, like most things in Kubernetes, all this is
‫changeable and customizable.
‫One of the things you can change is the port range.
‫One of them is you can avoid the cluster IP.
‫A lot of that stuff you can do, but most of it
‫you can only do in YAML, which we will get to later.
‫When you're doing stuff at the command line like this,
‫we're limited on the number of options we can use.
‫If you go beyond what those options allow, you're going
‫to need to start doing YAML. We'll talk about that later in
‫the course. OK. If you're on Linux, this
‫NodePort is accessible on localhost now.
‫So, you can use localhost, or the host IP itself, to
‫cURL this port. Whatever port you have.
‫It's probably not going to be the same port as mine since
‫mine's 32334.
‫If you're on Docker Desktop, it
‫will provide a convenience layer with something called
‫vpnkit that's just built into Docker Desktop where
‫it will connect this to the localhost on your Windows or
‫Mac machine.
‫I can sit here on my Mac and cURL
‫localhost 32334.
‫I get back that same response as if I cURLed the cluster
‫IP directly. The reason that this is here on
‫Docker Desktop is so that you don't have to jump into those
‫pods like we did earlier just so that you can access the
‫service. This is one way to have your Kubernetes services
‫shown up on your machines so that you can test things,
‫right. So, if you're developing or testing inside
‫Kubernetes, you can then just do a NodePort
‫and go to that port, which is going to be a little random
‫on your host.
‫Again, you can specify an exact port there,
‫but you'd have to do that in YAML, which we'll do later.
‫The other option, if you're on Docker Desktop, is that it
‫comes with a built-in load balancer service.
‫You remember me talking earlier when we were talking about
‫these four types of services, is that the load balancer
‫wasn't built in, by default, and the only way you could use
‫it was through an external service, usually in
‫a cloud service, right.
‫With Amazon, that would be ELBs and ALBs.
‫With DigitalOcean, that would be with their load balancers.
‫You would need to add that plugin in your Kubernetes so it
‫would work with those external load balancers.
‫Essentially, the kube API talks to their API
‫to control that load balancer.
‫If you're on Docker Desktop, then you can try this command.
‫We'll call it httpenv-lb
‫for this service, and we'll change the type to load
‫balancer.
‫If we do a kubectl get service, we should see three
‫load balancers for this Deployment.
‫Again, if you're on Docker Desktop, you'll be able to see
‫all three. If you're on Linux, or using
‫Minikube, or something like that, you would only see two.
‫Each load balancer, because it's technically like a
‫third-party plugin or service that's a remote API, the
‫load balancers will all have varying features and supported
‫stuff. You can sort of look at their documentation to
‫figure out what they'll do.
‫In this case, with Docker Desktop, all we did is we told
‫it the port 8888 that was a
‫part of the Deployment, and the load balancer service
‫plugin from Docker will then publish it
‫on port 8888.
‫The nice thing here is that that is the one way
‫to have your Kubernetes on a specific port
‫outside of the default set, right.
‫So, when we did the NodePort, we were defined,
‫or limited, to that set in the 30000
‫range. If you wanted to do something like you would with
‫Compose, or Swarm, or Docker run locally where you just
‫want to run it on a port like 80 or whatever,
‫you can now with Docker Desktop, using the built-in
‫Kubernetes, you can do that load balancer and just type
‫in that port there, and it will be responsive there.
‫Now on Mac or Windows, I could type cURL
‫localhost 8888 and get
‫that response back. You'll notice there for the load
‫balancer that it still has that other port.
‫Again, that's because the 32 range there, on mine
‫at least, is the built in
‫NodePort that it's creating. Technically in the background,
‫remember these are three different layers so the load
‫balancer is accepting my packet, then passing it to the
‫NodePort, and then the NodePort is passing it to the
‫cluster IP. There's always going to be that
‫NodePort shown there next to the
‫four 8's for a load balancer, even though that's not
‫really the port the load balancer is using on my localhost.
‫Before going to the next lecture, let's clean up everything
‫we just did. We created a lot of stuff.
‫These commands show you that you can use the delete command
‫with multiple objects at the same time.
‫They don't have to be related objects.
‫In fact, you could just make a big list of them if you
‫wanted on a single command.