利用minikube部署k8s集群并部署lnmp服务
利用k8s部署lnmp服务
部署minikube
参考官网进行安装部署
利用minikube部署k8s集群
1. 部署k8s集群
minikube start
k8s集群部署lnmp
1. 将如下内容存储为lnmp.yaml文件
---
apiVersion: v1
kind: Namespace
metadata:
name: lnmp
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-data
namespace: lnmp
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nginx-html
namespace: lnmp
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
namespace: lnmp
spec:
selector:
matchLabels:
app: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:8.0
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: mysql-data
---
apiVersion: v1
kind: Service
metadata:
name: mysql
namespace: lnmp
spec:
ports:
- port: 3306
selector:
app: mysql
clusterIP: None
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-php-fpm
namespace: lnmp
spec:
replicas: 1
selector:
matchLabels:
app: nginx-php-fpm
template:
metadata:
labels:
app: nginx-php-fpm
spec:
containers:
- name: nginx
image: nginx:1.19
ports:
- containerPort: 80
volumeMounts:
- name: nginx-conf
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
- name: nginx-html
mountPath: /usr/share/nginx/html
- name: php-fpm
image: php:7.4-fpm
volumeMounts:
- name: nginx-html
mountPath: /var/www/html
volumes:
- name: nginx-conf
configMap:
name: nginx-conf
- name: nginx-html
persistentVolumeClaim:
claimName: nginx-html
---
apiVersion: v1
kind: Service
metadata:
name: nginx-php-fpm
namespace: lnmp
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
selector:
app: nginx-php-fpm
---
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-conf
namespace: lnmp
data:
nginx.conf: |
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
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;
}
}
}
2. 执行应用部署
kubectl apply -f lnmp.yaml
3. 验证应用是否正常运行
kubectl get pods -n lnmp
kubectl get svc nginx-php-fpm -n lnmp
4. 测试服务是否正常
#进入Pod
kubectl exec -it $(kubectl get pod -l app=nginx-php-fpm -n lnmp -o jsonpath='{.items[0].metadata.name}') -n lnmp -c nginx -- /bin/bash
#创建info.php文件做测试
echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/info.php
5. 访问服务,验证是否成功
minikube service nginx-php-fpm -n lnmp
若无异常可以看到正确调整到网页,然后更改访问的路径,验证访问无异常
参考资料
更多推荐
所有评论(0)