In this video, we will go over the practice test
for imperative commands.
In this lab, we will get hands-on practice
with creating Kubernetes objects imperatively.
This is more to help with your exams.
You must be very familiar with imperative commands
while you're preparing for the exam.
Let's proceed.
The first task is to deploy a pod named nginx-pod
using the nginx:alpine image.
That we've done before.
It's easy.
It's kubectl run to create a pod.
The name is nginx-pod and the image is nginx:alpine.
Check it.
That's correct.
The next one, deploy a redis pod using the redis:alpine image
with the labels set to tier=db.
That's the similar task but with an additional field.
If you look at the kubectl run command,
let's see if there is an option to specify the labels.
If you look at this, there are some examples here.
This is what we just did.
Here, you have an option to specify the labels,
so --labels= and then here's the key-value pairs.
You can specify multiple labels separated by a comma.
We're going to use that approach.
Let's run the kubectl run command.
The name of the pod is redis.
Image is redis:alpine.
We're going to add some labels and then the label is tier=db.
That's good.
The next one is to create a service
called redis-service to expose the redis application
within the cluster on port 6379.
The service name has to be redis-service.
The type is ClusterIP, and the port is 6379.
For this, we could run the kubectl create service command.
Let's look at the help.
If it is within the cluster, then there's cluster IP
so we could run the cluster IP, kubectl create service clusterip.
Let's look at the help.
Then, you specify the name
of the service and the port here which happens to be 6379.
One other thing here is that we want to expose
the redis application.
With this command,
there is no way to specify a selector.
There is something that we discussed in this lecture.
If you look at this, there are three ways
to create a service.
This is to create a service named redis-service of ClusterIP.
We could do a kubectl create service command
but then you cannot really specify the selectors,
or you could do a kubectl expose command.
Ideally, for most use cases,
you could use the kubectl expose command
because it will automatically detect the labels,
and then use that as selectors for the service.
That's the most ideal.
The only time you should use kubectl create service
is when you need to specify a particular NodePort.
Here you have an example of creating a NodePort service.
There are two ways to do it
with the expose command and the create service command.
As of this recording, there is no way to specify a NodePort
in the command, so you have to export that
to a YAML file and edit it before creating it,
or if you go the other approach, you cannot specify the selectors.
That's the challenge here.
Here, what we're going to do
is we're going to use the exposed command.
If you look at the help, these are some examples.
We're going to specify expose and the application is redis.
We don't know if it's a pod or a deployment.
Let's find that out first.
It's a pod.
It's a redis pod.
We're going to do kubectl expose.
Instead of rc, we're going to do a pod or a po.
Remember that this is the name of the pod and not the service
that we want to create, so the pod has to be redis.
This should be a way to specify the name of the service
which is using name.
We're going to use name and specify it as redis-service.
Of course, we also have to specify
the port and the port is this one which is 6379.
That's what we are going to do.
kubectl expose redis
and then we're going to specify the port which is 6379.
You also need to specify a name that will be redis-service.
I missed specifying pod, so let me put that in.
Let's check it out.
get service redis, so that's the redis-service.
Let's see if it's got
the right labels and selectors and also the endpoint.
The labels is a tier=db, so it's automatically picked that up.
The endpoints indicates that it's automatically
discovered the pod.
Let's check.
That's good.
The next one is to create
a deployment named webapp using the image with three replicas.
We're going to do a kubectl create deployment command,
and the name of the deployment is webapp.
Image, it's going to copy and paste.
Replicas is three.
Oops, I forgot to specify the dashes.
We're going to check.
There's zero of three ready.
Okay, three of three ready.
The next one is to create a new pod called custom-nginx
using the nginx image and expose it on container port 8080.
We know that, to create a pod, we have kubectl run command,
and the name of the pod is custom-nginx.
The image is nginx.
We have to expose it on container port 8080.
We're talking about specifying the port on the container.
We just use port and specify the port number.
We're not really talking
about exposing the port by creating a service.
This is just specifying what port the container
runs the service on.
You can do that with the port option.
Let's check our work.
That's correct.
The next one is to create a new namespace called dev-ns.
That's pretty easy and straightforward.
kubectl create namespace dev-ns.
The next one is to create
a new deployment called redis-deploy in the dev-ns namespace.
We're going to do kubectl create deployment
and the name is redis-deploy.
The image is redis.
Replicas is two.
You could do =2 or space 2, either of that work.
The namespace is dev-ns.
Let's make sure that worked as expected.
We have the redis-deploy within the dev-ns namespace,
and it has two replicas.
Let's check our work.
That's correct.
The final question is to create a pod
called httpd using the image httpd:alpine in the default namespace
and then create a service of type ClusterIP by the same name.
The target port for the service should be 80.
I'll try to do this with as few steps as possible.
There are two ways of doing this.
One is you could first
do a kubectl run command and create the pod,
and then we know that we could use the kubectl expose command
to create a service but we can actually combine them both
in the run command itself.
Let's first create a pod.
We do a kubectl run httpd.
That's the name of the pod.
The image is supposed to be httpd:alpine,
and then we have a default namespace.
We don't have to specify the namespace.
The target port is 80.
What we're asked to do is also create a service.
One of the options for the run command,
we look into help is the expose option.
If you look right here, it has the expose option,
and it's by default set to false.
If it's set to true,
create a ClusterIP service associated with the pod.
It requires the pod field.
It's automatically going to create a service with the ClusterIP as type,
and it's going to expose the pod internally.
That's what we have to do.
We're going to use the same command but we're going to add
the expose=true option to the end of it.
It's created a pod and a service.
Let's just verify that.
We have the httpd pod.
We have the httpd service.
If we look at the service in more detail,
you see that it has got the selector: run=httpd,
and then it also has the endpoint discovered.
That's correct.
That's the end of this lab.