Files
rails-app/k8s/deployment.yaml
2026-02-04 23:11:52 +08:00

131 lines
4.5 KiB
YAML

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'
<!DOCTYPE html>
<html>
<head>
<title>Rails 8.1 on K3s</title>
<style>
body { font-family: Arial, sans-serif; margin: 0; padding: 0; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); display: flex; justify-content: center; align-items: center; min-height: 100vh; }
.container { background: white; padding: 40px; border-radius: 10px; box-shadow: 0 10px 40px rgba(0,0,0,0.2); text-align: center; max-width: 600px; }
h1 { color: #667eea; margin-bottom: 20px; }
.status { background: #10b981; color: white; padding: 10px 20px; border-radius: 5px; display: inline-block; margin: 20px 0; }
.info { text-align: left; background: #f3f4f6; padding: 20px; border-radius: 5px; margin-top: 20px; }
.info p { margin: 10px 0; }
.emoji { font-size: 48px; margin-bottom: 20px; }
</style>
</head>
<body>
<div class="container">
<div class="emoji">💎</div>
<h1>Rails <%= @rails_version %> on K3s</h1>
<div class="status">✅ 运行正常</div>
<div class="info">
<p><strong>Ruby版本:</strong> <%= @ruby_version %></p>
<p><strong>Rails版本:</strong> <%= @rails_version %></p>
<p><strong>数据库:</strong> SQLite3</p>
<p><strong>Pod主机名:</strong> <%= @hostname %></p>
<p><strong>访问域名:</strong> r1.jpd.net3w.com</p>
<p><strong>部署方式:</strong> GitOps (Gitea + ArgoCD)</p>
<p><strong>集群:</strong> JPD K3s Cluster</p>
</div>
<p style="margin-top: 20px; color: #6b7280;"><%= Time.now.strftime("%Y-%m-%d %H:%M:%S") %></p>
</div>
</body>
</html>
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: 600
periodSeconds: 10
timeoutSeconds: 5
failureThreshold: 3
readinessProbe:
httpGet:
path: /up
port: 3000
initialDelaySeconds: 300
periodSeconds: 10
timeoutSeconds: 5
failureThreshold: 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: {}