Spin up a kubernetes Pod using a local image

Spin up a kubernetes Pod using a local image

Spin up a kubernetes Pod using a local image

Views: 46
0 0
Read Time:5 Minute, 6 Second

In this article we will go through the steps for spinning up a kubernetes pod using a local built image.

Pre-requisite to this article is to have a local instance of kubernetes running on your computer, if not you can follow these easy steps https://brewedbrilliance.net/run-a-kubernetes-cluster-on-your-pc/

What is a Pod?

A Pod is the smallest deployable unit of computing that you can create and manage in kubernetes. In other words a Pod could be a docker container that is running one or more containers. Look at a Pod like a logical grouping method. You can find more information here https://kubernetes.io/docs/concepts/workloads/pods/

How do we create a Pod step by step?

For creating our Pod we need a docker image and for this article we are going to create our own Docker image running php and phalcon on ubuntu 20.04 that is a little bit different from the one explained here https://brewedbrilliance.net/how-to-setup-phalcon-framework-in-a-docker-container/

but the steps are exactly the same.

Step 1: Create the docker image

Copy the below content into a file of your preference /path/to/your/folder/phalcon/Dockerfile

FROM ubuntu:20.04 as intermediate
# install the dependencies
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update && \
    apt-get install -y \
        zlib1g-dev \
        libxml2-dev\
        locales \
        g++ \
        git \
        libpcre3-dev \
        make \
        php \
        php-dev \
        re2c \
        wget
RUN locale-gen en_US.UTF-8 && \
        export LANG=en_US.UTF-8

RUN  apt-get install -y --allow-unauthenticated \
        apache2 \
        iputils-ping \
        cron \
        curl \
        libc6-i386 \
        mcrypt \
        php \
        php-apcu \
        php-curl \
        php-mbstring \
        php-mysql \
        php-soap \
        php-xml \
        unzip \
        zip
RUN git clone --depth=1 "git://github.com/phalcon/cphalcon.git"
WORKDIR cphalcon/build
RUN ./install
RUN apt-get autoremove -y && \
        apt-get clean && \
        rm -rf /var/lib/apt/lists/* && \
        echo "extension=phalcon.so" > /etc/php/7.4/mods-available/phalcon.ini && \
        pecl install xdebug && \
        echo "zend_extension=\"/usr/lib/php/20170718/xdebug.so\"" > /etc/php/7.4/mods-available/xdebug.ini && \
        cp /etc/php/7.4/mods-available/xdebug.ini /etc/php/7.4/apache2/conf.d/20-xdebug.ini && \
        phpenmod phalcon && phpenmod mbstring && \
        a2enmod rewrite && \
        apt-get purge -y \
        zlib1g-dev \
        libxml2-dev\
        locales \
        g++ \
        git \
        libpcre3-dev \
        make \
        re2c \
        wget && \
        rm -rf /cphalcon && \
        mkdir /app

WORKDIR /app
EXPOSE 80
CMD service apache2 start && \
        service cron start && \
    /bin/bash
CMD ["apache2ctl", "-D", "FOREGROUND"]

From the command line navigate to /path/to/your/folder/phalcon/ and run

docker build -t phalcon_local .

This will take few minutes and will create your local docker image called phalcon_local. When everything is finished you can run the “docker images” command and verify that the local-phalcon image is in the list.

$> docker images | grep phalcon_local
phalcon_local    latest     0789d41db40d   12 minutes ago   698MB

Step 2: Create the yaml file

If you are not familiar with kubernetes you are wondering at this stage what is the yaml file!? The yaml file is a structured file that will describe which kind of object you want to create in kubernetes. In our case will looks like something below, so just copy and paste this into a file i.e. local-kube-phalcon.yaml

apiVersion: v1
kind: Pod
metadata:
 name: phalcon-test
 namespace: default
spec:
 containers:
 - name: phalcon
   image: phalcon_local:latest
   imagePullPolicy: Never
 restartPolicy: Never

Now that we have our local-kube-phalcon.yaml on our PC all we need to do is to create the Pod

Step 3: Create the Pod

For creating the Pod we need to run this command

kubectl apply -f local-kube-phalcon.yaml

Output should be

$> kubectl apply -f local-kube-phalcon.yaml
pod/phalcon-test created

Step 4: Check the Pod

For checking the Pod status we need to run this command kubectl get pods

$> kubectl get pods
NAME               READY   STATUS             RESTARTS      AGE
phalcon-test       1/1     Running            0             7m47s

This is telling us that the pod is running, if we want to get more information we can use the describe command: kubectl describe <pod-name>

kubectl describe pod phalcon-test

Will give you as result a lot of information.

Step 5: Accessing the Pod

Everything up to here looks great, we have our pod running and is doing its own stuff (in our case is just an Apache page). But how can we access to that page? Remember, at the moment we have just an Apache page, but in reality could be anything, like some REST APIs, a database, etc. As it is now we can’t access unless we use a small trick that consists in having a port forward to the port that the Pod is exposing. For achieving this we can use this command

kubectl port-forward POD_NAME HOST_PORT:POD_PORT

That in our case would be

kubectl port-forward phalcon-test 8089:80

If you run this command from the command line, we will able to load the Apache web page by navigating to http://localhost:8089 and it will work as long that shell is open. The end result should be something like this

$> kubectl port-forward phalcon-test 8089:80
Forwarding from 127.0.0.1:8089 -> 80
Forwarding from [::1]:8089 -> 80
Handling connection for 8089
Handling connection for 8089
Screenshot-2022-03-08-at-10.54.57 Spin up a kubernetes Pod using a local image
Port-forward for accessing the Pod

If you are looking on how to import a docker image in kubernetes, checkout this article https://brewedbrilliance.net/import-a-docker-image-in-kubernetes/

If you want to have your environment setup and configured follow this article instead

If you liked this article share!

happy Spin up a kubernetes Pod using a local image
Happy
0 %
sad Spin up a kubernetes Pod using a local image
Sad
0 %
excited Spin up a kubernetes Pod using a local image
Excited
0 %
sleepy Spin up a kubernetes Pod using a local image
Sleepy
0 %
angry Spin up a kubernetes Pod using a local image
Angry
0 %
surprise Spin up a kubernetes Pod using a local image
Surprise
0 %

Share this content:

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x