關於網路那些事...

Marketing, SEO, Web trends, Programming tutorial, Web design, and Life event...

Docker - Get Start Part 3. Services

這裡記錄 Services 實作流程

在之前,要先透過 part2 建立了 image gordon/get-started:part2

在這裡,建立 docker-compose.yml ,透過 image 來產生五個重複的 container

version: "3"
services:
  web:
    # replace username/repo:tag with your name and image details
    image: gordon/get-started:part2
    deploy:
      replicas: 5
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "4000:80"
    networks:
      - webnet
networks:
  webnet:

接著先執行 swarm 初始化(這部分會在 part 4 介紹)

docker swarm init

接著,執行建立 service app 名稱為 getstartedlab

Continue Reading

Docker - Get Start Part 2. Containers

這裡記錄 Containers 實作流程

在這裡按照下方流程就能快速建立出一個 python demo web

預計將專案放置在 docker_project/get_started/

建立檔案: Dockerfile

# Use an official Python runtime as a parent image
FROM python:2.7-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

requirements.txt

Continue Reading

Docker - 基本指令紀錄

Docker 基本操作常用的指令:

docker build -t friendlyhello .  # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyhello  # Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyhello         # Same thing, but in detached mode
docker container ls                                # List all running containers
docker container ls -a             # List all containers, even those not running
docker container stop <hash>           # Gracefully stop the specified container
docker container kill <hash>         # Force shutdown of the specified container
docker container rm <hash>        # Remove specified container from this machine
docker container rm $(docker container ls -a -q)         # Remove all containers
docker image ls -a                             # List all images on this machine
docker image rm <image id>            # Remove specified image from this machine
docker image rm $(docker image ls -a -q)   # Remove all images from this machine
docker login             # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag  # Tag <image> for upload to registry
docker push username/repository:tag            # Upload tagged image to registry
docker run username/repository:tag                   # Run image from a registry

Continue Reading

Docker - TMPFS

Docker Mount 下圖清楚了說明Docker host的 Volume with bind mounts 以及tmpfs mount 機制

這裡針對 tmpfs 部分進行介紹:

tmpfs mount

tmpfs mounts 只會儲存於host system的記憶體,不會寫入 host system的 filesystem。

因此,tmpfs mount 資料不會保存在硬碟、Docker host或 container 裡,只會存在container 執行的期間

通常會把 tmpfs mount 應用在非敏感或不必持續保留的狀態資料,舉例來說,swarm service 就是使用 tmpfs mount 去 mount secrets 到 service的 container。

在 Docker 17.06 以上版本,建議使用 –mount flag 來進行 bind mounts, volumes, 或者 tmpfs mount。

tmpfs 使用案例

tmpfs mounts 最適合使用於不必持續保留的資料,或者當你的應用程式需要寫入大量的非持續保留的資料時。在 host machine 或者 container,適當的採用 tmpfs mount 幾可以讓container執行效能提升。

Continue Reading

Docker - Bind Mounts

Docker Mount 下圖清楚了說明Docker host的 Volume with bind mounts 以及tmpfs mount 機制

這裡針對 Bind Mounts 部分進行介紹:

Bind Mounts

Bind mounts 在早期 Docker 版本就已經存在,相對於 volumes ,使用 Bind mounts 有一些功能限制:

當 container 在host machine 中的檔案或目錄使用 bind mount

host machine 是以完整的路徑來參照這些檔案或目錄

因此,在Docker host 中不需要存在這些檔案或目錄

若參照的目錄或檔案不存在,則會自動建立。

透過 Bind Mounts 的方式是非常有效率的方式,但使用 bind mounts 需要 host machine 有特定的 filesystem 結構才能運作

如果你正開發一個新的 Docker 應用程式,考慮改用 Volumes 時,就不能直接透過Docker CLI 來管理 bind mounts。

Bind mounts 可以指定在敏感的檔案

使用 bind mounts 其中一個特點就是,

Continue Reading

Docker - Volumes 介紹

Docker Mount

下圖清楚了說明Docker host的 Volume with bind mounts 以及tmpfs mount 機制

這裡針對 Volumes 部分進行介紹:

Continue Reading

Docker - Storage drivers

Docker - Storage drivers

為了能有效率的使用 storage drivers,一定要先了解 Docker builds 以及 stores images,container如何使用images。

Storage drivers可以允許你在容器可寫入層(container writable layer) 建立資料

但是,在 Storage driver 操作資料,有這些缺點:

  • 讀寫效率很差
  • 停止運行container之後,這些資料並不會保存

通常會透過 volumes 來保存資料以及改善效能問題

一個 storage driver 負責處理各layer之間的互動

container 與 image 最大的差別就在於頂層可寫入層,在container新增或修改的資料都會存在可寫入層,當container刪除,這個可寫入層就會一併刪除,這底層的 image 則不會影響。

主要是因為每一個 container 都擁有自己的可寫入層,而多個 containers 可以共享同一個 image

例如: 這裡顯示多個 containers 共享同一個 Ubuntu 15.04 image

因此,Storage driver 用來管理 image layers 以及 可寫入層的內容,

雖然所有的 sotrage dirver 處理的方式各有差異,但所有的 driver 使用的都是同樣的 image 堆疊,以及寫入時複製策略(copy-on-write (CoW) strategy)。

可以透過下方指令檢查目前 可寫入層的使用量

docker ps -s

寫入時複製策略 copy-on-write (CoW) strategy

若某一個資料位於底層image,

Continue Reading

Docker - 資料管理簡介

在 Docker 管理資料

在 container 所建立的檔案,預設都會存放在可容器寫入層(writable container layer)

這表示:

  • 當container不沒有運行時,資料就會跟著消失
  • container中的數據很難從外部取得
  • 可寫入容器層的資料,與container高度耦合,很難將資料搬移到其他地方
  • 需要透過 storage driver 來管理filesystem

當你建立一個新的 container,會同時在最上層建立一個新的可寫入層(writable layer),也稱為 container layer。

在 container內所有的改變都會在這裡面,例如: 建立新檔案,編輯檔案,刪除檔案,都會寫入這個容器可寫入層。

Continue Reading

Laravel Migrate-Generate

安裝擴充

composer require --dev "xethron/migrations-generator"

檢查laravel 版本

php artisan --version

Continue Reading

Docker - Compose 介紹 (上)

Docker-Compose 是一個可以用來定義且執行多個 Container 應用程式的工具

可以很簡單的透過 Compose 的 yaml 來設定你的container應用程式

並且透過一個指令,就可以建立及啟動所有yaml設定的應用程式

Compose 可以在產品、階段產品、開發或測試項目使用

在使用上大致可以區分為三個過程:

Continue Reading

Docker - 安裝 MySQL

MySQL 是一個廣泛被使用,開源的關聯式資料庫管理系統( relational database management system, RDBMS)

目前的MySQL執行效能不斷提升,可靠且易於使用,

因此經常被作為網路應用程式開發資料庫的首選.

其中包括 Facebook, Twitter, YouTube, Yahoo 等公司都有使用。

詳細說明可參考官網: www.mysql.com.

這裡將說明如何在 Docker 安裝即執行 MySQL 環境

Continue Reading

Docker - 執行 Nginx Webserver

執行 Nginx Container

-d 表示在背景( Detached )執行,Docker 預設前景( foreground )執行

-p 表示將本機 8080 port 的來源轉發到 container 的 80 port

–name 表示為 container 的名稱

–rm 表示當 exit container 時,會移除 container( incompatible with -d )

docker run -d -p 80:80 --name mywebserver nginx

//if need custom port (ex 3000) can do like this

docker run -d -p 3000:80 --name mywebserver nginx

由於 local 還沒有 nginx 的 image,因此Docker 會先執行檢查及下載 nginx image

Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
...下載安裝
Status: Downloaded newer image for nginx:latest

安裝完畢之後,就會直接對 nginx image 進行 instance 為 nginx container,並執行

Continue Reading

Docker - MAC安裝與執行 Hello World

Docker 在 Mac 環境可以直接透過 Install Docker for Mac 來安裝

前往 Docker for Mac 下載 Docker.dmg 執行安裝

檢查版本

透過下列方式來檢查 docker 版本,確定安裝完畢

docker --version

docker-compose --version

docker-machine --version

Continue Reading

Docker - Remove all images and container

Delete all docker containers

docker rm $(docker ps -a -q)

Delete one containers by name

docker rm mycontainername

Delete all docker images

docker rmi $(docker images -q)

Delete images by name

docker rmi images_name

Check Image and containers

//all container (include un-run container)
docker ps -a
//on running container
docker ps

//all images
docker images

Remove Laradock file

rm -rf laradock/

Continue Reading

Docker - 常用指令介紹

##【 Docker 】

關閉 Docker

docker-compose down

【 Container 】

建立container

docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]

OPTION:

-d 表示在背景( Detached )執行,Docker 預設前景( foreground )執行

-p 表示將本機 8080 port 的來源轉發到 container 的 80 port

–name 表示為 container 的名稱

–rm 表示當 exit container 時,會移除 container( incompatible with -d )

Continue Reading

Redis - PUBSUB、PUBLISH 介紹

Redis - PUBSUB、PUBLISH 介紹

PUBSUB

查詢訂閱狀態與發佈系統的狀態

> PUBSUB xxxchanne

PUBLISH

將訊息發送到指定的 channel

> PUBLISH xxxchanne "Message Say Hi"

Continue Reading

Redis - SUBSCRIBE、PSUBSCRIBE、UNSUBSCRIBE、PUNSUBSCRIBE 訂閱功能介紹

Redis - SUBSCRIBE、PSUBSCRIBE、UNSUBSCRIBE、PUNSUBSCRIBE 訂閱功能介紹

SUBSCRIBE 訂閱

Subscribe 允許 client 端訂閱一個或多個 channels

> SUBSCRIBE channel [channel ...]

當 client 端 subscribe 某個 channels 之後,

除了subscribe相關指令(subscribe, psubscribe, unsubscribe, punsubscribe ),

一律不允許使用其他 commands

> SUBSCRIBE xxxchannel 
Reading messages...(press Ctrl-C to quit)
1) "subscribe"
2) "xxxchannel"
3) (integer) 1
1) "message"
2) "xxxchannel"
3) "a"

PSUBSCRIBE

使用 PSUBSCRIBE 訂閱一個或多個頻道時,可以透過一些模式,來訂閱

? : 表示任一字元

  • : 表示多個任意字元 [nnnn] : 表示在這其中任一字元

例如: H?llo 會包含 Hello, Hallo, Hbllo, Hcllo, ……. H*llo 會包含 Heeeeello, Hllo, Hxllo…… H[ae]llo 會包含 Hello, Hallo (但不包括 Hllo)

Continue Reading

Redis - pipeline 簡介

Redis - pipeline 簡介

Redis client 每次發送一筆 command 都會經過 發送命令> 命令列隊(排隊)> 執行 > 返回結果 ,這期間所需要的時間,就稱為RTT(Round Trip Time, 往返時間)

Redis 通常可以執行在微秒等級,但是在不同網路環境,就會因網路本身的傳輸限制而有所差異,因此才有Redis 性能瓶頸就是網路 一說

pipeline 可以將多筆資料同時發送出去,

如果不使用 pipelining,每一個command 從訪問資料結構、取得回覆,看起來耗費的流量都非常小,

但對於 socket I/O 而言,每個訪問都執行一次comman,占用的效能成本就相當大

當使用 pipelining,可以將多個指令組成一次發送出去,讓系統只需讀取一次 read(),並且將多個回覆內容也組成一次輸出 write()

雖然 pipeline 可以減少 RTT,減少IO調用次數

但是直接發送 command 只需要 Redis server 端處理,

透過 pipeline 則需要 server 端-client端 共同實現

因此仍須注意 pipeline 大小,是否會超過 Client端的緩存限制,若超過則緩存刷新或直接發送,可能會發生無法預期的問題

通常對於過大的 pipeline ,可以透過拆分來完成減少單次大小

Pipeline 代理

通常為了讓Redis可以有更好的負載能力,都會在 Redis 前面外加一個代理,例如: Codis Pipeline 緩存默認10K,3.1則是1024,Jedis 緩存默認為 8192

Continue Reading

NEW FIX 4.4 電文說明

NEW FIX 4.4 電文說明

台灣證券交易所規範了FIX通訊協定作業平台,簡稱FIX

證交所TCP/IP資訊網路透過MPLS架構,讓證交所與每個券商主機之間都設定了點對點固定IP VC(Virtual Circuit)連線

通常發起端為證券商,建立連線之後傳送登入訊息讓證交所驗證,

FIX協定為非同步傳輸方式,可連續傳送委託單

基本介紹如下:

FIX 組成

FIX tag 組成包括:

  • Header 表頭
  • Body 訊息內容
  • Trailer 表尾

Header 固定開頭為:

8=FIX.4.4<SOH>

Body 是要傳送的內容,基本格式為:

Key=Value<SOH>....

Trailer 結束使用的格式為: 其中 nnn 是透過 CheckSum 計算出的數值

10=nnn<SOH>

建立FIX連線

  • 建立連線
  • 驗證連線
  • 發起端傳送登入訊息(Logon)

FIX Session 流程

FIX Session 主要用於登入、交換訊息、登出:

  • Logon : 登入
  • Message exchange : 訊息交換
  • Logout : 登出

連線驗證

  1. 發起端無傳送登入訊息 當發起端原本應該傳送登入訊息給接收端,超過60秒未傳送登入訊息,連線就會主動中斷 這時發起端就要再重新連線

  2. 接收端驗證 接收端會驗證登入訊息,若驗證成功會立即回覆 Logon 登入訊息,若驗證失敗會回覆 Logout 登出信息

  3. 開始同步 發起端接收到 Logon 登入訊息後,就能開始傳送其他訊息

    Continue Reading

16 進位簡介

16 進位簡介

電腦世界處理的基礎是 0 跟 1,我們稱之為二進位(binary)

二進位可以在組成十進位 0~9

不同的系統或程式對於十六進位都有不同的描述方式,但是基本概念如下:

十六進位則是由 09 AF 組成

十六進位剛好是 4^2 ,轉二進位相當容易

例如: 二進位 01011110101101010010 可直接轉十六進位 5EB52

另外是 Byte 共 8 Bit

其中將 4 個 Bit 剛好可儲存一個16進位

各種場合帶入16進位方式

環境 格式 備註
URL %hex
XML,XHTML &#xhex
HTML,CSS #hex 6位,用於表示顏色
Unicode U+hex 6位,表示字符編碼
MIME =hex
Modula-2 #hex
Smalltalk,ALGOL 68 16rhex
Common Lisp #xhex或#16rhex
IPv6 8個hex用:分隔

Continue Reading