- 安裝docker環境
1. 先確定環境中是否存在docker舊版本,如果存在就先卸載。筆者使用的是最新安裝的虛擬機系統,不需要執行此步驟
sudo apt-get remove docker docker-engine docker-ce docker.io
2. 更新apt包。關於鏡像倉庫,ubuntu默認的是美國的地址,比較慢且容易出現網絡下載問題,建議改成國內的鏡像倉庫,筆者使用的是阿里雲的鏡像倉庫。
sudo apt-get update
3. 先將用戶切換到root權限,防止出現訪問權限問題。安裝以下包確保apt可以通過HTTPS使用存儲庫(repository)
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
4. 添加Docker官方的GPG密鑰
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –
5. 使用以下命令來設置stable存儲庫
sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \stable"
6. 再更新以下apt包後,安裝最新版本的docker ce
sudo apt-get update
sudo apt-get install -y docker-ce
7. 驗證docker服務是否啓動,如未啓動,則使用start命令啓動
systemctl status docker
sudo systemctl start docker
8. 運行hello world容器,有如下顯示則安裝成功
sudo docker run hello-world
- 安裝envoy(鏡像)及啓動
1. 在https://hub.docker.com/r/envoyproxy/envoy-alpine/鏡像網站中的tag頁面中查詢最新版鏡像
使用如下命令拉取鏡像(鏡像名稱tag默認爲latest,測試發現拉取不到鏡像,後修改爲具體的版本):
docker pull envoyproxy/envoy-alpine:v1.14.1
2. 查找鏡像二進制安裝目錄,進入目錄後將自定義的envoy.yaml配置文件掛載到指定目錄下(注:<>中的爲指定目錄,如筆者用的是/etc/envoy。-l debug爲設置日記級別,方便調試),命令如下:
./envoy -c </etc/envoy>.{json,yaml,pb,pb_text} --v2-config-only -l debug
3. 做一個最簡化的靜態配置測試envoy。編輯envoy.yaml做如下配置
admin:
access_log_path: /tmp/admin_access.log
address:
socket_address: { address: 127.0.0.1, port_value: 9901 }
static_resources:
listeners:
- name: listener_0
address:
socket_address: { address: 0.0.0.0, port_value: 10000 }
filter_chains:
- filters:
- name: envoy.http_connection_manager
config:
stat_prefix: ingress_http
codec_type: AUTO
route_config:
name: local_route
virtual_hosts:
- name: local_service
domains: ["*"]
routes:
- match: { prefix: "/" }
route: { cluster: some_service }
http_filters:
- name: envoy.router
clusters:
- name: some_service
connect_timeout: 0.25s
type: STATIC
lb_policy: ROUND_ROBIN
hosts: [{ socket_address: { address: 127.0.0.1, port_value: 80 }}]
在此基礎上啓動兩個容器,envoyproxy容器和nginx容器,nginx容器共享envoyproxy容器的網絡,以此來模擬sidecar。
docker run -d -p 10000:10000 -v `pwd`/envoy.yaml:/etc/envoy/envoy.yaml --name envoyproxy envoyproxy/envoy-alpine:v1.14.1
docker run -d --network=container:envoyproxy --name nginx nginx
根據配置文件的規則,envoy監聽10000端口,同時該端口也暴露在主機上。當有請求到達監聽上後,envoy會對所有請求路由到some_service這個cluster上,而該cluster的上游指向本地的80端口,也就是nginx服務上。訪問nginx如下: