kubernetes(K8S)存储卷管理
2021-07-07 本文已影响0人
一个小运维
configmap配置管理
创建 configmap
由于 apache 与 nginx 都使用 80 端口,把之前的实验容器全部删除
[root@master ~]# mkdir /var/webconf
[root@master ~]# docker run -itd --name mynginx 192.168.1.100:5000/myos:nginx
9f719d0e797f81887b21985a31f426c1f2c48efd24a2c6666ecf41396fb99e93
[root@master ~]# docker cp mynginx:/usr/local/nginx/conf/nginx.conf /var/webconf/
[root@master ~]# docker rm -f mynginx
mynginx
[root@master ~]# ls -l /var/webconf/
total 4
-rw-r--r-- 1 root root 2656 Jul 25 2020 nginx.conf
[root@master ~]# vim /var/webconf/nginx.conf
... ...
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
... ...
[root@master ~]# kubectl create configmap nginx-conf --from-file=/var/webconf/nginx.conf
configmap/nginx-conf created
[root@master ~]# kubectl get configmaps
NAME DATA AGE
nginx-conf 1 8s
[root@master ~]#
配置 configmap
在 yaml 中引用 configmap 定义
[root@master ~]# vim webnginx.yaml
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: webnginx
spec:
selector:
matchLabels:
myapp: nginx
replicas: 1
template:
metadata:
labels:
myapp: nginx
spec:
volumes: # 新添加
- name: nginx-php # 新添加(标记1)
configMap: # 新添加
name: nginx-conf # 新添加,必须与 configmap 命令创建的名称相同
containers:
- name: nginx
image: 192.168.1.100:5000/myos:nginx
volumeMounts: # 新添加
- name: nginx-php # 新添加,必须与(标记1)名称相同
subPath: nginx.conf # 新添加
mountPath: /usr/local/nginx/conf/nginx.conf # 新添加
ports:
- protocol: TCP
containerPort: 80
restartPolicy: Always
[root@master ~]# kubectl apply -f webnginx.yaml
deployment.apps/webnginx created
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
webnginx-844859695b-5s7m7 1/1 Running 0 10s
[root@master ~]# kubectl exec -it webnginx-844859695b-5s7m7 -- /bin/bash
[root@webnginx-844859695b-5s7m7 html]# cat /usr/local/nginx/conf/nginx.conf
# 查看配置文件是否改变了
[root@webnginx-844859695b-kmwwh html]# ss -ltun
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp LISTEN 0 128 *:80 *:*
[root@webnginx-844859695b-kmwwh html]# exit
[root@master ~]# kubectl delete -f webnginx.yaml
deployment.apps "webnginx" deleted
[root@master ~]#
添加 php 容器
[root@master ~]# vim webnginx.yaml
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: webnginx
spec:
selector:
matchLabels:
myapp: nginx
replicas: 1
template:
metadata:
labels:
myapp: nginx
spec:
volumes:
- name: nginx-php
configMap:
name: nginx-conf
containers:
- name: nginx
image: 192.168.1.100:5000/myos:nginx
volumeMounts:
- name: nginx-php
subPath: nginx.conf
mountPath: /usr/local/nginx/conf/nginx.conf
ports:
- protocol: TCP
containerPort: 80
- name: php-backend # 新添加
image: 192.168.1.100:5000/myos:php-fpm # 新添加
restartPolicy: Always
[root@master ~]# kubectl apply -f config/webnginx.yaml
deployment.apps/webnginx created
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
webnginx-6c9f6fd675-7rmzk 2/2 Running 0 5s 10.244.2.25 node-0002
[root@master ~]# kubectl exec -it webnginx-6c9f6fd675-7rmzk -c nginx -- /bin/bash
[root@webnginx-6c9f6fd675-7rmzk html]# ss -ltun
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp LISTEN 0 128 *:80 *:*
tcp LISTEN 0 128 127.0.0.1:9000 *:*
[root@webnginx-6c9f6fd675-7rmzk html]# exit
[root@master ~]# curl http://10.244.2.25/info.php
<pre>
Array
(
[REMOTE_ADDR] => 10.244.0.0
[REQUEST_METHOD] => GET
[HTTP_USER_AGENT] => curl/7.29.0
[REQUEST_URI] => /info.php
)
php_host: webnginx-6c9f6fd675-7rmzk
1229
[root@master ~]#
emptydir 存储卷
[root@master ~]# vim webcache.yaml
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: webcache
spec:
selector:
matchLabels:
myapp: cache
replicas: 1
template:
metadata:
labels:
myapp: cache
spec:
volumes: # 新添加
- name: empty-data # 新添加
emptyDir: {} # 新添加
containers:
- name: apache
image: 192.168.1.100:5000/myos:httpd
stdin: false
tty: false
volumeMounts: # 新添加
- name: empty-data # 新添加
mountPath: /var/cache # 新添加
ports:
- protocol: TCP
containerPort: 80
restartPolicy: Always
[root@master ~]# kubectl apply -f webcache.yaml
deployment.apps/webcache created
[root@master ~]# kubectl exec -it webcache-c58847c54-qw9lh -- /bin/bash
[root@webcache-c58847c54-qw9lh html]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 2.9G 35G 8% /var/cache
... ...
[root@webcache-c58847c54-qw9lh html]# exit
[root@master ~]#
hostpath 存储卷
[root@master ~]# cat webcache.yaml
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: webcache
spec:
selector:
matchLabels:
myapp: cache
replicas: 1
template:
metadata:
labels:
myapp: cache
spec:
volumes:
- name: empty-data
emptyDir: {}
- name: log-data # 新添加
hostPath: # 新添加
path: /var/weblog # 新添加
type: DirectoryOrCreate # 新添加
containers:
- name: apache
image: 192.168.1.100:5000/myos:httpd
stdin: false
tty: false
volumeMounts:
- name: empty-data
mountPath: /var/cache
- name: log-data # 新添加
mountPath: /var/log/httpd # 新添加
ports:
- protocol: TCP
containerPort: 80
restartPolicy: Always
[root@master ~]# kubectl apply -f webcache.yaml
deployment.apps/webcache created
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
webcache-75588b9cc5-xzkvc 1/1 Running 0 4s 10.244.2.30 node-0002
[root@master ~]# curl http://10.244.2.30/
this is apache
[root@master ~]# ssh 192.168.1.32
root@192.168.1.32's password:
Last login: Mon Apr 26 10:41:58 2021 from 192.168.1.252
Welcome to Huawei Cloud Service
[root@node-0002 ~]# ls -l /var/weblog/
total 16
-rw-r--r-- 1 root root 86 Apr 26 13:12 access_log
-rw-r--r-- 1 root root 489 Apr 26 13:12 error_log
[root@node-0002 ~]# cat /var/weblog/access_log
10.244.0.0 - - [26/Apr/2021:05:12:59 +0000] "GET / HTTP/1.1" 200 15 "-" "curl/7.29.0"
[root@node-0002 ~]#
持久卷
搭建NFS服务器
[root@registry ~]# yum install -y nfs-utils
[root@registry ~]# mkdir -m 777 /var/webroot
[root@registry ~]# vim /etc/exports
/var/webroot *(rw)
[root@registry ~]# systemctl enable --now nfs
#---------------------------------所有节点都需要 nfs 软件包-------------------------
[root@node-0001 ~]# yum install -y nfs-utils
#--------------------------------------------------------------------------------
[root@node-0002 ~]# yum install -y nfs-utils
#--------------------------------------------------------------------------------
[root@node-0003 ~]# yum install -y nfs-utils
#-------------------------------下面在任意其他节点测试------------------------------
[root@master ~]# yum install -y nfs-utils
[root@master ~]# showmount -e 192.168.1.100
Export list for 192.168.1.100:
/var/webroot *
[root@master ~]#
创建 pv
[root@master ~]# vim mypv.yaml
---
kind: PersistentVolume
apiVersion: v1
metadata:
name: pv-nfs
spec:
volumeMode: Filesystem
capacity:
storage: 30Gi
accessModes:
- ReadWriteOnce
- ReadOnlyMany
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
nfs:
server: 192.168.1.100
path: /var/webroot
[root@master ~]# kubectl apply -f mypv.yaml
persistentvolume/pv-nfs created
[root@master ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS AGE
pv-nfs 30Gi RWO,ROX,RWX Retain Available 3s
创建 pvc
[root@master configmap]# vim mypvc.yaml
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: pvc-nfs
spec:
volumeMode: Filesystem
accessModes:
- ReadWriteMany
resources:
requests:
storage: 25Gi
[root@master configmap]# kubectl apply -f mypvc.yaml
[root@master configmap]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM
pv-nfs 30Gi RWX Retain Bound default/pvc-nfs
[root@master configmap]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc-nfs Bound pv-nfs 30Gi RWO,ROX,RWX 27s
应用持久卷
[root@master ~]# cat webnginx.yaml
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: webnginx
spec:
selector:
matchLabels:
myapp: nginx
replicas: 1
template:
metadata:
labels:
myapp: nginx
spec:
volumes:
- name: nginx-php
configMap:
name: nginx-conf
- name: website # 新添加
persistentVolumeClaim: # 新添加
claimName: pvc-nfs # 新添加
containers:
- name: nginx
image: 192.168.1.100:5000/myos:nginx
volumeMounts:
- name: nginx-php
subPath: nginx.conf
mountPath: /usr/local/nginx/conf/nginx.conf
- name: website # 新添加
mountPath: /usr/local/nginx/html # 新添加
ports:
- protocol: TCP
containerPort: 80
- name: php-backend
image: 192.168.1.100:5000/myos:php-fpm
volumeMounts: # 新添加
- name: website # 新添加
mountPath: /usr/local/nginx/html # 新添加
restartPolicy: Always
[root@master ~]# kubectl delete -f webnginx.yaml
deployment.apps "webnginx" deleted
[root@master ~]# kubectl apply -f webnginx.yaml
deployment.apps/webnginx created
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
webnginx-d488b9447-t62cl 2/2 Running 0 7s 10.244.2.32 node-0002
[root@master ~]# curl http://10.244.2.32/
# 在 nfs 上创建修改页面,然后在容器端访问测试