溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

宜信開源|手把手教你安裝第一個LAIN應用

發(fā)布時間:2020-08-01 22:01:28 來源:網(wǎng)絡 閱讀:3102 作者:宜信技術(shù) 欄目:軟件技術(shù)

一、安裝 LAIN 集群

本節(jié)包含三種場景下安裝LAIN集群

  • 本地啟動虛擬機安裝LAIN集群,可供本地測試開發(fā)
  • 物理服務器/虛擬機安裝LAIN集群
  • 云服務器安裝LAIN集群

三者都需要從 GitHub 獲取已經(jīng)發(fā)布的 LAIN 版本源代碼:
https://github.com/laincloud/lain/releases

下載源碼后在目標機器上解壓即可。

tar xf lain-VERSION.tar.gz

1、本地安裝LAIN集群

1)環(huán)境依賴

  • Linux / MacOS
  • 能夠連接到互聯(lián)網(wǎng)
  • VirtualBox 5.1.22 r115126 (Qt5.6.2)
  • Vagrant 1.9.4
  • 最少 2G 剩余內(nèi)存(如果需要拉起多個節(jié)點,最少 3G)

2)初始化

啟動并初始化第一個節(jié)點

cd lain-VERSION
vagrant up --provision

啟動耗時取決于 vagrant box 下載時間, 啟動完成后 vagrant 會自動 執(zhí)行bootstrap進行初始化, 初始化需要至少20分鐘,取決于網(wǎng)絡速度。 初始化過程為集群默認配置vip=192.168.77.201

如果出現(xiàn)以下錯誤:

Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attempted was:

mount -t vboxsf -o uid=1000,gid=1000 vagrant /vagrant

The error output from the command was:

/sbin/mount.vboxsf: mounting failed with the error: No such device

這個錯誤是因為宿主機的 Virtual Box 的 Guest Additions 與 laincloud/centos-lain box 已安裝的 Guest Additions 版本不一致引起的,導致無法創(chuàng)建 /vagrant 這個同步 目錄。請修改工程根目錄下的 Vagrantfile,禁止宿主機強行安裝新版本的 Guest Additions, 即添加如下配置:

config.vbguest.auto_update = false

3)添加更多節(jié)點

vagrant up node2

# 待 node2 啟動后
[vagrant@node1 ~]$ cd /vagrant
[vagrant@node1 ~]$ sudo lainctl node add -p playbooks node2:192.168.77.22
# root 密碼為 vagrant

4)同理可以如此添加 node3

2、物理服務器/虛擬機安裝LAIN集群

1)環(huán)境依賴

  • CentOS 7.2
  • NTP 服務保證節(jié)點間時間一致
  • 需要能訪問到可用的 yum 源(包括 epel)
  • 能夠連接到互聯(lián)網(wǎng)
  • 各節(jié)點之間能夠互相 ssh
  • 各節(jié)點 hostname 不同
  • 各個節(jié)點位于同一個路由器之內(nèi)

2)初始化

第一個節(jié)點

cd lain-VERSION
# 選擇一個同網(wǎng)段的未被使用的 IP 地址作為 VIP
sudo ./bootstrap -r docker.io/laincloud --vip={{ vip }}

國內(nèi)用戶建議通過 -m 參數(shù)使用 aliyun 的加速器下載鏡像,使用方式為

sudo ./bootstrap -m https://l2ohopf9.mirror.aliyuncs.com \
-r docker.io/laincloud --vip=192.168.77.201

添加更多節(jié)點

# 需要輸入 root 密碼
sudo lainctl node add -p playbooks {{ hostname }}:{{ ip }}

3、云服務器安裝LAIN集群

1)環(huán)境依賴

  • CentOS 7.2
  • NTP 服務保證節(jié)點間時間一致
  • 需要能訪問到可用的 yum 源(包括 epel)
  • 能夠連接到互聯(lián)網(wǎng)
  • 各節(jié)點之間能夠互相 ssh
  • 各節(jié)點 hostname 不同
  • 各個節(jié)點位于同一個 VPC (或虛擬路由器)之內(nèi)

3) 初始化

第一個節(jié)點

cd lain-VERSION

# 如果 VPC 不對數(shù)據(jù)包進行來源 IP 限制(如×××)
sudo ./bootstrap -r docker.io/laincloud

# 如果 VPC 限制了數(shù)據(jù)包的來源 IP(如阿里云)
sudo ./bootstrap -r docker.io/laincloud --ipip

添加更多節(jié)點

# 需要輸入 root 密碼
sudo lainctl node add -p playbooks {{ hostname }}:{{ ip }}

4、配置LAIN Console 的域名解析

LAIN Console 組件是 LAIN 集群的控制臺,配置域名解析后即可在瀏覽器訪問 http://console.lain.local

echo "IP/VIP  console.lain.local" >> /etc/hosts

5、常見問題

add-node ssh-copy-id 失敗

如果出現(xiàn) ssh-copy-id 失敗,可能需要把 node1:/root/.ssh/lain.pub 內(nèi)容放到 node2:/root/.ssh/authorized_keys里,新增一行。當然原因可能是多樣的,最有可能就是 lain-02 的 /root/.ssh 目錄或者目錄中的文件權(quán)限不對

二、安裝 LAIN 客戶端

開發(fā) LAIN 應用時,需要安裝 LAIN 客戶端。LAIN 客戶端依賴于:

  • docker
  • python

只要支持 docker 和 python 的系統(tǒng),就可以使用 LAIN 客戶端,比如 Linux 和 macOS。

以下均假設 LAIN 集群的 domain 為 lain.local,對于其他 domain,將下文中的 lain.local 替換即可。

1、安裝與配置 docker

1)安裝

請參考 https://docs.docker.com/engine/installation/ 安裝 docker >= 1.12。

2)配置

Insecure Registries

安裝好之后,請參考 https://docs.docker.com/registry/insecure/ 將 registry.lain.local 添加進 docker daemon 的 insecure-registries 選項。

例如,在 Linux 上,如下的 /etc/docker/daemon.json 表示將 registry.lain.local 加入了 insecure-registries

{
  "insecure-registries" : ["registry.lain.local"]
}

macOS 上的 insecure registries 配置如下圖所示:

宜信開源|手把手教你安裝第一個LAIN應用

配置 insecure-registries 是因為 LAIN 集群的鏡像倉庫只提供了 HTTP 服務,沒有提供 HTTPS 服務。

2、安裝與配置 lain-cli

pip install lain-cli

推薦使用 virtualenv 安裝這個 python 包,即:

pip install virtualenv  # 安裝 virtualenv
virtualenv lain  # lain 是虛擬環(huán)境的名字,也可以取其他名字
source lain/bin/activate  # 激活 lain 虛擬環(huán)境
pip install lain-cli
deactivate  # 退出 lain 虛擬環(huán)境

以后需要使用 lain-cli 的時候,用 source ${lain-virtualenv-path}/bin/activate 激活 lain 虛擬環(huán)境; 不使用 lain-cli 的時候,用 deactivate 退出 lain 虛擬環(huán)境

安裝好之后,需要配置 lain-cli,讓 lain-cli 知道 LAIN 集群的 Domain 等信息:

lain config show  # 顯示當前配置
lain config save-global private_docker_registry registry.lain.local # 配置 docker 私有倉庫
lain config save local domain lain.local  # 保存 lain.local 集群,并取名為 local

假如 LAIN 集群開啟了 sso 驗證(默認未開啟),請配置 sso:

lain config save local sso_url https://sso.lain.local

3、配置 LAIN 組件的域名解析

如果 LAIN 集群不能在公網(wǎng)上解析(比如本地啟動的集群),請配置 /etc/hosts。

如果啟動集群時,使用了 vip 模式,即使用了 /vagrant/bootstrap -r docker.io/laincloud --vip=192.168.77.201 啟動,請執(zhí)行:

echo "192.168.77.201  registry.lain.local console.lain.local entry.lain.local lvault.lain.local ipaddr-client.lain.local ipaddr-service.lain.local ipaddr-client.ipaddr-resource.resource.lain.local" >> /etc/hosts

192.168.77.201 是啟動集群時的 vip 參數(shù)

如果啟動集群時,沒有使用 vip 模式,即使用了 /vagrant/bootstrap -r docker.io/laincloud 啟動,請執(zhí)行:

echo "192.168.77.21  registry.lain.local console.lain.local entry.lain.local lvault.lain.local ipaddr-client.lain.local ipaddr-service.lain.local ipaddr-client.ipaddr-resource.resource.lain.local" >> /etc/hosts

192.168.77.21 是集群的主節(jié)點的 IP,即啟動集群的節(jié)點的 IP。

三、第一個 LAIN 應用

本節(jié)會演示如何基于 LAIN 集群創(chuàng)建一個 LAIN 應用,它提供 HTTP 服務,當用戶訪問 / 時,返回 Hello, LAIN.。

1、前置條件

  • 首先需要一個 LAIN 集群,建議由 2 個節(jié)點組成
  • 其次需要本地的開發(fā)環(huán)境。具體步驟見安裝 LAIN 客戶端。

LAIN 是基于 docker 的 PaaS 系統(tǒng),建議先了解下 docker 的基本概念:

  • Docker 官方文檔:https://docs.docker.com/
  • Docker 從入門到實踐:https://yeasy.gitbooks.io/docker_practice/content/

2、業(yè)務代碼

package main

import (
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Hello, LAIN."))
    })

    http.ListenAndServe(":8080", nil)
}

代碼的邏輯為:

  • 監(jiān)聽 0.0.0.0:8080 端口
  • 收到 /的 HTTP 請求時,返回 Hello, LAIN.

3、lain.yaml

lain.yaml 是 LAIN 應用的配置文件,如下例所示:

appname: hello-world  # 應用名,在集群內(nèi)唯一,由小寫字母、數(shù)字和 `-` 組成,且開頭不能為數(shù)字,不能有連續(xù)的 `-`

build:  # 描述如何構(gòu)建 hello-world:build-${git-committer-date}-${git-commit-hash} 鏡像
  base: golang:1.8  # 基礎鏡像,類似于 Dockerfile 里的 FROM
  script:
    - go build -o hello-world  # 編譯指令,類似于 Dockerfile 里的 RUN,WORKDIR 為 /lain/app

proc.web:  # 定義一個 proc,名字為 web
  type: web  # proc 類型為 web(LAIN 會為 web 類型的 proc 配置 ${appname}.${LAIN-domain} 的域名,對外提供 HTTP 服務)
  cmd: /lain/app/hello-world  # 因為 WORKDIR 為 /lain/app,所以編譯好的程序在 /lain/app 目錄下
  port: 8080  # hello-world 監(jiān)聽的端口

因為我們需要提供 HTTP 服務,所以定義一個 web 類型的 proc,LAIN 集群會為 web 類型的 proc 自動分配 ${appname}.${LAIN-domain} 的域名。

proc.type 為 web 時,其名字也必須為 web,即一個 app 只能有一個 web 類型的 proc,且其名字為 web。

laincloud/hello-world@basic 的完整代碼在這里:https://github.com/laincloud/hello-world/tree/basic

4、本地運行

[vagrant@lain ~]$ cd ${hello-world-project}  # 進入工程目錄

[vagrant@lain hello-world]$ lain build  # 構(gòu)建 hello-world:build-${git-committer-date}-${git-commit-hash} 鏡像,生成編譯結(jié)果
>>> Building meta and release images ...
>>> found shared prepare image at remote and local, sync ...
>>> generating dockerfile to /Users/bibaijin/Projects/go/src/github.com/laincloud/hello-world/Dockerfile
>>> building image hello-world:build-1496631978-a46ef7afcbbc18c1c1248bcbb84fa770d0758005 ...
Sending build context to Docker daemon 6.656 kB
Step 1/4 : FROM registry.lain.local/hello-world:prepare-0-1494908044
 ---> 7406706a7f21
Step 2/4 : COPY . /lain/app/
 ---> 45f6215362ad
Removing intermediate container 41e822d3b086
Step 3/4 : WORKDIR /lain/app/
 ---> 75c0f3094b6e
Removing intermediate container 24065cf1d7de
Step 4/4 : RUN ( go build -o hello-world )
 ---> Running in 43cefd489608
 ---> 644f596f83c8
Removing intermediate container 43cefd489608
Successfully built 644f596f83c8
>>> build succeeded: hello-world:build-1496631978-a46ef7afcbbc18c1c1248bcbb84fa770d0758005
>>> tag hello-world:build-1496631978-a46ef7afcbbc18c1c1248bcbb84fa770d0758005 as hello-world:release-1496631978-a46ef7afcbbc18c1c1248bcbb84fa770d0758005
>>> generating dockerfile to /Users/bibaijin/Projects/go/src/github.com/laincloud/hello-world/Dockerfile
>>> building image hello-world:meta-1496631978-a46ef7afcbbc18c1c1248bcbb84fa770d0758005 ...
Sending build context to Docker daemon 6.656 kB
Step 1/2 : FROM scratch
 --->
Step 2/2 : COPY lain.yaml /lain.yaml
 ---> cfdb9c518f0d
Removing intermediate container ab94a3603b8a
Successfully built cfdb9c518f0d
>>> build succeeded: hello-world:meta-1496631978-a46ef7afcbbc18c1c1248bcbb84fa770d0758005
>>> Done lain build.

[vagrant@lain hello-world]$ lain run web  # 在本地運行
>>> run proc hello-world.web.web with image hello-world:release-1496631978-a46ef7afcbbc18c1c1248bcbb84fa770d0758005
59f7fe4b1a7c6214361ecd5e06b19023ab7e02058888aa625749028af7b92954
>>> container name: hello-world.web.web
>>> port mapping:
>>> 8080/tcp -> 0.0.0.0:32769
  • lain-cli 的所有命令均需要在包含 lain.yaml 文件的目錄下運行。
  • lain-cli 依賴于 git 管理版本,所以要先安裝 git,而且在 lain <br/>build之前進行 git commit

docker ps 時,可以看到:

CONTAINER ID        IMAGE                                                                     COMMAND                  CREATED             STATUS              PORTS                     NAMES
59f7fe4b1a7c        hello-world:release-1496631978-a46ef7afcbbc18c1c1248bcbb84fa770d0758005   "/lain/app/hello-w..."   27 seconds ago      Up 31 seconds       0.0.0.0:32769->8080/tcp   hello-world.web.web

上面的輸出表示 lain 通過 docker 把 hello-world.web.web 容器里的 8080 端口映射到了主機的 32769,所以,可以在主機上訪問:

[vagrant@lain hello-world]$ curl http://localhost:32769
Hello, LAIN.

得到了預期的結(jié)果。

5、部署到 LAIN 集群

從上一小節(jié)可以看到,本地運行沒有問題,現(xiàn)在可以部署到 LAIN 集群了:

[vagrant@lain ~]$ cd ${hello-world-project}  # 進入工程目錄

[vagrant@lain hello-world]$ lain build  # 構(gòu)建 hello-world:build-${git-committer-date}-${git-commit-hash} 鏡像,生成編譯結(jié)果

[vagrant@lain hello-world]$ lain tag local  # 類似于 docker tag,為 hello-world:(meta/release)-${git-committer-date}-${git-commit-hash} 鏡像添加倉庫前綴
>>> Taging meta and relese image ...
>>> tag hello-world:meta-1496631978-a46ef7afcbbc18c1c1248bcbb84fa770d0758005 as registry.lain.local/hello-world:meta-1496631978-a46ef7afcbbc18c1c1248bcbb84fa770d0758005
>>> tag hello-world:release-1496631978-a46ef7afcbbc18c1c1248bcbb84fa770d0758005 as registry.lain.local/hello-world:release-1496631978-a46ef7afcbbc18c1c1248bcbb84fa770d0758005
>>> Done lain tag.

[vagrant@lain hello-world]$ lain push local  # 類似于 docker push,將鏡像推送到 LAIN 集群
>>> Pushing meta and release images ...
>>> pushing image registry.lain.local/hello-world:meta-1496631978-a46ef7afcbbc18c1c1248bcbb84fa770d0758005 ...
The push refers to a repository [registry.lain.local/hello-world]
1a4886bd9611: Layer already exists
meta-1496631978-a46ef7afcbbc18c1c1248bcbb84fa770d0758005: digest: sha256:daed70190af5fa980d6963fd3a6350591708c1568e180fe85e7eb6cfdd12d998 size: 524
>>> pushing image registry.lain.local/hello-world:release-1496631978-a46ef7afcbbc18c1c1248bcbb84fa770d0758005 ...
The push refers to a repository [registry.lain.local/hello-world]
1a2245680fe1: Layer already exists
dfe083dd50ba: Layer already exists
edac683c8e67: Layer already exists
0372f18510d4: Layer already exists
c0b53d6ac422: Layer already exists
bcf20a0a17f3: Layer already exists
9d039e60afe3: Layer already exists
a172d29265f3: Layer already exists
e6562eb04a92: Layer already exists
596280599f68: Layer already exists
5d6cbe0dbcf9: Layer already exists
release-1496631978-a46ef7afcbbc18c1c1248bcbb84fa770d0758005: digest: sha256:1cea69b6ed882fcc16f1f5661b3830a8b3f20263264c51d0610b8ec09e72a439 size: 2626
>>> Done lain push.

[vagrant@lain hello-world]$ lain deploy local  # 將應用部署到 LAIN 集群
>>> Begin deploy app hello-world to local ...
upgrading... Done.
>>> app hello-world deploy operation:
>>>     last version: 1496631978-a46ef7afcbbc18c1c1248bcbb84fa770d0758005
>>>     this version: 1496631978-a46ef7afcbbc18c1c1248bcbb84fa770d0758005
>>> if shit happened, rollback your app by:
>>>     lain deploy -v 1496631978-a46ef7afcbbc18c1c1248bcbb84fa770d0758005
  • local 為 LAIN 集群的名字,請參考安裝 LAIN 客戶端中的設置
  • lain tag 為鏡像添加倉庫前綴,之后才能進行 lain push
  • release 鏡像包含了編譯成果,將來會以這個鏡像為基礎運行容器
  • meta 鏡像包含 lain.yaml 文件,用于 LAIN 集群解析,用戶不需要關心
  • 部署的過程是一個異步的過程,在 lain deploy local 之后可以使用 lain ps local 查詢部署結(jié)果。

此時,可以通過以下命令訪問 hello-world

[vagrant@lain hello-world]$ curl -H "Host: hello-world.lain.local" http://192.168.77.201
Hello, LAIN.

或者可以先更改 /etc/hosts 文件,然后直接使用域名訪問:

[vagrant@lain hello-world]$ echo "192.168.77.201 hello-world.lain.local" >> /etc/hosts
[vagrant@lain hello-world]$ curl http://hello-world.lain.local
Hello, LAIN.

上面的 192.168.77.201 是本地 LAIN 集群的虛擬 IP,沒有以 vip方式啟動,請使用 192.168.77.21

得到了 Hello, LAIN. 的響應,符合我們的預期。

GitHub地址:https://github.com/laincloud

白皮書:https://laincloud.gitbooks.io/white-paper/content/

來源:宜信技術(shù)學院

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI