《Kubernetes权威指南:从Docker到Kubernetes实践全接触》学习实验记录-创建volume
2019-01-14 23:50 it长青 阅读(154) 评论(0) 收藏 举报volume
容器中的磁盘的生命周期是短暂的,这就带来了一系列的问题,第一,当一个容器损坏之后,kubelet 会重启这个容器,但是文件会丢失-这个容器会是一个全新的状态,第二,当很多容器在同一Pod中运行的时候,很多时候需要数据文件的共享。Kubernete Volume解决了这个问题
Background背景
Docker有一个Volumes的概念,虽然这个Volume有点宽松和管理性比较小。在Docker中,一个 Volume 是一个简单的所在主机的一个目录或者其它容器中的。生命周期是没有办法管理,直到最近才有 local-disk-backed 磁盘。Docker现在提供了磁盘驱动,但是功能非常有限(例如Docker1.7只能挂在一个磁盘每个容器,并且无法传递参数)
从另外一个方面讲,一个Kubernetes volume,拥有明确的生命周期,与所在的Pod的生命周期相同。因此,Kubernetes volume独立与任何容器,与Pod相关,所以数据在重启的过程中还会保留,当然,如果这个Pod被删除了,那么这些数据也会被删除。更重要的是,Kubernetes volume 支持多种类型,任何容器都可以使用多个Kubernetes volume。
它的核心,一个 volume 就是一个目录,可能包含一些数据,这些数据对pod中的所有容器都是可用的,这个目录怎么使用,什么类型,由什么组成都是由特殊的volume 类型决定的
想要使用一个volume,Pod必须指明Pod提供了那些磁盘,并且说明如何挂在到容器中
====
**注意
因为关键字没写对或格式问题的报错
[root@k8s_master ~]# kubectl create -f web_rc.yaml
error: error validating "web_rc.yaml": error validating data: found invalid field volume for v1.PodSpec; if you choose to ignore these errors, turn validation off with --validate=false
====
在rc中添加volume
[root@k8s_master ~]# cat web_rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: webrc # rc的名字
labels:
name: webrc # 标签的名字
spec:
replicas: 2 # 幅本数
selector:
name: pod1 # 标签名,指定pod的label名字,可通过
template:
metadata:
name: pod1
labels:
name: pod1 # 注意,这里要和上面spec.selector中的name一致
spec:
volumes:
- name: "apache-storage"
hostPath:
path: "/data" # 宿主机的存储所在位置
containers:
- name: pod1
image: 172.16.111.200:5000/httpd:v1
ports:
- containerPort: 80
protocol: TCP
hostIP: 0.0.0.0
hostPort: 8080
volumeMounts:
- name: "apache-storage" # 是上面spec.volume.name
mountPath: "/yy" # 容器里面的路径
[root@k8s_master ~]#
[root@k8s_master ~]# kubectl create -f web_rc.yaml
replicationcontroller "webrc" created
[root@k8s_master ~]#
然后可以通过kubectl describe 来看 path 的挂载路径在那
[root@k8s_master ~]# kubectl describe rc webrc
Name: webrc
Namespace: default
Image(s): 172.16.111.200:5000/httpd:v1
Selector: name=pod1
Labels: name=webrc
Replicas: 2 current / 2 desired
Pods Status: 1 Running / 1 Waiting / 0 Succeeded / 0 Failed
Volumes:
apache-storage:
Type: HostPath (bare host directory volume)
Path: /data
Events:
FirstSeen LastSeen Count From SubObjectPath Type Reason Message
--------- -------- ----- ---- ------------- -------- ------ -------
2m 2m 1 {replication-controller } Normal SuccessfulCreate Created pod: webrc-dcqsw
2m 2m 1 {replication-controller } Normal SuccessfulCreate Created pod: webrc-k9f40
[root@k8s_master ~]#
----
在node 节点中看是否已经有创建volume,并且在容器的/yy 中写入东西,回到宿主机的/data看看是否有
[root@k8s_node1 ~]# date
Sun Dec 9 08:33:22 EST 2018
[root@k8s_node1 ~]# ll / |grep data
drwxr-xr-x 2 root root 6 Dec 9 08:24 data
[root@k8s_node1 ~]#
[root@k8s_node1 ~]# docker ps | grep httpd:v1
99dfd55499a0 172.16.111.200:5000/httpd:v1 "/usr/sbin/httpd -..." 10 minutes ago Up 10 minutes k8s_pod1.e9cf2547_webrc-dcqsw_default_be84a33b-fbb5-11e8-9a1d-000c29bd7985_4f038f17
[root@k8s_node1 ~]#
[root@k8s_node1 ~]# docker exec -ti 99dfd55499a0 /bin/bash
[root@webrc-dcqsw /]#
[root@webrc-dcqsw /]# ls /yy
[root@webrc-dcqsw /]#
[root@webrc-dcqsw /]# touch /yy/test.txt
[root@webrc-dcqsw /]# exit
exit
[root@k8s_node1 ~]#
[root@k8s_node1 ~]# ll /data
total 0
-rw-r--r-- 1 root root 0 Dec 9 08:37 test.txt
[root@k8s_node1 ~]#
导出pod 的信息为yaml格式
[root@k8s_master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
web 1/1 Running 0 1h
webrc-dcqsw 1/1 Running 0 18m
webrc-k9f40 0/1 Pending 0 18m
[root@k8s_master ~]#
[root@k8s_master ~]# kubectl get pod webrc-dcqsw -o yaml
apiVersion: v1
kind: Pod
metadata:
annotations:
kubernetes.io/created-by: |
{"kind":"SerializedReference","apiVersion":"v1","reference":{"kind":"ReplicationController","namespace":"default","name":"webrc","uid":"be82f8f9-fbb5-11e8-9a1d-000c29bd7985","apiVersion":"v1","resourceVersion":"198277"}}
creationTimestamp: 2018-12-09T13:24:23Z
generateName: webrc-
labels:
name: pod1
name: webrc-dcqsw
namespace: default
ownerReferences:
- apiVersion: v1
controller: true
kind: ReplicationController
name: webrc
uid: be82f8f9-fbb5-11e8-9a1d-000c29bd7985
resourceVersion: "198300"
selfLink: /api/v1/namespaces/default/pods/webrc-dcqsw
uid: be84a33b-fbb5-11e8-9a1d-000c29bd7985
spec:
containers:
- image: 172.16.111.200:5000/httpd:v1
imagePullPolicy: IfNotPresent
name: pod1
ports:
- containerPort: 80
hostIP: 0.0.0.0
hostPort: 8080
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
volumeMounts:
- mountPath: /yy
name: apache-storage
dnsPolicy: ClusterFirst
nodeName: node1
restartPolicy: Always
securityContext: {}
terminationGracePeriodSeconds: 30
volumes:
- hostPath:
path: /data
name: apache-storage
status:
conditions:
- lastProbeTime: null
lastTransitionTime: 2018-12-09T13:24:23Z
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: 2018-12-09T13:24:25Z
status: "True"
type: Ready
- lastProbeTime: null
lastTransitionTime: 2018-12-09T13:24:23Z
status: "True"
type: PodScheduled
containerStatuses:
- containerID: docker://99dfd55499a03735410790c3d946e456bc041ed1eb76136fcc8a3fa1a6f34913
image: 172.16.111.200:5000/httpd:v1
imageID: docker-pullable://172.16.111.200:5000/httpd@sha256:ec406e0e23d424a149409c20bc733b7ac5318ff0c84d990fc63b4db9c43a01ef
lastState: {}
name: pod1
ready: true
restartCount: 0
state:
running:
startedAt: 2018-12-09T13:24:24Z
hostIP: 172.16.111.201
phase: Running
podIP: 172.17.0.2
startTime: 2018-12-09T13:24:23Z
[root@k8s_master ~]#
浙公网安备 33010602011771号