码迷,mamicode.com
首页 > 其他好文 > 详细

12.存储卷

时间:2019-08-07 00:02:06      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:docker   contain   回收   亚马逊   self   自动   data   定时   ever   

12.存储卷

重点: pv pvc configMap secret

1.emptyDir 只在节点本地使用,pod一删除,存储卷也就删除,临时目录,可以当缓存空间使用,无持久性,生命周期与pod一样
存储卷是可以在一个pod中的各个容器中共享。

2.gitRepo
本质上也是一个emptyDir
不会去实时同步git仓库更新,但是也可以通过一个辅助的容器去定时同步

3.hostPath 存储在宿主机上,对于集群来讲它也无持久性,如果pod被删除,新建的pod如果是在其他的node上创建,那么会访问不到之前的存储卷

[root@k8s-master volumes]# cat pod-hostpath.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-vol-hostpath
namespace: default
spec:
containers:
- name: myapp-vol
image: ikubernetes/myapp:v1
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html/
volumes:
- name: html
hostPath:
path: /data/pod/volumel
type: DirectoryOrCreate


4.网络直连存储
SAN: iSCS
NAS: nfs cifs

nfs:
[root@k8s-master volumes]# cat pod-nfs.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-vol-nfs
namespace: default
spec:
containers:
- name: myapp-vol
image: ikubernetes/myapp:v1
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html/
volumes:
- name: html
nfs:
path: /data/volumes
server: 10.250.0.99 #nfs server地址

 


5.分布式存储
glusterfs
rbd
cephfs
6.云存储 需要k8s托管在云上,关键数据还是需要有自己的异地备份
EBS 亚马逊
Azure Disk
阿里等块存储


============
persistentVolumeClaim pvc
persistentVolumeClaim 简称pvc
pvc pv(存储系统上的一块存储空间) 动态供给

pvc -- pv 一一对应
一个pvc是可以定义被多个pod引用。

查看pv:
kubectl get pv

pv回收策略
RECLAIM POLICY : 回收策略
Retain 保留

创建pv资源:
[root@k8s-master volumes]# cat pv-demo.yml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv001 #定义pv的时候一定不要指定名称空间(namespace),它属于集群级别的资源
labels:
name: pv001
spec:
nfs:
path: /data/volumes/v1
server: 10.250.0.88
accessModes: ["ReadWriteMany","ReadWriteOnce"] #定义访问模型,参照官网
capacity:
storage: 2Gi #注意单位

创建pvc资源:

 

 

 

如果pv被pvc绑定则pv不能被删除。

特殊存储卷
7.configMap :可以从外部映射配置到pod内部,实现配置信息的注入 ,配置中心也是存储卷的一种,明文存储数据的。将镜像与配置文件解耦。
使用环境变量的前提,是容器应用能支持从变量中加载

 

8.secrit :功能和configMap一样,但是内容是加密的。

 

 

应用可以根据状态分为
有状态要存储
有状态不存储
无状态要存储
无状态不存储

存储卷属于pod

===========================================
容器化应用的配置的修改加载的方式:
1.自定义命令行参数
command
args: []
2.把配置文件直接焙进镜像
3.环境变量env (这种方式不能动态更新加载,pod需要重启)
(1) Cloud Native 的应用程序一般可以直接通过环境变量加载配置
(2)通过entrypoint脚本来预处理变量为配置文件中的配置信息

eg:
使用命令创建configMap:
kubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=myapp.magedu.com
也可以使用文件:
--

[root@k8s-master volumes]# cat pod-configmap.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-cm-1
namespace: default
labels:
app: myapp
tier: frontend
annotations:
magedu.com/created-by: "cluster admin"
spec:
containers:
- name: myapp-nginx
image: nginx:1.14-alpine
ports:
- name: http
containerPort: 80 #这里填写并不完全决定真正的暴露
env:
- name: NGINX_SERVER_PORT #变量只能是下划线
valueFrom:
configMapKeyRef:
name: nginx-config
key: nginx_port
- name: NGINX_SERVER_NAME
valueFrom:
configMapKeyRef:
name: nginx-config
key: server_name

连接到对应的pod中查看环境变量:printenv,当我们在外部修改变量的值过后,pod中的变量值不会被修改



4.存储卷(使用configMap 就可以动态的加载变量)

[root@k8s-master volumes]# cat pod-configmap-cm2.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-cm-2
namespace: default
labels:
app: myapp
tier: frontend
annotations:
magedu.com/created-by: "cluster admin"
spec:
containers:
- name: myapp-nginx
image: nginx:1.14-alpine
ports:
- name: http
containerPort: 80 #这里填写并不完全决定真正的暴露
volumeMounts:
- name: nginxconf
mountPath: /etc/nginx/config.d/
readOnly: true
volumes:
- name: nginxconf
configMap:
name: nginx-config

启动pod后进入查看 /etc/nginx/config.d 下面会发现有两个文件,文件名称是键名,内容为值

/ # cat /etc/nginx/config.d/nginx_port
8080

手动修改configMap中的变量值nginx_port 为8088
[root@k8s-master volumes]# kubectl edit cm nginx-config

稍等片刻(可能需要一两分钟)后再次进入pod查看挂载的变量文件的值:
[root@k8s-master volumes]# kubectl exec -it pod-cm-2 -- /bin/sh
/ # cat /etc/nginx/config.d/nginx_port
8088/ #
发现已经自动变成8088

nginx操作实例:

[root@k8s-master volumes]# cat www.conf
server {

listen 80;
server_name test.long.com;
root /usr/share/html/;
}

使用文件创建configMap:
[root@k8s-master volumes]# kubectl create configmap nginx-www --from-file=./www.conf
configmap/nginx-www created

[root@k8s-master volumes]# cat pod-configmap-nginx-cm3.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-cm-nginx-3
namespace: default
labels:
app: myapp
tier: frontend
annotations:
magedu.com/created-by: "cluster admin"
spec:
containers:
- name: myapp
image: nginx:1.14-alpine
ports:
- name: http
containerPort: 80 #这里填写并不完全决定真正的暴露
volumeMounts:
- name: nginxconf
mountPath: /etc/nginx/conf.d/
readOnly: true
volumes:
- name: nginxconf
configMap:
name: nginx-www

启动:
kubectl apply -f pod-configmap-nginx-cm3.yaml

连入pod:
kubectl exec -it pod-cm-nginx-3 -- /bin/sh

/ # cat /etc/nginx/conf.d/www.conf
server {

listen 80;
server_name test.long.com;
root /usr/share/html/;

}

在根目录/usr/share/html/ 创建文件index.html然后可以通过集群中的其他机器绑定hosts 然后curl访问测试
[root@k8s-node1 ~]# curl test.long.com/index.html
you get it!!!!!


--
然后实时修改configMap
我们修改监听的端口为 8080
# [root@k8s-master volumes]# kubectl edit cm nginx-www

再次进入pod查看www.conf
# cat /etc/nginx/conf.d/www.conf
server {

listen 8080;
server_name test.long.com;
root /usr/share/html/;

}
文件已经自动变更

我们需要重新reload nginx
# /usr/sbin/nginx -s reload
2019/06/26 08:01:33 [notice] 34#34: signal process started


然后再curl访问:
[root@k8s-node1 ~]# curl test.long.com:8080/index.html
you get it!!!!!
---

 


=======================
secret
secret有三种类型:
docker-registry 连接私有仓库秘钥
generic
tls ssl证书时候使用

创建secret:
[root@k8s-master volumes]# kubectl create secret generic mysql-root-pswd --from-literal=password=MYP@123
secret/mysql-root-pswd created

查看:
[root@k8s-master volumes]# kubectl get secret mysql-root-pswd -o yaml
apiVersion: v1
data:
password: TVlQQDEyMw==
kind: Secret
metadata:
creationTimestamp: "2019-06-26T08:09:33Z"
name: mysql-root-pswd
namespace: default
resourceVersion: "34373"
selfLink: /api/v1/namespaces/default/secrets/mysql-root-pswd
uid: bb4644aa-97e9-11e9-bb28-000c299c8399
type: Opaque


解密:
[root@k8s-master volumes]# echo TVlQQDEyMw== | base64 -d
MYP@123


由于直接就可以解密所以还是不怎么安全。

这个的使用方式和configMap类似,
使用方式也是有env及存储卷挂载的方式。同configMap

 

12.存储卷

标签:docker   contain   回收   亚马逊   self   自动   data   定时   ever   

原文地址:https://www.cnblogs.com/heaven-xi/p/11312614.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!