此博文介绍华南理工大学软件学院“云计算开发与应用”实验课。本人在大二期间选修了这门课,实验课内容是把已有的app部署到云服务器上运行。虽然实验内容很简单(作者大概一个小时就做完了两天的实验内容),但觉得这次实验是我在华工期间做过的为数不多有意义的实验之一,故作此博客记录实验详细过程。此博客以linux环境为准。
实验所需材料与环境配置:
本地环境:Manjaro 18.0.4 (kernel version: x86_64 Linux 5.0.15-1-MANJARO)。需先生成ssh密钥对以便访问服务器。需要安装node.js。
云服务器:IBM LinuxONE Community Cloud。登录/注册地址:https://linuxone.cloud.marist.edu/cloud/#/login
IBM账户(登录/注册地址:https://developer.ibm.com/linuxone/ ),一个自己的github账号。
先到IBM LinuxONE Community Cloud注册账号。因为是人工审核,所以注册流程比想象中要久一些。登录之后就可以看到这样的界面:
我们不着急去创建实例,先去右上角的个人用户设置,把本地生成的id_rsa.pub公钥文件上传上去。(什么你告诉我你不会?还不赶紧学?)
新创建的账户最上方不会有Containers的选项,不用管。点击Virtual Server就可以看到下面的界面,右边的Quota显示我们最多只能创建一个vm实例。点击黄色的Create按钮,来创建我们第一个vm实例。
点击Create之后稍等一会(可能有点久,要手动刷新一下),就可以看到我们的vm已经准备就绪了。
启动terminal,ssh linux1@ip连接到我们的vm实例。
ssh linux1@148.100.244.30 The authenticity of host '148.100.244.30 (148.100.244.30)' can't be established. ECDSA key fingerprint is SHA256:JPXviVE88jNGDCGvyFFg1iVyQ1fngC6V78pqJCsKCrg. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added '148.100.244.30' (ECDSA) to the list of known hosts. ============================================================================== Welcome to IBM LinuxONE Community Cloud! This server is for authorized users only. All activity is logged and monitored. Individuals using this server must abide to the Terms and Conditions listed here: https://developer.ibm.com/linuxone/terms-of-use/ Your access will be revoked for any non-compliance. =============================================================================View Code
第一个实验是把已有的app(Github Repo: https://github.com/IBM/Cloud-Native-Workloads-on-LinuxONE)部署到云服务器上,通过docker来运行。输入以下内容:
1 sudo su 2 cd ~ 3 wget ftp://ftp.unicamp.br/pub/linuxpatch/s390x/redhat/rhel7.3/docker-17.05.0-ce-rhel7.3-20170523.tar.gz 4 tar -xzvf docker-17.05.0-ce-rhel7.3-20170523.tar.gz 5 cp docker-17.05.0-ce-rhel7.3-20170523/docker* /usr/bin/
这里直接cp
到/usr/bin
就好,因为/usr/local/bin
不在PATH
环境变量里。效果如下所示。
[linux1@virtual-machine ~]$ sudo su [root@virtual-machine linux1]# cd ~ [root@virtual-machine ~]# wget ftp://ftp.unicamp.br/pub/linuxpatch/s390x/redhat/rhel7.3/docker-17.05.0-ce-rhel7.3-20170523.tar.gz --2019-05-18 07:12:50-- ftp://ftp.unicamp.br/pub/linuxpatch/s390x/redhat/rhel7.3/docker-17.05.0-ce-rhel7.3-20170523.tar.gz => ‘docker-17.05.0-ce-rhel7.3-20170523.tar.gz’ Resolving ftp.unicamp.br (ftp.unicamp.br)... 143.106.10.149 Connecting to ftp.unicamp.br (ftp.unicamp.br)|143.106.10.149|:21... connected. Logging in as anonymous ... Logged in! ==> SYST ... done. ==> PWD ... done. ==> TYPE I ... done. ==> CWD (1) /pub/linuxpatch/s390x/redhat/rhel7.3 ... done. ==> SIZE docker-17.05.0-ce-rhel7.3-20170523.tar.gz ... 27694090 ==> PASV ... done. ==> RETR docker-17.05.0-ce-rhel7.3-20170523.tar.gz ... done. Length: 27694090 (26M) (unauthoritative) 100%[==========================================================================>] 27,694,090 8.20MB/s in 3.2s 2019-05-18 07:12:55 (8.20 MB/s) - ‘docker-17.05.0-ce-rhel7.3-20170523.tar.gz’ saved [27694090] [root@virtual-machine ~]# tar -xzvf docker-17.05.0-ce-rhel7.3-20170523.tar.gz docker-17.05.0-ce-rhel7.3-20170523/ docker-17.05.0-ce-rhel7.3-20170523/docker docker-17.05.0-ce-rhel7.3-20170523/docker-containerd docker-17.05.0-ce-rhel7.3-20170523/docker-containerd-ctr docker-17.05.0-ce-rhel7.3-20170523/docker-containerd-shim docker-17.05.0-ce-rhel7.3-20170523/docker-init docker-17.05.0-ce-rhel7.3-20170523/docker-proxy docker-17.05.0-ce-rhel7.3-20170523/docker-runc docker-17.05.0-ce-rhel7.3-20170523/dockerd docker-17.05.0-ce-rhel7.3-20170523/INSTALL docker-17.05.0-ce-rhel7.3-20170523/URLs docker-17.05.0-ce-rhel7.3-20170523/gcc-libffi-LICENSE docker-17.05.0-ce-rhel7.3-20170523/gcc-libgo-LICENSE docker-17.05.0-ce-rhel7.3-20170523/gcc-libgo-PATENTS docker-17.05.0-ce-rhel7.3-20170523/libgo-PATENTS docker-17.05.0-ce-rhel7.3-20170523/LICENSE docker-17.05.0-ce-rhel7.3-20170523/NOTICE [root@virtual-machine ~]# cp docker-17.05.0-ce-rhel7.3-20170523/docker* /usr/bin/View Code
之后启动docker daemon:
1 docker daemon -g /local/docker/lib &
[root@virtual-machine ~]# docker daemon -g /local/docker/lib & [1] 21401 [root@virtual-machine ~]# Command "daemon" is deprecated, and will be removed in Docker 17.12. Please run `dockerd` directly. WARN[0000] the "-g / --graph" flag is deprecated. Please use "--data-root" instead WARN[0000] could not change group /var/run/docker.sock to docker: group docker not found INFO[0000] libcontainerd: new containerd process, pid: 21410 WARN[0000] containerd: low RLIMIT_NOFILE changing to max current=1024 max=4096 INFO[0001] Graph migration to content-addressability took 0.00 seconds INFO[0001] Loading containers: start. INFO[0001] Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip can be used to set a preferred IP address INFO[0001] Loading containers: done. INFO[0001] Daemon has completed initialization INFO[0001] Docker daemon commit=89658be graphdriver=overlay version=17.05.0-ce INFO[0001] API listen on /var/run/docker.sockView Code
启动完成后检查一下后台有无docker进程在运行:
1 ps aux | grep docker
[root@virtual-machine ~]# ps aux | grep docker root 21401 0.2 0.6 332008 24708 pts/0 Sl 07:18 0:00 dockerd -g /local/docker/lib root 21410 0.0 0.1 203992 5536 ? Ssl 07:18 0:00 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime docker-runc root 21532 0.0 0.0 106276 904 pts/0 S+ 07:19 0:00 grep --color=auto dockerView Code
接下来我们要安装docker-compose:
1 yum info python-setuptools 2 # 要先禁用ipv6服务,不然接下来安装pip会非常慢 3 sysctl -w net.ipv6.conf.all.disable_ipv6=1 4 sysctl -w net.ipv6.conf.default.disable_ipv6=1 5 easy_install pip 6 pip install backports.ssl_match_hostname --upgrade --ignore-installed
[root@virtual-machine ~]# yum info python-setuptools Loaded plugins: product-id, search-disabled-repos, subscription-manager This system is not registered with an entitlement server. You can use subscription-manager to register. Installed Packages Name : python-setuptools Arch : noarch Version : 0.9.8 Release : 7.el7 Size : 1.9 M Repo : installed From repo : rhel7-base Summary : Easily build and distribute Python packages URL : http://pypi.python.org/pypi/setuptools License : Python or ZPLv2.0 Description : Setuptools is a collection of enhancements to the Python distutils that allow : you to more easily build and distribute Python packages, especially ones that : have dependencies on other packages. : : This package contains the runtime components of setuptools, necessary to : execute the software that requires pkg_resources.py. : : This package contains the distribute fork of setuptools. [root@virtual-machine ~]# sysctl -w net.ipv6.conf.all.disable_ipv6=1 net.ipv6.conf.all.disable_ipv6 = 1 [root@virtual-machine ~]# sysctl -w net.ipv6.conf.default.disable_ipv6=1 net.ipv6.conf.default.disable_ipv6 = 1 [root@virtual-machine ~]# easy_install pip Searching for pip Reading https://pypi.python.org/simple/pip/ Best match: pip 19.1.1 Downloading https://files.pythonhosted.org/packages/93/ab/f86b61bef7ab14909bd7ec3cd2178feb0a1c86d451bc9bccd5a1aedcde5f/pip-19.1.1.tar.gz#sha256=44d3d7d3d30a1eb65c7e5ff1173cdf8f7467850605ac7cc3707b6064bddd0958 Processing pip-19.1.1.tar.gz Writing /tmp/easy_install-VwlKKm/pip-19.1.1/setup.cfg Running pip-19.1.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-VwlKKm/pip-19.1.1/egg-dist-tmp-7Zx6_R /usr/lib64/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'python_requires' warnings.warn(msg) warning: no files found matching 'docs/docutils.conf' warning: no previously-included files found matching '.coveragerc' warning: no previously-included files found matching '.mailmap' warning: no previously-included files found matching '.appveyor.yml' warning: no previously-included files found matching '.travis.yml' warning: no previously-included files found matching 'tox.ini' warning: no files found matching 'Makefile' under directory 'docs' warning: no files found matching '*.bat' under directory 'docs' warning: no previously-included files found matching 'src/pip/_vendor/six' warning: no previously-included files found matching 'src/pip/_vendor/six/moves' warning: no previously-included files matching '*.pyi' found under directory 'src/pip/_vendor' no previously-included directories found matching '.github' no previously-included directories found matching '.azure-pipelines' no previously-included directories found matching 'docs/build' no previously-included directories found matching 'news' no previously-included directories found matching 'tasks' no previously-included directories found matching 'tests' no previously-included directories found matching 'tools' Adding pip 19.1.1 to easy-install.pth file Installing pip script to /usr/bin Installing pip2.7 script to /usr/bin Installing pip2 script to /usr/bin Installed /usr/lib/python2.7/site-packages/pip-19.1.1-py2.7.egg Processing dependencies for pip Finished processing dependencies for pip [root@virtual-machine ~]# pip install backports.ssl_match_hostname --upgrade --ignore-installed DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. Collecting backports.ssl_match_hostname Downloading https://files.pythonhosted.org/packages/ff/2b/8265224812912bc5b7a607c44bf7b027554e1b9775e9ee0de8032e3de4b2/backports.ssl_match_hostname-3.7.0.1.tar.gz Installing collected packages: backports.ssl-match-hostname Running setup.py install for backports.ssl-match-hostname ... done Successfully installed backports.ssl-match-hostname-3.7.0.1View Code
最后,使用pip
安装docker-compose:
1 yum install python-devel libffi-devel 2 pip install docker-compose==1.13.0
[root@virtual-machine ~]# yum install python-devel libffi-devel Loaded plugins: product-id, search-disabled-repos, subscription-manager This system is not registered with an entitlement server. You can use subscription-manager to register. Resolving Dependencies --> Running transaction check ---> Package libffi-devel.s390x 0:3.0.13-18.el7 will be installed ---> Package python-devel.s390x 0:2.7.5-77.el7_6 will be installed --> Finished Dependency Resolution Dependencies Resolved ==================================================================================================================== Package Arch Version Repository Size ==================================================================================================================== Installing: libffi-devel s390x 3.0.13-18.el7 rhel7-base 23 k python-devel s390x 2.7.5-77.el7_6 rhel7-base 398 k Transaction Summary ==================================================================================================================== Install 2 Packages Total download size: 421 k Installed size: 1.1 M Is this ok [y/d/N]: y Downloading packages: (1/2): libffi-devel-3.0.13-18.el7.s390x.rpm | 23 kB 00:00:00 (2/2): python-devel-2.7.5-77.el7_6.s390x.rpm | 398 kB 00:00:00 -------------------------------------------------------------------------------------------------------------------- Total 5.7 MB/s | 421 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : python-devel-2.7.5-77.el7_6.s390x 1/2 Installing : libffi-devel-3.0.13-18.el7.s390x 2/2 Verifying : libffi-devel-3.0.13-18.el7.s390x 1/2 Verifying : python-devel-2.7.5-77.el7_6.s390x 2/2 Installed: libffi-devel.s390x 0:3.0.13-18.el7 python-devel.s390x 0:2.7.5-77.el7_6 Complete! [root@virtual-machine ~]# pip install docker-compose==1.13.0 DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. Collecting docker-compose==1.13.0 Downloading https://files.pythonhosted.org/packages/37/2b/f564105d548d8d92925aac550468b14282504e144d83b0c17139ce350fa3/docker_compose-1.13.0-py2.py3-none-any.whl (94kB) |████████████████████████████████| 102kB 12.3MB/s Requirement already satisfied: PyYAML<4,>=3.10 in /usr/lib/python2.7/site-packages/PyYAML-3.11-py2.7-linux-s390x.egg (from docker-compose==1.13.0) (3.11) Collecting websocket-client<1.0,>=0.32.0 (from docker-compose==1.13.0) Downloading https://files.pythonhosted.org/packages/29/19/44753eab1fdb50770ac69605527e8859468f3c0fd7dc5a76dd9c4dbd7906/websocket_client-0.56.0-py2.py3-none-any.whl (200kB) |████████████████████████████████| 204kB 29.7MB/s Requirement already satisfied: backports.ssl-match-hostname>=3.5; python_version < "3.5" in /usr/lib/python2.7/site-packages (from docker-compose==1.13.0) (3.7.0.1) Collecting dockerpty<0.5,>=0.4.1 (from docker-compose==1.13.0) Downloading https://files.pythonhosted.org/packages/8d/ee/e9ecce4c32204a6738e0a5d5883d3413794d7498fe8b06f44becc028d3ba/dockerpty-0.4.1.tar.gz Collecting cached-property<2,>=1.2.0 (from docker-compose==1.13.0) Downloading https://files.pythonhosted.org/packages/3b/86/85c1be2e8db9e13ef9a350aecd6dea292bd612fa288c2f40d035bb750ded/cached_property-1.5.1-py2.py3-none-any.whl Collecting docker<3.0,>=2.2.1 (from docker-compose==1.13.0) Downloading https://files.pythonhosted.org/packages/6b/01/74d61c7de9fb99cebbbfab62ee5c88a9b23e8b1c25a57eded5fc22e87bd2/docker-2.7.0-py2.py3-none-any.whl (119kB) |████████████████████████████████| 122kB 90.7MB/s Requirement already satisfied: requests!=2.11.0,<2.12,>=2.6.1 in /usr/lib/python2.7/site-packages/requests-2.11.1-py2.7.egg (from docker-compose==1.13.0) (2.11.1) Requirement already satisfied: ipaddress>=1.0.16; python_version < "3.3" in /usr/lib/python2.7/site-packages (from docker-compose==1.13.0) (1.0.16) Collecting colorama<0.4,>=0.3.7 (from docker-compose==1.13.0) Downloading https://files.pythonhosted.org/packages/db/c8/7dcf9dbcb22429512708fe3a547f8b6101c0d02137acbd892505aee57adf/colorama-0.3.9-py2.py3-none-any.whl Collecting enum34<2,>=1.0.4; python_version < "3.4" (from docker-compose==1.13.0) Downloading https://files.pythonhosted.org/packages/c5/db/e56e6b4bbac7c4a06de1c50de6fe1ef3810018ae11732a50f15f62c7d050/enum34-1.1.6-py2-none-any.whl Requirement already satisfied: six<2,>=1.3.0 in /usr/lib/python2.7/site-packages (from docker-compose==1.13.0) (1.9.0) Collecting texttable<0.9,>=0.8.1 (from docker-compose==1.13.0) Downloading https://files.pythonhosted.org/packages/74/49/23d944a6f7d4dd20846b2df2914fe108c96a253567c140c416edbf270f80/texttable-0.8.8.tar.gz Collecting jsonschema<3,>=2.5.1 (from docker-compose==1.13.0) Downloading https://files.pythonhosted.org/packages/77/de/47e35a97b2b05c2fadbec67d44cfcdcd09b8086951b331d82de90d2912da/jsonschema-2.6.0-py2.py3-none-any.whl Collecting docopt<0.7,>=0.6.1 (from docker-compose==1.13.0) Downloading https://files.pythonhosted.org/packages/a2/55/8f8cab2afd404cf578136ef2cc5dfb50baa1761b68c9da1fb1e4eed343c9/docopt-0.6.2.tar.gz Collecting docker-pycreds>=0.2.1 (from docker<3.0,>=2.2.1->docker-compose==1.13.0) Downloading https://files.pythonhosted.org/packages/f5/e8/f6bd1eee09314e7e6dee49cbe2c5e22314ccdb38db16c9fc72d2fa80d054/docker_pycreds-0.4.0-py2.py3-none-any.whl Collecting functools32; python_version == "2.7" (from jsonschema<3,>=2.5.1->docker-compose==1.13.0) Downloading https://files.pythonhosted.org/packages/c5/60/6ac26ad05857c601308d8fb9e87fa36d0ebf889423f47c3502ef034365db/functools32-3.2.3-2.tar.gz ERROR: docker 2.7.0 has requirement requests!=2.18.0,>=2.14.2, but you'll have requests 2.11.1 which is incompatible. Installing collected packages: websocket-client, dockerpty, cached-property, docker-pycreds, docker, colorama, enum34, texttable, functools32, jsonschema, docopt, docker-compose Running setup.py install for dockerpty ... done Running setup.py install for texttable ... done Running setup.py install for functools32 ... done Running setup.py install for docopt ... done Successfully installed cached-property-1.5.1 colorama-0.3.9 docker-2.7.0 docker-compose-1.13.0 docker-pycreds-0.4.0 dockerpty-0.4.1 docopt-0.6.2 enum34-1.1.6 functools32-3.2.3.post2 jsonschema-2.6.0 texttable-0.8.8 websocket-client-0.56.0View Code
别忘记验证一下docker-compose是否已经安装:
1 docker-compose version
[root@virtual-machine ~]# docker-compose version docker-compose version 1.13.0, build 1719ceb docker-py version: 2.7.0 CPython version: 2.7.5 OpenSSL version: OpenSSL 1.0.2k-fips 26 Jan 2017View Code
接下来我们要手动拉取websphere-liberty
镜像到本地,后台运行docker并指定端口映射规则:
1 docker image pull s390x/websphere-liberty:webProfile7 2 docker images 3 docker run -d -p 80:9080 -p 443:9443 s390x/websphere-liberty:webProfile7 4 docker ps
[root@virtual-machine ~]# docker image pull s390x/websphere-liberty:webProfile7 webProfile7: Pulling from s390x/websphere-liberty a39cfce7a60d: Pull complete 4e699efbddb6: Pull complete 9a3ffeac4412: Pull complete 52c5a080fd6d: Pull complete 6f0d27faa63a: Pull complete a3d346a6c218: Pull complete e9129f75e0bc: Pull complete 905ebfd4a924: Pull complete bd9b8600bfe7: Pull complete 5746a3a16c6e: Pull complete 621479e04496: Pull complete 26db9a45b5d9: Pull complete 32c81cd7fa4a: Pull complete 705855d9301f: Pull complete 0bd5ae8e4470: Pull complete Digest: sha256:87e41c209fa1c8ab33fc0cd0e126eec1493a50c49fe557f398707b4f4755d07a Status: Downloaded newer image for s390x/websphere-liberty:webProfile7 [root@virtual-machine ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE s390x/websphere-liberty webProfile7 def868b21def 2 days ago 473MB [root@virtual-machine ~]# docker run -d -p 80:9080 -p 443:9443 s390x/websphere-liberty:webProfile7 8a2910700aab9c7b459e10c65778b20318822a957112d20f9bb9f4d948bde546 [root@virtual-machine ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8a2910700aab s390x/websphere-liberty:webProfile7 "/opt/ibm/helpers/..." 5 seconds ago Up 4 seconds 0.0.0.0:80->9080/tcp, 0.0.0.0:443->9443/tcp goofy_hooverView Code
打开浏览器,直接访问服务器的公网ip就可以看到如图效果:
接下来我们要安装并运行WorkPress:
1 alias vim='vi' 2 source ~/.bashrc 3 vim docker-compose.yml
把以下内容复制到新的文件中去:
1 version: '2' 2 services: 3 wordpress: 4 image: s390x/wordpress 5 ports: 6 - 8080:80 # 将本地 8080 端口映射到容器的 80 端口 7 environment: 8 WORDPRESS_DB_PASSWORD: example 9 mysql: 10 image: brunswickheads/mariadb-5.5-s390x 11 environment: 12 MYSQL_ROOT_PASSWORD: example
创建新的wordpress目录方便整理:
1 mkdir wordpress 2 mv docker-compose.yml wordpress/ 3 cd wordpress/ 4 ls
[root@virtual-machine ~]# alias vim='vi' [root@virtual-machine ~]# source ~/.bashrc [root@virtual-machine ~]# vim docker-compose.yml [root@virtual-machine ~]# cat docker-compose.yml version: '2' services: wordpress: image: s390x/wordpress ports: - 8080:80 # 将本地 8080 端口映射到容器的 80 端口 environment: WORDPRESS_DB_PASSWORD: example mysql: image: brunswickheads/mariadb-5.5-s390x environment: MYSQL_ROOT_PASSWORD: example [root@virtual-machine ~]# mkdir wordpress [root@virtual-machine ~]# mv docker-compose.yml wordpress/ [root@virtual-machine ~]# cd wordpress/ [root@virtual-machine wordpress]# ls docker-compose.ymlView Code
最后,根据docker-compose.yml
中定义的服务启动容器:
1 docker-compose up -d
[root@virtual-machine wordpress]# docker-compose up -d Creating network "wordpress_default" with the default driver Pulling wordpress (s390x/wordpress:latest)... latest: Pulling from s390x/wordpress a0a92d62c165: Pull complete 23fb66856860: Pull complete b38a4e89ae8f: Pull complete 2fa08f5a322d: Pull complete bbf2af7622ca: Pull complete 3dacb3b3a74e: Pull complete 7b49a00cab66: Pull complete 0226e5908d56: Pull complete af4747f0be82: Pull complete caa0c91ea082: Pull complete ecabe806d92c: Pull complete 5d55f6a2617f: Pull complete d0e6cfaadf87: Pull complete 0f9356af4b28: Pull complete e61aa2870c8c: Pull complete 0c6cf289ae6c: Pull complete 22cc2d6c6f63: Pull complete e43117da3e19: Pull complete Digest: sha256:435a9522b41c7e2f5c8dd984a5fde0422ecd87cef67811c269601618e21c5b03 Status: Downloaded newer image for s390x/wordpress:latest Pulling mysql (brunswickheads/mariadb-5.5-s390x:latest)... latest: Pulling from brunswickheads/mariadb-5.5-s390x 67ebcd5ec325: Pull complete a3ed95caeb02: Pull complete 817149461e92: Pull complete 124b1e67e903: Pull complete Digest: sha256:7e67bc8c7d4323dcb3398539f0f819beba4dcae93ded0f72ea6dcf35f39ad10f Status: Downloaded newer image for brunswickheads/mariadb-5.5-s390x:latest Creating wordpress_mysql_1 ... Creating wordpress_wordpress_1 ... Creating wordpress_mysql_1 Creating wordpress_wordpress_1 ... doneView Code
查看相关容器的状态:
1 docker-compose ps
[root@virtual-machine wordpress]# docker-compose ps Name Command State Ports ------------------------------------------------------------------------------------- wordpress_mysql_1 /docker-entrypoint.sh mysq ... Up 3306/tcp wordpress_wordpress_1 docker-entrypoint.sh apach ... Up 0.0.0.0:8080->80/tcpView Code
登录服务器公网ip:8080,可看到如下界面:
接下来我们要准备MEAN Stack环境。
1 cd ~ 2 git clone https://github.com/IBM/Cloud-Native-Workloads-on-LinuxONE 3 cp -r Cloud-Native-Workloads-on-LinuxONE/files/mean-docker ./ 4 yum install -y tree 5 tree mean-docker
[root@virtual-machine wordpress]# cd ~ [root@virtual-machine ~]# git clone https://github.com/IBM/Cloud-Native-Workloads-on-LinuxONE Cloning into 'Cloud-Native-Workloads-on-LinuxONE'... remote: Enumerating objects: 866, done. remote: Total 866 (delta 0), reused 0 (delta 0), pack-reused 866 Receiving objects: 100% (866/866), 2.80 MiB | 0 bytes/s, done. Resolving deltas: 100% (102/102), done. [root@virtual-machine ~]# cp -r Cloud-Native-Workloads-on-LinuxONE/files/mean-docker ./ [root@virtual-machine ~]# yum install -y tree Loaded plugins: product-id, search-disabled-repos, subscription-manager This system is not registered with an entitlement server. You can use subscription-manager to register. Resolving Dependencies --> Running transaction check ---> Package tree.s390x 0:1.6.0-10.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ==================================================================================================================== Package Arch Version Repository Size ==================================================================================================================== Installing: tree s390x 1.6.0-10.el7 rhel7-base 45 k Transaction Summary ==================================================================================================================== Install 1 Package Total download size: 45 k Installed size: 87 k Downloading packages: tree-1.6.0-10.el7.s390x.rpm | 45 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : tree-1.6.0-10.el7.s390x 1/1 Verifying : tree-1.6.0-10.el7.s390x 1/1 Installed: tree.s390x 0:1.6.0-10.el7 Complete! [root@virtual-machine ~]# tree mean-docker mean-docker ├── docker-compose.yml ├── express-server │ ├── app │ │ ├── models │ │ │ └── todo.js │ │ └── routes.js │ ├── config │ │ └── database.js │ ├── Dockerfile │ ├── license │ ├── package.json │ ├── public │ │ ├── index.html │ │ └── js │ │ ├── controllers │ │ │ └── main.js │ │ ├── core.js │ │ └── services │ │ └── todos.js │ ├── README.md │ └── server.js └── README.md 8 directories, 14 filesView Code
因为我们之前占用了8080端口,所以需要修改一下配置文件使服务正常启动。
1 cd mean-docker/ 2 vim docker-compose.yml 3 # 把两个8080都改成8081并保存退出 4 vim express-server/Dockerfile 5 # 把8080改成8081,并在文件末尾新建一行添加"ENV PORT 8081"
[root@virtual-machine ~]# cd mean-docker/ [root@virtual-machine mean-docker]# vim docker-compose.yml [root@virtual-machine mean-docker]# vim express-server/Dockerfile [root@virtual-machine mean-docker]# ^C [root@virtual-machine mean-docker]# cat docker-compose.yml version: '2' # specify docker-compose version # Define the services/containers to be run services: express: #name of the first service build: express-server # specify the directory of the Dockerfile ports: - "8081:8081" #specify ports forwarding links: - database database: # name of the second service image: sinenomine/mongodb-s390x # specify image to build container from ports: - "27017:27017" # specify port forwarding [root@virtual-machine mean-docker]# cat express-server/Dockerfile # Create image based off of the official Node 6 image FROM s390x/ibmnode:latest # Create a directory where our app will be placed RUN mkdir -p /usr/src # Change directory so that our commands run inside this new dir WORKDIR /usr/src # Copy dependency definitions COPY package.json /usr/src # Install dependecies RUN npm install # Get all the code needed to run the app COPY . /usr/src # Expose the port the app runs in EXPOSE 8081 # Serve the app CMD ["npm", "start"] # Express listening port ENV PORT 8081View Code
在mean-docker
目录下运行docker-compose up:
1 docker-compose up
[root@virtual-machine mean-docker]# docker-compose up Creating network "meandocker_default" with the default driver Pulling database (sinenomine/mongodb-s390x:latest)... latest: Pulling from sinenomine/mongodb-s390x 7c5b25b1702b: Pull complete f7e2beee24dc: Pull complete f77a8563476a: Pull complete Digest: sha256:e2c2f0a83664b31d4148b35e6c31895fd8397efec55830823379efa227682bc2 Status: Downloaded newer image for sinenomine/mongodb-s390x:latest Building express Step 1/9 : FROM s390x/ibmnode:latest latest: Pulling from s390x/ibmnode b1ebf925223c: Pull complete cd8a07f0c922: Pull complete 2d1c359f5ed9: Pull complete c35fcc66064c: Pull complete d624275f8e7d: Pull complete 818d5fa6bb92: Pull complete 64c9cabe7e6e: Pull complete c052205f20b1: Pull complete 7df1929f5b12: Pull complete 639d72997f7b: Pull complete Digest: sha256:32e54c7e09999a0113966754324a56009506403fb73635edacc753af0df09213 Status: Downloaded newer image for s390x/ibmnode:latest ---> 9a87e4f94692 Step 2/9 : RUN mkdir -p /usr/src ---> Running in e30a90bc8bb9 INFO[2622] Layer sha256:11fecd029e26edf344a0dec27d27e73f5f5ab6447296850e7def60577dcf7d12 cleaned up ---> d68450f5b19a Removing intermediate container e30a90bc8bb9 Step 3/9 : WORKDIR /usr/src INFO[2624] Layer sha256:11fecd029e26edf344a0dec27d27e73f5f5ab6447296850e7def60577dcf7d12 cleaned up ---> 834db0a5158c Removing intermediate container 0acfc10c188c Step 4/9 : COPY package.json /usr/src ---> 6250d20c51df Removing intermediate container aba7994dfa15 Step 5/9 : RUN npm install ---> Running in 987b22a5c3a5 npm notice created a lockfile as package-lock.json. You should commit this file. npm WARN node-todo@0.0.1 No repository field. added 82 packages in 2.46s ---> 5ce9e9ccc178 Removing intermediate container 987b22a5c3a5 Step 6/9 : COPY . /usr/src ---> 2deda2d689e5 Removing intermediate container 3711106be602 Step 7/9 : EXPOSE 8081 ---> Running in 856746e042e0 INFO[2640] Layer sha256:d382aae1568c2bb9c4a9fb25b7d809059d4742b00bfd7df97b34bc2a42938a70 cleaned up ---> 0b78f2cfb933 Removing intermediate container 856746e042e0 Step 8/9 : CMD npm start ---> Running in 9e32fa5348f2 INFO[2646] Layer sha256:d382aae1568c2bb9c4a9fb25b7d809059d4742b00bfd7df97b34bc2a42938a70 cleaned up ---> ff4f8b8205a2 Removing intermediate container 9e32fa5348f2 Step 9/9 : ENV PORT 8081 ---> Running in e369dd139028 INFO[2650] Layer sha256:d382aae1568c2bb9c4a9fb25b7d809059d4742b00bfd7df97b34bc2a42938a70 cleaned up ---> 470ea5a42df7 Removing intermediate container e369dd139028 Successfully built 470ea5a42df7 Successfully tagged meandocker_express:latest WARNING: Image for service express was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`. Creating meandocker_database_1 ... Creating meandocker_database_1 ... done Creating meandocker_express_1 ... Creating meandocker_express_1 ... done Attaching to meandocker_database_1, meandocker_express_1 database_1 | note: noprealloc may hurt performance in many applications express_1 | express_1 | > node-todo@0.0.1 start /usr/src express_1 | > node server.js express_1 | express_1 | App listening on port 8081 express_1 | (node:17) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http://mongoosejs.com/docs/4.x/docs/connections.html#use-mongo-clientView Code
此时访问服务器公网ip:8081即可使用一个Todo App。实验一也就到此结束了。
实验二也是把一个app部署到云服务器上。这次我们需要登录自己的github账户,并fork https://github.com/IBM/ICp-banking-microservices,然后clone到本机中(不是服务器)。用vscode打开repo里的banking-application文件夹(然后不用理它)。
浏览器访问 https://developer-contest-spbodieusibmcom-prod.developer.us.apiconnect.ibmcloud.com 并注册一个账户(邮箱必须与注册IBM ID的邮箱相同)
注册完毕后登录,在Apps
选项卡下点击Create new App
创建新应用,Title
输入My Banking Application
即可,之后点击Submit
提交。这时需要记录下Client ID
和Client Secret
,之后会用到。
然后需要申请banking API
。在API Products
选项卡下找到Banking Product,点击
Subscribe
申请Default Plan,选择刚才创建的应用
My Banking Application。
最后回到vscode,打开banking-application/public/js/bankingAPI.js并修改里面的Client ID
和Client Secret。在vscode里启动终端:
1 npm install 2 node app.js
此时浏览器访问http://localhost:3000,即可使用应用。随便选择一个customer ID
测试,若有JSON
格式的数据返回,则说明 API 可用。(实际上经常出现超时的情况)
更新代码并推送到远程仓库:
1 git add public/js/bankingAPI.js 2 git commit -m "Update of bankingAPI.js" 3 git push origin master
回到服务器终端,把刚刚更新完的repo clone下来:
1 cd ~ 2 git clone https://github.com/JHSeng/ICp-banking-microservices.git
之后使用docker build
命令构建镜像:
1 cd ICp-banking-microservices/ 2 cd banking-application/ 3 ls 4 #可以把jhseng改成你的用户名 5 docker build -t "jhseng-banking-image:latest" ./
[root@virtual-machine ~]# cd ICp-banking-microservices/ [root@virtual-machine ICp-banking-microservices]# cd banking-application/ [root@virtual-machine banking-application]# ls app.js Dockerfile package.json public [root@virtual-machine banking-application]# docker build -t "jhseng-banking-image:latest" ./ Sending build context to Docker daemon 316.9kB Step 1/9 : FROM ibmcom/ibmnode latest: Pulling from ibmcom/ibmnode b1ebf925223c: Already exists cd8a07f0c922: Already exists 2d1c359f5ed9: Already exists c35fcc66064c: Already exists d624275f8e7d: Already exists 818d5fa6bb92: Already exists 64c9cabe7e6e: Already exists c052205f20b1: Already exists 7df1929f5b12: Already exists 639d72997f7b: Already exists Digest: sha256:44c68d2ee535472c3be166263bd5c3f0f208da64d59deb19fcd8c409ad0d9908 Status: Downloaded newer image for ibmcom/ibmnode:latest ---> 9a87e4f94692 Step 2/9 : WORKDIR "/app" ---> b11ff994a9ba Removing intermediate container 24b113bc87db Step 3/9 : COPY package.json /app/ ---> 6a5bab8455bd Removing intermediate container e857801a6aaf Step 4/9 : RUN cd /app; npm install; npm prune --production ---> Running in 21acc1391fa2 npm notice created a lockfile as package-lock.json. You should commit this file. npm WARN banking-application@1.0.0 No repository field. added 58 packages in 1.672s npm WARN banking-application@1.0.0 No repository field. up to date in 0.217s ---> 3479d760c479 Removing intermediate container 21acc1391fa2 Step 5/9 : COPY . /app ---> 1f716ac1e072 Removing intermediate container f3fce68e43a6 Step 6/9 : ENV NODE_ENV production ---> Running in 6d99b81bf11c INFO[4152] Layer sha256:7c0bd4434c13bc4e8510693eaf43b29d2e2dec4a8b4e61cb72a428781511c4ae cleaned up ---> b2a5fa1cd291 Removing intermediate container 6d99b81bf11c Step 7/9 : ENV PORT 3000 ---> Running in b6d671bdfff3 INFO[4155] Layer sha256:7c0bd4434c13bc4e8510693eaf43b29d2e2dec4a8b4e61cb72a428781511c4ae cleaned up ---> f50429c9c3aa Removing intermediate container b6d671bdfff3 Step 8/9 : EXPOSE 3000 ---> Running in 970f3c104aea INFO[4159] Layer sha256:7c0bd4434c13bc4e8510693eaf43b29d2e2dec4a8b4e61cb72a428781511c4ae cleaned up ---> f910bdb952f5 Removing intermediate container 970f3c104aea Step 9/9 : CMD npm start ---> Running in 2480f35ef6c8 INFO[4163] Layer sha256:7c0bd4434c13bc4e8510693eaf43b29d2e2dec4a8b4e61cb72a428781511c4ae cleaned up ---> 47523b9ade76 Removing intermediate container 2480f35ef6c8 Successfully built 47523b9ade76 Successfully tagged jhseng-banking-image:latestView Code
运行容器:
1 docker run -p 3000:3000 jhseng-banking-image
访问服务器公网ip:3000即可访问应用。
至此,整个实验也就做完了。虽然实验内容比较简单,但让我们提前体验到docker的使用,实验过程涉及到较多必须掌握的技能,算是比较好的实验。