|NO.Z.00190|——————————|CloudNative|——|KuberNetes&持久存储.V03|——|volume.v03|VolumesHostPath挂载|宿主机与容器间时间同步|
一、VolumesHostPath挂载宿主机路径:实现宿主机与容器间时间同步
### --- hostPath:
~~~ hostPath卷可将节点上的文件或目录挂载到Pod上,
~~~ 用于Pod自定义日志输出或访问Docker内部的容器等。
二、使用hostPath卷的示例。将主机的/data目录挂载到Pod的/test-pd目录:
### --- 查看容器的时间
[root@k8s-master01 ~]# kubectl exec nginx-67948c7d6b-9cnx8 -c nginx -- date
Mon Apr 26 12:49:38 UTC 2021
[root@k8s-master01 ~]# date
Mon Apr 26 20:49:45 CST 2021
### --- 可以通过timezone把这个文件给挂载进去
[root@k8s-master01 ~]# cat /etc/timezone
Asia/Shanghai
三、创建yaml文件:将timezone这个文件挂载到容器中,实现时间同步
### --- 创建nginx-deployment.hostPath.yaml
[root@k8s-master01 ~]# vim nginx-deploy.hostpath.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
creationTimestamp: "2020-09-19T02:41:11Z"
generation: 1
labels:
app: nginx
name: nginx
namespace: default
spec:
progressDeadlineSeconds: 600
replicas: 2
revisionHistoryLimit: 10
selector:
matchLabels:
app: nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:1.15.2
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /opt
name: share-volume
- mountPath: /etc/timezone
name: timezone
- image: nginx:1.15.2
imagePullPolicy: IfNotPresent
name: nginx2
command:
- sh
- -c
- sleep 3600
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /mnt
name: share-volume
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
volumes:
- name: share-volume
emptyDir: {}
#medium: Memory
- name: timezone
hostPath:
path: /etc/timezone
type: File
四、重新replace配置容器;实现pod与宿主机间时间同步
### --- 重新replace容器
[root@k8s-master01 ~]# kubectl replace -f nginx-deploy.hostpath.yaml
[root@k8s-master01 ~]# kubectl create -f nginx-deploy.hostpath.yaml
deployment.apps/nginx created
[root@k8s-master01 ~]# kubectl get po
NAME READY STATUS RESTARTS AGE
busybox 1/1 Running 87 6d21h
nginx-866594ffc6-dzjdl 2/2 Running 0 27s
nginx-866594ffc6-llx7m 2/2 Running 0 27s
### --- 查看容器的时间
[root@k8s-master01 ~]# kubectl exec nginx-866594ffc6-dzjdl -c nginx -- cat /etc/timezone
Asia/Shanghai // 可以看到时间为上海时间了
[root@k8s-master01 ~]# kubectl exec nginx-866594ffc6-dzjdl -c nginx2 -- cat /etc/timezone
Etc/UTC // nginx2的时间我们没有更改,所以它的时区配置是没有任何变化的
### --- 查看时间同步状态
[root@k8s-master01 ~]# kubectl exec nginx-866594ffc6-dzjdl -c nginx -- date
Mon Apr 26 13:00:18 UTC 2021 // 它的时间没有更改
五、hostPath卷常用的Type(类型)如下:
### --- hostPath卷常用的Type(类型)如下:
~~~ type为空字符串:默认选项,意味着挂载hostPath卷之前不会执行任何检查。
~~~ DirectoryOrCreate:如果给定的path不存在任何东西,那么将根据需要创建一个权限为0755的空目录,和Kubelet具有相同的组和权限。
~~~ Directory:目录必须存在于给定的路径下。
~~~ FileOrCreate:如果给定的路径不存储任何内容,则会根据需要创建一个空文件,权限设置为0644,和Kubelet具有相同的组和所有权。
~~~ File:文件,必须存在于给定路径中。
~~~ Socket:UNIX套接字,必须存在于给定路径中。
~~~ CharDevice:字符设备,必须存在于给定路径中。
~~~ BlockDevice:块设备,必须存在于给定路径中。
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了