一个脚本完成初始化机器(含git/docker/docker-compose/go/make,监控)
前言:
之前一台新机器需要手动安装好多个软件,耗时费力。
#!/bin/bash #vol=` lsblk |grep '1T' |awk '{print $1}'` vol=`lsblk |grep 'disk' |grep -v '40G' |awk '{print $1}'` initEnv() { gdisk /dev/$vol <<EOF n w y EOF mkfs.ext4 /dev/${vol}p1 mkdir -p /data echo "/dev/${vol}p1 /data ext4 defaults 1 2" >> /etc/fstab mount -a } installEnv() { # install docker https://docs.docker.com/engine/install/ubuntu/ sudo apt-get install -y \ ca-certificates \ curl \ gnupg \ lsb-release curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null sudo apt-get update sudo apt-get install -y docker-ce docker-ce-cli containerd.io # install docker-compose https://docs.docker.com/compose/install/ sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose # install go (ubuntu arm) wget -c https://go.dev/dl/go1.17.10.linux-amd64.tar.gz tar -C /usr/local -xzf go1.17.10.linux-amd64.tar.gz export PATH=$PATH:/usr/local/go/bin go version # make sudo apt-get update sudo apt-get install ubuntu-make #end of scripts } dockerData() { systemctl stop docker cd /var/lib cp -r docker docker_ori mkdir /data/docker -p mv /var/lib/docker /data/docker ln -s /data/docker/docker /var/lib/docker systemctl start docker } initLrpDir() { cd "/data" mkdir "lrp_node_piece" cd "/data" mkdir "lrp_script" cd "lrp_script" if [ ! -d oec_devops ]; then git clone -b main git@github.com:okex/oec_devops.git fi } monitor() { cat >./tmp <<eof version: '2' services: node-exporter: image: prom/node-exporter container_name: node-exporter hostname: node-exporter restart: always volumes: - /proc:/host/proc:ro - /sys:/host/sys:ro - /:/rootfs:ro command: - --path.procfs=/host/proc - --path.sysfs=/host/sys - --path.rootfs=/rootfs ports: - "9100:9100" cap_add: - SYS_TIME cadvisor: image: google/cadvisor:latest container_name: cadvisor command: "--enable_load_reader=true" privileged: true restart: always volumes: - /:/rootfs:ro - /var/run:/var/run:rw - /sys:/sys:ro - /var/lib/docker/:/var/lib/docker:ro - /cgroup:/sys/fs/cgroup:ro - /dev/disk/:/dev/disk:ro ports: - "9101:8080" eof docker-compose -f ./tmp up -d rm ./tmp } initEnv installEnv dockerData monitor