mesos入门(四)——docker应用的部署

简易的docker应用部署以及修改了部分启动脚本的bug

bug修改

在启动了marathon后,我在mesos的webUI上发现marathon一直处于inactive状态,后来想了想,marathon是放在启动master的脚本中了,当它启动时,slave/agent还未启动,所以才会导致framework呈现未激活的状态,所以将在mesos上运行的framework的启动单独做成一个启动脚本,问题解决

修改如下:

原start_master/tasks/main.yml

---

- name: get ip
  shell: ip addr|grep eth1|grep inet|awk '{print $2}'| cut -d / -f 1
  register: local_ip

- name: start zookeeper
  shell: "{{remote_dir}}/zookeeper/bin/zkServer.sh start"


- name: start mesos master
  shell: "{{remote_dir}}/mesos/sbin/mesos.sh start_master --hostname {{local_ip['stdout']}} --advertise_ip {{local_ip['stdout']}} --quorum {{quorum}} --zk {{mesos_zk}}"

- name: start marathon
  shell: "{{remote_dir}}/marathon/bin/marathon.sh start --master {{master}} --zk {{marathon_zk}} --libmesos_path {{remote_dir}}/mesos/lib/libmesos.so --hostname {{local_ip['stdout']}}"

现start_master/tasks/main.yml

---

- name: get ip
  shell: ip addr|grep eth1|grep inet|awk '{print $2}'| cut -d / -f 1
  register: local_ip

- name: start zookeeper
  shell: "{{remote_dir}}/zookeeper/bin/zkServer.sh start"


- name: start mesos master
  shell: "{{remote_dir}}/mesos/sbin/mesos.sh start_master --hostname {{local_ip['stdout']}} --advertise_ip {{local_ip['stdout']}} --quorum {{quorum}} --zk {{mesos_zk}}"

同时添加start_framework/tasks/main.yml

---

- name: get ip
  shell: ip addr|grep eth1|grep inet|awk '{print $2}'| cut -d / -f 1
  register: local_ip

- name: start marathon
  shell: "{{remote_dir}}/marathon/bin/marathon.sh start --master {{master}} --zk {{marathon_zk}} --libmesos_path {{remote_dir}}/mesos/lib/libmesos.so --hostname {{local_ip['stdout']}}"

docker应用部署

我前两章提出了一个问题,关于能否在mesos进行Dockerfile的build这件事,我暂时好像没有找到有效的解决方法,所以目前还是以拉取镜像部署为场景

  • 应用场景

一个项目托管在github或者私人的gitlab上,当远程仓库有push时,就会触发webhook,将信息post给一个CI服务,CI将项目clone下来并进行镜像的build,并将镜像push到私有docker仓库,随后用API接口调用marathon进行部署

  • 简单的应用

这里我们编写了一个简单的测试服务及其Dockerfile,我们在这里并没有用到ci,docker私有镜像则采用阿里云的

app.py

from flask import Flask, jsonify


app = Flask(__name__)


@app.route('/test')
def hello():
    return jsonify({'msg': 'hello'})


if __name__ == '__main__':
    app.run(host='0.0.0.0')

Dockerfile

扫描二维码关注公众号,回复: 1066576 查看本文章
FROM python:3

COPY ["./", "/var/test_server"]

WORKDIR /var/test_server

RUN pip3 install -r requirements.txt -i https://pypi.douban.com/simple/

CMD ["python3", "app.py"]

marathon的部署配置

{
  "id": "mesos-learn",
  "cpus": 0.5,
  "mem": 64.0,
  "networks": [ { "mode": "container/bridge" } ],
  "container": {
    "type": "DOCKER",
    "docker": {
      "forcePullImage": false,
      "image": "xxxx:xxxx"
    },
    "portMappings": [
      {
        "containerPort": 5000,
        "hostPort": 31000,
        "protocol": "tcp",
        "servicePort": 10000
      }
    ]
  }
}

tips: slave上的docker都需要配置你的私有仓库才可以

写了一个添加私有docker仓库的模块
add_docker_registry.py

import json
import os
from ansible.module_utils.basic import AnsibleModule


def add_registry(uri, auth, docker_home):
    if not os.path.isfile(docker_home+'/config.json'):
        with open(docker_home+'/config.json', 'w') as f:
            json.dump({}, f)
    with open(docker_home+'/config.json') as f:
        d = json.load(f)
        if d.get('auths') is None:
            d['auths'] = {}
        d['auths'][uri] = {}
        d['auths'][uri]['auth'] = auth
    with open(docker_home+'/config.json', 'w') as f:
        json.dump(d, f)


def main():
    module = AnsibleModule(
        argument_spec=dict(
            uri=dict(),
            auth=dict(),
            docker_home=dict(type='str', default='~/.docker')
        )
    )
    uri, auth, docker_home = module.params['uri'], module.params['auth'], module.params['docker_home']
    if uri.strip() == '':
        module.fail_json(msg='uri could not be None')
    if auth.strip() == '':
        module.fail_json(msg='auth could not be None')
    docker_home = docker_home.replace('~', os.path.expanduser('~'))
    if not os.path.isdir(docker_home):
        os.mkdir(docker_home)
    add_registry(uri, auth, docker_home)
    module.exit_json(
        uri=uri,
        docker_home=docker_home,
        msg='success'
    )


if __name__ == '__main__':
    main()

Usage

- name: add private registry
  add_docker_registry: uri=xxx auth=xxx

auth就是username:passwordbase64编码后的字符串,其中username和password需要替换成你自己的值

具体项目: https://github.com/ncuwaln/mesos-learn

猜你喜欢

转载自blog.csdn.net/tjq980303/article/details/79849937