操作场景
海量文件服务适用于共享访问的场景,当业务中需要使用多台云主机访问同一文件系统时,您可以通过我们提供的脚本,填写配置文件、执行脚本命令,实现多台云主机批量挂载同一文件系统,减少操作时间,提高工作效率。
注意
执行批量挂载的云主机实例需要与海量文件系统归属于同一资源池的同一VPC下。
目前仅支持Linux操作系统的云主机,请参考支持的操作系统。
仅支持root账户进行批量挂载操作。
务必按照本文中要求的固定格式填写需要执行批量挂载的云主机的相关信息。
请保证本文中创建的inventory、auto_batch_mount.sh文件与mount_nfs.yml文件在同一目录下,否则会导致挂载失败。
操作步骤
-
登录天翼云控制中心,单击管理控制台左上角的,选择地域。
-
选择“计算>弹性云主机”,进入弹性云主机控制台页面,找到即将执行批量挂载操作的任何一台云主机。
-
以root用户登录该弹性云主机,登录方法参考登录Linux弹性云主机。
-
创建并填写配置文件inventory。
1)在Linux vi命令行工具中依次执行以下命令创建配置文件inventory并打开。
touch inventory
vi inventory
2)将按照下述格式填写的内容复制到配置文件inventory中,保存并退出。需要将各参数替换成相应的实际值,各参数说明见下方表格。配置文件内容可在windows记事本中或者Linux vi命令实现编辑,供后续使用。复制完成后输入
:wq
保存并退出。[all:vars] share_path=挂载地址 [target_host] 弹性IP passwd=root用户密码 local_path=本地挂载路径 弹性IP passwd=root用户密码 local_path=本地挂载路径 弹性IP passwd=root用户密码 local_path=本地挂载路径 ……
参数 说明 云主机弹性IP 在云主机详情页中“弹性IP”页签获取该云主机公网IP的IP地址。 root用户密码 root用户密码。 本地挂载路径 本地挂载路径为云主机上用于挂载文件系统的本地路径,本操作中将通过脚本自动创建,无须额外创建。 挂载地址 可在文件系统详情页获取。 -
创建批量挂载脚本auto_batch_mount.sh。
1)创建脚本文件,并打开:
touch auto_batch_mount.sh
vi auto_batch_mount.sh
2)将下列内容复制到挂载脚本中,保存退出
(:wq)
:#!/bin/bash # configure password-free function ssf_free_login() { success_install_expect=0 # Check if expect is installed type expect &>/dev/null if [ $? -eq 0 ]; then echo "Expect is already installed." success_install_expect=1 else echo "Expect is not installed. Install now..." # Install expect if file exists if type yum &>/dev/null; then yum install -y expect elif type apt-get &>/dev/null; then apt install expect || ( apt update apt install expect -y ) else echo "Unsupported operating system." exit 1 fi success_install_expect=1 fi if [ $success_install_expect -eq 0 ]; then echo "Expect installation failed." exit 1 fi [ ! -f /root/.ssh/id_rsa.pub ] && ssh-keygen -t rsa -P "" -f /root/.ssh/id_rsa # conf file config_file="inventory" target_hosts=$(awk '/^\[target_host\]/{flag=1;next}/^\[/{flag=0}flag' "$config_file") while read line; do # export ip ip=$(echo "$line" | awk '{print $1}') # export password pass_word=$(echo "$line" | awk -F 'passwd=' '{print $2}' | awk '{print $1}') expect <<EOF spawn ssh-copy-id $ip expect { "yes/no" { send "yes\n";exp_continue } "password" { send "$pass_word\n";exp_continue } eof } EOF done <<<"$target_hosts" } # Check if ansible is installed function install_ansible() { command -v ansible >/dev/null 2>&1 || { if [ -f /etc/redhat-release ]; then echo "Install ansible now......" sudo yum install -y ansible elif [ -f /etc/lsb-release ]; then echo "Install ansible now......" apt install -y ansible else echo "Unsupported operating system." exit 1 fi } } ssf_free_login install_ansible # Run Ansible playbook if [ -f inventory ] && [ -f mount_nfs.yml ]; then ansible-playbook -i inventory mount_nfs.yml else echo "One or both files are missing." exit 1 fi
-
创建ansible脚本执行的yml文件:mount_nfs.yml。
1)创建yml文件,并打开:touch mount_nfs.yml
vi mount_nfs.yml
2)复制下述内容到yml文件中,保存退出
(:wq)
:说明可根据实际需求修改mount_nfs.yml末尾处tasks中挂载命令的参数,参数说明请参考挂载NFS文件系统到弹性云主机 (Linux)。
--- - name: Test Connectivity hosts: all gather_facts: false tasks: - name: "Check whether node is reachable" action: ping ignore_errors: false tags: sfs_ping - name: Install NFS Client hosts: all gather_facts: yes tasks: - name: Install NFS Client on CentOS yum: name: nfs-utils state: present when: ansible_distribution == 'CentOS' ignore_errors: yes - name: Install NFS Client on Ubuntu apt: name: nfs-common state: present when: ansible_distribution == 'Ubuntu' ignore_errors: yes - name: Creat local path hosts: all gather_facts: false tasks: - name: "Create local mount path" file: path: "{{local_path}}" state: directory - name: Check if local_path is mounted hosts: target_host gather_facts: yes tasks: - name: Check if local_path is mounted command: "mountpoint {{ local_path }}" ignore_errors: yes register: mountpoint_check failed_when: false - name: Fail if local_path is mounted on any host assert: that: "not mountpoint_check.rc == 0" success_msg: "local_path is not mounted on any host." fail_msg: "local_path is mounted on at least one host." - name: Mount NFS Filesystems hosts: target_host gather_facts: yes tasks: - name: Mount NFS Share mount: src: "{{share_path}}" path: "{{local_path}}" fstype: nfs opts: "vers=3,proto=tcp,async,nolock,noatime,nodiratime,wsize=1048576,rsize=1048576,timeo=600" state: mounted become: yes
-
执行批量挂载脚本。执行时注意不同操作系统执行命令不同。
CentOS操作系统
sh auto_batch_mount.sh
Ubuntu操作系统
bash auto_batch_mount.sh
执行成功结果可参考下图。
也可执行
df -h
命令,查看当前云主机是否已经当前挂载文件系统,参考下图。