DEV Community

Cover image for Create Kubernetes pods
Preetham
Preetham

Posted on • Updated on

Create Kubernetes pods

Let us create two pods in Kubernetes.
Let us create a pod having a WordPress container and another pod having a MySQL container.

Now, why WordPress and MySQL? What would be a better example than setting up the good old Apache webserver with PHP connecting to a MySQL database? Here WordPress is a free and open-source content management system written in PHP and paired with a MySQL or MariaDB database. So instead of creating your PHP from scratch, we are going to create containers from pre-created WordPress images.

What we will have at the end of this tutorial is as below.
pod1

Creating pods in Kubernetes is simple. We will use the YAML based approach.

Let us create a file named wordpressDeploy

ment.yaml and we will use vim editor for this.

vim wordpressDeployment.yaml
Enter fullscreen mode Exit fullscreen mode

A blank page would be opened. Press escape and the type i. The page will open in insert mode. Type the below.

apiVersion: v1
kind: Pod
metadata:
        name: wordpress-app
        labels:
                app: wordpress
                type: app
spec:
        containers:
                - name: wordpress-app
                  image: wordpress
Enter fullscreen mode Exit fullscreen mode

Press escape ,then type :wq! and press enter. The page saves.
Here,
. apiVersion represents the version and type of the API we are
going to use to create this Kubernetes object. Any new
feature, beta, alpha versioning is done at the API level
rather than the field or resource level.
. kind represents the type/kind of object you want to create
. metadata represents the data that helps uniquely identify the
object, including a name string, UID, and an optional
namespace. name in metadata is the name of the object you
are going to create. labels in metadata is the
identification used by other objects to select this object. We
will look at how labels and selectors in future posts.
. spec represents the kind of (what) state you desire for the
object. In containers, we are specifying the name and image
of the container we are going to create in this pod.

So when we apply this, we are going to create a pod named WordPress-app with the official WordPress image in dockerhub.

$ kubectl apply -f wordpressDeployment.yaml
pod/wordpress-app created

#check pod status
$ kubectl get pods
NAME            READY   STATUS    RESTARTS   AGE
wordpress-app   1/1     Running   0          21s
Enter fullscreen mode Exit fullscreen mode

When we check the status, we see that the pod is up and running.

Now let us create the MySQL pod and so we will create/open WordPressDb.yaml

apiVersion: v1
kind: Pod
metadata: 
        name: wordpress-db
        labels:
                app: Wordpress
                type: db
spec:
        containers:
                - name: wordpress-db-container
                  image: mysql:5.7
Enter fullscreen mode Exit fullscreen mode

Apply the yaml to create a MySQL pod, created from the official MySQL image.

$ kubectl apply -f wordpressDb.yaml

$ kubectl get pods
NAME            READY   STATUS             RESTARTS   AGE
wordpress-app   1/1     Running            0          26m
wordpress-db    0/1     CrashLoopBackOff   1          22s
Enter fullscreen mode Exit fullscreen mode

We see that the status is having an error named CrashLoopBackOff.

#checking the logs for the pod
$ kubectl logs -p wordpress-db
Error from server: Get https://172.31.38.44:10250/containerLogs/default/wordpress-db/wordpress-db-container?previous=true: dial tcp 172.31.38.44:10250: i/o timeout

#desribing the pods
$ kubectl describe pods wordpress-db
Name:         wordpress-db
Namespace:    default
Priority:     0
Node:         ip-172-31-38-44/172.31.38.44
Start Time:   Thu, 16 Jul 2020 01:39:37 +0000
Labels:       app=wordpress
Annotations:  cni.projectcalico.org/podIP: 10.13.108.2/32
              cni.projectcalico.org/podIPs: 10.13.108.2/32
Status:       Running
IP:           10.13.108.2
IPs:
  IP:  10.13.108.2
Containers:
  wordpress-db-container:
    Container ID:   docker://e6a336394be171103dc0aacc386d3cfb348ef565a9c69969cc3e5d268c1e79ea
    Image:          mysql:5.7
    Image ID:       docker-pullable://mysql@sha256:ea560da3b6f2f3ad79fd76652cb9031407c5112246a6fb5724ea895e95d74032
    Port:           <none>
    Host Port:      <none>
    State:          Waiting
      Reason:       CrashLoopBackOff
    Last State:     Terminated
      Reason:       Error
      Exit Code:    1
      Started:      Thu, 16 Jul 2020 02:00:45 +0000
      Finished:     Thu, 16 Jul 2020 02:00:45 +0000
    Ready:          False
    Restart Count:  9
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-48pkn (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             False
  ContainersReady   False
  PodScheduled      True
Volumes:
  default-token-48pkn:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-48pkn
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason     Age                   From                      Message
  ----     ------     ----                  ----                      -------
  Normal   Scheduled  <unknown>             default-scheduler         Successfully assigned default/wordpress-db to ip-172-31-38-44
  Normal   Pulled     22m (x5 over 23m)     kubelet, ip-172-31-38-44  Container image "mysql:5.7" already present on machine
  Normal   Created    22m (x5 over 23m)     kubelet, ip-172-31-38-44  Created container wordpress-db-container
  Normal   Started    22m (x5 over 23m)     kubelet, ip-172-31-38-44  Started container wordpress-db-container
  Warning  BackOff    3m25s (x96 over 23m)  kubelet, ip-172-31-38-44  Back-off restarting failed container
Enter fullscreen mode Exit fullscreen mode

We still don't understand what the issue is, but we can understand that the container starts, crashes, again restarts.
I am still not sure why it's crashing, but I'll take a look at the official MySQL container page in dockerhub.

I can observe that the environment variable MYSQL_ROOT_PASSWORD is mandatory. So I am going to pass it.

apiVersion: v1
kind: Pod
metadata:
        name: wordpress-db
        labels:
                app: wordpress
                type: db
spec:
        containers:
                - name: wordpress-db-container
                  image: mysql:5.7
                  env:
                          - name: MYSQL_ROOT_PASSWORD
                            value: DEVOPS1
Enter fullscreen mode Exit fullscreen mode

Now let's apply it and observe.

$ kubectl apply -f wordpressDb.yaml
pod/wordpress-app created

$ kubectl get pods
NAME            READY   STATUS    RESTARTS   AGE
wordpress-app   1/1     Running   0          10m
wordpress-db    1/1     Running   0          21s
Enter fullscreen mode Exit fullscreen mode

It works and both the pods are up and running.

Top comments (0)