DEV Community

Cover image for K8S - exercise 1: create, fix, get, and delete PODS
Sebastian
Sebastian

Posted on

K8S - exercise 1: create, fix, get, and delete PODS

This exercise is based on the course LinuxFoundationX LFS158x
Introduction to Kubernetes.

Create a pod in a declarative way

create a file called pod.yaml

touch pod.yaml
vim pod.yaml

past this code in pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
    - name: nginx
      image: nginx:1.14.2
      ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

Run the command:

kubectl create -f pod.yaml
Enter fullscreen mode Exit fullscreen mode

1

Create a pod in a imperative way

kubectl run firstrun --image=nginx
Enter fullscreen mode Exit fullscreen mode

Get pods to see the new pod:

kubectl get pods
kubectl get pods -o wide
Enter fullscreen mode Exit fullscreen mode

2

Create a pod in a mix way (declarative and imperative)

kubectl run firstrun --image=ngixx --port=88 --dry-run -o yaml > secondrun.yaml
Enter fullscreen mode Exit fullscreen mode

If you notice, I introduced some errors in the YAML file so that I can fix them.

Modify because it is not possible to have two pods with the same name and label:

name, label, container -> name

kubectl describe secondrun
Enter fullscreen mode Exit fullscreen mode

Run this command, to replace the previous pod:

kubectl replace --force -f secondrun.yaml
Enter fullscreen mode Exit fullscreen mode

3

Now let's delete the pods:

kubectl delete -f pod.yaml
Enter fullscreen mode Exit fullscreen mode
kubectl delete pods firstrun secondrun
Enter fullscreen mode Exit fullscreen mode

4

Top comments (0)