Lädt...


🔧 Lesson 11 – Getting Started with TensorFlow, Docker, and Kubernetes


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Hi devs,

In this post, I'll guide you through the process of creating a simple machine learning app using TensorFlow, containerizing it with Docker, and deploying it on Kubernetes. Don't worry if you're new to these tools—I'll break everything down step by step.

Step 1: Building a Basic TensorFlow Model in Python

Let's kick things off by writing a simple TensorFlow app that trains a model using the MNIST dataset, which is a classic dataset for handwritten digit recognition.

Here’s the Python code to get started:

# app.py
import tensorflow as tf
from tensorflow.keras import datasets, layers, models

# Load and preprocess the MNIST dataset
(train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data()
train_images, test_images = train_images / 255.0, test_images / 255.0

# Build a simple convolutional neural network
model = models.Sequential([
    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
    layers.MaxPooling2D((2, 2)),
    layers.Flatten(),
    layers.Dense(64, activation='relu'),
    layers.Dense(10, activation='softmax')
])

# Compile and train the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=1)

# Evaluate the model
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f'Test accuracy: {test_acc}')

This script does a few things:

  1. Loads the MNIST dataset and normalizes it.
  2. Builds a simple convolutional neural network (CNN).
  3. Trains the model on the training data and evaluates it on the test data.

After running this code, you should see the model train for one epoch, and then it will print out the test accuracy.

Step 2: Dockerizing the TensorFlow App

Now, let's containerize this Python app using Docker. This ensures the app runs consistently across different environments.

First, create a Dockerfile in the same directory as your app.py:

# Dockerfile
FROM tensorflow/tensorflow:2.7.0

WORKDIR /app

COPY . .

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

This Dockerfile is pretty straightforward:

  • It starts from an official TensorFlow image (tensorflow/tensorflow:2.7.0).
  • Sets /app as the working directory.
  • Copies all the files in the current directory to the Docker image.
  • Runs app.py when the container starts.

To build and run the Docker container, use the following commands:

# Build the Docker image
docker build -t tensorflow-app .

# Run the Docker container
docker run tensorflow-app

Once the container starts, the TensorFlow app will run inside the container, and you'll see the output in your terminal.

Step 3: Deploying with Kubernetes

Now that our app is containerized, the next step is deploying it to a Kubernetes cluster. We’ll use a basic YAML file to describe the deployment.

Here’s a simple kubernetes.yaml for deploying the TensorFlow app:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tensorflow-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tensorflow-app
  template:
    metadata:
      labels:
        app: tensorflow-app
    spec:
      containers:
        - name: tensorflow-app
          image: tensorflow-app:latest
          ports:
            - containerPort: 80

This configuration defines:

  • A Deployment named tensorflow-app with 1 replica.
  • The app will be using the Docker image tensorflow-app:latest.
  • The app exposes port 80 for access.

To deploy this to your Kubernetes cluster, run the following:

kubectl apply -f kubernetes.yaml

This will create a deployment and run the container inside your cluster. To expose the app externally, you can create a service:

kubectl expose deployment tensorflow-app --type=LoadBalancer --port=80

Once the service is up, Kubernetes will assign an external IP (if you're using a cloud provider) to access your app.

Conclusion

This workflow is essential for scaling machine learning models in production environments, allowing you to manage and deploy models efficiently.

With these tools in your arsenal, you're well on your way to tackling more complex ML workloads and scaling them across environments. Keep experimenting, and stay tuned for more advanced topics on scaling AI with Kubernetes!

Keep Coding :)

...

🔧 Lesson 11 – Getting Started with TensorFlow, Docker, and Kubernetes


📈 58.01 Punkte
🔧 Programmierung

🔧 Docker and Kubernetes: The Backbone of Modern Application Development Part 4 (Docker and Kubernetes)


📈 32.22 Punkte
🔧 Programmierung

🔧 Getting Started with Docker, Kubernetes, and Istio on Windows


📈 32.04 Punkte
🔧 Programmierung

🔧 Getting Started with Nginx, Docker, and Kubernetes on Windows


📈 32.04 Punkte
🔧 Programmierung

🔧 Python from 0 to Hero: Lesson 1 - Getting Started with Python for HR & Payroll


📈 31.44 Punkte
🔧 Programmierung

🔧 React from 0 to Hero: Lesson 1 – Getting Started


📈 31.44 Punkte
🔧 Programmierung

🔧 Getting Started with Kubernetes: Orchestrating My Docker Containers


📈 30.73 Punkte
🔧 Programmierung

🎥 Getting Started with Distributed TensorFlow on GCP


📈 26.39 Punkte
🎥 Künstliche Intelligenz Videos

🎥 Getting started with TensorFlow Cloud


📈 26.39 Punkte
🎥 Video | Youtube

🔧 Getting Started with Machine Learning in JavaScript: A Beginner’s Guide with TensorFlow.js


📈 26.39 Punkte
🔧 Programmierung

🔧 Lesson 12 - What is TensorFlow?


📈 25.96 Punkte
🔧 Programmierung

🎥 Getting started With Monitoring and Alerting for Kubernetes


📈 24.67 Punkte
🎥 Video | Youtube

🔧 Getting Started with Docker and Node.js


📈 24.62 Punkte
🔧 Programmierung

🔧 Get Started with Docker: How to Install Docker on Ubuntu 22.04


📈 23.55 Punkte
🔧 Programmierung

🔧 Docker and Kubernetes: The Backbone of Modern Application Development Part 1 (Docker)


📈 23.48 Punkte
🔧 Programmierung

🐧 Docker users unhappy with latest forced login to download Docker and Docker Store images


📈 23.43 Punkte
🐧 Linux Tipps

🔧 Docker Advanced Concepts - Docker Compose and Docker Swarm: Day 6 of 50 days DevOps Tools Series


📈 23.43 Punkte
🔧 Programmierung

🐧 Resources for Getting Started with Kubernetes


📈 23.36 Punkte
🐧 Linux Tipps

🔧 Kubernetes: Getting Started With a Minimal MySQL Installation


📈 23.36 Punkte
🔧 Programmierung

🔧 Getting Started With Kubernetes on AWS Tutorial (2023 Update)


📈 23.36 Punkte
🔧 Programmierung

🔧 Getting started with Kubernetes: Introduction


📈 23.36 Punkte
🔧 Programmierung

🔧 Recap of "Getting Started with Kubernetes" Course


📈 23.36 Punkte
🔧 Programmierung

🔧 Tutorial: Getting Started with @kubernetes-client/node


📈 23.36 Punkte
🔧 Programmierung

🔧 Getting Started with Kubernetes: A Beginner’s Guide to Container Orchestration


📈 23.36 Punkte
🔧 Programmierung

🐧 Getting started with Docker on your VPS | Serverwise


📈 23.31 Punkte
🐧 Linux Tipps

🔧 Getting started with Docker


📈 23.31 Punkte
🔧 Programmierung

🔧 ReactJS + Docker : Getting started


📈 23.31 Punkte
🔧 Programmierung

🔧 Getting Started With NCache Java Edition (Using Docker)


📈 23.31 Punkte
🔧 Programmierung

🔧 Getting Started with Docker for AI/ML: A Beginner’s Guide


📈 23.31 Punkte
🔧 Programmierung

🔧 Getting Started with Docker: A Guide for Developers


📈 23.31 Punkte
🔧 Programmierung

🔧 Getting Started with Docker.


📈 23.31 Punkte
🔧 Programmierung

🔧 Dive Into Docker: A Hands-On Lab For Getting Started


📈 23.31 Punkte
🔧 Programmierung

🔧 Day 1 of My DevOps Journey: Getting Started with Docker 🐳


📈 23.31 Punkte
🔧 Programmierung

🔧 Getting Started with Docker: Essential Commands for Beginners


📈 23.31 Punkte
🔧 Programmierung

matomo