kubernetes基於NFS實現動靜分離
阿新 • • 發佈:2021-12-13
一 部署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埠