# Creating the first pod in Kubernetes

To create the pod let's first install the Minikube from [here](https://blog.sandeepdhungana.com.np/installation-of-minikube-on-aws-ubuntu-2004).

Let's start with the basic pod.

```plaintext
vi first-pod.yml
```

```yaml
kind: Pod
apiVersion: v1
metadata:
  name: my-pod
spec:
  containers:
    - name: container1
      image: ubuntu
      command: ["/bin/bash","-c","while true; do echo I-am first-pod; sleep 5; done"]
  restartPolicy: Never
```

1. kind: This field specifies the type of resources being used. In this case, the resource is a 'Pod', which is the most basic unit of deployment in Kubernetes.
    
2. apiVersion: The version of the Kubernetes API that the manifest file is utilizing is specified in this field.
    
3. metadata: Information about the resource being defined is contained in this field. In this case, the name field gives the pod a name.
    
4. spec: The description of the pod, including the desired state and operating instructions, are contained in this field.
    
5. containers: This field specifies the containers that should be run as part of the pod. In this case, there is a single container defined, with the name of container1, an image of ubuntu, and a command to execute when the container starts.
    
6. command: This field specifies the command that should be run when the container starts.
    
7. restartPolicy: This field specifies the restart policy for the pod. In this case, the restart policy is set to 'Never', which means that the pod will not be restarted if it exists of crashes
    

To execute the manifest file, use the command

`kubectl apply -f <podname>`

```bash
kubectl apply -f first-pod.yml
```

The above command is used to create or update resources in the Kubernetes cluster.

The 'apply' subcommand tells 'kubectl' to apply the configuration specified in the specified file in our case which is first-pod.yml to the cluster. If the resources defined in the file do not already exist in the cluster, 'kubectl apply' will create then. If the resources already exist 'kubectl apply' will update them with the new configuration.

The '-f' flag specifies the path to the file containing the resources configuration to apply.

Now to see the pod apply

```bash
kubectl get pods
```

If you want to see, where or in which node pod is running

```bash
kubectl get pods -o wide
```

To see more detailed information about pods

`kubectl describe pod <podname>` OR

`kubectl describe pod/<podname>`

```bash
kubectl describe my-pod
```

To view the log generated by the pod in the Kubernetes cluster

`kubectl logs -f <podname>`

```bash
kubectl logs -f my-pod
```

To delete the pod from the Kubernetes cluster

`kubectl delete pod <podname>` OR

`kubectl delete -f <manifestfile_name.yml>`

```bash
kubectl delete pod my-pod
```

## Create a multi-container pod

```bash
vi multi-container.yml
```

```yaml
kind: Pod
apiVersion: v1
metadata:
  name: second-pod
spec:
  containers:
    - name: container1
      image: ubuntu
      command: ["/bin/bash","-c","while true; do echo I-am-container-1; sleep 5; done"]
    - name: container2
      image: ubuntu
      command: ["/bin/bash","-c","while true; do echo I-am-container-1; sleep 5; done"]
```

```bash
kubectl apply -f mulit-container-yml
```

```bash
kubectl logs -f container1
```

![](https://lh6.googleusercontent.com/0AT9WFL4SmWCRny_ze59NgnRfuC4A7N8YwG9k7dkTthRSeE2ogFp9CqePKp8vqB6SH3EkBiUNlbe63G8gIpfX2UYUZFDejmQDjiiEncr5KAzvzKYxIsfGr9FeJbmZ1X1eg0TBDJACwGwMjTapzM9UaHhpg0NO9lDM-kJ-DDQcfn29BtbHc2p-l8U6EP1NQ align="left")

`cltrl + c`

```bash
kubectl logs -f container2
```

![](https://lh5.googleusercontent.com/DNJpu3ShsI9hoBKXWu6VeoOFKFw3RQ1OACbc8zg7e2W2k1oYZs7MWST0ypfggRXiWFakTlgi6JvrvNh6jDEWqfiaWP4KedffVgGGd3_2Qif0VC4IZ2gF84yTonvbDSfaeW_xez1OYEredhJziPX14FBn7iUexCSd4yDEIbuoScBrN3b9dCXgu0t2l0UZvQ align="left")

To execute a command in a container

`kubectl exec <pod-name> -it -c <container-name> -- /bin/bash`

```bash
kubectl exec second-pod -it -c container1 -- /bin/bash
```

In Kubernetes, we can define environment variables for a container in a pod using the 'env' field in the container specification

For example

```bash
vi env-pod.yml
```

```yaml
kind: Pod
apiVersion: v1
metadata:
  name: env-pod
spec:
  containers:
    - name: container1
      image: ubuntu
      command: ["/bin/bash","-c","while true; do echo It-is-environment demo; sleep 5; done"]
      env:
        - name: MY_NAME
          value: SANDEEP_DHUNGANA
```

```bash
kubectl apply -f env-pod.yml
```

Now execute a command inside container1 to see the environment variable.

```bash
kubectl exec env-pod -it -c container1 --/bin/bash
```

```bash
echo $MY_NAME
```

To specify the pod with ports

```bash
vi pod-port.yml
```

```yaml
kind: Pod
apiVersion: v1
metadata:
  name: port-pod
spec:
  containers:
    - name: container1
      image: httpd
      ports:
        - containerPort: 80
```

```bash
kubectl apply -f port-pod.yml
```

`kubectl describe pod <pod-name>` for more detail.
