Rails 8.1 + SQLite app - 2026-02-04 22:51:42
This commit is contained in:
128
k8s/deployment.yaml
Normal file
128
k8s/deployment.yaml
Normal file
@@ -0,0 +1,128 @@
|
||||
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: 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: {}
|
||||
47
k8s/ingress.yaml
Normal file
47
k8s/ingress.yaml
Normal file
@@ -0,0 +1,47 @@
|
||||
apiVersion: networking.k8s.io/v1
|
||||
kind: Ingress
|
||||
metadata:
|
||||
name: rails-app-http
|
||||
namespace: rails-app
|
||||
annotations:
|
||||
traefik.ingress.kubernetes.io/router.entrypoints: web
|
||||
spec:
|
||||
ingressClassName: traefik
|
||||
rules:
|
||||
- host: r1.jpd.net3w.com
|
||||
http:
|
||||
paths:
|
||||
- path: /
|
||||
pathType: Prefix
|
||||
backend:
|
||||
service:
|
||||
name: rails-app
|
||||
port:
|
||||
number: 80
|
||||
---
|
||||
apiVersion: networking.k8s.io/v1
|
||||
kind: Ingress
|
||||
metadata:
|
||||
name: rails-app-https
|
||||
namespace: rails-app
|
||||
annotations:
|
||||
cert-manager.io/cluster-issuer: "letsencrypt-prod"
|
||||
traefik.ingress.kubernetes.io/router.entrypoints: websecure
|
||||
traefik.ingress.kubernetes.io/router.tls: "true"
|
||||
spec:
|
||||
ingressClassName: traefik
|
||||
tls:
|
||||
- hosts:
|
||||
- r1.jpd.net3w.com
|
||||
secretName: rails-app-tls
|
||||
rules:
|
||||
- host: r1.jpd.net3w.com
|
||||
http:
|
||||
paths:
|
||||
- path: /
|
||||
pathType: Prefix
|
||||
backend:
|
||||
service:
|
||||
name: rails-app
|
||||
port:
|
||||
number: 80
|
||||
4
k8s/namespace.yaml
Normal file
4
k8s/namespace.yaml
Normal file
@@ -0,0 +1,4 @@
|
||||
apiVersion: v1
|
||||
kind: Namespace
|
||||
metadata:
|
||||
name: rails-app
|
||||
12
k8s/pvc.yaml
Normal file
12
k8s/pvc.yaml
Normal file
@@ -0,0 +1,12 @@
|
||||
apiVersion: v1
|
||||
kind: PersistentVolumeClaim
|
||||
metadata:
|
||||
name: rails-sqlite-pvc
|
||||
namespace: rails-app
|
||||
spec:
|
||||
accessModes:
|
||||
- ReadWriteOnce
|
||||
resources:
|
||||
requests:
|
||||
storage: 5Gi
|
||||
storageClassName: local-path
|
||||
16
k8s/service.yaml
Normal file
16
k8s/service.yaml
Normal file
@@ -0,0 +1,16 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: rails-app
|
||||
namespace: rails-app
|
||||
labels:
|
||||
app: rails-app
|
||||
spec:
|
||||
type: ClusterIP
|
||||
ports:
|
||||
- port: 80
|
||||
targetPort: 3000
|
||||
protocol: TCP
|
||||
name: http
|
||||
selector:
|
||||
app: rails-app
|
||||
Reference in New Issue
Block a user