Sunday 12 July 2020

Kubernetes: Define a pod in yaml

In this post, I am going to explain how to define a pod in YAML file and create a pod using this YAML definition file. You can also create a manifest file using json, but YAML is the preferred approach because of readability.

 

Step 1: Create employeeServicePod.yml file

 

employeeServicePod.yml
apiVersion: v1
kind: Pod
metadata:
  name: employee-service
  labels:
    app: employee-service
    author: krishna
    serviceType: webservice
spec:
  containers:
    - name: employee-service-container
      image: jboss/wildfly

Step 2: Execute below command to create a pod from employeeServicePod.yml file.

$kubectl create -f employeeServicePod.yml
pod/employee-service created

Explanation

apiVersion:

It specifies the version of the Kubernetes API that you are using to create the object.

 

Following table summarizes possible values for apiVersion.

 

Kind

apiVersion

Pod

v1

Service

v1

ReplicationController

v1

ReplicaSet

apps/v1

Deployment

apps/v1

 

kind:

It specifies the type of object that we are trying to create. Possible values are Pod, Service, ReplicaSet, and Deployment

 

metadata:

Specifies information about the object like labels, name etc., You can add any number of custom key:value pairs under labels section. Labels are used to identify the object when hundreds of pods managed by Kubernetes.

 

spec:

This section is different for different types of objects. As you see the yaml file, 'containers:' is a list, we can specify multiple containers in a pod object.

 

In the containers: section you add the following properties.

 

Property

Description

name

Name Of the Container

image

Image that should be used

command

Command to run in the container

args

Arguments that are passed to the command

env

Environment variables that are available to the container.

 

Step 3: You can execute the command ‘kubectl get pods’ to get the information about pods in your system.

$kubectl get pods
NAME               READY   STATUS    RESTARTS   AGE
employee-service   1/1     Running   0          3m1s

You can delete a pod by executing the below command.

kubectl delete pod {podName}

$kubectl delete pod employee-service
pod "employee-service" deleted
$
$kubectl get pods
No resources found in default namespace.

Previous                                                    Next                                                    Home

No comments:

Post a Comment