[Practical exercise kubernetes & docker-docker] Installation and Use

0, docker introduction

World trend, long period of division, together for a long score.

Former computer has a mainframe, minicomputer (high performance), the back and the emergence of x86 servers and PC personal computers (mainframes, minicomputers performance surplus). Then found Stand-alone is not enough (less than single x86 server performance), so they need multiple x86 servers, or do load balancing cluster technology.

X86 server then found some upper run only one or two applications, excess performance, resource slack (excessive portion x86 performance single server), then appeared virtualization, a single physical computer into a plurality of independent virtual Virtual machine.

Then some virtual machine performance is not enough, they need to do load balancing cluster (part of the virtual machine performance is low), but also some virtual machine performance excessive ...... Thus it happened today protagonist, Docker up.


In the past we need to install an apache and mysql, to build a web service, we need a laptop or x86 server, install a windows or linux systems come deployed applications.

And when we use virtual machines, physical machines also need to install the Hypervisor, then install windows or linux system again deploy applications in virtual machines on it.

Anyway, to deploy any application, you need not do without the need + server operating system.

And run the operating system has always been there overhead, so in order to deploy an application, the application would have been required in addition to the CPU, memory,

But also allocating resources to run the operating system, bringing unnecessary overhead is very wasteful.


Is there a way, do not give direct operating system, and then install the application directly to it? Docker can be done.

Docker linux operating system may be above, directly through the docker mirror, to deploy the application, for example a ready made docker mysql the mirror, the mirror can be pulled directly from the image storage, and a key deployment instances docker. The system will then automatically be part of the CPU, memory for operating the docker instance, can be provided directly can be used of a mysql.

In the same inside the virtual machine, you can continue to create multiple independent mysql instance, can be a normal external services, and between instances are isolated from each other. Or you can continue to deploy instances of other applications (such as apahce / tomcat) to the same virtual machine inside.


Experimental environment: CentOS7


1, environment preparation:

1.1 turn off the firewall

systemctl stop firewalld
systemctl disable firewalld

1.2 Close selinux

setenforce 0
vi /etc/selinux/config
SELINUX=disabled

Configuring and mounting extensions source yum yum

Ali cloud using the yum source configured slightly, refer to [practice] drills Linux operating system 04- to configure yum source https://blog.51cto.com/14423403/2416049


2, installation docker

yum -y install docker


3, open docker

service docker start
ps -ef | grep docker

  # Check whether to activate the docker

Thus, docker single installation, need to deploy the application instance, need to pull the corresponding docker mirror.


4, pulling the mirror

docker pull mysql

# Mirrored pull mysql

docker pull tomcat

# Pull tomcat image

docker pull mysql:5.7.19

# Pull mysql5.7.19 mirrored version

Since the question pulled direct access to foreign sources, so the pull of a very long time, and may even pull less than prompt timeout, they can add their own domestic source pull, faster.

001.png

Ali cloud access the link, you can create a free docker mirror accelerator. (Need to register on their own account, then enable, there will be a string of token code)

we /etc/docker/daemon.json
{ 
"registry-mirrors": ["https://XXXXXXXX.mirror.aliyuncs.com"] 
}

XXXXXXXX fill in their actual token, then you need to run the following command to reload take effect.

systemctl daemon-reload
systemctl restart docker

Pulling back and found a lot of speed has improved significantly.

002.png


5. Other docker command

docker images

 # View image has been pulling the

003.png

docker version

# View docker version


6, create a container

1) Create a mysql container

docker run --name mysql01 -e MYSQL_ROOT_PASSWORD=123456 -d -p 3306:3306 mysql

# Run mysql container, pay attention to whether there are warning ipv4 forwarding After you create the container, if any, go to Troubleshooting 1

docker run --name mysql01 -e MYSQL_ROOT_PASSWORD=123456 -d -p 3306:3306 mysql:5.7.19

# Run mysql5.7.19 container, note that the previous 3306 is exposed to the external port provides access to the back of the 3306 is docker instances inside the internal port .

2) Create a tomcat container

#docker run --name tomcat1 -d -p 8080:8080 --link mysql01:mysql tomcat
docker run --name tomcat1 -d -p 8080:8080 tomcat

# Test, you can open web pages, access hosyt IP address (ip non-tomcat container), http: // Host IP: 8080 /

# You can also copy test.war bag into the container, then placed under webapps / folder, then enter / usr / local / tomcat / bin /, run ./catalina.sh run, then it will start up the tomcat .

# Test, open the browser https: // hostIP: 8080 / test, you can see the page returned.

# Note that: the front port of the access port is really docker, e.g. mysql to 306: 3306, back inside the container is the port docker mysql used in the host 306 to this port occupied docker container.


7. Check the container

docker ps -a

We were able to see the ID of the container vessel, and the operating status (up, exist, etc.)

004.png

ps -ef | grip docker


8, into the container

docker exec -ti tomcat1 bash
docker exec -ti mysql01 bash

Exit container according to exit, with mysql, for example, into the container to operate a test, you can directly log in to access the database.

mysql -u root -p

Then we grant remote access to the root user can try

grant all on *.* to 'root'@'%' identified by '1qaz!QAZ';
flush privileges;

Casually looking for a network of up to machines, just docker database access test.

mysql -u root -h 10.1.30.22 -P 3306 -p

IP address for the machine #h be accessed is specified, the port is designated P -P capital access, since the test machine (10.1.30.22) itself is not installed mysql, so if the above command can log in normally to mysql this machine, mysql instance docker sure it is built up. The rest is conventional mysql database operated.

005.png


9, the container stop or restart

docker stop tomcat1
docker stop mysql01
docker restart tomcat1
docker restart mysql01


10、查看容器的私网IP,vim等

进入容器后,是没有ifconfig,vim等命令的,需要每个容器单独安装。

apt-get update
apt install net-tools   #安装ifconfig
apt install iputils-ping  #安装ping
apt install vim#安装vim


11、主机往容器拷贝文件

docker cp **.sql mysql01:/root/

#将**.sql文件拷贝到mysql01容器的root目录

docker cp ROOT/ tomcat1:/root/

#将ROOT/目录及里面的内容全部拷贝到tomcat1容器的root/目录下。


12、删除容器

docker rm tomcat1
docker rm mysql01


13、排错

14.1运行docker时,显示WARNING: IPv4 forwarding is disabled. Networking will not work.

解决办法:vim  /usr/lib/sysctl.d/00-system.conf

增加net.ipv4.ip_forward=1

然后systemctl restart network


14.2mysql的docker运行后,远程访问不到

grant all privileges on *.* to root@"%" identified by "1qaz!QAZ";
flush privileges;

mysql默认只能本地登录,如需远程登录,需要放开权限。


14.3mysql8.0.15后,toad等工具远程报错 Authentication method 'caching_sha2_password' not supported by any of the available plugins.

如果拉取镜像的时候,没有指定版本,那么mysql可能拉取了最新的mysql8镜像,因为mysql8修改了默认的加密方式,所以toad与navicat for mysql可能链接不上,可以这样查看。

select Host,User,plugin from mysql.user;

如要修改mysql的加密方式,可以修改配置文件

vim /etc/mysql/my.cnf

然后添加

default_authentication_plugin=mysql_native_password

还需要

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456'; 
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';

可以用这个命令再次验证

select Host,User,plugin from mysql.user;


14.4 docker里面安装JDK17

请参考tomcat+mysql搭建网站,但是alternatives运行要变为update-alternatives,其他命令一样

Guess you like

Origin blog.51cto.com/14423403/2416583