background
With the large-scale application of cloud server, the migration of traditional computer room environment to cloud environment has become a general trend. The cabinet installation, strong and weak current wiring installation, cable power selection, network cable optical fiber selection and connection of traditional computer room environment, server rack, switch firewall rack, switch firewall debugging, server RAID setting, hardware capacity expansion The overall connectivity debugging is greatly alleviated by the cloud environment. People can focus on the maintenance and construction of software service environment. The installation of the operating system is also omitted. The requirements for the hardware environment are estimated according to the business flow, concurrent computing and throughput of the business system, and the corresponding parameter data is selected in the graphical interface provided by the cloud service provider. The operating system only needs the corresponding selection. The main thing used in the work is the linux operating system. This paper takes CentOS7 as an example.
objective
After the corresponding operating system is purchased and selected, the common initialization work required by the system is operated. Commonality means repetitive work, necessity, and mechanical. The personnel's operation is realized by script operation. Upload the code to the git code repository. Each time you use it, you can download it to the local machine for execution.
thinking
During the selection of the operating system, the system super administrator password will be set, and the security rules are issued 22 and 3389 by default to facilitate remote connection. After setting, use the remote tool to connect remotely (use the system EasySSH tool to connect here)
1. Create user
2. Install common component tools
3. Docker and docker compose
4. Create the overall script and call the above three scripts
The above is just a simple process script execution, and more is to use scripts to run their repetitive work. At present, linux system comes with python, which can also be implemented in python. This paper is implemented by shell script. The main idea is that the tools can be better implemented.
implementation
Create overall script
0_init_os.sh
#!/bin/bash # --------------------------------------------- # File Name : 0_init_os.sh # Author : shalter # Mail : shalter@xxxx.com # Date : 2022-01-11 # Description : # --------------------------------------------- # 1 create user source 1_create-user.sh # 2. Install common software git/vim/gcc/update source 2_install-tool.sh # 3. Install docker and docker compose . 3_install-docker.sh echo 'Init system is finish!'
explain:
The total script is mainly executed by calling three function scripts
Modify file permissions
chmod +x 0_init_os.sh
Create system user
1_create-user.sh
Fill in the user name, group and password to be created into the corresponding user_ NAME/USER_ GROUP/USER_ In passwd, save the file
#!/bin/bash # User name to be created, example: USER_NAME=myuser USER_NAME= # Create a user group to which the user belongs. Example: USER_GROUP=mygroup USER_GROUP= # User password, example: USER_PASSWD=Cloud12#$ USER_PASSWD= # Calibration parameters function check_param() { if [[ ! -n ${USER_NAME} ]] || [[ ! -n ${USER_GROUP} ]] || [[ ! -n ${USER_PASSWD} ]]; then echo "ERROR: Please check the param USER_NAME,USER_GROUP,USER_PASSWD can not be null" exit 1; fi } # Create user function creat_user() { check_param #create group grep "^${USER_GROUP}" /etc/group &> /dev/null if [ $? -ne 0 ]; then groupadd ${USER_GROUP} fi #create user id ${USER_NAME} &> /dev/null if [ $? -ne 0 ]; then useradd -g ${USER_GROUP} ${USER_NAME} -d /home/${USER_NAME} echo ${USER_PASSWD}| passwd ${USER_NAME} --stdin chage -M 99999 ${USER_NAME} fi }
Modify file permissions
chmod +x 1_create-user.sh
Installation component tool
2_install-tool.sh
#!/bin/bash # --------------------------------------------- # File Name : 2_installtool.sh # Author : shalter # Mail : shalter@xxxx.com # Date : 2022-01-12 # Description : install git/vim/tools # --------------------------------------------- # 1 git echo 'install git' yum install git -y # 2 vim echo 'install vim' yum install vim -y # 3 conf vim git clone https://gitee.com/shalter/vim.git cd vim vim_path=$(vim --version | grep -n /usr/share/vim | awk '{ print $5 }'| sed 's/\"//g') mv freya.vim $vim_path/colors mv vimrc ~./vimrc # 4 install tools echo 'install tools' yum install gcc gcc-c++ cmake pcre pcre-devel zlib zlib-devel openssl openssl-devel wget telnet setuptool lrzsz dos2unix net-tools bind-utils tree screen iftop ntpdate tree lsof iftop iotop -y
explain:
Installation of common tools
Modify permissions
chmod +x 2_install-tool.sh
Install the container
3_install-docker.sh
#!/bin/bash # View kernel version uname -r # Update system yum update -y # Uninstall old version yum remove docker docker-common docker-selinux docker-engine # Install dependent packages yum install -y yum-utils device-mapper-persistent-data lvm2 # Set yum source yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo # View docker version yum list docker-ce --showduplicates | sort -r # Install docker yum install docker-ce -y # Start docker systemctl start docker # Set startup systemctl enable docker # Get user name with UID=1000 user_name=$(getent passwd 1000|awk -F : '{print $1}') # Add to docker group gpasswd -a $user_name docker # Update user groups newgrp docker # Verify installation docker version # output echo "Complete! # Download docker compose git clones https://gitee.com/shalter/docker-compose.git # Enter the download directory cd docker-compose # Modify file terms mv docker-compose-Linux-x86_64 docker-compose # Modify file executable permissions chmod +x docker-compose # move file mv docker-compose /usr/local/bin/docker-compose # View validation version docker-compose -v
Modify permissions
chmod +x 3_install-docker.sh
Code warehouse
Upload the above code to the code warehouse. When you use it, you can download and install it from the code warehouse.
Create warehouse
Create code warehouse
Git global settings:
git config --global user.name "xxxxx" git config --global user.email "xxxxhp@126.com"
Upload code
Enter script directory
cd script git init git add 0_init_os.sh 1_create-user.sh 2_install-tool.sh 3_install_docker.sh git commit -m "init system of centos7" git remote add origin https://gitee.com/shalter/init_centos7.git git push -u origin "master"
Log in to the new server
Download execution
# Create directory mkdir -p /home/{$user}/script/ # Enter directory cd /home/{$user}/script/ # Download script git clone https://gitee.com/shalter/init_centos7.git # Enter folder cd init_centos7 # Modify script executable chmod +x *.sh # Execute script ./0_init_os.sh
The above is to initialize CentOS 7 operating system. If multiple servers cooperate with ansible operation. You can customize the operation according to your own needs for the server.