一、数据卷类型

1 emptyDir

emptyDir: 简单的空目录

不持久存储数据,只能作为临时存储,常用在一个pod的多个Docker容器之间交换数据,共享数据
在这里插入图片描述
创建包含两个容器的pod, 它们共享同一个卷

cat <<EOF > fortune-pod.yml
apiVersion: v1
kind: Pod
metadata:
  name: fortune
  labels:
    app: fortune
spec:
  containers:
  - image: luksa/fortune                 # 镜像名
    name: html-genrator                  # 容器名
    imagePullPolicy: Never
    volumeMounts:
    - name: html                         # 卷名为 html
      mountPath: /var/htdocs             # 容器中的挂载路径
  - image: nginx:alpine                  # 第二个镜像名
    name: web-server                     # 第二个容器名
    imagePullPolicy: Never
    volumeMounts:
    - name: html                         # 相同的卷 html
      mountPath: /usr/share/nginx/html   # 在第二个容器中的挂载路径
      readOnly: true                     # 设置为只读
    ports:
    - containerPort: 80
      protocol: TCP
  volumes:                               # 卷
  - name: html                           # 为卷命名
    emptyDir: {}                         # emptyDir类型的卷
EOF
k create -f fortune-pod.yml
k get po -o wide

进入pod容器会发现两个docker容器
在这里插入图片描述
访问集群id,curl 172.20.2.7内容10秒会变依次

查看emptyDir目录所在位置
查看数据卷容器所部署的节点,即NODE值

[root@localhost ~]# k get po -o wide
NAME      READY   STATUS    RESTARTS   AGE    IP           NODE             NOMINATED NODE   READINESS GATES
fortune   2/2     Running   0          7m4s   172.20.2.7   192.168.64.192   <none>           <none>

# 登录节点服务器
ssh 192.168.64.192

# 查看容器
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                       COMMAND                  CREATED          STATUS          PORTS     NAMES
58d0d51b2ec2   a6eb2a334a9f                "/docker-entrypoint.…"   8 minutes ago    Up 8 minutes              k8s_web-server_fortune_default_5943f81f-f5a6-428a-827c-657728895106_0
6419851d4319   5dce1e9bc302                "/bin/sh -c /bin/for…"   8 minutes ago    Up 8 minutes              k8s_html-genrator_fortune_default_5943f81f-f5a6-428a-827c-657728895106_0
d87ef9890ca9   easzlab/pause-amd64:3.4.1   "/pause"                 8 minutes ago    Up 8 minutes              k8s_POD_fortune_default_5943f81f-f5a6-428a-827c-657728895106_0

#查看容器描述
 docker inspect 58d

# 找到"Mounts"
  "Mounts": [
            {
                "Type": "bind",
                "Source": "/var/lib/kubelet/pods/5943f81f-f5a6-428a-827c-657728895106/volumes/kubernetes.io~empty-dir/html",
                "Destination": "/usr/share/nginx/html",
                "Mode": "ro",
                "RW": false,
                "Propagation": "rprivate"
            }
#进入目录查看
[root@localhost ~]# cd /var/lib/kubelet/pods/5943f81f-f5a6-428a-827c-657728895106/volumes/kubernetes.io~empty-dir/html
[root@localhost html]# ll
总用量 4
-rw-r--r-- 1 root root 51 1021 17:10 index.html

临时目录容器删除,数据卷也删除

2 nfs共享文件系统

在三台服务器上安装 nfs:dnf install nfs-utils
将191的文件夹共享到网络上

在 master 节点 192.168.64.191 上创建 nfs 目录 /etc/nfs_data,
并允许 1921.68.64 网段的主机共享访问这个目录

# 创建文件夹
mkdir /etc/nfs_data

# 在exports文件夹中写入配置
# no_root_squash: 服务器端使用root权限
cat <<EOF > /etc/exports
/etc/nfs_data    192.168.64.0/24(rw,async,no_root_squash)
EOF
systemctl enable nfs-server
systemctl enable rpcbind
systemctl start nfs-server
systemctl start rpcbind

尝试在客户端主机上,例如192.168.64.192,挂载远程的nfs目录

# 新建挂载目录
mkdir /etc/web_dir/

# 在客户端, 挂载服务器的 nfs 目录
mount -t nfs 192.168.64.191:/etc/nfs_data /etc/web_dir/

持久化存储

在这里插入图片描述

创建 PersistentVolume - 持久卷资源

cat <<EOF > mongodb-pv.yml
apiVersion: v1
kind: PersistentVolume
metadata: 
  name: mongodb-pv
spec:
  capacity:
    storage: 1Gi                          # 定义持久卷大小
  accessModes:
    - ReadWriteOnce                       # 只允许被一个客户端挂载为读写模式
    - ReadOnlyMany                        # 可以被多个客户端挂载为只读模式
  persistentVolumeReclaimPolicy: Retain   # 当声明被释放,持久卷将被保留
  nfs:                                    # nfs远程目录定义
    path: /etc/nfs_data
    server: 192.168.64.191
EOF
# 创建持久卷
k create -f mongodb-pv.yml

# 查看持久卷
k get pv
----------------------------------------------------------------------------------------------------------
NAME         CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
mongodb-pv   1Gi        RWO,ROX        Retain           Available                                   4s

持久卷声明

使用持久卷声明,使应用与底层存储技术解耦

cat <<EOF > mongodb-pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata: 
  name: mongodb-pvc
spec:
  resources:
    requests:
      storage: 1Gi           # 申请1GiB存储空间
  accessModes:
    - ReadWriteOnce          # 允许单个客户端读写
  storageClassName: ""       # 参考动态配置章节
EOF
k create -f mongodb-pvc.yml

k get pvc
-----------------------------------------------------------------------------------
NAME          STATUS   VOLUME       CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mongodb-pvc   Bound    mongodb-pv   1Gi        RWO,ROX                       3s
cat <<EOF > mongodb-pod-pvc.yml
apiVersion: v1
kind: Pod
metadata:
  name: mongodb
spec:
  containers:
  - image: mongo
    name: mongodb
    imagePullPolicy: Never
    securityContext:
      runAsUser: 0
    volumeMounts:
    - name: mongodb-data
      mountPath: /data/db
    ports:
    - containerPort: 27017
      protocol: TCP
  volumes:
  - name: mongodb-data
    persistentVolumeClaim:
      claimName: mongodb-pvc     # 引用之前创建的"持久卷声明"
EOF

查看在 nfs 远程目录中的文件 cd /etc/nfs_data/

3 hostPath: 工作节点中的磁盘路径

4 gitRepo 从git克隆的本地仓库

二、配置启动参数

docker 的命令行参数
Dockerfile中定义命令和参数的指令

  • ENTRYPOINT 启动容器时,在容器内执行的命令
  • CMD 对启动命令传递的参数

CMD可以在docker run命令中进行覆盖,例如

......
ENTRYPOINT ["java", "-jar", "/opt/sp05-eureka-0.0.1-SNAPSHOT.jar"]
CMD ["--spring.profiles.active=eureka1"]

启动容器时,可以执行: docker run <image>
或者启动容器时覆盖CMD docker run <image> --spring.profiles.active=eureka2
在这里插入图片描述
k8s中覆盖dockerENTRYPOINTCMD

  • command 可以覆盖ENTRYPOINT
  • args 可以覆盖CMD

在镜像luksa/fortune:args中,设置了自动生成内容的间隔时间参数为10秒 docker inspect luksa/fortune:arg 查看cmd

可以通过k8s的args来覆盖docker的CMD

cat <<EOF > fortune-pod-args.yml
apiVersion: v1
kind: Pod
metadata:
  name: fortune
  labels:
    app: fortune
spec:
  containers:

  - image: luksa/fortune:args
    args: ["2"]                  # docker镜像中配置的CMD是10,这里用args把这个值覆盖成2
    name: html-genrator
    imagePullPolicy: Never
    volumeMounts:
    - name: html
      mountPath: /var/htdocs

  - image: nginx:alpine
    name: web-server
    imagePullPolicy: Never
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
      readOnly: true
    ports:
    - containerPort: 80
      protocol: TCP

  volumes:
  - name: html
    emptyDir: {}
EOF
k create -f fortune-pod-args.yml

# 查看pod
k get po -o wide
--------------------------------------------------------------------------------------------------------------
NAME      READY   STATUS    RESTARTS   AGE   IP            NODE             NOMINATED NODE   READINESS GATES
fortune   2/2     Running   0          34s   172.20.2.55   192.168.64.192   <none>           <none>

重复地执行curl命令,访问该pod,会看到数据每2秒刷新一次,注意要修改成你的pod集群的ip curl http://172.20.2.5

三、环境变量

在这里插入图片描述
在镜像luksa/fortune:env中通过环境变量INTERVAL来指定内容生成的间隔时间

下面配置中,通过env配置,在容器中设置了环境变量INTERVAL的值

cat <<EOF > fortune-pod-env.yml
apiVersion: v1
kind: Pod
metadata:
  name: fortune
  labels:
    app: fortune
spec:
  containers:

  - image: luksa/fortune:env
    env:                        # 设置环境变量 INTERVAL=5
    - name: INTERVAL
      value: "5"
    name: html-genrator
    imagePullPolicy: Never
    volumeMounts:
    - name: html
      mountPath: /var/htdocs

  - image: nginx:alpine
    name: web-server
    imagePullPolicy: Never
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
      readOnly: true
    ports:
    - containerPort: 80
      protocol: TCP

  volumes:
  - name: html
    emptyDir: {}
EOF
k create -f fortune-pod-env.yml 

# 查看pod
[root@localhost nfs_data]# k get po -o wide
NAME      READY   STATUS    RESTARTS   AGE   IP           NODE             NOMINATED NODE   READINESS GATES
fortune   2/2     Running   0          16s   172.20.2.6   192.168.64.192   <none>           <none>

#重复地执行curl命令,访问该pod,会看到数据每5秒刷新一次
[root@localhost nfs_data]# curl http://172.20.2.6
You have an ambitious nature and may make a name for yourself.
[root@localhost nfs_data]# curl http://172.20.2.6
Chicken Little only has to be right once.

# 进入pod
k exec -it fortune bash
# 查看pod的环境变量
env

ConfigMap
通过ConfigMap资源,可以从pod中把环境变量配置分离出来,是环境变量配置与pod解耦

可以从命令行创建ConfigMap资源: k create configmap fortune-config --from-literal=sleep-interval=10

或者从部署文件创建ConfigMap:

# 或从文件创建
cat <<EOF > fortune-config.yml
apiVersion: v1
kind: ConfigMap
metadata:
  name: fortune-config
data:
  sleep-interval: "10"
EOF
# 创建ConfigMap
k create -f fortune-config.yml

# 查看ConfigMap的配置
k get cm fortune-config -o yaml

从ConfigMap获取配置数据,设置为pod的环境变量

cat <<EOF > fortune-pod-env-configmap.yml
apiVersion: v1
kind: Pod
metadata:
  name: fortune
  labels:
    app: fortune
spec:
  containers:

  - image: luksa/fortune:env
    imagePullPolicy: Never
    env:
    - name: INTERVAL              # 环境变量名
      valueFrom:
        configMapKeyRef:          # 环境变量的值从ConfigMap获取
          name: fortune-config    # 使用的ConfigMap名称
          key: sleep-interval     # 用指定的键从ConfigMap取数据
    name: html-genrator
    volumeMounts:
    - name: html
      mountPath: /var/htdocs

  - image: nginx:alpine
    imagePullPolicy: Never
    name: web-server
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
      readOnly: true
    ports:
    - containerPort: 80
      protocol: TCP

  volumes:
  - name: html
    emptyDir: {}
EOF
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐