基于docker for windows的host path用法
https://kubernetes.io/docs/concepts/storage/volumes/
1 - 声明一个PV(persistent volume),也就是声明一个空间
kind: PersistentVolume
apiVersion: v1
metadata:
name: task-pv-volume
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 2Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/usr/local/" 对应 MobyLinuxVM中的/containers/services/docker-ce/rootfs/usr/local/目录。
2 - 定义PVC,也就是我要消费你的空间了,注意了哈。。
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: task-pv-claim
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
3 - 定义deployment
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: task-pv-pod
spec:
replicas: 2 -- 副本数是2
template:
metadata:
labels:
app: task-pv-pod
spec:
containers:
- name: task-container
image: nginx
imagePullPolicy: IfNotPresent -- 没有镜像再去pull
ports:
- containerPort: 80
name: "http-server"
volumeMounts:
- mountPath: "/usr/share/nginx/html" -- 将上面的pvc挂到容器的这个目录
name: task-pv-storage
volumes:
- name: task-pv-storage
persistentVolumeClaim:
claimName: task-pv-claim --对应上面的pvc名字
4 - 暴露端口号
apiVersion: v1
kind: Service
metadata:
name: task-service
spec:
type: NodePort
selector:
app: task-pv-pod
ports:
- protocol: TCP
port: 80
nodePort: 30000
POD运行正常,2个副本
c:\e\temp>kubectl get pods
NAME READY STATUS RESTARTS AGE
task-pv-pod-65bfb999d7-cmkgz 1/1 Running 0 17m ==刚刚定义的
task-pv-pod-65bfb999d7-jtrdm 1/1 Running 0 17m ==刚刚定义的
service正常
c:\e\temp>kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 13d
task-service NodePort 10.109.189.166 <none> 80:30000/TCP 8m ==刚刚定义的
Endpoints正常
c:\e\temp>kubectl describe service task-service
Name: task-service
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=task-pv-pod
Type: NodePort
IP: 10.109.189.166
LoadBalancer Ingress: localhost
Port: <unset> 80/TCP
TargetPort: 80/TCP
NodePort: <unset> 30000/TCP
Endpoints: 10.1.0.199:80,10.1.0.200:80
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
用下面的方法,登录Moby,向挂在的目录写入一个文件
https://forums.docker.com/t/can-i-kvm-with-the-hyper-v-mobylinuxvm/17716/2
docker run -it --privileged --pid=host debian nsenter -t 1 -m -u -i sh
创建如下文件,并且写入了文本"test host path"
/ # cat /containers/services/docker-ce/rootfs/usr/local/myshare/index.html
test host path
然后登录上面的任意一个pod,查看文件十分能够看到
kubectl exec -it task-pv-pod-65bfb999d7-jtrdm /bin/sh
# ls -al /usr/share/nginx/html/myshare/index.html
-rw-r--r-- 1 root root 15 Mar 18 13:20 /usr/share/nginx/html/myshare/index.html
#
通过浏览器验证,可以看到页面返回了之前写入index.html的文字了
http://localhost:30000/myshare/index.html
test host path
MOBY, do you want to update your name??? :)
再次鸣谢一下下面的link,thanks!!
https://forums.docker.com/t/can-i-kvm-with-the-hyper-v-mobylinuxvm/17716/2
docker run -it --privileged --pid=host debian nsenter -t 1 -m -u -i sh