Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Serf in Docker

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Serf in Docker". The content in the article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Serf in Docker".

# using Serf in Docker

Writing ideas from Decentralizing Docker: How to Use Serf with Docker this article has been submitted to dockboard for release, here take advantage of open source China's popularity spread, thank you.

In the previous Docker Link usage example, we briefly demonstrated the link feature of Docker, this time the theme is to use Serf to implement a lower coupling container relational structure, the final result is to service each service.

Serf is a decentralized service discovery and choreography solution characterized by lightweight and highly available, as well as fault-tolerant.

# # Building Serf Image

I do not use Supervisord to start the Serf service here, you can refer to the following example to start Serf.

Dockerfile_serf:

FROM ubuntu:12.04MAINTAINER Marker.King RUN echo "deb http://mirrors.aliyun.com/ubuntu precise main universe" > / etc/apt/sources.listRUN apt-get updateRUN apt-get install-y wget unzipRUN wget-- no-check-certificate https://dl.bintray.com/mitchellh/serf/0.4.1_linux_amd64.zipRUN unzip 0.4.1_linux_amd64.zipRUN rm 0.4.1_linux_amd64.zipRUN mv serf / usr/bin/EXPOSE 7946 7373CMD ["- tag" "role=serf-agent"] ENTRYPOINT ["serf", "agent"]

Build a Serf image:

$docker build-t serf-

< Dockerfile_serf ##构建mysql镜像 这一步比较复杂,我们将编写多个shell脚本,用于启动supervisor、启动mysql、创建数据库用户、创建数据库等,先让我们看一下目录结构 mysql + | - create_db.sh | - create_mysql_admin_user.sh | - Dockerfile | - join-cluster.sh | - my.cnf | - run.sh | - start-serf.sh | - start.sh | - supervisord-mysqld.conf | - supervisord-serf.conf create_db.sh: #!/bin/bashread -r lineif [ -n "$line" ]; then echo "=>

Creating database $line "mysql-uroot-e" CREATE DATABASE $line "echo" = > Done! "else echo" Usage: $0 "exit 1fi

Create_mysql_admin_user.sh:

#! / bin/bashif [- f / .mysql_admin_created]; then echo "MySQL 'admin' user already created!" Exit 0fi/usr/bin/mysqld_safe > / dev/null 2 > & 1 & PASS=$ (pwgen-s 121) echo "= > Creating MySQL admin user with random password" RET=1while [[RET-ne 0]] Do sleep 5 mysql-uroot-e "CREATE USER 'admin'@'%' IDENTIFIED BY' $PASS'" RET=$?donemysql-uroot-e "GRANT ALL PRIVILEGES ON *. * TO 'admin'@'%' WITH GRANT OPTION" mysqladmin-uroot shutdownecho "= > Done!" touch / .mysql_admin_createdecho "= =" echo "You can now connect to this MySQL Server using:" echo "" echo "mysql-uadmin-p$PASS-h-P" echo "" echo "Please remember to change The above password as soon as possible! "echo" MySQL user 'root' has no password but only allows local connections "echo" =

Join-cluster.sh:

#! / bin/bashexec serf join $SERF_AGENT_PORT_7946_TCP_ADDR:$SERF_AGENT_PORT_7946_TCP_PORT

My.cnf:

[mysqld] bind-addres=0.0.0.0

Run.sh:

#! / bin/bashif [!-f / .mysql_admin_created]; then / create_mysql_admin_user.shfiexec supervisord-n

Start-serf.sh:

#! / bin/bashexec serf agent-tag role=db\-event-handler= "user:create_db=/create_db.sh"

Start.sh:

#! / bin/bashexec mysqld_safe

Supervisord-mysqld.conf:

[program:mysqld] command=/start.shnumprocs=1autostart=trueautorestart=true

Supervisord-serf.conf:

[program:serf] command=/start-serf.shnumprocs=1autostart=trueautorestart=true [program:serf-join] command=/join-cluster.shautorestart=false

Dockerfile:

FROM ubuntu:12.04MAINTAINER Marker King RUN echo "deb http://mirrors.aliyun.com/ubuntu precise main universe" > / etc/apt/sources.listRUN apt-get updateRUN apt-get-y upgradeRUN! DEBIAN_FRONTEND=noninteractive apt-get-qy install unzip supervisor mysql-server pwgen LsADD https://dl.bintray.com/mitchellh/serf/0.4.1_linux_amd64.zip serf.zipRUN unzip serf.zipRUN rm serf.zipRUN mv serf / usr/bin/ADD / run.sh / run.shADD / start.sh / start.shADD / start-serf.sh / start-serf.shADD / join-cluster.sh / join-cluster.shADD / supervisord-mysqld.conf / etc/supervisor/conf.d/supervisord-mysqld.confADD / supervisord-serf.conf / etc/supervisor/conf.d/ Supervisord-serf.confADD / my.cnf / etc/mysql/conf.d/my.cnfADD / create_mysql_admin_user.sh / create_mysql_admin_user.shADD / create_db.sh / create_db.shRUN chmod 755 / * .shEXPOSE 3306 7946 7373CMD ["/ run.sh"]

As you may see, ADD https://dl.bintray.com/mitchellh/serf/0.4.1_linux_amd64.zip serf.zip is used to add a remote file to the image, which is different from the method used when building the Serf service image, but achieves the same effect. I need to explain here that RUN wget can be cached while ADD cannot be cached, which means that the file is downloaded again with each build, and RUN wget uses caching to speed up the build. You can try to build the image twice, and you can find that the Serf image is all cached the second time, and mysql will download the file again in Step 6.

Enter the mysql directory to build the image:

$docker build-t mysql.

# # testing Serf connection

Start a container through the Serf image:

$SERF_ID=$ (docker run-d-p 7946-p 7373-name serf_agent serf)

Let's test whether Serf is working properly and install Serf on our host (the machine where docker is installed) to connect the Serf of the container:

$wget-- no-check-certificate https://dl.bintray.com/mitchellh/serf/0.4.1_linux_amd64.zip$ unzip 0.4.1 "linuxamd64.zip$ rm 0.4.1" linuxamd64.zip$ sudo mv serf / usr/bin/$ nohup serf agent & $serf memberspacker-virtualbox 10.0.2.15) 7946 alive

Now that we have an available Serf on our machine, you can connect to Docker's Serf proxy container to see the effect:

$serf join $(docker port $SERF_ID 7946) Successfully joined cluster by contacting 1 nodes.$ serf membersprecise64 10.0.2.15 SERF_ID 7946 alive 9be517551dda 172.17.0.2 Successfully joined cluster by contacting 7946 alive role=serf-agent

# # Test Serf events dispatched to mysql service

Let's start a mysql container and connect with serf_agent, using the-link parameter. Note that the format is name:alias. Here alias must use serf_agent, because two environment variables, SERF_AGENT_PORT_7946_TCP_ADDR and SERF_AGENT_PORT_7946_TCP_PORT, are always called in join-cluster.sh, and can be connected according to different name requirements. The command is as follows:

$MYSQL_ID=$ (docker run-d-p 3306-p 7946-p 7373-link serf_agent:serf_agent mysql)

View the Serf cluster in the host:

$serf membersprecise64 10.0.2.15 alive role=serf-agent410d5a7f709a 7946 alive 9be517551dda 172.17.0.2 alive role=serf-agent410d5a7f709a 172.17.0.3 alive role=db

Because when the mysql container starts, the join-cluster.sh script is executed to connect to the serf_agent, so we can see the mysql added later in the host.

At this point, we can view the user name and password information output from mysql through docker logs $MYSQL_ID (you can change the user information or output format by modifying create_mysql_admin_user.sh, etc.).

$docker logs $MYSQL_ID= > Creating MySQL admin user with random password= > calling from customers you can now connect to this MySQL Server using: mysql-uadmin-paWQD8mdb3N1j-h-PPlease remember to change the above password as soon as possibleMySQL user 'root' has no password but only allows local connections====

Execute a custom command to inform mysql to create the database:

$serf event create_db wordpressEvent 'create_db' dispatched! Coalescing enabled: true

Connect to the database and see if the database was created successfully:

$mysql-uadmin-paWQD8mdb3N1j-h227.0.0.1-P49157mysql > show databases;+-+ | Database | +-+ | information_schema | | mysql | | performance_schema | | test | | wordpress | +-+ 5 rows in set (0.00 sec)

Succeed! We see that the database has been created.

# Summary

This is the end of today's example. You can continue to think that my mysql container already has the ability to accept commands, so if I create another wordpress container, when the container starts, it will send create_db commands to serf_agent, so mysql achieves the goal of service. Of course, there are many services, such as: load balancing, memcached, redis and so on. If you have any better ideas, you can share them. My email address is majk@vip.qq.com.

Thank you for your reading, the above is the content of "how to use Serf in Docker", after the study of this article, I believe you have a deeper understanding of how to use Serf in Docker, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report