使用deployment部署lnmp
时间: 2023-11-15 17:06:36 浏览: 168
LNMP的部署
LNMP是Linux + Nginx + MySQL + PHP的缩写,是一种常见的Web服务器架构。下面是使用deployment部署LNMP的步骤:
1. 创建一个deployment对象
```
apiVersion: apps/v1
kind: Deployment
metadata:
name: lnmp
spec:
selector:
matchLabels:
app: lnmp
replicas: 2
template:
metadata:
labels:
app: lnmp
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
- name: php-fpm
image: php:latest-fpm
ports:
- containerPort: 9000
```
2. 创建一个service对象
```
apiVersion: v1
kind: Service
metadata:
name: lnmp
spec:
selector:
app: lnmp
ports:
- name: http
port: 80
targetPort: 80
- name: php
port: 9000
targetPort: 9000
```
3. 创建一个存储MySQL密码的Secret对象
```
apiVersion: v1
kind: Secret
metadata:
name: mysql-secret
type: Opaque
data:
password: base64-encoded-password
```
4. 创建一个deployment对象来启动MySQL容器
```
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
replicas: 1
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secret
key: password
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pvc
```
5. 创建一个PersistentVolumeClaim对象来提供持久化存储
```
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
```
6. 创建一个service对象来暴露MySQL服务
```
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
selector:
app: mysql
ports:
- name: mysql
port: 3306
targetPort: 3306
```
以上步骤完成后,LNMP架构就可以在Kubernetes集群中运行了。需要注意的是,以上示例中涉及到的镜像、端口号、存储大小等各项参数需要根据实际情况进行调整。
阅读全文