跳到主要内容

部署

2024年08月29日
柏拉文
越努力,越幸运

一、认识


Kubernetes (k8s) 中部署 Node.js 服务以适应生产环境,涉及几个关键方面:高可用性、可扩展性、持久性、日志管理和配置管理。以下是一个生产环境级别的 Node.js 服务部署示例,包括使用 DeploymentServiceConfigMapSecretPersistentVolumePersistentVolumeClaim(如需要)等资源。

二、镜像


三、部署


3.1 配置 ConfigMap

如果你的 Node.js 应用有配置文件,可以将其存储在 ConfigMap 中,以便在 Pod 启动时读取。

apiVersion: v1
kind: ConfigMap
metadata:
name: node-config
data:
config.json: |
{
"app": "production",
"port": 8080
}

3.2 配置 PV 和 PVC

如果 Node.js 应用需要持久化存储(例如日志),你可以创建 PersistentVolumePersistentVolumeClaim

apiVersion: v1
kind: PersistentVolume
metadata:
name: node-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /mnt/data/node
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: node-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi

3.3 配置 Secret

Secret 用于存储敏感数据,如数据库密码或 API 密钥。

apiVersion: v1
kind: Secret
metadata:
name: node-secrets
type: Opaque
data:
db-password: cGFzc3dvcmQ= # base64 encoded password

3.4 配置 Deployment

Deployment 中配置容器镜像、环境变量、资源请求和限制、以及卷挂载等。

apiVersion: apps/v1
kind: Deployment
metadata:
name: node-deployment
spec:
replicas: 3 # 高可用性设置多个副本
selector:
matchLabels:
app: node
template:
metadata:
labels:
app: node
spec:
containers:
- name: node
image: your-docker-registry/your-node-image:latest
ports:
- containerPort: 8080
env:
- name: NODE_ENV
value: "production"
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: node-secrets
key: db-password
volumeMounts:
- name: config
mountPath: /app/config/config.json
subPath: config.json
- name: logs
mountPath: /app/logs
volumes:
- name: config
configMap:
name: node-config
- name: logs
persistentVolumeClaim:
claimName: node-pvc
resources:
requests:
memory: "256Mi"
cpu: "100m"
limits:
memory: "512Mi"
cpu: "500m"
volumeMounts:

3.5 配置 Service

创建 Service 以便将 Node.js 服务暴露到集群外部。

apiVersion: v1
kind: Service
metadata:
name: node-service
spec:
type: LoadBalancer # 使用 LoadBalancer 以便在云环境中分配公共 IP,或者使用 NodePort
ports:
- port: 80
targetPort: 8080
selector:
app: node

3.6 配置 HorizontalPodAutoscaler

配置 HorizontalPodAutoscaler 设置自动扩缩容,以应对流量变化。

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: node-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: node-deployment
minReplicas: 2
maxReplicas: 5
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 80

3.7 应用以上配置

使用 kubectl apply -f 命令来将以上 yaml 文件应用到 Kubernetes 集群中。

kubectl apply -f node-configmap.yaml
kubectl apply -f node-secret.yaml
kubectl apply -f node-deployment.yaml
kubectl apply -f node-service.yaml
kubectl apply -f node-hpa.yaml
kubectl apply -f node-pv-pvc.yaml # 如果使用了 PVC

使用 kubectl delete -f 命令来将以上应用删除

kubectl delete -f node-configmap.yaml
kubectl delete -f node-secret.yaml
kubectl delete -f node-deployment.yaml
kubectl delete -f node-service.yaml
kubectl delete -f node-hpa.yaml
kubectl delete -f node-pv-pvc.yaml # 如果使用了 PVC

使用 kubectl get pods 来检查 Pod 状态

kubectl get pods 

使用 kubectl describe pod nginx 来查看 Pod 运行日志

kubectl describe pod nginx