2015-12-27

<从Docker到Kubernetes之技术实战>教程作业(三)

构建一个基于centos 7的,包括java 8, tomcat 7 ,php ,mysql+mycat 的镜像
要求如下:
tomcat 启动,暴漏8080端口
mycat +mysql启动,并且暴漏 mycat 的8066和9066端口到主机对应端口
mycat 安装在 /opt/mycat 目录下
如果擅长linux脚本,则可考虑通过CMD 参数或者ENV变量 存放mycat的用户名和密码,然后编写一个启动脚本,获取上述用户名密码 并写入到mycat的配置文件/opt/mycat/config/server.xml里

验证从本机(非宿主机)能访问 8080 ,能用mysql 客户端连接 mycat的 8066,9066端口并贴上截图


1.下载并安装 jdk8

下载后安装

rpm -ivh jdk-8u66-linux-x64.rpm

配置环境变量

vim /etc/profile

export JAVA_HOME=/usr/java/jdk1.8.0_66
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH

使之生效

source /etc/profile

验证

2.下载解压 tomcat

3.下载安装 mysql

wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-community-server

初始化数据库

mysql_install_db --user=mysql

启动/停止

#启动
mysqld --user=mysql
#停止
mysqladmin shutdown

为保证可以远程访问 mysql:

mysql -u root -p
mysql> use mysql;
mysql> update user set host = '%' where user ='root' and host='localhost';  
mysql> update user set password=PASSWORD("root") where User='root';
mysql> flush privileges;
mysql> quit

4.下载解压 mycat

修改配置文件中 mysql 的 root 密码:

vi /software/mycat/conf/schema.xml

5.下载安装 supervisor

安装 supervisor

yum install python-setuptools
easy_install supervisor

配置文件

vi /etc/supervisord.conf

[supervisord]
nodaemon=true

[program:mysqld]
command=mysqld --user=mysql

[program:tomcat]
command=/software/apache-tomcat-7.0.67/bin/catalina.sh run

[program:mycat]
command=/software/mycat/bin/mycat console

启动 supervisor

/usr/bin/supervisord &
[root@d352687f7382 /]# /usr/bin/supervisord &
[1] 21
[root@d352687f7382 /]# /usr/lib/python2.7/site-packages/supervisor-3.2.0-py2.7.egg/supervisor/options.py:296: UserWarning: Supervisord is running as root and it is searching for its configuration file in default locations (including its current working directory); you probably want to specify a "-c" argument specifying an absolute path to a configuration file for improved security.
  'Supervisord is running as root and it is searching '
2015-12-26 16:14:05,959 CRIT Supervisor running as root (no user in config file)
2015-12-26 16:14:05,962 INFO supervisord started with pid 21
2015-12-26 16:14:06,965 INFO spawned: 'mysqld' with pid 24
2015-12-26 16:14:06,968 INFO spawned: 'tomcat' with pid 25
2015-12-26 16:14:06,973 INFO spawned: 'mycat' with pid 26
2015-12-26 16:14:08,480 INFO success: mysqld entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2015-12-26 16:14:08,481 INFO success: tomcat entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2015-12-26 16:14:08,481 INFO success: mycat entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)

注意
supervisor 不能监控后台程序, tomcat/mycat 需要使用前台启动,否则会导致 supervisor 监控不到然后启动多个应用.

6.运行容器

docker run -ti -p 8080:8080 -p 3306:3306 -p 8066:8066 -p 9066:9066 054a465e5b57 /bin/bash

7.在宿主机访问

➜  ~  docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM
default   *        virtualbox   Running   tcp://192.168.99.100:2376

访问 tomcat: http://192.168.99.100:8080


使用mysql 客户端连接, 3306

8066:

9066:

查看镜像

2015-12-27 01:0723