Linux云盘算 |【第五阶段】CLOUD-DAY9
主要内容:Metrics资源使用率监控、存储卷管理(临时卷ConfitMap、EmptyDir、长期卷HostPath、NFS(PV/PVC))
一、Metrics介绍
metrics是一个监控体系资源使用的插件,可以监控Node节点上的CPU、内存的使用率,或Pod对资源的占用率,通过对资源占用的了解,可以更加合理的部署容器应用;
补充:metrics从Kubernetes 1.8开始,资源使用情况的监控可以通过Metrics API的情势获取,具体的组件为Metrics Server,用来更换之前的heapster插件,所以heapster从1.11开始逐渐被放弃;
报错说明:没有http:heapster插件,该插件已被Metrics Server更换,需安装Metrics插件;
# kubectl top node //查看资源占用情况
Error from server (NotFound): the server could not find the requested resource (get services http:heapster:) 部署metrics-server服务示例:
① 开启聚合服务参数
② 签发Kubelet证书
③ 安装metrics-server插件
④ 执行所有资源文件
- apiservice.yaml //注册集群API的资源文件
- deployment.yaml //主进程metrics的资源文件
- metrisc-server.tar.gz //metrisc-server插件镜像压缩包
- pdb.yaml //中断控制器
- rbac.yaml //授权控制器
- service.yaml //后端是metrics主进程的服务
1)开启聚合服务
Metrics-server是扩展的Apiserver,依赖于kube-aggregator,因此需要在Apiserver中开启聚合API相关参数(-- enable-aggregator-routing=true)
修改Api-server启动参数流程:
① 配置文件:/etc/kubernetes/manifests/kube-apiserver.yaml
[*]- 配置spec.containers.command添加:- -- enable-aggregator-routing=true
② 重启服务:systemctl restart kubelet
③ 验证配置:kubectl -n kube-system get pod 容器名 -o yaml | grep enable-aggregator-routing
步骤1:开启apiserver聚合服务参数
① 备份配置文件(强烈建议备份)
# cd /etc/kubernetes/manifests/
# cp kube-apiserver.yaml kube-apiserver.yaml.bak //备份文件
# mv kube-apiserver.yaml.bak /root/ 注意:K8S默认会载入所有配置文件,该目次下的两个文件即使修改差一个参数不同,也会一并载入可能导致覆盖,所以必须将备份文件放在其它目次,制止干扰;
② 修改配置文件,并重启验证服务
# 在spec.containers.command 最下面手动添加如下一行
# vim kube-apiserver.yaml
- --enable-aggregator-routing=true
# systemctl restart kubelet //重启服务
# kubectl -n kube-system get pod kube-apiserver-master -o yaml | grep enable-aggregator-routing //验证服务
- --enable-aggregator-routing=true 常见报错:配置文件修改错误,会导致Apiserver无法使用,需备份文件还原
https://i-blog.csdnimg.cn/direct/fd36642724b340d590ec59cc1b797b3e.png
修改配置示例:
https://i-blog.csdnimg.cn/direct/15a1811dfda441ffb3062ad6543e7e9e.png
2)kubelet证书
metrics插件需使用kubelet证书,需手动为kubelet签发证书;
证书的申请与签发流程:
① 配置文件:/var/lib/kubelet/config.yaml
[*]- 配置:serverTLSBootstrap: true //文件的最后一行添加
② 重启服务:systemctl restart kubelet
③ 签发证书:
查看证书
[*]- 格式:kubectl get certificatesigningrequests
签发证书
[*]- 格式:kubectl certificate approve 证书名称
删除证书(多余证书)
[*]- 格式:kubectl delete certificatesigningrequests 证书名称
步骤2:证书的申请与签发(master,node-0001,node-0002,node-0003操作)
补充:证书在签发过程中是偶然效性的,当证书申请后一直未签发,kubelet会以为证书失效或丢弃,重新创建带Pending的证书,只要将该些证书签发即可,申请多余证书可删除;
① 修改配置文件,并重启服务(以master为例)
# vim /var/lib/kubelet/config.yaml
serverTLSBootstrap: true //在文件的最后一行添加(申请证书参数开启)
# systemctl restart kubelet //重启服务 ② 签发证书(必须在 master 上执行)
# kubectl get certificatesigningrequests //查看证书
NAME AGE REQUESTOR CONDITION
csr-5zbkt 13m system:node:node-0001 Pending
csr-7g67s 13m system:node:node-0002 Pending
csr-bqzjk 14m system:node:master Pending
csr-cw9dz 12m system:node:node-0003 Pending # 在Master上,为每个节点的证书进行签发
# kubectl certificate approve csr-bqzjk
certificatesigningrequest.certificates.k8s.io/csr-bqzjk approved
# kubectl certificate approve csr-5zbkt
certificatesigningrequest.certificates.k8s.io/csr-5zbkt approved
# kubectl certificate approve csr-7g67s
certificatesigningrequest.certificates.k8s.io/csr-7g67s approved
# kubectl certificate approve csr-cw9dz
certificatesigningrequest.certificates.k8s.io/csr-cw9dz approved
# kubectl get certificatesigningrequests //查看证书
NAME AGE REQUESTOR CONDITION
csr-5zbkt 11m system:node:node-0001 Approved,Issued
csr-7g67s 10m system:node:node-0002 Approved,Issued
csr-bqzjk 11m system:node:master Approved,Issued
csr-cw9dz 10m system:node:node-0003 Approved,Issued
3)安装Metrics-server插件
方法1:官网https://github.com/kubernetes-sigs/metrics-server
方法2:下载镜像和资源文件,并导入私有堆栈
[*]- 格式:kubectl top node //查询节点的资源占用率(CPU、内存的占用情况)
步骤3:安装mertics插件
# 拷贝云盘的kubernetes/v1.17.6/metrics 目次到 master 上
# rsync -av metrics/ 192.168.1.21:/root
# tree metrics/
metrics/
├── apiservice.yaml //注册集群API的资源文件
├── deployment.yaml //主进程metrics的资源文件
├── metrisc-server.tar.gz //metrisc-server插件镜像压缩包
├── pdb.yaml //中断控制器
├── rbac.yaml //授权控制器
└── service.yaml //后端是metrics主进程的服务
0 directories, 6 files ① 导入mertics插件镜像,并上传私有镜像堆栈
# cd metrics/
# docker load -i metrisc-server.tar.gz
# docker tag gcr.io/k8s-staging-metrics-server/metrics-server:master 192.168.1.100:5000/metrics-server:master
# docker push 192.168.1.100:5000/metrics-server:master ② 修改deployment资源文件
# vim deployment.yaml
29: image: 192.168.1.100:5000/metrics-server:master //修改镜像地址 ③ 执行资源文件(按照顺序依次创建)
# kubectl apply -f rbac.yaml //授权控制器
# kubectl apply -f pdb.yaml //中断控制器
# kubectl apply -f deployment.yaml //主进程metrics
# kubectl apply -f service.yaml //后端是metrics主进程的服务
# kubectl apply -f apiservice.yaml //注册集群API ④ 查询验证
# Metrics插件默认将pod、service、apiservices放在kube-system体系命名空间
# kubectl -n kube-system get pod //查看Pod资源
metrics-server-67fbd5bb7b-qbgkx 1/1 Running 0 2m2s
# kubectl -n kube-system get service //查看service资源
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kube-dns ClusterIP 10.254.0.10 <none> 53/UDP,53/TCP,9153/TCP 4d22h
metrics-server ClusterIP 10.254.60.149 <none> 443/TCP 2m28s
# kubectl -n kube-system get apiservices //查看apiservices资源 https://i-blog.csdnimg.cn/direct/9efb99cc3c1b4d1a86adf21d32e867fe.png
注意:当kube-system/metrics-server的状态为True,部署Metrics插件才完成;
⑤ 验证:查询节点的资源占用率(CPU、内存的占用情况)
# kubectl top node
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
master 79m 3% 2322Mi 62%
node-0001 23m 1% 938Mi 25%
node-0002 20m 1% 943Mi 25%
node-0003 20m 1% 1150Mi 31% 扩展:CPU毫核:CPU的计量单位叫毫核。集群中的每一个节点可以通过操作体系确认本节点的CPU内核数量,将这个数量乘以1000,得到的就是节点总的CPU总数量。
⑥ 验证:监控POD容器资源使用率
# kubectl apply -f myapache.yaml
deployment.apps/myapache configured
# watch -n 1 'kubectl top pod' //使用watch实时监控(每秒) https://i-blog.csdnimg.cn/direct/15be35f633164a45b8c5f8cf76d22324.png
# 打开另个终端测试nodePort访问
# kubectl apply -f mynodeport.yaml
service/mynodeport created
# kubectl get service https://i-blog.csdnimg.cn/direct/4045277d1960452f894414da16f5611b.png
# curl http://192.168.1.31:30625
this is apache # 循环访问测试
# while true; do curl 192.168.1.31:30625; done # 查看CPU监控状态
https://i-blog.csdnimg.cn/direct/7dc47d8176e946d5a7c6c7fb328c6036.png
常见题目:Metrics-server服务启动后,需要等候大约 30 秒的时间用来监测资源使用率,正常现象
# kubectl top pod
error: metrics not available yet
二、存储卷管理
1、ConfigMap(临时卷)
ConfigMap是在Pod中映射(文件/目次)的一种方式,答应将配置文件与镜像文件分离,以使容器化的应用程序具有可移植性;
场景:在日常工作中经常要修改各种配置文件的参数,数据库的地址、用户名密码等,这些操作在容器内非常麻烦,POD在重启大概迁移时候又会恢复到初始的状态,使用ConfigMa[就可以办理这样的题目;
常见ConfigMap案例:web集群(nginx+php),nginx需修改配置文件才气支持PHP,例如:
https://i-blog.csdnimg.cn/direct/3aad17a71b9c44ada3fcc11d72d34ff2.png
定义ConfigMap
ConfigMap可以映射单一文件,也可以映射一个目次
创建ConfigMap卷
[*]- 格式:kubectl create configmap 名称 --from-file=文件路径
查看ConfigMap卷
[*]- 格式:kubectl get configmaps
补充:创建完ConfigMap之后,要在资源文件中引用ConfigMap才气生效
资源文件引用ConfigMap模板:
https://i-blog.csdnimg.cn/direct/d7fdaa5cbcc84ac5aef57e192323675b.png
https://i-blog.csdnimg.cn/direct/837dce97fb5043d8b5eea9aefadffcfb.png
示例:创建ConfigMap存储卷
步骤1:修改Nginx配置文件,并创建ConfigMap
① 扫除所有POD(由于apache与nginx都使用 80 端口,需把实行容器全部删除)
# kubectl delete -f myapache.yaml
# kubectl delete -f mypod.yaml
# kubectl get pod
No resources found in default namespace. ② 拷贝myos:nginx容器的Nginx配置文件,并完成修改
# mkdir /var/webconf
# docker run -itd --name mynginx 192.168.1.100:5000/myos:nginx //启动容器
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b24b9f2e5f34 192.168.1.100:5000/myos:nginx "/usr/local/nginx/sb…" 28 seconds ago Up 27 seconds 80/tcp mynginx
# docker cp mynginx:/usr/local/nginx/conf/nginx.conf /var/webconf/ //拷贝容器目录
# docker rm -f mynginx
mynginx
# ls -l /var/webconf/nginx.conf
-rw-r--r-- 1 root root 2656 Jul 25 2020 /var/webconf/nginx.conf
# vim /var/webconf/nginx.conf //修改配置文件
...
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
... ③ 创建ConfigMap卷
[*]格式:kubectl create configmap 名称 --from-file=文件路径
# kubectl create configmap nginx-conf --from-file=/var/webconf/nginx.conf
configmap/nginx-conf created
# kubectl get configmaps //查看ConfigMap卷
NAME DATA AGE
nginx-conf 1 6s
步骤2:在webnginx.yaml资源文件中,引用Configmap存储卷定义
# vim webnginx.yaml
---
kind: Deployment //定义资源的类型
apiVersion: apps/v1
metadata:
name: webnginx //资源类型的名称
spec:
selector: //标签选择器
matchLabels:
myapp: nginx
replicas: 1 //副本数为1
template: //POD资源模板
metadata:
labels:
myapp: nginx
spec: //POD资源的详细定义
volumes: //卷的定义,映射存储卷
- name: nginx-php //卷的名称(标记1)
configMap: //定义ConfigMap卷(加载的卷可以写多个)
name: nginx-conf //加载的configmap,必须与create 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
# kubectl apply -f webnginx.yaml
deployment.apps/webnginx created
# kubectl get pod //查询POD资源
NAME READY STATUS RESTARTS AGE
webnginx-6fdf585d7d-lptqb 1/1 Running 0 7s # 验证服务,进入容器查看配置文件是否改变
# kubectl exec -it webnginx-6fdf585d7d-lptqb -- /bin/bash
# cat /usr/local/nginx/conf/nginx.conf https://i-blog.csdnimg.cn/direct/0ae972f5c9ce4a55945995c9dc43777f.png
# ss -ltun https://i-blog.csdnimg.cn/direct/a92184995d3b40ffb7273b492c287455.png
# exit
Exit
# kubectl delete -f webnginx.yaml
deployment.apps "webnginx" deleted 补充:以上步骤只完成了ConfigMap卷的创建、存储卷的映射,但由于PHP文件需要后端PHP程序剖析,还需添加PHP容器;其次两个容器通信需要共享网卡,最简单的方法即将两个容器写在同一个资源文件中;
步骤3:在webnginx.yaml资源文件的基础上,添加 php 容器
① 在资源文件template.spec.containers添加PHP容器
# 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 //添加PHP容器
image: 192.168.1.100:5000/myos:php-fpm //指定私有仓库PHP的镜像
restartPolicy: Always ② 执行资源文件,创建资源对象
# kubectl apply -f webnginx.yaml
deployment.apps/webnginx created
# kubectl get pod -o wide https://i-blog.csdnimg.cn/direct/11c650d4640941a2acf966ca119e6a37.png
③ 进入容器验证服务(1个POD有2个容器,需使用-c 指定容器)
# kubectl exec -it webnginx-6c9f6fd675-hkqth -c nginx -- /bin/bash
# ss -ltun https://i-blog.csdnimg.cn/direct/3a0ac950fc654a479a2f4c47138e0c11.png
# exit
exit
# curl http://10.244.2.22/info.php //访问POD容器IP
<pre>
Array
(
=> 10.244.0.0
=> GET
=> curl/7.29.0
=> /info.php
)
php_host: webnginx-6c9f6fd675-hkqth
1229 补充:在同一个POD中,两个容器共享了主机名、信号向量、网卡、进程等,但没有文件体系,需要通过长期卷的加持,才气完成nginx+php方案
2、临时卷
用途:有些应用程序需要额外的存储,但并不关心数据在重启后仍然可用,即数据是否被长期地保存;例如:缓存服务经常受限于内存大小,将不常用的数据转移到比内存慢、但对总体性能的影响很小的存储中;
常见的临时卷:emptyDir、configMap、downwardAPI、secret
2.1 emptyDir卷
emptyDir是最基础的Volume类型(存储卷),用于存储临时数据的简单空目次,当Pod设置了emptyDir类型存储卷,Pod被分配到Node上时候会创建emptyDir;只要运行在Node上,emptyDir都会存在(容器挂掉不会导致emptyDir丢失数据),但是如果Pod从Node上被删除(Pod被删除,大概Pod产生迁移,emptyDir也会被删除,并且永久丢失)
emptyDir可以实现同一个Pod中数据的共享;
资源文件引用emptyDir模板:
https://i-blog.csdnimg.cn/direct/003524026f7f4425aee8b03d14543af8.png
示例:创建emptydir存储卷
① 编写资源文件,引用emptydir定义
# 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 //卷的名称(标记1)
emptyDir: {} //定义存储临时数据空目录(卷类型)
containers:
- name: apache
image: 192.168.1.100:5000/myos:httpd
stdin: false
tty: false
volumeMounts: //挂载存储卷
- name: empty-data //指定卷的名称,必须与(标记1)名称相同
mountPath: /var/cache //存储卷加载在容器的路径
ports:
- protocol: TCP
containerPort: 80
restartPolicy: Always ② 执行资源文件
# kubectl apply -f webcache.yaml
deployment.apps/webcache created
# kubectl get pod //查看POD资源
NAME READY STATUS RESTARTS AGE
webcache-6444646d9d-4nwkc 1/1 Running 0 11s ③ 进入容器,验证测试
# kubectl exec -it webcache-6444646d9d-4nwkc -- /bin/bash
# df -h https://i-blog.csdnimg.cn/direct/1917a291aad04624a84f1ad857dd3ea4.png
3、长期卷
用途:容器磁盘上的文件的生命周期是短暂的(容器本质是进程),这就使得在容器中运行重要应用时会出现一些题目。起首,当容器崩溃时,Kubelet会重启容器,但是容器中的文件将丢失,容器以干净的状态(镜像最初的状态)重新启动,其次,在Pod中同时运行多个容器时,这些容器之间通常需要共享文件,而Kubernetes中的Volume抽象就很好的办理这些题目;
目前K8S支持的Volume存储卷类型:
https://i-blog.csdnimg.cn/direct/a52f08c394884c56b1582d9646a3a7f0.png
3.1 HostPath卷
HostPath类型则是映射node文件体系中的文件大概目次到Pod里(类似Dokcer -v),在使用hostPath类型的存储卷时,也可以设置type字段,支持的类型有文件、目次、Socket、CharDevice和BlockDevice。
注意:配置雷同Pod,可能在不同的Node上体现不同,因为不同节点上映射的文件内容不同;
hostPath里面的数据不会随着Pod的结束而消散;
资源文件引用hostPath模板
https://i-blog.csdnimg.cn/direct/e2289783dfe540b6a1ee3ba73751c51d.png
示例:创建hostpath存储卷
① 映射当地目次/var/weblog到容器中
# 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: {}
- name: log-data //卷的名称(标记1)
hostPath: //定义HostPath卷
path: /var/weblog //Node节点加载路径
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 //指定卷的名称,必须与(标记1)名称相同
mountPath: /var/log/httpd //存储卷映射在容器的路径
ports:
- protocol: TCP
containerPort: 80
restartPolicy: Always ② 执行资源文件
# kubectl apply -f webcache.yaml
deployment.apps/webcache configured
# kubectl get pod -o wide https://i-blog.csdnimg.cn/direct/53ff30a4f0f34e8f8dce20745836e510.png
# curl http://10.244.1.17 //测试访问容器IP地址
this is apache ③ 登录Node节点上,查看挂载的目次是否有文件
# ssh 192.168.1.31
# ls -l /var/weblog/
total 8
-rw-r--r-- 1 root root 86 Jul 6 16:42 access_log
-rw-r--r-- 1 root root 489 Jul 6 16:41 error_log
# cat /var/weblog/access_log
10.244.0.0 - - "GET / HTTP/1.1" 200 15 "-" "curl/7.29.0" ④ 删除POD资源,查看挂载的目次中,文件是否保留
# kubectl delete -f webcache.yamldeployment.apps "webcache" deleted# ssh 192.168.1.31
# ls -l /var/weblog/
total 8
-rw-r--r-- 1 root root 86 Jul 6 16:42 access_log
-rw-r--r-- 1 root root 489 Jul 6 16:41 error_log
# cat /var/weblog/access_log
10.244.0.0 - - "GET / HTTP/1.1" 200 15 "-" "curl/7.29.0"
3.2 NFS共享卷
在生产环境中经常需要多个Pod或多个APP应用中共享数据,而这些应用又在不同的机器的不同Pod里面,网络文件体系经常用来办理这一题目;Kubernetes中通过简单地配置就可以挂载NFS到Pod中,而NFS中的数据是可以永久保存的,同时NFS支持并发写操作,当然cephfs、glusterfs等都能很好的办理这个题目;
PV、PVC配置
① PersistentVolume(长期卷,PV)
PV是资源的提供者,根据集群的基础办法变化而变化,由K8S集群管理员配置;
[*]- 格式:kubectl get pv
② Persistent VolumeClaim(长期卷声明,PVC)
PVC是资源的使用者,根据业务服务的需求变化来配置;
[*]- 格式:kubectl get pvc
(PC/PVC的引入,是K8S集群具备了存储的逻辑抽象本事)
配置PV(NFS资源)模板:
https://i-blog.csdnimg.cn/direct/889ef9373e434d5189b1995facd1e516.png
配置PVC(使用PV资源)模板:
https://i-blog.csdnimg.cn/direct/127bba894aa048269add464cd16e81c9.png
引用配置文件模板:
https://i-blog.csdnimg.cn/direct/1c3a7fc33cf74d8dadf14903bb38f780.png
示例:创建NFS长期卷
步骤1:搭建NFS服务器(registry操作)
# yum install -y nfs-utils
# mkdir -m 777 /var/webroot //创建共享目录
# ls -ld /var/webroot/
drwxrwxrwx 2 root root 4096 Jul 6 16:58 /var/webroot/
# vim /etc/exports
/var/webroot *(rw)
# systemctl enable --now nfs # 所有节点都需要nfs软件包(master,node-0001,node-0002,node-0003)
# ansible nodes --list-hosts
hosts (3):
192.168.1.31
192.168.1.33
192.168.1.32
# cd node-install/
# ansible nodes -m yum -a 'name="nfs-utils" state=present' # 在任意其他节点测试NFS
# yum install -y nfs-utils
# showmount -e 192.168.1.100
Export list for 192.168.1.100:
/var/webroot *
步骤2:创建 PV长期卷
官网:长期卷 | Kubernetes
# vim mypv.yaml
---
kind: PersistentVolume //定义PV持久卷类型
apiVersion: v1
metadata:
name: pv-nfs //类型的名称
spec: //PV类型的详细定义
volumeMode: Filesystem //Volume模型(支持Filesystem文件系统、Block块设备)
capacity: //定义卷的容量
storage: 30Gi //提供卷的大小
accessModes: //支持的模式(RWO、ROX,RWX,查看官网查询)
- ReadWriteOnce //卷可以被一个节点以读写方式挂载;
- ReadOnlyMany //卷可以被多个节点以只读方式挂载;
- ReadWriteMany //卷可以被多个节点以读写方式挂载
persistentVolumeReclaimPolicy: Retain //卷资源回收方式Retain自动删除,Delete手动删除
nfs: //指定NFS共享
server: 192.168.1.100 //指定NFS服务器地址
path: /var/webroot //指定共享目录
# kubectl apply -f mypv.yaml
persistentvolume/pv-nfs created
# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv-nfs 30Gi RWO,ROX,RWX Retain Available 8s
步骤3:创建PVC长期卷声明
# vim mypvc.yaml
---
kind: PersistentVolumeClaim //定义PVC持久卷声明类型
apiVersion: v1
metadata:
name: pvc-nfs //类型的名称
spec: //PVC类型的详细定义
volumeMode: Filesystem //需要Volume模型(Filesystem文件系统、Block块设备)
accessModes: //需要支持的模式
- ReadWriteMany //卷可以被多个节点以读写方式挂载
resources: //计算所需的资源
requests: //描述所需的最小计算资源量
storage: 25Gi //需要提供卷的空间
# kubectl apply -f mypvc.yaml
persistentvolumeclaim/pvc-nfs created # 查看PV和PVC服务的状态
# kubectl get pv https://i-blog.csdnimg.cn/direct/7e6c447a04c04428b189b375c59782b3.png
# kubectl get pvc https://i-blog.csdnimg.cn/direct/9303ef8f92fa4d2d82d7289a663fb45b.png
补充:当PV长期卷被Bound绑定后,就不能同时被其它服务绑定;
步骤4:在webnginx.yaml资源文件中,引用长期卷
# vim webnginx.yaml---kind: DeploymentapiVersion: apps/v1metadata: name: webnginxspec: selector: matchLabels: myapp: nginx replicas: 1 template: metadata: labels: myapp: nginx spec: volumes: //卷的定义 - name: nginx-php configMap: name: nginx-conf - name: website //卷的名称(标签1) persistentVolumeClaim: //定义PVC卷类型 claimName: pvc-nfs //PVC的名称 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 //指定卷的名称(标签1) mountPath: /usr/local/nginx/html //存储卷映射在容器的路径 ports: - protocol: TCP containerPort: 80 - name: php-backend //PHP容器定义 image: 192.168.1.100:5000/myos:php-fpm volumeMounts: //挂载存储卷 - name: website //指定卷的名称(标签1) mountPath: /usr/local/nginx/html //存储卷映射在容器的路径 restartPolicy: Always# kubectl delete -f webnginx.yamldeployment.apps "webnginx" deleted# kubectl apply -f webnginx.yaml
deployment.apps/webnginx created
# kubectl get pod -o wide https://i-blog.csdnimg.cn/direct/3d503a1cd27d4160aaaebdd37bc68873.png
# 拷贝网页文件到NFS服务器上,然后在容器端访问测试
# scp kubernetes/docker-images/info.* 192.168.1.100:/var/webroot/
# kubectl exec -it webnginx-d488b9447-ddfr4 -- /bin/bash
# ls
info.html info.php
# curl http://10.244.2.23/info.html
<html>
<marquee behavior="alternate">
<font size="12px" color=#00ff00>Hello World</font>
</marquee>
</html>
# curl http://10.244.2.23/info.php
<pre>
Array
(
=> 10.244.0.0
=> GET
=> curl/7.29.0
=> /info.php
)
php_host: webnginx-d488b9447-ddfr4
1229
综合案例:
完整Nginx+php部署案例
[*]① 集群可以或许剖析静态html页面和动态PHP页面(ConfigMap)
[*]② 访问日记,存放到当地Node的/var/weblog中(HostPath)
[*]③ 网页内容,存储到NFS服务器(PC、PVC)
[*]④ 集群至少三节点,发布到互联网(Service\Ingress\nodePort\ELB+VIP)
实行架构图:
https://i-blog.csdnimg.cn/direct/1771290f686d4a5fba0aa8d0aab23d6c.png
# cat webcluster.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
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: pvc-nfs
spec:
volumeMode: Filesystem
accessModes:
- ReadWriteMany
resources:
requests:
storage: 25Gi
---
kind: ConfigMap
apiVersion: v1
metadata:
name: nginx-conf
data:
nginx.conf: |2
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: webnginx
spec:
selector:
matchLabels:
myapp: nginx
replicas: 3
template:
metadata:
labels:
myapp: nginx
spec:
volumes:
- name: nginx-php
configMap:
name: nginx-conf
- name: log-data
hostPath:
path: /var/log/weblog
type: DirectoryOrCreate
- 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: log-data
mountPath: /usr/local/nginx/logs
- 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
---
kind: Service
apiVersion: v1
metadata:
name: webcluster
spec:
ports:
- protocol: TCP
port: 80
targetPort: 80
selector:
myapp: nginx
type: ClusterIP
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: myweb
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
backend:
serviceName: webcluster
servicePort: 80
# kubectl apply -f webcluster.yaml
persistentvolume/pv-nfs created
persistentvolumeclaim/pvc-nfs created
configmap/nginx-conf created
deployment.apps/webnginx created
service/webcluster created
ingress.extensions/myweb created
# kubectl get pod
NAME READY STATUS RESTARTS AGE
webnginx-647877b59-hdb64 2/2 Running 0 11s
webnginx-647877b59-ljb6g 2/2 Running 0 11s
webnginx-647877b59-rqmdr 2/2 Running 0 11s
# kubectl get ingresses
NAME HOSTS ADDRESS PORTS AGE
myweb * 192.168.1.31 80 17s # 给 node-0001 绑定弹性公网IP,或使用 ELB 发布到互联网即可验证
小结:
本篇章节为【第五阶段】CLOUD-DAY9 的学习条记,这篇条记可以开端了解到 Metrics资源使用率监控、存储卷管理(临时卷ConfitMap、EmptyDir、长期卷HostPath、NFS(PV/PVC))。
Tip:究竟两个人的智慧大于一个人的智慧,如果你不明白本章节的内容或需要相关条记、视频,可私信小安,请不关键羞和回避,可以向他人请教,花点时间直到你真正的明白。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]