kubernetes基于NFS实现nginx+tomcat动静分离
一 部署nfs server
1.1 安装nfs server
root@harbor-01:~# apt-get -y install nfs-kernel-server
1.2 创建挂载点
root@harbor-01:~# mkdir -pv /data/webapp/{images,static}
mkdir: created directory '/data/webapp'
mkdir: created directory '/data/webapp/images'
mkdir: created directory '/data/webapp/static'
1.3 配置nfs server
root@harbor-01:~# cat /etc/exports
/data/webapp *(rw,sync,no_root_squash)
1.4 重启nfs server
root@harbor-01:~# systemctl restart nfs-server.service
1.5 创建挂载点
root@k8s-master-01:~# mkdir /mnt/data
1.6 挂载测试
root@k8s-master-01:~# mount -t nfs 192.168.174.120:/data/webapp /mnt/data
1.7 查看挂载
root@k8s-master-01:~# df -TH
Filesystem Type Size Used Avail Use% Mounted on
udev devtmpfs 982M 0 982M 0% /dev
tmpfs tmpfs 206M 1.6M 204M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv ext4 20G 11G 8.7G 55% /
tmpfs tmpfs 1.1G 0 1.1G 0% /dev/shm
tmpfs tmpfs 5.3M 0 5.3M 0% /run/lock
tmpfs tmpfs 1.1G 0 1.1G 0% /sys/fs/cgroup
192.168.174.120:/data/webapp nfs4 104G 18G 82G 18% /mnt/data
1.8 验证挂载内容
root@k8s-master-01:~# tree /mnt/data/
/mnt/data/
├── images
└── static
2 directories, 0 files
1.9 写入数据测试
root@k8s-master-01:~# cp /var/log/syslog /mnt/data/static
1.10 nfs验证数据
root@harbor-01:~# ls -l /data/webapp/static/
total 4
-rw-r----- 1 root root 1304 Dec 13 17:52 syslog
1.11 卸载nfs
root@k8s-master-01:~# umount /mnt/data
二 修改nginx.yaml
查看代码
root@k8s-master-01:/data/k8s/yaml# cat nginx/nginx.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
labels:
app: wgs-nginx-deployment-label
name: wgs-nginx-deployment
namespace: wgs
spec:
replicas: 1
selector:
matchLabels:
app: wgs-nginx-selector
template:
metadata:
labels:
app: wgs-nginx-selector
spec:
containers:
- name: wgs-nginx-container
image: 192.168.174.120/baseimages/nginx-web1:v1.1
imagePullPolicy: Always
ports:
- containerPort: 80
protocol: TCP
name: http
- containerPort: 443
protocol: TCP
name: https
env:
- name: "nginx-version"
value: "1.20.2"
resources:
limits:
cpu: 1
memory: 512Mi
requests:
cpu: 500m
memory: 256Mi
volumeMounts:
- name: wgs-images
mountPath: /usr/local/nginx/html/webapp/images
readOnly: false
- name: wgs-static
mountPath: /usr/local/nginx/html/webapp/static
readOnly: false
volumes:
- name: wgs-images
nfs:
server: 192.168.174.120
path: /data/webapp/images
- name: wgs-static
nfs:
server: 192.168.174.120
path: /data/webapp/static
---
kind: Service
apiVersion: v1
metadata:
labels:
app: wgs-nginx-service-label
name: wgs-nginx-service
namespace: wgs
spec:
type: NodePort
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
nodePort: 40002
- name: https
port: 443
protocol: TCP
targetPort: 443
nodePort: 40443
selector:
app: wgs-nginx-selector
三 修改tomcat.yaml
查看代码
root@k8s-master-01:/data/k8s/yaml# cat tomcat/tomcat-app1.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
labels:
app: wgs-tomcat-app1-deployment-label
name: wgs-tomcat-app1-deployment
namespace: wgs
spec:
replicas: 1
selector:
matchLabels:
app: wgs-tomcat-app1-selector
template:
metadata:
labels:
app: wgs-tomcat-app1-selector
spec:
containers:
- name: wgs-tomcat-app1-container
image: 192.168.174.120/baseimages/tomcat-app1:v1.0
imagePullPolicy: Always
ports:
- containerPort: 8080
protocol: TCP
name: http
env:
- name: "tomcat-version"
value: "8.5.0"
resources:
limits:
cpu: 1
memory: "512Mi"
requests:
cpu: 500m
memory: "512Mi"
volumeMounts:
- name: wgs-images
mountPath: /data/tomcat/webapps/myapp/images
readOnly: false
- name: wgs-static
mountPath: /data/tomcat/webapps/myapp/static
readOnly: false
volumes:
- name: wgs-images
nfs:
server: 192.168.174.120
path: /data/webapp/images
- name: wgs-static
nfs:
server: 192.168.174.120
path: /data/webapp/static
---
kind: Service
apiVersion: v1
metadata:
labels:
app: wgs-tomcat-app1-service-label
name: wgs-tomcat-app1-service
namespace: wgs
spec:
type: NodePort
ports:
- name: http
port: 80
protocol: TCP
targetPort: 8080
nodePort: 40003
selector:
app: wgs-tomcat-app1-selector
四 更新pod
4.1 更新nginx pod
root@k8s-master-01:/data/k8s/yaml# kubectl apply -f nginx/nginx.yaml
deployment.apps/wgs-nginx-deployment configured
service/wgs-nginx-service unchanged
4.2 更新tomcat pod
root@k8s-master-01:/data/k8s/yaml# kubectl apply -f tomcat/tomcat-app1.yaml
deployment.apps/wgs-tomcat-app1-deployment configured
service/wgs-tomcat-app1-service unchanged
五 验证pod挂载
5.1 查看pod信息
root@k8s-master-01:~# kubectl get pods -n wgs
NAME READY STATUS RESTARTS AGE
wgs-nginx-deployment-5b9c4554c9-nplfg 1/1 Running 0 59s
wgs-tomcat-app1-deployment-7df6b45b59-66ns4 1/1 Running 0 44s
5.2 查看nginx pod挂载
root@k8s-master-01:~# kubectl exec wgs-nginx-deployment-5b9c4554c9-nplfg -n wgs -- df -Th
Filesystem Type Size Used Available Use% Mounted on
overlay overlay 18.6G 9.7G 7.9G 55% /
tmpfs tmpfs 64.0M 0 64.0M 0% /dev
tmpfs tmpfs 979.7M 0 979.7M 0% /sys/fs/cgroup
/dev/mapper/ubuntu--vg-ubuntu--lv
ext4 18.6G 9.7G 7.9G 55% /dev/termination-log
shm tmpfs 64.0M 0 64.0M 0% /dev/shm
/dev/mapper/ubuntu--vg-ubuntu--lv
ext4 18.6G 9.7G 7.9G 55% /etc/resolv.conf
/dev/mapper/ubuntu--vg-ubuntu--lv
ext4 18.6G 9.7G 7.9G 55% /etc/hostname
/dev/mapper/ubuntu--vg-ubuntu--lv
ext4 18.6G 9.7G 7.9G 55% /etc/hosts
tmpfs tmpfs 979.7M 12.0K 979.7M 0% /run/secrets/kubernetes.io/serviceaccount
192.168.174.120:/data/webapp/images
nfs4 96.3G 16.1G 76.1G 17% /usr/local/nginx/html/webapp/images
192.168.174.120:/data/webapp/static
nfs4 96.3G 16.1G 76.1G 17% /usr/local/nginx/html/webapp/static
5.3 查看tomcat pod挂载
root@k8s-master-01:~# kubectl exec wgs-tomcat-app1-deployment-7df6b45b59-66ns4 -n wgs -- df -Th
Filesystem Type Size Used Available Use% Mounted on
overlay overlay 18.6G 9.7G 7.9G 55% /
tmpfs tmpfs 64.0M 0 64.0M 0% /dev
tmpfs tmpfs 979.7M 0 979.7M 0% /sys/fs/cgroup
/dev/mapper/ubuntu--vg-ubuntu--lv
ext4 18.6G 9.7G 7.9G 55% /dev/termination-log
shm tmpfs 64.0M 0 64.0M 0% /dev/shm
/dev/mapper/ubuntu--vg-ubuntu--lv
ext4 18.6G 9.7G 7.9G 55% /etc/resolv.conf
/dev/mapper/ubuntu--vg-ubuntu--lv
ext4 18.6G 9.7G 7.9G 55% /etc/hostname
/dev/mapper/ubuntu--vg-ubuntu--lv
ext4 18.6G 9.7G 7.9G 55% /etc/hosts
tmpfs tmpfs 979.7M 12.0K 979.7M 0% /run/secrets/kubernetes.io/serviceaccount
192.168.174.120:/data/webapp/static
nfs4 96.3G 16.1G 76.1G 17% /data/tomcat/webapps/myapp/static
192.168.174.120:/data/webapp/images
nfs4 96.3G 16.1G 76.1G 17% /data/tomcat/webapps/myapp/images
六 访问测试
6.1 上传数据到NFS
root@harbor-01:~# echo "nfs mount test" >> /data/webapp/static/index.html
root@ceph-node04:~# wget -P /data/webapp/images/ https://docs.sonarqube.org/latest/images/dev-cycle.png
6.2 查看当前svc
root@k8s-master-01:~# kubectl get svc -n wgs
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
wgs-nginx-service NodePort 10.100.114.185 <none> 80:40002/TCP,443:40443/TCP 121m
wgs-tomcat-app1-service NodePort 10.100.39.62 <none> 80:40003/TCP 3h17m
6.3 访问nginx
6.3.1 访问nginx静态页面
root@k8s-master-01:~# curl http://192.168.174.100:40002/webapp/static/
nfs mount test
6.3.2 访问nginx图片
6.4 访问tomcat
6.4.1 访问tomcat静态页面
root@k8s-master-01:~# curl http://192.168.174.100:40002/myapp/static/ #访问nginx service端口
nfs mount test
root@k8s-master-01:~# curl http://192.168.174.100:40003/myapp/static/ #访问tomcat service端口
nfs mount test
6.4.2 访问tomcat图片
访问tomcat service端口
访问nginx service端口