Difference between revisions of "NFS and Microk8s"

From UVOO Tech Wiki
Jump to navigation Jump to search
Line 5: Line 5:
 
```
 
```
 
#!/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 13: Line 14:
 
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
 
# 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"
+
# client_ips="10.206.225.187 10.206.225.186 10.206.225.185"
  
 
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 28: Line 30:
 
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 49: Line 53:
 
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 59: Line 66:
 
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."

Revision as of 23:29, 16 July 2021

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}"
# local_mnt=/var/snap/microk8s/common/nas-nfs-standard
client_mnt=/opt/local-path-provisioner
# client_ips="10.206.225.187 10.206.225.186 10.206.225.185"

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
    echo "$server_mnt    $ip(rw,sync,no_subtree_check,insecure,no_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_host:/28a1f1f8-e686-11eb-bf45-7f257ca7269b ~/mnt echo hi > ~/mnt/hi.txt cat ~/mnt/hi.txt