Lesson 8 of AcWing basic Linux course renting cloud server and configuration environment

8. Renting ECs and configuration environment

8.1 introduction

Role of cloud platform

  • Store the docker container and let the calculation be carried out in the cloud
  • Get the public IP address so that others can access our services

Select any cloud platform and configure it as follows

  • 1-core 2GB (it can be dynamically expanded in the later stage, and it doesn't matter if the configuration is lower in the early stage)
  • The network bandwidth is paid according to the amount, and the maximum bandwidth can be filled (the cost depends on the amount and has nothing to do with the maximum bandwidth)
  • System version: Ubuntu 20.04 LTS (Unified version is recommended to avoid configuration incompatibility later)

Rented server

  • roughcast
    • Frame (DJango)
    • Thrift
  • Services (MySQL, etc.)
    • socket mode: IP:Port
    • http mode

It is not recommended to build a house directly on the blank server, but build a docker first to facilitate later migration to other platforms, and it is used for standardization and improve development efficiency. Therefore, the main development work is in the docker in the blank server.

8.2 basic configuration

Create user

After logging in with ssh, create user acs

adduser acs

Assign sudo permissions

usermod -aG sudo acs  # Give acs user sudo permission

Configure password free login mode

Configure ~ /. Locally ssh/config

Put local ~ / ssh/id_ rsa. Copy pub to ~ /. Of the server ssh/authorzied_keys

Configure the working environment of the new server

Copy the bash, vimrc and tmux configurations of Ac Terminal to the server

scp .bashrc .vimrc .tmux.conf server_name:

Install tmux

sudo apt-get update
sudo apt-get install tmux

Open tmux and install docker in tmux, which can prevent the terminal from closing abnormally and recover to the previous progress.

8.3 installation and use of docker

8.3.1 installation

Installation reference Official website tutorial

(1) Configure warehouse

sudo apt-get update

sudo apt-get install \
    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

(2) Install docker

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

After installation, you can view the version through docker --version. If it appears, the installation is successful.

Before using, you can add sudo permission to the docker command to avoid adding sudo every time you use docker, $USER represents the current USER. After executing the following command, the docker group will be automatically created and the current USER will join.

sudo usermod -aG docker $USER

8.3.2 tutorial

(1) Basic principles

There are many kinds of cloud platforms: lightweight cloud platform, ECS, etc. In order to solve the difficulty of migration, the server can be configured in docker, so that it can be changed to different platforms through docker to reduce the cost of migration.

A docker can have multiple image images, and an image can have multiple container containers. But this is only a logical structure, not a storage structure. Dockers will not be stored independently. The relationship between image and container can be regarded as the relationship between seal and pattern.

In practical application, a container is a server. When migration is needed, package the container into an image on the old cloud platform, and then read the image on the new cloud platform.

(2) Images

  • Pull image: docker pull ubuntu:20.04
  • Enumerate local images: docker images
  • Delete image: docker image rm ubuntu:20.04 or docker rmi ubuntu:20.04
  • Create a container image: docker [container] commit CONTAINER IMAGE_NAME:TAG
  • Export image Ubuntu: 20.04 to file ubuntu_20_04.tar: docker save -o ubuntu_20_04.tar ubuntu:20.04
  • From file ubuntu_20_04.tar loading image ubuntu:20.04: docker load -i ubuntu_20_04.tar

(3) container

  • Create container through image: docker [container] create -it ubuntu:20.04
  • Enumerate local containers: docker ps -a
  • Start container: docker [container] start CONTAINER
  • Stop container: docker [container] stop CONTAINER
  • Restart container: docker [container] restart CONTAINER
  • Create container through image and start: docker [container] run -itd ubuntu:20.04
  • Enter container: docker [container] attach CONTAINER
    • Suspend container: press Ctrl-p first, then Ctrl-q
  • Execute the command in the container: docker [container] exec CONTAINER COMMAND
  • Delete container: docker [container] rm CONTAINER
  • Delete all stopped containers: docker container prune
  • Export container to file XXX tar: docker export -o xxx.tar CONTAINER
  • The local file XXX Tar is imported as a mirror image_name:tag: docker import xxx.tar image_name:tag
  • Viewing the processes in the container: docker top CONTAINER
  • View the status of all containers: docker stats
  • Copy (similar to scp): docker cp xxx CONTAINER:xxx
  • Rename: docker rename CONTAINER1 CONTAINER2
  • Modify memory limit: docker update container -- memory 500MB

docker export./ The difference between import and docker save/load

  • export/import will discard the history and metadata information and only save the snapshot state of the container at that time
  • Load / save will save a larger volume of complete records

8.3.3 SSH login docker

Enter AC Terminal and

scp /var/lib/acwing/docker/images/docker_lesson_1_0.tar server_name:  # Upload the image to your cloud server
ssh server_name  # Log in to your own cloud server

docker load -i docker_lesson_1_0.tar  # Load the image locally
docker run -p 20000:22 --name my_docker_server -itd docker_lesson:1.0  # Map the port 22 in the container (SSH port in the container) to the local port 20000, and then create and run docker_ Less: 1.0 image

docker attach my_docker_server  # Enter the created docker container
passwd  # Set root password

Note: first go to the cloud platform to control and modify the security group configuration. Open port 20000, and fill in 0.0.0.0/0 for IP, which means all are allowed. The protocol is TCP, otherwise the command to bind ports is invalid.

Then use the IP of the cloud platform server as the IP of the docker and 20000 as the port number to access the docker through SSH

After setting, you can use SSH to log in to the server in docker. You can set password free login according to the SSH section to optimize the SSH login method. Note that SSH needs to add the parameter - p 20000.

If apt get downloads the software slowly, you can modify the software source to Tsinghua University open source software mirror station.

When using the container for the first time, use the command apt get install sudo to install sudo first

8.4 operation core code

task1

(1) First, rent a cloud server in Tencent cloud. After obtaining the public IP, log in to the root account on the ECS, set the root account password with passwd, and then log in to the directory ~ / Create file authorized under ssh_ Keys to copy the public key of AC Terminal to the file.

(2) Modify ~ /. In AC Terminal ssh/config file

Host acs
	HostName xxx.xxx.xxx.xxx
	User root

(3) Save server alias

Execute at AC Terminal

cd ~/homework/lesson_8/homework_0
echo "acs" > server_name.txt

task2

Log in to ECS with ssh acs in AC Terminal and execute the following command

sudo apt-get update

sudo apt-get install \
    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 docker-ce docker-ce-cli containerd.io

sudo usermod -aG docker $USER

task3

(1) Copy image

scp /var/lib/acwing/docker/images/docker_lesson_1_0.tar acs:  # Upload the image to the cloud server you rent
ssh acs  # Log in to your own cloud server

(2) Load image

docker load -i docker_lesson_1_0.tar  # Load the image locally

(3) Open port

Open port 20000 on the ECS console, set the IP to 0.0.0.0/0, and select TCP as the protocol

(4) Create containers with images and bind ports

docker run -p 20000:22 --name my_docker_server -itd docker_lesson:1.0  # Create and run docker_ Less: 1.0 image

(5) Open container

docker attach my_docker_server  # Enter the created docker container

(6) Create a new user

passwd  # Set root password

adduser acs  # Create user acs
su acs  # Switch to acs user

(7) Add SSH public key

Copy the SSH public key of AC Terminal to ~ /. Of server acs user ssh/authorized_keys

cd ~
mkdir .ssh
vim .ssh/authorized_keys

(8) Configure docker container alias

Modify ~ /. In AC Terminal ssh/config, add the following configuration information, where IP is the public network IP of the cloud platform server.

Host acs_docker
	HostName xxx.xxx.xxx.xxx
	User acs

(9) Save docker container alias

Execute at AC Terminal

cd ~/homework/lesson_8/homework_2
echo "acs_docker" > server_name.txt

Keywords: Linux AcWing

Added by wikstov on Wed, 02 Mar 2022 09:00:19 +0200