‫Now that we've talked about the four types of Kubernetes
‫services, let's create some from the command line.
‫First, we're going to need to create a Deployment
‫that we can point these services to. Then we'll create
‫different types of services to point at this Deployment.
‫In this demo, we're going to create two different shells,
‫or different windows, into your Kubernetes environment
‫so we can see what's happening with a watch
‫command while we're doing it with the other commands.
‫All right. In my case, I have two windows, one on top of
‫the other. It doesn't really matter how you do that.
‫It's just for easier use for you to see what I'm doing.
‫We're going to use the kubectl get pods.
‫We're going to add a -w.
‫This is like a watch command where it will keep
‫running and show us over time as things change.
‫You'll notice that this is an option on multiple, different
‫kubectl commands. We'll do that here just to see what's
‫happening in the background.
‫get pods with -w.
‫Then in my other window, we're going to create a
‫Deployment.
‫create deployment. We're going to call this httpenv.
‫What this is using is a very simple HTTP
‫Web server that essentially when you hit it with a cURL,
‫it just spits back all the environment variables it's
‫getting at the command line.
‫It's a very simple, rudimentary way for us to just validate
‫that it's working, but also to get back interesting values
‫like the name of the pod.
‫Stuff like that.
‫Now that we've created that, you'll see at the top that
‫it's creating containers.
‫Think of it more like a log where each time something
‫changes state, or the status changes, or something like
‫that, you'll see it as a new line printing down
‫the page like a log file.
‫You'll see in that last line, it now shows that we have
‫that pod running in the running state there.
‫Let's spin up more of these pods so that we can
‫have more than one of these containers running
‫behind our service that we'll create a second.
‫The scale command is useful with a Deployment because
‫it allows us to change the number of replicas in a single,
‫friendly command. Of course, with everything in Kubernetes,
‫there are multiple ways to do this, but this is the easiest
‫way in our situation here.
‫All right. Now you'll see that it's created four more pods
‫and it's showing those there as running.
‫Now, we're going to create that service.
‫For that, we use the expose command.
‫In this case, we're telling it which Deployment to expose
‫and the port that that Deployment is listening on.
‫You'll see that this didn't change any of the pods because
‫this is creating an additional feature that's not
‫related to the pods themselves.
‫This is actually going to create a cluster IP by default.
‫That's the cluster IP type of service.
‫It will create that in front of the
‫Deployment. Let's check out that service we just created.
‫You should see two here.
‫The first one will be the Kubernetes endpoint.
‫That's for the API.
‫Then you'll see our service we just created and its type is
‫cluster IP. Remember, cluster IP is only usable
‫inside the cluster for nodes and other pods
‫to be able to access it.
‫This IP address we can reach, but it depends
‫on where we are and whether or not we can reach it.
‫Since that IP address is only accessible from either the
‫nodes themselves or other pods
‫inside the cluster.
‫When you're on Docker Desktop, if you remember that this is
‫not running on the host OS of Mac and Windows.
‫This is technically running in a small Linux VM.
‫We need to run, in this case, another
‫pod that's running on that cluster so that we can cURL
‫it. We don't want to run a Deployment with a replica set.
‫In this case, we really just want to run one, single pod.
‫The easiest way to do that is with kubectl run.
‫We're going to use an alternate generator.
‫You might remember that from the error earlier.
‫We'll talk about what generators are in a little bit.
‫There's a lot going on in this command, so let's break it
‫down. The kubectl run you've seen before.
‫The generator is the type of template we want the
‫run command to use.
‫We'll talk a little bit more about that in a future lecture.
‫We're picking the pod template because we only want to run
‫a pod. We don't want a full Deployment with replica sets or
‫anything. We're going to call it tmp-shell.
‫You'll notice that some of these
‫options are similar to docker run. That's on purpose.
‫One of the future goals of the
‫kubectl run command is to act more like a docker run
‫command. In fact, we'll talk later about how it's
‫going to eventually, by default, just create a pod for you
‫and you won't have to use this generator option.
‫We're going to use the --rm here which means once the
‫pod stops, go ahead and remove it.
‫The -it gives us a shell into that pod
‫once we hit the enter command. Then --image is
‫telling us what image we prefer.
‫In this case, we're using my netshoot image, which
‫is a whole bunch of different networking and Linux
‫utilities in it. One of which is cURL.
‫Then with the run command, we can change
‫the cmd, or the thing that's going to run
‫inside the container.
‫We do that with a space -- space.
‫That tells the run command to stop looking at options
‫and that anything after that double dashes is the command
‫to run when I execute this container.
‫You'll see up at the top that the temp shell pod
‫is showing up and now it's a status of running, and we're
‫in the pod with our bash prompt.
‫It's something we'll talk about in a little bit is that the
‫service name that we created becomes part
‫of the DNS name for this service.
‫We can use...the very similar to what we just did in Docker
‫and Swarm, we can use the service name to get it with DNS.
‫Remember, it's running on Port 8888.
‫When we do that, we get back the response that I would
‫expect. This httpenv utility
‫simply dumps all the environment variables on
‫that machine which is inside that unique
‫pod, right. You'll see the pod name is the hostname and
‫other Kubernetes defaults out-of-the-box.
‫That method will work for any of you, on
‫any cluster, as long as your kubectl command
‫line can talk to the API, that command will get you
‫into a new pod running that image
‫inside a shell. That's great.
‫But, if you're on Linux, there's an easier way.
‫Because the way that Kubernetes works is that all nodes
‫and containers, by default, can talk to each other
‫out-of-the-box. The node that you're on, assuming that
‫you're running Kubernetes on that local machine, has access
‫to all of these private IP addresses.
‫You should just be able to cURL the service IP.
‫Let me get out of my shell and do a kubectl
‫get service again.
‫That IP address for the cluster IP should be resolvable
‫from your host. Your host won't know the
‫DNS name like we did with inside the pod because
‫that's exclusive to being inside the cluster.
‫But, it will know the IP address and be able to access it.
‫You can cURL the IP address
‫:8888 just like you would for another machine on
‫your network. The reason that doesn't work from Docker
‫Desktop is because you're sitting on Mac or Windows when
‫you're typing those commands, and they are not
‫on the same cluster of Kubernetes
‫as that Linux VM running your Kubernetes for you.
‫All right. Let's go ahead and leave that stuff running for
‫the next lecture where we are going to create more
‫services.