In this video,
we're going to go over the solution for the lab on ReplicaSets.
Let's start with the first question.
The first question is, how many pods exist on the system?
Just like before, we were on the kubectl get pods command.
There are no pods at the moment, so select zero.
The next question, is how many ReplicaSets exist on the system?
We run the kubectl
get ReplicaSet command.
It's zero.
Some changes we're made, and let's check the ReplicaSet now.
We see that there's one created.
Let's just select one.
Now the question is, how many pods are desired in the new ReplicaSet?
We know that from the output,
the desired number of pods in the ReplicaSet is number four.
Let's select four.
Now, what is the image used to create the pods in the new ReplicaSet?
We can find that out by looking at the ReplicaSet in a bit more detail.
For that we know we have to run the kubectl describe command.
We're going to run describe ReplicaSet, and then new ReplicaSet.
Hit the top key to auto-complete the names of what you're looking for.
In this output, we see the pod template and the image that's used.
It's busybox 777.
That's the question.
Here is the answer, so busybox 777.
How many pods are ready in the new ReplicaSet?
If you look at the output here,
it says the number of pods that are ready are zero.
You can also see the same kind of here, the pod status, zero running.
It is zero.
Now, why do you think the pods are not ready?
We'll need to look at one of these pods in a bit more detail.
Of course, it's obvious,
but I just want to show you if you're not sure what the error is,
then you've got to look at one of these pods in a bit more detail.
Let's run the kubectl describe command
and look at any one of the pods in a bit more detail.
You see it's basically because it failed to pull the image busybox 777.
The repository does not exist.
There's no image by the name busybox 777.
That's a reason.
Let's check that.
Now, the question is to delete one of the pod.
Sorry.
Let's clear.
Run the get pods command.
We've got to delete any one of these pods.
Okay, so that's done.
How many pods exist now?
Let's run the get pods command again.
We see that there's still four parts running.
Even though we deleted one, the one that we deleted is not here,
the one that starts with W-K, but there are still four pods.
That exercise was just to show
that even if you delete a pod part of ReplicaSet,
it's automatically going to recreate a new one.
If you look at the age here, you can see that
the new one that was created was just 17 seconds ago,
as opposed to the other ones
that have been running for three minutes or more.
Why are there still four pods even after you deleted it?
It's not because I didn't delete it properly.
It's not because you cannot delete a pod from a ReplicaSet.
It's because Replica ensures
that the desired number of pods always run.
The next one is to create a ReplicaSet using
the replicaset definition file located at root.
Let's look at where that is.
There's one here, and there's an issue with the file,
so we have to try and fix it.
Let's first try to create it.
Kubectl create -f /root
replicaset definition 1.YAML.
The error is that it's unable to recognize this file,
no matches for kind ReplicaSet in version v1.
There's something wrong here.
Either the kind ReplicaSet is incorrect,
or the version we want is incorrect.
Let's look at the file,
and we see that the API version is v1 and kind is ReplicaSet.
As we learned from the previous video, the API version is incorrect.
If you're not sure what it is, one way to find
that out is using the kubectl explain command.
Run the Kubectl explain replicaset command.
Here, you can see the version.
The version is supposed to be apps/v1.
That's the problem.
Let's go ahead and fix that.
Apps/v1
and let's try and do that again.
It is now being created.
Let's go to the next one.
Next task is to fix the issue in the second file
and create the ReplicaSet.
Let's try that.
Let's just first try to
create it.
Okay, it says, "There's an invalid value.
Selector does not match template labels."
Let's take a look at the file here.
We see the API version is app/v1, which is correct,
the kind is ReplicaSet.
That also seems to be correct.
The metadata is the name.
Then you have the spec section, so we have replicas, we have selector.
There is something wrong here, which is why it didn't work.
Let's find that out.
Here, it says, "Invalid value.
Selector does not match the template labels."
Basically, the issue here is that you have the labels
specified here called tier frontend,
and then the label for the pod that the replicas will create is Nginx.
These two should match, otherwise,
the replica set cannot basically manage the pod that it creates.
We've got to either change this to frontend or change this to Nginx.
At the end, both of them should match.
Let's change this to Nginx.
Save this file
and run it again.
That's now created.
Let's go to the next one.
Now, we've got to delete the two newly created ReplicaSets.
Let's first look at the status right now.
You can also run just rs instead of ReplicaSet, which is the short form.
Now we've got to delete both of these ReplicaSets,
so kubectl Delete rs replicaset one
and two.
You can also give multiple in the same command.
Okay, so that's done.
Now, fix the original ReplicaSet to use the correct busybox image.
We know that the first one which is--
We now only have one
and we know that there are none of the pods are in a ready state,
and we also know that because the image name is incorrect,
so we've got to now fix that.
For this,
let's go with the kubectl edit command
and edit the new ReplicaSet.
Let's go into the container section
and change the image name to just busybox.
Save that file.
Let's check status of pods now and the ReplicaSet.
It's still zero.
Let's just give it some time for it to delete the old pods
and create new ones with the new image.
Describe rs.
When you update a ReplicaSet, even though the image name is updated,
the pods are not automatically recreated.
You have to either delete and recreate the entire ReplicaSet
or you have to delete all the pods so that the ReplicaSet
creates new pods with the new image name.
Let's go back and run kubectl get pods.
You see they're all still in the error state.
We're just going to go through and delete them.
We're going to use the name for each one.
Now we just wait all of them to be terminated.
Let's run get pods again.
It's in the creating state, and it's in the running state.
Let's check the status of the ReplicaSet now.
You can see that they're all in a ready state.
Let's go to the next one.
Now, we've got to scale the ReplicaSet to five pods.
Let's run the kubectl scale command, and
specify the number of replicas to five.
Let's check the status and we can see that a new pod has been added.
We can also do that using the edit command.
We can do our kubectl edit rs and specify the name of the ReplicaSet.
Then go here and just change this number to the desired number,
and then save the file, but we're not going to do that right now.
That's done.
Now, the task is to scale the ReplicaSet down to two pods.
This time let's use the other approach.
Let's edit the ReplicaSet.
Go to the number here and set it to two.
Okay.
Let's check.
That's it for this lab, and next we'll look at deployments.