一、配置用于存储的NFS服务
1.1服务端配置 nfs
#yum安装
[root@k8smaster lnmp]# yum -y install nfs-utils
#编辑 配置文件 (确保目录存在)
[root@k8smaster lnmp]# vim /etc/exports
/data/lnmp 10.7.0.0/16(rw,no_root_squash)
#启动服务
[root@k8smaster lnmp]# systemctl start nfs
[root@k8smaster lnmp]# systemctl status nfs
● nfs-server.service - NFS server and services
Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled)
Drop-In: /run/systemd/generator/nfs-server.service.d
└─order-with-mounts.conf
Active: active (exited) since Sat 2021-09-11 17:32:16 HKT; 1 weeks 0 days ago
Main PID: 21698 (code=exited, status=0/SUCCESS)
Tasks: 0
Memory: 0B
CGroup: /system.slice/nfs-server.service
#showmount确认
[root@k8smaster lnmp]# showmount -e
Export list for k8smaster:
/data/lnmp 10.7.0.0/16
1.2客户端挂载服务
#安装nfs客户端
[root@k8snode ~]# yum -y install nfs-utils.x86_64
#showmount -e查看
[root@k8snode ~]# showmount -e 10.7.48.223
Export list for 10.7.48.223:
/data/lnmp 10.7.0.0/16
#创建挂载点挂载
[root@k8snode data]# mkdir /data
[root@k8snode data]# mount -t nfs 10.7.48.223/data/lnmp /data
二 、配置LNMP环境
2.1mysql配置清单
配置前先创建需要的命名空间
[root@k8smaster lnmp]# kubectl create ns lnmp
mysql清单配置如下
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
replicas: 1
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:5.7
imagePullPolicy: IfNotPresent
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: lnmp-mysql
mountPath: /var/lib/mysql
volumes:
- name: lnmp-mysql
nfs:
server: 10.7.48.223 #nfs server服务器IP
path: "/data/lnmp/mysql"
---
apiVersion: v1
kind: Service
metadata:
name: mysql
labels:
app: mysql
spec:
ports:
- name: mysql
port: 3306
targetPort: 3306
selector:
app: mysql
这里面mysql 密码是通过环境 变量的方式导入的密码配置参考下面方法
kubectl create secret generic mysql-pass --from-literal=password=123456 -n lnmp
#启动
[root@k8smaster lnmp]# kubectl apply -f mysql.yaml -n lnmp
deployment.apps/mysql created
service/mysql unchanged
#查看pod状态
[root@k8smaster lnmp]# kubectl get po -n lnmp
NAME READY STATUS RESTARTS AGE
mysql-6cc8488986-2wf62 1/1 Running 0 13m
#查看svc地址并确认端口连通性
[root@k8smaster lnmp]# kubectl get svc -n lnmp
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql ClusterIP 10.97.108.119 <none> 3306/TCP 2d20h
[root@k8smaster lnmp]# telnet 10.97.108.119 3306
Trying 10.97.108.119...
Connected to 10.97.108.119.
Escape character is '^]'
#端口可以正常访问mysql的配置就是正常的
2.2 nginx+php配置清单
清单文件如下
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-php
spec:
selector:
matchLabels:
app: nginx-php
replicas: 1
template:
metadata:
labels:
app: nginx-php
spec:
containers:
- name: php
image: registry.cn-shenzhen.aliyuncs.com/user-sum/php
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9000
volumeMounts:
- name: nginx-data
mountPath: /var/www/html/
- name: nginx
image: registry.cn-shenzhen.aliyuncs.com/user-sum/alpine:nginx1.18.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
volumeMounts:
- name: nginx-data
mountPath: /usr/share/nginx/html
- name: nginx-conf
mountPath: /etc/nginx/conf.d/
volumes:
- name: nginx-data
nfs:
server: 10.7.48.223 #nfs server服务器IP
path: "/data/lnmp/app"
- name: nginx-conf
configMap:
name: ngin-conf
items:
- key: default.conf
path: add.conf
---
apiVersion: v1
kind: Service
metadata:
name: nginx-php
spec:
type: NodePort
ports:
- name: nginx
port: 80
protocol: TCP
targetPort: 80
nodePort: 30010
selector:
app: nginx-php
nginx服务配置文件
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
include fastcgi_params;
}
}
#创建configmap
[root@k8smaster lnmp]# kubectl create cm ngin-conf --from-file=default.conf -n lnmp
#启动
[root@k8smaster lnmp]# kubectl apply -f nginx-php.yaml -n lnmp
2.3测试
测试 php连通性
#创建测试文件测试
[root@k8snode app]# cat test.php
<?php phpinfo(); ?>
测试 mysql连通性
[root@k8snode app]# cat mysql.php
<h1>Test php-mysql </h1>
<?php
mysqli_connect('10.97.108.119','root','123456') or die('failed');
echo 'success';
phpinfo();
?>
至此配置完成后面就可以吧自己的项目放到nginx工作目录愉快的玩耍了(๑•̀ㅁ•́ฅ)