Creating a pod in Kubernetes
When creating a pod in Kubernetes you have two ways to achieve this the first being the imperative way which is using the kubectl command and the second being the declarative way, which is to use a yaml file.
in this blog post we will create a pod based on the NGINX image. We need two parameter’s to create a pod:
- The pod’s name, which is arbitrarily defined by you
- The Docker image(s) to build it’s underlying containers.
Imperative Way
To create a pod using the imperative you type the following:
1 |
kubectl run nginx-pod --image nginx:latest |
The above command the Pod’s name is set to nginx-pod, the name is important because it is a pointer to the Pod: when you run the delete or update command you will need to know the pods name. The –image flag will be used to build the Docker container that this Pod will run. The image is the latest version of nginx.
Declarative Syntax
1 2 3 4 5 6 7 |
kind: Pod metadata: name: nginx-pod spec: containers: - name: nginx-container image: nginx:latest |
The above is the declarative syntax way of creating a pod. I added the above to a yaml file called nginx-pod.yaml. I then issued the below commands to create the pod.
1 |
kubectl create -f nginx-pod.yaml |
or you can use the following
1 |
kubectl apply -f nginx-pod.yaml |
remember that Kubernetes cannot run two pods with the same name.
Reading the Pod’s information and metadata
At this point you should have a running Pod on your Kubernetes cluster. Here we are going to try and read its information.
- kubectl get – command is a list operation; you use this command to list a set of objects.
- kubectl describe – command is quite different. its intended to retrive a complete set of information for specific object that’s been identified from both its Kind and object name.
for example to reteieve information of our previsouly created Pod you issue the command –
1 |
kubectl describe pod nginx-pod |
Listing the objects in JSON or YAML
The -o option is one of the most useful options offered by the kubectl command line. This one has some benfits you must be aware of.
to list pod information in yaml format
1 |
kubectl get pods -o yaml |
to list pod information in json format
1 |
kubectl get pods -o json |
you can also get a specific pod if you know the name
1 |
kubectl get pods <POD_NAME> -o json |
1 |
kubectl get pods <POD_NAME> -o yaml |
Getting more information from the list operation
it’s also worth mentioning the -o wide format, which is going to be very useful for you: using this option allows you to expand the default output to add more data.
1 |
kubectl get pods -o wide |
Other Resources
Installing Kubernetes cluster on Linux