Kubernetes Certification Exam Self-study Series | Container Interconnection

Book source: "CKA/CKAD Test Guide: From Docker to Kubernetes Complete Raiders"

Organize the reading notes while studying, and share them with everyone. If the copyright is violated, it will be deleted. Thank you for your support!

Attach a summary post: Kubernetes Certification Exam Self-study Series | Summary_COCOgsta's Blog-CSDN Blog


Sometimes we need multiple applications to work together to provide external services, such as using two applications wordpress and mysql to build a blog. Wordpress needs to be connected to mysql, so two containers are needed, and the wordpress container needs to be connected to the mysql container.

1.9.1 Method 1: Access via container IP

In the previous actual combat exercise, a container named db has been created with an ip of 172.17.0.2 and a database called blog inside.

[root@vms100 ~]# docker exec db ip a | grep 'inet '
  inet 127.0.0.1/8 scope host lo
  inet 172.17.0.2/16 scope global eth0
[root@vms100 ~]#
复制代码

Next, create a container using the wordpress image, which needs to be connected to mysql.

The variables that this container needs to use are as follows.

--WORDPRESS_DB_HOST is used to specify the address of the mysql server.

--WORDPRESS_DB_USER is used to specify the username to log in to mysql.

--WORDPRESS_DB_PASSWORD is used to specify the password to log in to mysql.

--WORDPRESS_DB_DATABASE is used to specify the required database.

Step 1: Create a wordpress container and publish the port so that external hosts can access it.

[root@vms100 ~]# docker run -dit --name blog --restart=always -e WORDPRESS_DB_HOST=172.17.0.2 -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=redhat -e WORDPRESS_DB_NAME=blog -p 80:80 hub.c.163.com/library/wordpress
85647b3af3d21d110971a4b40ccf650a6e396baae6d8e775d32333e16349cc45
[root@vms100 ~]#
复制代码

Here the address of the mysql server is specified by the variable WORDPRESS_DB_HOST.

Enter 192.168.26.100 in the address bar, as shown in Figure 1-18.

Select Chinese→Continue, as shown in Figure 1-19.

There is no requirement to enter the database information, because it can already automatically connect to the database.

There is a problem with this method, that is, if the db container has a problem and is regenerated, the IP may change, and wordpress will not be able to connect.

1.9.2 Method 2: Using link

When creating a container, the --link option is used as follows.

--link 容器名:别名
复制代码

If you want to connect to this container in the subsequent part of this command, you can use the alias directly.

Step 1: Create wordpress container again.

[root@vms100 ~]# docker run -dit --name blog --restart=always --link db:mysqlxx -e WORDPRESS_DB_HOST=mysqlxx -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=redhat -e WORDPRESS_DB_NAME=blog -p 80:80 hub.c.163.com/library/wordpress
4805eeod8a5cd079ad7e2d028e30695288172645fa354874c2aadc642f4a7e17
[root@vms100 ~]#
复制代码

Create a container named blog here, use --link to connect to the container named db, and alias it as mysqlxx. In the variable WORDPRESS_DB_HOST, the IP of db is no longer written, but the alias mysqlxx is written directly. At this time, the blog runs normally. , and can access the database.

Note: The alias of the container in --link can be arbitrarily chosen.

But this command is still too complicated and can be further simplified. I just introduced the variables used by the wordpress image, now let's look at their default values.

--WORDPRESS_DB_HOST defaults to the container connected to the link, and the alias is mysql.

--WORDPRESS_DB_USER defaults to root.

--WORDPRESS_DB_PASSWORD defaults to the password used by mysql root.

--WORDPRESS_DB_DATABASE defaults to wordpress.

So if we specify a database as wordpress instead of blog when we create the MySQL container, the above options can be omitted.

Step 2: Delete the blog and db containers.

[root@vms100 ~]# docker rm -f db 
db
[root@vms100 ~]# docker rm -f blog 
blog
[root@vms100 ~]#
复制代码

Step 3: Create a mysql container.

[root@vms100 ~]# docker run -d --name=db --restart=always -e MYSQL_ROOT_PASSWORD=redhat -e MYSQL_DATABASE=wordpress hub.c.163.com/library/mysql
7a422e94a4bd3d747f6d434593da730532b14334a3ddd814103e3f4e9f98a939
[root@vms100 ~]#
复制代码

In this mysql container, create a library named wordpress through the variable MYSQL_DATABASE. If you do not specify which library in mysql to use in the wordpress container, the library named wordpress is used by default.

Step 4: Create a wordpress container with default values ​​for all variables.

[root@vms100 ~]# docker run -dit --name blog --restart=always --link db:mysql -p 80:80 hub.c.163.com/library/wordpress 
33d96d183cceaa01f65caa97ef137a7ad5025fc7dbac9c30324ce319d 0827773
[root@vms100 ~]#
复制代码

The alias used here is mysql. --WORDPRESS_DB_HOST will connect to the container with the alias mysql by default. There are few options for creating a wordpress container here, because they all use the default value.

Test in the browser, as shown in Figure 1-20.

Although there is not much mysql information specified when creating the wordpress container, the database settings can still be skipped, indicating that the wordpress container is correctly connected to the database.

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/130541643