Skip to content

Instantly share code, notes, and snippets.

@jdluther2020
Last active April 10, 2023 21:42
Show Gist options
  • Save jdluther2020/8516a2e30531be290a5d106b4dff13cc to your computer and use it in GitHub Desktop.
Save jdluther2020/8516a2e30531be290a5d106b4dff13cc to your computer and use it in GitHub Desktop.
Mastering Kubernetes One Task at a Time - Persistent Storage Volumes with 'hostPath'-Sharing Volumes among Multiple Containers
#!/usr/bin/bash
# Purpose: Persistent Storage Volumes with 'hostPath'-Sharing Volumes among Multiple Containers
# Blog Ref: https://medium.com/the-aws-way/mastering-kubernetes-one-task-at-a-time-persistent-storage-volumes-with-hostpath-ad645714bcc7
# GitHub Ref: https://github.com/jdluther2020/jdluther-kubernetes-io-tasks/
# Author's NOTE
# 1. # are comment lines
# 2. Command output wherever helpful is shown inside {}
# 3. Everything is executed on a local dev environment (MacOS)
#
# OBJECTIVE 3 -SHARING VOLUMES AMONG MULTIPLE CONTAINERS
# - Create a pod with two containers sharing the same volume, one for writing, one for reading
#
# Create the pod manifest with two containers, one writer and another reader
# (to avoid shell variable interpretation, copy the manifest straight instead of cat command)
cat <<EOF | shared-volume-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: shared-volume-pod
spec:
containers:
- name: main-container
image: busybox
command: ['sh', '-c', 'while true; do echo "`date` : $CONTAINER_NAME : Test Message" >> /outdir/log.txt; sleep 5; done']
env:
- name: CONTAINER_NAME
value: "main-container"
volumeMounts:
- name: vol-shared
mountPath: /outdir
- name: sidecar-container
image: alpine
command: ['sh', '-c', 'tail -f /indir/log.txt']
volumeMounts:
- name: vol-shared
mountPath: /indir
volumes:
- name: vol-shared
hostPath:
path: /usr/share
EOF
# Create pod
kubectl apply -f shared-volume-pod.yaml
# Check pod created successfully and list the hosted node
kubectl get pod shared-volume-pod -o wide
{
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
shared-volume-pod 2/2 Running 0 34s 10.244.1.4 basic-multi-node-cluster-worker2 <none> <none>
}
# Check log to see sidecar container output
kubectl logs shared-volume-pod -c sidecar-container
{
Mon Apr 10 07:11:17 UTC 2023 : main-container : Test Message
...
Mon Apr 10 07:13:12 UTC 2023 : main-container : Test Message
}
# END OF DEMONSTRATION
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment