apiVersion: apps/v1 kind: Deployment metadata: name: rails-app namespace: rails-app labels: app: rails-app spec: replicas: 1 selector: matchLabels: app: rails-app template: metadata: labels: app: rails-app spec: containers: - name: rails image: ruby:3.3-slim imagePullPolicy: IfNotPresent command: ["/bin/bash", "-c"] args: - | apt-get update -qq && apt-get install -y build-essential libsqlite3-dev nodejs npm git curl gem install rails -v '8.1.0' --no-document cd /app if [ ! -f config/database.yml ]; then rails new . --force --database=sqlite3 --skip-git --skip-bundle fi bundle install rails db:create db:migrate || true # 创建欢迎页面 cat > app/controllers/welcome_controller.rb <<'RUBY' class WelcomeController < ApplicationController def index @hostname = Socket.gethostname @rails_version = Rails.version @ruby_version = RUBY_VERSION end end RUBY mkdir -p app/views/welcome cat > app/views/welcome/index.html.erb <<'HTML' Rails 8.1 on K3s
💎

Rails <%= @rails_version %> on K3s

✅ 运行正常

Ruby版本: <%= @ruby_version %>

Rails版本: <%= @rails_version %>

数据库: SQLite3

Pod主机名: <%= @hostname %>

访问域名: r1.jpd.net3w.com

部署方式: GitOps (Gitea + ArgoCD)

集群: JPD K3s Cluster

<%= Time.now.strftime("%Y-%m-%d %H:%M:%S") %>

HTML cat > config/routes.rb <<'RUBY' Rails.application.routes.draw do root "welcome#index" get "up" => "rails/health#show", as: :rails_health_check end RUBY rails server -b 0.0.0.0 -p 3000 ports: - containerPort: 3000 name: http env: - name: RAILS_ENV value: "development" - name: RAILS_LOG_TO_STDOUT value: "true" volumeMounts: - name: sqlite-storage mountPath: /app/db - name: app-storage mountPath: /app livenessProbe: httpGet: path: /up port: 3000 initialDelaySeconds: 60 periodSeconds: 10 timeoutSeconds: 5 readinessProbe: httpGet: path: /up port: 3000 initialDelaySeconds: 30 periodSeconds: 5 timeoutSeconds: 3 resources: requests: memory: "512Mi" cpu: "250m" limits: memory: "1Gi" cpu: "1000m" volumes: - name: sqlite-storage persistentVolumeClaim: claimName: rails-sqlite-pvc - name: app-storage emptyDir: {}