Difference between revisions of "NFS and Microk8s"

From UVOO Tech Wiki
Jump to navigation Jump to search
(Created page with "``` #!/usr/bin/env bash if [ "$#" -ne 2 ]; then echo "Usage: $0 <uuid> <nfs node type server/client>" echo "Example: $0 28a1f1f8-e686-11eb-bf45-7f257ca7269b server" exit...")
 
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
# Change default StorageClass
 +
```
 +
kubectl patch storageclass local-path -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
 +
kubectl patch storageclass microk8s-hostpath -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"false"}}}'
 +
```
 +
 +
# Clients Quick
 +
```
 +
apt install -y nfs-client
 +
mkdir -p /opt/local-path-provisioner
 +
 +
vi /etc/fstab
 +
nas1:/<your mount point> /opt/local-path-provisioner nfs4  _netdev,auto  0  0
 +
mount -a
 +
ls /opt/local-path-provisioner/
 +
```
 +
 +
# Using a script
 +
 +
 +
It is important to understand this file. Client ip addresses are highly trusted and controlled kubernetes nodes.
 +
 +
Do not run files on nas directory it should only be used as a file store.
 +
 
```
 
```
 
#!/usr/bin/env bash
 
#!/usr/bin/env bash
if [ "$#" -ne 2 ]; then
+
set -e
   echo "Usage: $0 <uuid> <nfs node type server/client>"
+
if [ "$#" -ne 3 ]; then
   echo "Example: $0 28a1f1f8-e686-11eb-bf45-7f257ca7269b server"
+
   echo "Usage: $0 <uuid> <nfs node type server/client> \"<client ips>\""
 +
   echo "Example: $0 28a1f1f8-e686-11eb-bf45-7f257ca7269b server \"10.x.x.x 10.x.x.y 10.x.x.z\""
 
   exit
 
   exit
 
fi
 
fi
Line 9: Line 34:
 
uuid=$1
 
uuid=$1
 
node_type=$2
 
node_type=$2
 +
client_ips=$3
 
server_host=nas
 
server_host=nas
 
server_mnt=/$uuid
 
server_mnt=/$uuid
server_net_mnt=${server_host}:{server_dir}
+
server_net_mnt="${server_host}:${server_mnt}"
# local_mnt=/var/snap/microk8s/common/nas-nfs-standard
 
 
client_mnt=/opt/local-path-provisioner
 
client_mnt=/opt/local-path-provisioner
client_ips="10.x.x.x 10.x.x.x 10.x.x.x"
+
 
  
 
if ! [[ $uuid =~ ^\{?[A-F0-9a-f]{8}-[A-F0-9a-f]{4}-[A-F0-9a-f]{4}-[A-F0-9a-f]{4}-[A-F0-9a-f]{12}\}?$ ]]; then
 
if ! [[ $uuid =~ ^\{?[A-F0-9a-f]{8}-[A-F0-9a-f]{4}-[A-F0-9a-f]{4}-[A-F0-9a-f]{4}-[A-F0-9a-f]{12}\}?$ ]]; then
Line 24: Line 49:
 
install_client(){
 
install_client(){
 
   echo Installing client.
 
   echo Installing client.
   grep $server_mnt /etc/fstab || sudo echo "$server_net_mnt $client_mnt nfs" | sudo tee -a /etc/fstab
+
  sudo sed -i "/$uuid/d" /etc/fstab
 +
   grep $server_mnt /etc/fstab || echo "$server_net_mnt $client_mnt nfs" | sudo tee -a /etc/fstab
 
   sudo mkdir -p $client_mnt && sudo chmod 0755 $client_mnt
 
   sudo mkdir -p $client_mnt && sudo chmod 0755 $client_mnt
 
   sudo apt install -y nfs-client  # nfs-common
 
   sudo apt install -y nfs-client  # nfs-common
 +
  sudo mount -a
 
}
 
}
  
Line 36: Line 63:
 
   sudo mv /etc/exports /etc/exports.bkp
 
   sudo mv /etc/exports /etc/exports.bkp
 
   for ip in $client_ips; do
 
   for ip in $client_ips; do
     echo "$server_mnt    $ip(rw,sync,no_subtree_check,insecure,no_root_squash)" | sudo tee -a /etc/exports
+
     # risky echo "$server_mnt    $ip(rw,sync,no_subtree_check,insecure,no_root_squash)" | sudo tee -a /etc/exports
 +
    # http://fullyautolinux.blogspot.com/2015/11/nfs-norootsquash-and-suid-basic-nfs.html?m=1
 +
    echo "$server_mnt    $ip(rw,sync,no_subtree_check,insecure,root_squash)" | sudo tee -a /etc/exports
 
   done
 
   done
 
   sudo chmod 0644 /etc/exports
 
   sudo chmod 0644 /etc/exports
Line 45: Line 74:
 
install_localpathprov(){
 
install_localpathprov(){
 
   # https://github.com/rancher/local-path-provisioner
 
   # https://github.com/rancher/local-path-provisioner
 +
  kubectl get sc | grep ^local-path && return
 +
  echo "Installing rancher local-path-provisioner in 10 seconds."
 +
  sleep 10
 
   kubectl apply -f https://raw.githubusercontent.com/rancher/local-path-provisioner/master/deploy/local-path-storage.yaml
 
   kubectl apply -f https://raw.githubusercontent.com/rancher/local-path-provisioner/master/deploy/local-path-storage.yaml
 
   kubectl create -f https://raw.githubusercontent.com/rancher/local-path-provisioner/master/examples/pvc/pvc.yaml
 
   kubectl create -f https://raw.githubusercontent.com/rancher/local-path-provisioner/master/examples/pvc/pvc.yaml
Line 55: Line 87:
 
elif [[ "$node_type" == "client" ]]; then
 
elif [[ "$node_type" == "client" ]]; then
 
   install_client
 
   install_client
 +
  install_localpathprov
 
else
 
else
 
   echo "E: Unsupported node_type."
 
   echo "E: Unsupported node_type."
Line 62: Line 95:
  
 
# Testing
 
# Testing
 +
```
 
mkdir ~/mnt
 
mkdir ~/mnt
sudo mount -t nfs $server_host:/28a1f1f8-e686-11eb-bf45-7f257ca7269b ~/mnt
+
sudo mount -t nfs $server_net_mnt ~/mnt
 
echo hi > ~/mnt/hi.txt
 
echo hi > ~/mnt/hi.txt
 
cat ~/mnt/hi.txt
 
cat ~/mnt/hi.txt
 +
```
 +
 +
https://www.linuxtechi.com/configure-nfs-persistent-volume-kubernetes/

Latest revision as of 15:59, 28 November 2023

Change default StorageClass

kubectl patch storageclass local-path -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
kubectl patch storageclass microk8s-hostpath -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"false"}}}'

Clients Quick

apt install -y nfs-client
mkdir -p /opt/local-path-provisioner

vi /etc/fstab
nas1:/<your mount point> /opt/local-path-provisioner nfs4  _netdev,auto  0  0
mount -a
ls /opt/local-path-provisioner/

Using a script

It is important to understand this file. Client ip addresses are highly trusted and controlled kubernetes nodes.

Do not run files on nas directory it should only be used as a file store.

#!/usr/bin/env bash
set -e
if [ "$#" -ne 3 ]; then
  echo "Usage: $0 <uuid> <nfs node type server/client> \"<client ips>\""
  echo "Example: $0 28a1f1f8-e686-11eb-bf45-7f257ca7269b server \"10.x.x.x 10.x.x.y 10.x.x.z\""
  exit
fi

uuid=$1
node_type=$2
client_ips=$3
server_host=nas
server_mnt=/$uuid
server_net_mnt="${server_host}:${server_mnt}"
client_mnt=/opt/local-path-provisioner


if ! [[ $uuid =~ ^\{?[A-F0-9a-f]{8}-[A-F0-9a-f]{4}-[A-F0-9a-f]{4}-[A-F0-9a-f]{4}-[A-F0-9a-f]{12}\}?$ ]]; then
  echo "E: Invalid uuid format."
exit
fi


install_client(){
  echo Installing client.
  sudo sed -i "/$uuid/d" /etc/fstab
  grep $server_mnt /etc/fstab || echo "$server_net_mnt $client_mnt nfs" | sudo tee -a /etc/fstab
  sudo mkdir -p $client_mnt && sudo chmod 0755 $client_mnt
  sudo apt install -y nfs-client  # nfs-common
  sudo mount -a
}


install_server(){
  echo Installing server.
  sudo apt install nfs-kernel-server
  sudo mkdir -p $server_mnt && sudo chmod 0755 $server_mnt
  sudo mv /etc/exports /etc/exports.bkp
  for ip in $client_ips; do
    # risky echo "$server_mnt    $ip(rw,sync,no_subtree_check,insecure,no_root_squash)" | sudo tee -a /etc/exports
    # http://fullyautolinux.blogspot.com/2015/11/nfs-norootsquash-and-suid-basic-nfs.html?m=1
    echo "$server_mnt    $ip(rw,sync,no_subtree_check,insecure,root_squash)" | sudo tee -a /etc/exports
  done
  sudo chmod 0644 /etc/exports
  sudo systemctl reload nfs-server
}


install_localpathprov(){
  # https://github.com/rancher/local-path-provisioner
  kubectl get sc | grep ^local-path && return
  echo "Installing rancher local-path-provisioner in 10 seconds."
  sleep 10
  kubectl apply -f https://raw.githubusercontent.com/rancher/local-path-provisioner/master/deploy/local-path-storage.yaml
  kubectl create -f https://raw.githubusercontent.com/rancher/local-path-provisioner/master/examples/pvc/pvc.yaml
  kubectl create -f https://raw.githubusercontent.com/rancher/local-path-provisioner/master/examples/pod/pod.yaml
}


if [[ "$node_type" == "server" ]]; then
  install_server
elif [[ "$node_type" == "client" ]]; then
  install_client
  install_localpathprov
else
  echo "E: Unsupported node_type."
fi

Testing

mkdir ~/mnt
sudo mount -t nfs $server_net_mnt ~/mnt
echo hi > ~/mnt/hi.txt
cat ~/mnt/hi.txt

https://www.linuxtechi.com/configure-nfs-persistent-volume-kubernetes/