I showed you how to run a container using the “run” command in Kubernetes. This tutorial, I introduce you another way is to use the “create” command with a configuration file!
The syntax of the “create” command is basically the following:
1 |
kubectl create -f <path_to_configuration_file> |
Where <path_to_configuration_file> is the path to the file defining the container information that we need to initialize.
The file defining the container information is a YAML file. The basic content that we need to create a container in this configuration file is as follows:
1 2 3 4 5 6 7 8 |
apiVersion: v1 kind: Pod metadata: name: <name> spec: containers: - name: <container_name> image: <container_image> |
Inside:
- <name> is the name of the Pod after running the command “create”
- <container_name> is the name of the container in the Pod after initialization
- <container_image> is the name of the image used to create the container.
Here, as you can see, there is a “kind” attribute that defines the component that the “create” command will create in the Kubernetes cluster. My definition is that Pod because the containers are attached to a certain Pod, running the container also means that we create the Pod in the Kubernetes cluster.
Command “create” can also create other components in the Kubernetes cluster according to the “kind” that we define in configuration file!
Now, for example, I have a configuration file with the following contents:
1 2 3 4 5 6 7 8 |
apiVersion: v1 kind: Pod metadata: name: node-hello spec: containers: - name: node-hello image: gcr.io/google-samples/node-hello:1.0 |
I will create a new container from the above configuration file with the “create” command!
I will run the following statement:
1 |
kubectl create -f node-hello.yaml |
Result:
Compared to the “run” command, as you can see with the “create” command, depending on the “kind” that we define, the message returned will be different. In my example, because I defined “kind” as Pod, after successfully creating the container, the message “pod “node-hello” created” was printed.
Now you can check the results:
There will be no Deployment components created.
Pod information: