Skip to content

Instantly share code, notes, and snippets.

@jdluther2020
Last active April 14, 2023 09:26
Show Gist options
  • Save jdluther2020/e7496d9f14206035560bdd2fe8465f10 to your computer and use it in GitHub Desktop.
Save jdluther2020/e7496d9f14206035560bdd2fe8465f10 to your computer and use it in GitHub Desktop.
Mastering Kubernetes One Task at a Time - Ephemeral Storage Volumes with 'emptyDir'-Memory or RAM-backed emptyDir volume
#!/usr/bin/bash
# Purpose: Mastering Kubernetes One Task at a Time - Ephemeral Storage Volumes with 'emptyDir'-Memory or RAM-backed emptyDir volume
# Blog Ref: https://medium.com/the-aws-way/mastering-kubernetes-one-task-at-a-time-ephemeral-storage-volumes-with-emptydir-6cb08546b0ff
# 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 2 - MOUNT A TMPFS (RAM-BACKED FILESYSTEM) TO EMPTYDIR MEDIUM SPECIFICATION
# - Create a pod with an emptyDir volumes type using memory medium
# - Ref - https://kubernetes.io/docs/concepts/storage/volumes/#emptydir
# - Ref: SizeMemoryBackedVolumes feature gate - https://kubernetes.io/docs/reference/command-line-tools-reference/feature-gates/
#
cat <<EOF | tee emptydir-memory.yaml
apiVersion: v1
kind: Pod
metadata:
name: emptydir-memory
spec:
containers:
- name: nginx-container
image: nginx
volumeMounts:
- mountPath: /usr/share/nginx/html
name: memory-volume
- name: fedora-container
image: fedora
command: ['sh', '-c', 'while true; do echo "$HOSTNAME: `date` : Welcome to Nginx! Hosted on emptyDir Memory Volume!!" >> /html/index.html; sleep 10; done']
volumeMounts:
- name: memory-volume
mountPath: /html
volumes:
- name: memory-volume
emptyDir:
medium: Memory
EOF
# Create pod
kubectl apply -f emptydir-memory.yaml
{
pod/emptydir-memory created
}
# Get pod status details and obtain POD_IP for next command
kubectl get -f emptydir-memory.yaml -o wide
{
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
emptydir-memory 2/2 Running 0 16s 10.244.2.2 basic-multi-node-cluster-worker <none> <none>
}
# Access nginx web server via fedora container to make sure it's up and serving
kubectl exec emptydir-memory -c fedora-container -- curl -s 10.244.2.2
{
emptydir-memory: Fri Apr 14 01:37:17 UTC 2023 : Welcome to Nginx! Hosted on emptyDir Memory Volume!!
emptydir-memory: Fri Apr 14 01:37:27 UTC 2023 : Welcome to Nginx! Hosted on emptyDir Memory Volume!!
emptydir-memory: Fri Apr 14 01:37:37 UTC 2023 : Welcome to Nginx! Hosted on emptyDir Memory Volume!!
...
}
# Check the filesystem tmpfs mounted on /html.
# tmpfs memory backed volumes are sized to 50% of the memory on a Linux host unless sizeLimit specified.
# In the 'df' output, used and availale space should change as index.html grows every second.
kubectl exec emptydir-memory -c fedora-container -- df -H /html
{
Filesystem Size Used Avail Use% Mounted on
tmpfs 4.1G 4.1k 4.1G 1% /html
}
# Proceed to OBJECTIVE 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment