0.前言
一開始學的以後,是想一步到位直接上Kubernetes(K8s)的,後面沒想到,好像有點複雜,有些概念不是很懂。所以學習東西仍是要按部就班,慢慢來。先了解單機編排技術Docker Compose,瞭解一些技術細節及原理後,在入手K8s。仍是不能一口吃成胖子,要多吃幾口才能夠。並且目前公司都是一些小項目,能用得上DockerCompose已經很不錯了,還想要上K8s,估計是不現實的。html
1. 安裝java
能夠經過運行下面命令進行安裝,git
1 curl -L https://github.com/docker/compose/releases/download/1.23.1/docker-compose-`uname -s`-`uname -m` -o docker-compose 2 chmod +x docker-compose 3 mv docker-compose /usr/local/bin
安裝Tab自動補全github
curl -L https://raw.githubusercontent.com/docker/compose/1.23.1/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose
因爲我看得是《Spring Cloud與Docker微服務架構實戰 第二版》這本書,裏面恰好有個例子,這裏就用這個作練手。spring
1 git clone https://github.com/itmuch/spring-cloud-docker-microservice-book-code-docker/ 2 cd spring-cloud-docker-microservice-book-code-docker 3 cd docker-1-simple 4 mvn clean package docker:build
裏面的每一個微服務都構建成一個Docker Image,而後就能夠開幹了。docker
2. 簡單例子bash
從一個簡單的例子開始
Dockerfile 文件架構
1 FROM java:latest 2 VOLUME /tmp 3 ADD microservice-discovery-eureka-0.0.1-SNAPSHOT.jar app.jar 4 RUN bash -c 'touch /app.jar' 5 EXPOSE 9000 6 ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar"]
docker-compose.yml 文件app
1 version: '3' 2 services: 3 eureka: 4 build: . 5 ports: 6 - "8761:8761"
執行 docker-compose updom
從下圖能夠看出,docker-compose 執行後,分別建立了 compose_eureka Image, compose_default Network, compose_eureka_1_b5 Container
以上就是入門了,關於docker-compose更多的語法,這裏就不展開了,具體確定是參考其餘博客,或者官方文檔吧
3. 實戰練習
參考書本上的例子進行練習
3.1 基於Spring Boot微服務的Compose編寫
這裏要使用上述 github 代碼裏 docker-1-simple 這個目錄下的代碼進行編譯
1 version: '3' 2 services: 3 discovery: 4 image: registry.cn-shenzhen.aliyuncs.com/wunaozai/microservice-discovery-eureka:0.0.1-SNAPSHOT 5 ports: 6 - "8761:8761" 7 microservice-provider-user: 8 image: registry.cn-shenzhen.aliyuncs.com/wunaozai/microservice-provider-user:0.0.1-SNAPSHOT 9 microservice-consumer-movie-ribbon-hystrix: 10 image: registry.cn-shenzhen.aliyuncs.com/wunaozai/microservice-consumer-movie-ribbon-hystrix:0.0.1-SNAPSHOT 11 microservice-gateway-zuul: 12 image: registry.cn-shenzhen.aliyuncs.com/wunaozai/microservice-gateway-zuul:0.0.1-SNAPSHOT 13 microservice-hystrix-dashboard: 14 image: registry.cn-shenzhen.aliyuncs.com/wunaozai/microservice-hystrix-dashboard:0.0.1-SNAPSHOT 15 ports: 16 - "8030:8030" 17 microservice-hystrix-turbine: 18 image: registry.cn-shenzhen.aliyuncs.com/wunaozai/microservice-hystrix-turbine:0.0.1-SNAPSHOT
運行後,就啓動對應的6個Image,如下是運行後效果
使用以前的weave scope工具查看docker, 能夠看到5個微服務經過Spring Boot的Eureka鏈接在一塊兒了。還有一個microservice-hystrix-dashboard這個沒有在裏面是由於這個是一個儀表盤(Dashboard),不屬於實際業務,因此代碼裏面沒有關聯起來,其實是已啓動的。
從圖中還能夠看到,後面的那兩個集羣etcd和elasticsearch,這兩個是以前作練習時建立的。初學者,利用這個weave scope工具來查看和監控,挺好用的。
3.2 基於Eureka高可用(HA)的Compose編寫
這裏要使用上述 github 代碼裏 docker-2-eureka-ha 這個目錄下的代碼進行編譯
1 version: "3" 2 services: 3 peer1: # 默認狀況下,其餘服務可以使用服務名稱鏈接到該服務。對於peer2節點,它需鏈接http://peer1:8761/eureka/,所以,咱們可配置該服務的名稱爲peer1。 4 image: registry.cn-shenzhen.aliyuncs.com/wunaozai/microservice-discovery-eureka-ha:0.0.1-SNAPSHOT 5 ports: 6 - "8761:8761" 7 environment: 8 - spring.profiles.active=peer1 9 peer2: 10 image: registry.cn-shenzhen.aliyuncs.com/wunaozai/microservice-discovery-eureka-ha:0.0.1-SNAPSHOT 11 hostname: peer2 12 ports: 13 - "8762:8762" 14 environment: 15 - spring.profiles.active=peer2 16 17 ## 使用Compose編排高可用的Eureka Server
經過Eureka自帶的Dashboard能夠看到啓動的兩個容器已經互相註冊,實現高可用(HA)功能
3.3 基於Spring Boot微服務集羣及動態伸縮的Compose編寫
這裏要使用上述 github 代碼裏 docker-3-complex 這個目錄下的代碼進行編譯,要在每一個項目的根目錄下,執行 mvn clean package docker:build
1 version: "3" 2 services: 3 peer1: 4 image: itmuch/microservice-discovery-eureka-ha:0.0.1-SNAPSHOT 5 ports: 6 - "8761:8761" 7 environment: 8 - spring.profiles.active=peer1 9 peer2: 10 image: itmuch/microservice-discovery-eureka-ha:0.0.1-SNAPSHOT 11 hostname: peer2 12 ports: 13 - "8762:8762" 14 environment: 15 - spring.profiles.active=peer2 16 microservice-provider-user: 17 image: itmuch/microservice-provider-user:0.0.1-SNAPSHOT 18 microservice-consumer-movie-ribbon-hystrix: 19 image: itmuch/microservice-consumer-movie-ribbon-hystrix:0.0.1-SNAPSHOT 20 microservice-gateway-zuul: 21 image: itmuch/microservice-gateway-zuul:0.0.1-SNAPSHOT 22 microservice-hystrix-turbine: 23 image: itmuch/microservice-hystrix-turbine:0.0.1-SNAPSHOT
經過Eureka的Dashboard查看
仔細查看下圖,能夠觀察到各個Container間箭頭的指向,用於理解Spring Boot各個微服務間關係
執行如下命令,爲各個微服務動態擴容。讓各個微服務都執行3個實例,因爲docker-compose版本問題,建議使用 --scale 參數來設置啓動實例。
1 docker-compose up --scale microservice-provider-user=3 \ 2 --scale microservice-consumer-movie-ribbon-hystrix=3 \ 3 --scale microservice-gateway-zuul=3 \ 4 --scale microservice-hystrix-turbine=3
Eureka的Dashboard查看
3.4 修改以前博客中的etcd集羣配置,改成docker-compose方式啓動(本身實踐)
1 version: "3" 2 services: 3 etcd01: 4 image: etcd:3.3 5 ports: 6 - "10001:2379" 7 - "10011:2380" 8 #volumes: 9 #- "/root/workspace/docker/k8s/etcd/data11:/data" 10 command: 11 - /bin/etcd 12 - --data-dir 13 - /data 14 - -name 15 - etcd01 16 - -advertise-client-urls 17 - http://etcd01:2379 18 - -listen-client-urls 19 - http://0.0.0.0:2379 20 - -initial-advertise-peer-urls 21 - http://etcd01:2380 22 - -listen-peer-urls 23 - http://0.0.0.0:2380 24 - -initial-cluster-state 25 - new 26 - -initial-cluster-token 27 - docker-etcd 28 - -initial-cluster 29 - etcd01=http://etcd01:2380,etcd02=http://etcd02:2380,etcd03=http://etcd03:2380 30 etcd02: 31 image: etcd:3.3 32 ports: 33 - "10002:2379" 34 - "10012:2380" 35 #volumes: 36 #- "/root/workspace/docker/k8s/etcd/data22:/data" 37 command: 38 - /bin/etcd 39 - --data-dir 40 - /data 41 - -name 42 - etcd02 43 - -advertise-client-urls 44 - http://etcd02:2379 45 - -listen-client-urls 46 - http://0.0.0.0:2379 47 - -initial-advertise-peer-urls 48 - http://etcd02:2380 49 - -listen-peer-urls 50 - http://0.0.0.0:2380 51 - -initial-cluster-state 52 - new 53 - -initial-cluster-token 54 - docker-etcd 55 - -initial-cluster 56 - etcd01=http://etcd01:2380,etcd02=http://etcd02:2380,etcd03=http://etcd03:2380 57 etcd03: 58 image: etcd:3.3 59 ports: 60 - "10003:2379" 61 - "10013:2380" 62 #volumes: 63 #- "/root/workspace/docker/k8s/etcd/data33:/data" 64 command: 65 - /bin/etcd 66 - --data-dir 67 - /data 68 - -name 69 - etcd03 70 - -advertise-client-urls 71 - http://etcd03:2379 72 - -listen-client-urls 73 - http://0.0.0.0:2379 74 - -initial-advertise-peer-urls 75 - http://etcd03:2380 76 - -listen-peer-urls 77 - http://0.0.0.0:2380 78 - -initial-cluster-state 79 - new 80 - -initial-cluster-token 81 - docker-etcd 82 - -initial-cluster 83 - etcd01=http://etcd01:2380,etcd02=http://etcd02:2380,etcd03=http://etcd03:2380
測試,實際效果是,對3個節點的增刪改查操做都是等效的
curl http://127.0.0.1:10001/v2/keys/hello -XPUT -d value="hello world"
4. 小結
這幾天的練習與瞭解,算是對Docker及Docker Compose有了必定的瞭解,接下來,就以實際項目中須要用到的服務構建成一個個Docker Image並推送到阿里雲容器私有倉庫上。初期會構建的有STUN服務、Nginx-RTMP服務、EMQ-MQTT服務、Redis與MongoDB服務,暫時會先構建這幾個服務。服務間還涉及到認證與鑑權的,還會寫代碼,業務代碼應該仍是用Spring Cloud全家桶,以練促學,將來之路,任重而道遠呀!加油!