k8s disk mounted to container volume

1, Data volume type

1 emptyDir

emptyDir: simple empty directory

Data is not stored permanently, but can only be stored temporarily. It is often used to exchange and share data between multiple Docker containers in a pod

Create a pod containing two containers that share the same volume

cat <<EOF > fortune-pod.yml
apiVersion: v1
kind: Pod
metadata:
  name: fortune
  labels:
    app: fortune
spec:
  containers:
  - image: luksa/fortune                 # Image name
    name: html-genrator                  # Container name
    imagePullPolicy: Never
    volumeMounts:
    - name: html                         # The volume name is html
      mountPath: /var/htdocs             # Mount path in container
  - image: nginx:alpine                  # Second image name
    name: web-server                     # Second container name
    imagePullPolicy: Never
    volumeMounts:
    - name: html                         # Same volume html
      mountPath: /usr/share/nginx/html   # Mount path in the second container
      readOnly: true                     # Set to read-only
    ports:
    - containerPort: 80
      protocol: TCP
  volumes:                               # volume
  - name: html                           # Name the volume
    emptyDir: {}                         # Volume of type emptyDir
EOF
k create -f fortune-pod.yml
k get po -o wide

When you enter the pod container, you will find two docker containers

When accessing the cluster id, the curl 172.20.2.7 content will change in order in 10 seconds

View the location of the emptyDir directory
View the NODE deployed by the data volume container, that is, the NODE value

[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>

# Login node server
ssh 192.168.64.192

# View container
[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

#View container description
 docker inspect 58d

# Find "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"
            }
#Enter the directory to view
[root@localhost ~]# cd /var/lib/kubelet/pods/5943f81f-f5a6-428a-827c-657728895106/volumes/kubernetes.io~empty-dir/html
[root@localhost html]# ll
 Total consumption 4
-rw-r--r-- 1 root root 51 10 July 21-17:10 index.html

The temporary directory container is deleted, and the data volume is also deleted

2 nfs shared file system

Install nfs on three servers: DNF install nfs utils
Share 191 folders on the network

Create the nfs directory / etc / nfs on the master node 192.168.64.191_ data,
And allow the host share of 1921.68.64 network segment to access this directory

# create folder
mkdir /etc/nfs_data

# Write the configuration in the exports folder
# no_root_squash: the server side uses root privileges
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

Try to mount the remote nfs directory on the client host, such as 192.168.64.192

# New mount directory
mkdir /etc/web_dir/

# On the client side, mount the nfs directory of the server
mount -t nfs 192.168.64.191:/etc/nfs_data /etc/web_dir/

Persistent storage

Create PersistentVolume - persistent volume resource

cat <<EOF > mongodb-pv.yml
apiVersion: v1
kind: PersistentVolume
metadata: 
  name: mongodb-pv
spec:
  capacity:
    storage: 1Gi                          # Define persistent volume size
  accessModes:
    - ReadWriteOnce                       # Only one client is allowed to mount in read-write mode
    - ReadOnlyMany                        # Can be mounted in read-only mode by multiple clients
  persistentVolumeReclaimPolicy: Retain   # When the declaration is released, the persistent volume is retained
  nfs:                                    # nfs remote directory definition
    path: /etc/nfs_data
    server: 192.168.64.191
EOF
# Create persistent volume
k create -f mongodb-pv.yml

# View persistent volumes
k get pv
----------------------------------------------------------------------------------------------------------
NAME         CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
mongodb-pv   1Gi        RWO,ROX        Retain           Available                                   4s

Persistent volume declaration

Use persistent volume declaration to decouple the application from the underlying storage technology

cat <<EOF > mongodb-pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata: 
  name: mongodb-pvc
spec:
  resources:
    requests:
      storage: 1Gi           # Request 1GiB of storage space
  accessModes:
    - ReadWriteOnce          # Allow a single client to read and write
  storageClassName: ""       # Refer to the dynamic configuration section
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     # Reference the previously created persistent volume declaration
EOF

View the file CD / etc / nfs in the nfs remote directory_ data/

3 hostPath: the disk path in the work node

4 gitRepo local repository cloned from git

2, Configure startup parameters

Command line parameters for docker
Instructions defining commands and parameters in Dockerfile

  • ENTRYPOINT command executed within the container when the container is started
  • CMD parameters passed to start command

CMD can be overridden in the docker run command, for example

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

When you start the container, you can execute: docker run < Image >
Or overwrite CMD docker run < Image > -- spring. Profiles. Active = eureka2 when starting the container

Overwrite the entry point and CMD of docker in k8s

  • command can override ENTRYPOINT
  • args can override CMD

In the image luksa/fortune:args, set the interval time parameter for automatically generating content to 10 seconds. Docker inspect luksa / Fortune: argview cmd

You can override the CMD of docker by args of k8s

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

  - image: luksa/fortune:args
    args: ["2"]                  # The CMD configured in the docker image is 10. Here, args is used to override this value to 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

# View 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>

Repeatedly execute the curl command and access the pod. You will see that the data is refreshed every 2 seconds. Note that it should be modified to the IP curl of your pod cluster http://172.20.2.5

3, Environmental variables


In the image luksa/fortune:env, specify the INTERVAL between content generation through the environment variable INTERVAL

In the following configuration, the value of the environment variable INTERVAL is set in the container through env configuration

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

  - image: luksa/fortune:env
    env:                        # Set the environment variable 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 

# View 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>

#Repeat the curl command to access the pod, and you will see that the data is refreshed every 5 seconds
[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.

# Enter pod
k exec -it fortune bash
# View the environment variables of the pod
env

ConfigMap
Through the ConfigMap resource, the environment variable configuration can be separated from the pod, which decouples the environment variable configuration from the pod

You can create a ConfigMap resource from the command line: K create ConfigMap fortune config -- from literal = sleep interval = 10

Or create a ConfigMap from the deployment file:

# Or create from file
cat <<EOF > fortune-config.yml
apiVersion: v1
kind: ConfigMap
metadata:
  name: fortune-config
data:
  sleep-interval: "10"
EOF
# Create ConfigMap
k create -f fortune-config.yml

# View the configuration of ConfigMap
k get cm fortune-config -o yaml

Get the configuration data from ConfigMap and set it as the environment variable of 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              # Environment variable name
      valueFrom:
        configMapKeyRef:          # The value of the environment variable is obtained from ConfigMap
          name: fortune-config    # ConfigMap name used
          key: sleep-interval     # Fetch data from ConfigMap with the specified key
    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

Keywords: Operation & Maintenance Docker Kubernetes Container

Added by milind24 on Mon, 25 Oct 2021 12:41:12 +0300