Getting Started with Containers and Kubernetes

Getting Started with Containers and Kubernetes

This Kubernetes tutorial explains how to harness Kubernetes Meetup Kit technologies and deploy the “Hello World” Flask app onto a DigitalOcean Kubernetes cluster. Getting Started with Containers and Kubernetes. Getting Started with Kubernetes Meetup Kit. Containers offer the same process isolation, but are generally more lightweight, portable, and performant than full virtualization. Container clusters, which can be used to manage thousands of running containers across a set of physical machines, abstract away much of the work of rolling out new versions of applications, scaling them, and efficiently scheduling workloads. Out of these, Kubernetes has emerged as a mature, production-ready system.

Introduction

In the past decade, containerized applications and container clusters have rapidly replaced the old paradigm of scaling applications using virtual machines. Containers offer the same process isolation, but are generally more lightweight, portable, and performant than full virtualization. Container clusters, which can be used to manage thousands of running containers across a set of physical machines, abstract away much of the work of rolling out new versions of applications, scaling them, and efficiently scheduling workloads. Out of these, Kubernetes has emerged as a mature, production-ready system. It provides a rich set of features like rolling deployments, health checking, self-monitoring, workload autoscaling, and much, much more.

This tutorial, designed to accompany the Slides and speaker notes for the Getting Started with Kubernetes Meetup Kit, will show you how to harness these technologies and deploy the “Hello World” Flask app onto a DigitalOcean Kubernetes cluster.

Step 1 — Cloning the App Repository and Building the Flask Image

To begin, clone the demo Flask app repo onto your machine, navigate into the directory, and list the directory contents:

git clone https://github.com/do-community/k8s-intro-meetup-kit.git
cd k8s-intro-meetup-kit
ls
OutputLICENSE  README.md  app  k8s

The app directory contains the Flask demo app code, as well as the Dockerfile for building its container image. The k8s directory contains Kubernetes manifest files for a Pod, Deployment, and Service.

Navigate into the app directory and print out the contents of the app.py file:

cat app.py
Outputfrom flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0')

This code defines a single default route that will print “Hello World.” Additionally, the apps runs in debug mode to enable verbose output.

In a similar fashion, cat out the contents of the app’s Dockerfile:

cat Dockerfile
OutputFROM python:3-alpine

WORKDIR /app

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .

EXPOSE 5000

CMD ["python", "app.py"]

This Dockerfile first sources a lightweight Alpine Linux Python parent image. It then copies in the Python requirements file, installs Flask, copies the app code into the container image, defines port 5000 as the container port, and finally sets the default command to python app.py.

Next, build the app image:

cd app
docker build -t flask_demo:v0 .

We give the image a name, flask_demo, and tag, v0 using the -t option.

After Docker finishes the build, run the container using run:

docker run -p 5000:5000 flask:v0

This command runs a container using the flask:v0 image, and forwards local port 5000 to container port 5000.

If you’re running Docker on your local machine, navigate to http://localhost:5000 in your web browser. You should see “Hello World,” generated by the dockerized Flask app.

If you’re running Docker on a dev server, navigate instead to http://<span class="highlight">dev_server_external_IP</span>:5000. If you’re running a firewall like UFW, be sure to allow external access on port 5000.

At this point you can experiment with Docker commands like docker ps, docker top, and docker images to practice working with images and containers on your system.

In the next step, we’ll deploy this demo app to your Kubernetes cluster. We’ll use a prebuilt image shared publicly on Docker Hub. If you’d like to customize the Flask app and use your own image, you should create a Docker Hub account and follow the steps in this introduction to push your image to a public repository. From there, Kubernetes will be able to pull and deploy the container image into your cluster.

Step 2 — Deploying the Flask App on Kubernetes

The app and Docker image described in the previous step have already been built and made publicly available in the flask-helloworld Docker Hub repository. You can optionally create your own repository for the app and substitute it for flask-helloworld throughout this step.

We’ll first deploy this demo “Hello World” app into our cluster as a standalone Pod, then as a multi-pod Deployment, which we’ll finally expose as a LoadBalancer Service. At the end of this tutorial, the “Hello World” app will be publicly accessible from outside of the Kubernetes cluster.

Before we launch any workloads into the cluster, we’ll create a Namespace in which the objects will run. Namespaces allow you to segment your cluster and limit scope for running workloads.

Create a Namespace called flask:

kubectl create namespace flask

Now, list all the Namespaces in your cluster:

kubectl get namespace

You should see your new Namespace as well as some default Namespaces like kube-system and default. In this tutorial, we are going to exclusively work within the flask Namespace.

Navigate back out to the k8s directory in the demo repo:

cd ../k8s

In this directory, you’ll see three Kubernetes manifest files:

  • flask-pod.yaml: The app Pod manifest
  • flask-deployment.yaml: The app Deployment manifest
  • flask-service.yaml: The app LoadBalancer Service manifest

Let’s take a look at the Pod manifest:

cat flask-pod.yaml
OutputapiVersion: v1
kind: Pod
metadata:
  name: flask-pod
  labels:
    app: flask-helloworld
spec:
  containers:
  - name: flask
    image: hjdo/flask-helloworld:latest
    ports:
    - containerPort: 5000

Here, we define a minimal Pod called flask-pod and label it with the app: flask-helloworld key-value pair.

We then name the single container flask and set the image to flask-helloworld:latest from the hjdo/flask-helloworld Docker Hub repository. If you’re using an image stored in a different Docker Hub repo, you can reference it using the image field here. Finally, we open up port 5000 to accept incoming connections.

Deploy this Pod into the flask Namespace using kubectl apply -f and the -n Namespace flag:

kubectl apply -f flask-pod.yaml -n flask

After ten or so seconds, the Pod should be up and running in your cluster:

kubectl get pod -n flask
OutputNAME        READY   STATUS    RESTARTS   AGE
flask-pod   1/1     Running   0          4s

Since this Pod is running inside of the Kubernetes cluster, we need to forward a local port to the Pod’s containerPort to access the running app locally:

kubectl port-forward pods/flask-pod -n flask 5000:5000

Here we use port-forward to forward local port 5000 to the Pod’s containerPort 5000.

Navigate to http://localhost:5000, where you should once again see the “Hello World” text generated by the Flask app. If you’re running kubectl on a remote dev server, replace localhost with your dev server’s external IP address.

Feel free to play around with kubectl commands like kubectl describe to explore the Pod resource. When you’re done, delete the Pod using delete:

kubectl delete pod flask-pod -n flask

Next, we’ll roll out this Pod in a scalable fashion using the Deployment resource. Print out the contents of the flask-deployment.yaml manifest file:

cat flask-deployment.yaml
OutputapiVersion: apps/v1
kind: Deployment
metadata:
  name: flask-dep
  labels:
    app: flask-helloworld
spec:
  replicas: 2
  selector:
    matchLabels:
      app: flask-helloworld
  template:
    metadata:
      labels:
        app: flask-helloworld
    spec:
      containers:
      - name: flask
        image: hjdo/flask-helloworld:latest
        ports:
        - containerPort: 5000

Here, we define a Deployment called flask-dep with an app: flask-helloworld Label. Next, we request 2 replicas of a Pod template identical to the template we previously used to deploy the Flask app Pod. The selector field matches the app: flask-helloworld Pod template to the Deployment.

Roll out the Deployment using kubectl apply -f:

kubectl apply -f flask-deployment.yaml -n flask

After a brief moment, the Deployment should be up and running in your cluster:

kubectl get deploy -n flask
OutputNAME        READY   UP-TO-DATE   AVAILABLE   AGE
flask-dep   2/2     2            2           5s

You can also pull up the individual Pods that are managed by the Deployment controller:

kubectl get pod -n flask
OutputNAME                        READY   STATUS    RESTARTS   AGE
flask-dep-876bd7677-bl4lg   1/1     Running   0          76s
flask-dep-876bd7677-jbfpb   1/1     Running   0          76s

To access the app, we have to forward a port inside of the cluster:

kubectl port-forward deployment/flask-dep -n flask 5000:5000

This will forward local port 5000 to containerPort 5000 on one of the running Pods.

You should be able to access the app at http://localhost:5000. If you’re running kubectl on a remote dev server, replace localhost with your dev server’s external IP address.

At this point you can play around with commands like kubectl rollout and kubectl scale to experiment with rolling back Deployments and scaling them.

In the final step, we’ll expose this app to outside users using the LoadBalancer Service type, which will automatically provision a DigitalOcean cloud Load Balancer for the Flask app Service.

Step 3 — Creating the App Service

A Kubernetes Deployment allows the operator to flexibly scale a Pod template up or down, as well as manage rollouts and template updates. To create a stable network endpoint for this set of running Pod replicas, you can create a Kubernetes Service, which we’ll do here.

Begin by inspecting the Service manifest file:

cat flask-service.yaml
OutputapiVersion: v1
kind: Service
metadata:
  name: flask-svc
  labels:
    app: flask-helloworld
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 5000
    protocol: TCP
  selector:
    app: flask-helloworld

This manifest defines a Service called flask-svc. We set the type to LoadBalancer to provision a DigitalOcean cloud Load Balancer that will route traffic to the Deployment Pods. To select the already running Deployment, the selector field is set to the Deployment’s app: flask-helloworld Label. Finally, we open up port 80 on the Load Balancer and instruct it to route traffic to the Pods’ containerPort 5000.

To create the Service, use kubectl apply -f:

kubectl apply -f flask-service.yaml -n flask

It may take a bit of time for Kubernetes to provision the cloud Load Balancer. You can track progress using the -w watch flag:

kubectl get svc -w

Once you see an external IP for the flask-svc Service, navigate to it using your web browser. You should see the “Hello World” Flask app page.

Conclusion

This brief tutorial demonstrates how to containerize a minimal Flask app and deploy it to a Kubernetes cluster. It accompanies the meetup kit’s slides and speaker notes and GitHub repository.

Originally published by Hanif Jetha at https://www.digitalocean.com

Kubernetes Docker DevOps Python

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

DevOps Tutorial - Docker, Kubernetes, and Azure DevOps

In this article, we break down everything you need to know about DevOps, so that you can get started building your own CI/CD pipeline. DevOps Tutorial - Docker, Kubernetes, and Azure DevOps

Kubernetes vs Docker

Get Hands-on experience on Kubernetes and the best comparison of Kubernetes over the DevOps at your place at Kubernetes training

Docker vs. Kubernetes | Docker vs. Kubernetes Difference

Docker and Kubernetes are two orchestration tools that very popular. Many people have trouble picking one. In this video on Docker vs Kubernetes, we will be comparing these two tools end to end and see which one will suit your needs better.

DevOps and Docker Live Show (Ep 91)

Join me with guest Docker Captain Elton Stoneman to talk about the state of Docker Desktop and Docker Hub. Support this show on Patreon! It's the #1 way to support me interviewing DevOps and container experts, and doing this Live Q&A.

Docker manifest - A peek into image's manifest.json files

The docker manifest command does not work independently to perform any action. In order to work with the docker manifest or manifest list, we use sub-commands along with it. This manifest sub-command can enable us to interact with the image manifests. Furthermore, it also gives information about the OS and the architecture, that a particular image was built for. The image manifest provides a configuration and a set of layers for a container image. This is an experimenta