1. 部署nfs
yum install nfs-utils -y node节点也要安装
[root@nfs ~]# vim /etc/exports
/data/ 192.168.199.0/24(rw,no_root_squash)
mkdir /data/{v1,v2,v3} -p
systemctl start nfs
systemctl enable nfs
showmount -e 192.168.199.24 查看
2. 部署pv
[root@k8s-master lnmp]# vim mypv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv001
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs-mysql
nfs:
path: /data/v1/
server: 192.168.199.24
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv002
spec:
capacity:
storage: 3Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs-nginx
nfs:
path: /data/v2/
server: 192.168.199.24
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv003
spec:
capacity:
storage: 3Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs-php
nfs:
path: /data/v3/
server: 192.168.199.24
3. 部署pvc
[root@k8s-master lnmp]# vim mypvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mysql-pvc
spec:
storageClassName: nfs-mysql
accessModes:
- ReadWriteMany
resources:
requests:
storage: 8Gi
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: nginx-pvc
spec:
storageClassName: nfs-nginx
accessModes:
- ReadWriteMany
resources:
requests:
storage: 3Gi
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: php-pvc
spec:
storageClassName: nfs-php
accessModes:
- ReadWriteMany
resources:
requests:
storage: 3Gi
4. 部署mysql5.7
[root@k8s-master lnmp]# vim configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: myconfig
namespace: default
data:
rootpassword: admin888
dbname: word
dbuser: word
dbpassword: word
[root@k8s-master lnmp]# vim mysql.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: mysql
name: mysql
spec:
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:5.7
name: mysql
ports:
- containerPort: 3306
volumeMounts:
- name: nfs-vol
mountPath: /var/lib/mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
configMapKeyRef:
name: myconfig
key: rootpassword
- name: MYSQL_DATABASE
valueFrom:
configMapKeyRef:
name: myconfig
key: dbname
- name: MYSQL_USER
valueFrom:
configMapKeyRef:
name: myconfig
key: dbuser
- name: MYSQL_PASSWORD
valueFrom:
configMapKeyRef:
name: myconfig
key: dbpassword
volumes:
- name: nfs-vol
persistentVolumeClaim:
claimName: mysql-pvc
kubectl -f configmap.yaml
kubectl -f mysql.yaml mysql比较大 启动很慢
5. 部署php7.1-fpm 默认不支持mysqli
Docker php:7.1-fpm 添加 mysqli 扩展
git clone https://github.com/docker-library/php.git
mkdir php/7.1/stretch/fpm-mysqli -p
cd php/7.1/stretch/fpm-mysqli
[root@k8s-node1 fpm-mysqli]# vim Dockerfile
FROM php:7.1-fpm
RUN docker-php-ext-install -j$(nproc) mysqli
docker build -t repo:7.1-fpm-mysqli
docker save repo:7.1-fpm-mysqli >/tmp/php-7.1.tar scp拷贝其他node节点导入
docker image load -i /tmp/php-7.1.tar
[root@k8s-master lnmp]# vim php.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: php
name: php
spec:
replicas: 1
selector:
matchLabels:
app: php
template:
metadata:
labels:
app: php
spec:
containers:
- image: repo:7.1-fpm-mysqli
name: php
ports:
- containerPort: 9000
name: php
volumeMounts:
- name: php-persistent-storage
mountPath: /var/www/html
volumes:
- name: php-persistent-storage
persistentVolumeClaim:
claimName: php-pvc
---
apiVersion: v1
kind: Service
metadata:
name: php
labels:
app: php
spec:
ports:
- port: 9000
selector:
app: php
kubectl apply -f php.yaml 先启动php镜像 不然nginx会找不到php镜像 会启动不起来
6. 部署nginx
[root@k8s-master lnmp]# vim default.conf
server {
listen 80;
server_name localhost;
root /var/www/html;
index index.php index.index;
location ~ \.php$ {
root /var/www/html;
fastcgi_pass php:9000; ##指定php service名称
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
include fastcgi_params;
fastcgi_connect_timeout 60s;
fastcgi_read_timeout 300s;
fastcgi_send_timeout 300s;
}
}
kubectl create configmap nginx-config --from-file=default.conf
[root@k8s-master lnmp]# vim nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
ports:
- containerPort: 80
name: nginx
volumeMounts:
- name: nfs-nginx
mountPath: /var/www/html
- name: config-volume
mountPath: /etc/nginx/conf.d/default.conf
subPath: default.conf
volumes:
- name: nfs-nginx
persistentVolumeClaim:
claimName: nginx-pvc
- name: config-volume
configMap:
name: nginx-config
---
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
selector:
app: nginx
type: NodePort
sessionAffinity: ClientIP
kubectl apply -f nginx.yaml
[root@k8s-master lnmp]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
blog-mysql ClusterIP 10.102.199.104 <none> 3306/TCP 122m
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d17h
nginx NodePort 10.104.174.147 <none> 80:31847/TCP 30m
php ClusterIP 10.104.123.91 <none> 9000/TCP 31m
http://192.168.199.102:31847 可以访问了
默认/data/v1 mysql
/data/v2 nginx 静态页面
/data/v3 php 动态页面
[root@nfs v3]# cat /tmp/info.php
<?php
phpinfo();
?>
http://192.168.199.102:31847/info.php
git clone https://gitee.com/Discuz/DiscuzX
cp /root/DiscuzX/upload/* /data/v3
chmod 777 -R /data/v3
http://192.168.199.102:31847/index.php
数据库主机名:blog-mysql 其他根据上面mysql中的configmap设置的填写