Learn about strategies for creating and interacting with objects, including deployments, services, and pods with kubectl.
- [Instructor] Let's take a look at some of the different ways you can create an interactive cluster. The simplest possible way is to run the kubectl run command, pass a name to it, followed by an image --image and provide the image name, in this case, I'm going to use Karthequian/helloworld and I'm going to pick the latest. And this will create a deployment behind the scenes. We've seen this before, there's nothing new in this scenario. Another way is to use a file that's provided to the kubectl run command.
So let's take a look at our directory, and we notice that we have some files in here. We're going to pick red.yaml and create that. Clear my screen and kubectl create -from red.yaml this will create a deployment called red. Let's take a look at our deployments real quick. And we notice that we have a deployment called hw and a deployment called red, because we had the hello world deployment initially, and then red.
When you have multiple files that you're trying to create at the same time, you can actually pass multiple parameters, so you can actually do a kubectl create -f blue.yaml, and then pass another ymal to this, green.yaml, and this will create multiple deployments for you. This is a common scenario, when you have many applications that you're trying to deploy at the same time, doing a kubectl get deployments, will show you a blue, and a green, and a hello world, and a red.
The blue and green just started up. Now in some scenarios, you might have a folder, which has a lot of yaml files, and you might want to create every application in that specific folder. You can run a command called kubectl create -f and provide the whole folder path ./colors. And this will take all the files that are in the specific foledr called colors and create them as deployments. Let's take a look at the folder real quick. ./colors, we have a couple of yaml files here, let's take a look at the purple.yaml file, I'm going to use the vi for this, but you can use any text editor.
./colors/purple.yaml purple.yaml is just a deployment kind that is sitting in a specific folder, and, as you can notice, I can do a kubectl get deployments and this will show that purple and white, the specific files in that folder have also started up as new deployments in kubernetis. One thing to notice is if you already have deployments running, and in this case we already have a red deployment that we can create with a kubectl create -from red.yaml, if you try run this again, we'll get an error.
We see, error from server, error while creating red.yaml, deployments.apps, red already exists. This is because there's a deployment called red, that's already existing, that's been created from the first yaml file. So if we had to go change something, we would have to go edit the actual deployment and them make our changes that way. There's an easy way to do this, and we do this with the kubectl apply command. So I have another yaml file called pink.yaml, and I'm going to apply that. Kubectl apply, and apply basically allows you to either create or update the state of your yaml files.
Apply -f, where f is the filename, and I'm going to pick this file called pink.yaml and it says deployment ping created. Let's take a look at one of the things I do very often in software development. Kubectl get deployments, and I'm going to look at this from a wide setting, -o wide. I'm not sure if it's just me, but typically I end up fact fingering things, and I have to redo them, so I can actually open up the file, vi, but you can use notepad, or whatever text editor you'd like.
Pink.yaml, and as you notice, there've been a couple of places where I copied and pasted except I copied the wrong thing, so I'm going to change punk to pink, and I'm going to save this file, and I'm going to rerun the kubectl apply command, as I've done before, and it says that deployment pink was configured, and if you take a look at our deployments again, we'll notice that the deployment pink is running a container pink with the correct width, the correct selector, or label where app=pink.
So this is a simple way to update a deployment. You can use the kubectl apply command by itself multiple times, or you can also use the kubectl create command to create a cluster, and then apply changes to it, if you deem necessary at a later point of time. There's one last way that a lot of folks end up creating applications, and that is to use a URL, instead of a filename, or a directory structure. In this scenario I found a kubernetesHelloworld that I really want to run, and there is a yaml file associated with that.
And if I want to run this file directly from the URL, I can. So if I look at the raw version of this file, I can take that and I'm going to go back to my terminal, and I'm going to run a kubeclt apply -f, and I'm going to paste the URL that the whole overall file actually correspond it to. Run this, and behind the scenes, what kubernetes is doing is, is going to go to that specific URL.
Whatever yaml file kubernetes finds at that URL, it's going to take that and it's going to run that with the kubectl apply command. So if we take a look at our deployments, we'll notice that we have the hello-k8s-deployment created, and it's running as we expect. This is a common scenario because there might be examples that you might want to run, that you find online, and passing the URL to the apply command is a simple way to accomplish this.
- Strategies for creating and interacting with objects
- Searching, sorting, and filtering applications
- Deleting strategies for applications
- Running Kubernetes using Minikube, kubeadm, and kops
- Using the Kubernetes dashboard
- Federation and kubefed