K8s---Pod搭建LNMP(三种方法)
1、同一pod下的nginx+php+mysql
vim nginx+php_mysql.yaml
---
apiVersion: v1
kind: Secret
metadata:
name: mysql-pass
namespace: default
type: Qpaque
data:
#echo -n "Passwd123" |base64
password: UGFzc3dkMTIz
#echo -n "tmp" |base64
database: dG1w
#echo -n "123" |base64
passwd: MTIz
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-php-mysql
spec:
selector:
matchLabels:
app: nginx-php-mysql
replicas: 1
template:
metadata:
labels:
app: nginx-php-mysql
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: hqq365.com:8443/lmnp/nginx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
volumeMounts:
- name: nginx-data
mountPath: /usr/share/nginx/html
- name: nginx-conf
mountPath: /etc/nginx/conf.d/
- image: hqq365.com:8443/lmnp/mysql:5.7
imagePullPolicy: IfNotPresent
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
#kubectl create secret generic mysql-pass --from-literal=password=Passwd123
name: mysql-pass
key: password
- name: MYSQL_DATABASE
valueFrom:
secretKeyRef:
#kubectl create secret generic mysql-pass --from-literal=database=tmp
name: mysql-pass
key: database
- name: MYSQL_USER
value: sun
- name: MYSQL_PASSWORD
valueFrom:
secretKeyRef:
#kubectl create secret generic mysql-pass --from-literal=passwd=123
name: mysql-pass
key: passwd
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: nginx-data
nfs:
server: 192.168.222.145 #nfs server服务器IP
path: "/data/k8s/web"
#kubectl create cm ngin-conf --from-file=/conf/default.conf
- name: nginx-conf
configMap:
name: ngin-conf
items:
- key: default.conf
path: add.conf
- name: mysql-persistent-storage
emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
name: nginx-php-mysql
spec:
type: NodePort
ports:
- name: nginx
port: 80
protocol: TCP
targetPort: 80
nodePort: 30010
selector:
app: nginx-php-mysql
master安装nfs(注意work节点也要安装nfs并启动)
mkidr -p /data/k8s/web
# yum -y install nfs-utils
# systemctl start nfs
# echo "/data/k8s/ 192.168.222.0/24(rw,sync,no_root_squash)" >> /etc/exports
# systemctl restart nfs
# echo "test" > /data/k8s/web/index.html
# cat /data/k8s/web/index.php
<?php
phpinfo();
?>
配置文件default.conf
cat /conf/default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.php 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;
}
}
kubectl create cm ngin-conf --from-file=/conf/default.conf
注意:采用configMap的挂载方式,如果修改了文件,需要删除configMap并重新创建
创建:kubectl create cm ngin-conf --from-file=/conf/default.conf
查看:kubectl describe cm
删除:kubectl delete configmaps ngin-conf
cm=configmaps(简称)
kubectl apply -f nginx+php+mysql.yaml
2、nginx+php同一个Pod、mysql另一个Pod
---
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: 192.168.10.10 #nfs server服务器IP
path: "/www"
- 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
kubectl apply -f nginx+php.yaml
配置文件default.conf(和1一样,不用修改)
vim mysql.yaml
---
apiVersion: v1
kind: Secret
metadata:
name: mysql-pass
namespace: default
type: Qpaque
data:
#echo -n "Passwd123" |base64
password: UGFzc3dkMTIz
#echo -n "tmp" |base64
database: dG1w
#echo -n "123" |base64
passwd: MTIz
---
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:
#kubectl create secret generic mysql-pass --from-literal=password=Passwd123
name: mysql-pass
key: password
- name: MYSQL_DATABASE
valueFrom:
secretKeyRef:
#kubectl create secret generic mysql-pass --from-literal=database=tmp
name: mysql-pass
key: database
- name: MYSQL_USER
value: sun
- name: MYSQL_PASSWORD
valueFrom:
secretKeyRef:
#kubectl create secret generic mysql-pass --from-literal=passwd=123
name: mysql-pass
key: passwd
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
name: mysql
labels:
app: mysql
spec:
ports:
- name: mysql
port: 3306
targetPort: 3306
selector:
app: mysql
kubectl apply -f mysql.yaml
3、php一个Pod、nginx一个Pod、mysql一个Pod
php.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: php
spec:
selector:
matchLabels:
app: php
replicas: 1
template:
metadata:
labels:
app: 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/
volumes:
- name: nginx-data
nfs:
server: 192.168.222.150 #nfs server服务器IP
path: "/data/k8s/web"
---
apiVersion: v1
kind: Service
metadata:
name: php
spec:
ports:
- name: php
port: 9000
protocol: TCP
targetPort: 9000
selector:
app: php
kubectl apply -f php.yml
nginx.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- 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: 192.168.222.150 #nfs server服务器IP
path: "/data/k8s/web"
- name: nginx-conf
configMap:
name: ngin-conf
items:
- key: default.conf
path: add.conf
---
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
type: NodePort
ports:
- name: nginx
port: 80
protocol: TCP
targetPort: 80
nodePort: 30010
selector:
app: nginx
配置文件nginx.conf
![](https://img-blog.csdnimg.cn/2021091520054922.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6K-46JGb6ZKi6ZOB5LqR,size_16,color_FFFFFF,t_70,g_se,x_16)
cat /conf/default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
fastcgi_pass 10.108.129.142:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
include fastcgi_params;
}
}
删除:kubectl delete configmaps ngin-conf
重新创建:kubectl create cm ngin-conf --from-file=/conf/default.conf
kubectl apply -f nginx.yaml
mysql.yaml
---
apiVersion: v1
kind: Secret
metadata:
name: mysql-pass
namespace: default
type: Qpaque
data:
#echo -n "Passwd123" |base64
password: UGFzc3dkMTIz
#echo -n "tmp" |base64
database: dG1w
#echo -n "123" |base64
passwd: MTIz
---
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:
#kubectl create secret generic mysql-pass --from-literal=password=Passwd123
name: mysql-pass
key: password
- name: MYSQL_DATABASE
valueFrom:
secretKeyRef:
#kubectl create secret generic mysql-pass --from-literal=database=tmp
name: mysql-pass
key: database
- name: MYSQL_USER
value: sun
- name: MYSQL_PASSWORD
valueFrom:
secretKeyRef:
#kubectl create secret generic mysql-pass --from-literal=passwd=123
name: mysql-pass
key: passwd
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
name: mysql
labels:
app: mysql
spec:
ports:
- name: mysql
port: 3306
targetPort: 3306
selector:
app: mysql
kubectl apply -f mysql.yaml
测试:
修改vim /www/index.php
<h1>Test php-mysql </h1>
<?php
mysqli_connect('10.109.228.202','sun','123') or die('failed');
echo 'success';
# phpinfo();
?>
再次访问,success成功啦