微服务集群环境搭建步骤(Ubuntu+Eureka+Docker swarm compose dockerspace + Maven)

@[TOC](微服务集群环境搭建步骤(Ubuntu+Eureka+Docker swarm compose dockerspace + Maven))

1.更换阿里源镜像

sudo nano /etc/apt/sources.list

#deb cdrom:[Ubuntu 16.04 LTS Xenial Xerus - Release amd64 (20160420.1)]/ xenial main restricted
deb-src http://archive.ubuntu.com/ubuntu xenial main restricted #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse #Added by software-properties
deb http://archive.canonical.com/ubuntu xenial partner
deb-src http://archive.canonical.com/ubuntu xenial partner
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse

2.docker 国内镜像安装

http://get.daocloud.io/

curl -sSL https://get.daocloud.io/docker | sh

3.建立专门的工作组赋予sudo执行权限

docker进程使用Unix Socket而不是TCP端口。而默认情况下,Unix socket属于root用户,需要root权限才能访问。

sudo groupadd docker     #添加docker用户组
sudo gpasswd -a $USER docker     #将登陆用户加入到docker用户组中
newgrp docker     #更新用户组
docker ps    #测试docker命令是否可以使用sudo正常使用

4.docker-compose

在PC机上ubuntu系统安装docker-compose遇见的故障解决方案
原来的python有问题,卸载重新安装

sudo apt-get remove python-pip python-pip python
sudo apt-get install python-pip python-pip python

检查pip正常工作

pip list

下载安装
代码用到版本是v3根据对应表 是1.21.2 https://segmentfault.com/a/1190000020340570

sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose

执行安装命令成功安装

sudo chmod +x /usr/local/bin/docker-compose

查看版本

docker-compose -v

5.本地仓库

生成验证证书

mkdir registry && cd registry && mkdir certs && cd certs
openssl req -x509 -days 3650 -subj '/CN=10.168.2.171:5000/' -nodes -newkey rsa:2048 -keyout domain.key -out domain.crt

生成用户及密码

cd .. && mkdir auth
docker run --entrypoint htpasswd registry:2 -Bbn tangyihao w5> auth/htpasswd

(删除原先操作创建的本地仓库)
【重新创建本地仓库解决办法】

  1. 先查看所有的容器
    docker ps -a
  2. 发现有之前的容器,使用docker rm 容器名称 ,删除。即可重新创建本地仓库

搭建带安全验证的本地私有仓库

docker run -d \
-p 5000:5000 \
--restart=always \
--name registry \
-v /mnt/registry:/var/lib/registry \
-v `pwd`/auth:/auth \
-e "REGISTRY_AUTH=htpasswd" \
-e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \
-e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd \
-v `pwd`/certs:/certs \
-e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt \
-e REGISTRY_HTTP_TLS_KEY=/certs/domain.key \
registry:2

本地仓库地址

cd /mnt/registry/docker/registry/v2/repositories/

启动本地仓库服务

sudo mkdir -p /etc/docker/certs.d 10.168.2.171:5000

registry目录下

sudo cp certs/domain.crt /etc/docker/certs.d/10.168.2.171:5000

在集群每个节点修改daemon.json
注意:docker swarm集群的客户端编写测试文件daemon.json文件.不然无法登录到本地仓库
sudo nano /etc/docker/daemon.json
内容如下:

{"insecure-registries":["10.168.2.171:5000"]}

!!! 重启docker进程
systemctl restart docker.service 无效
或者是:sudo /etc/init.d/docker restart 有效
密码: w5

6.安装jdk

sudo apt-get purge openjdk-8-jre-headless
sudo apt-get install openjdk-8-jre-headless
sudo apt-get install openjdk-8-jre
sudo apt-get install openjdk-8-jdk

验证

pi@sw1:~ $ java -version
openjdk version "11.0.5" 2019-10-15
OpenJDK Runtime Environment (build 11.0.5+10-post-Raspbian-1deb10u1)
OpenJDK Server VM (build 11.0.5+10-post-Raspbian-1deb10u1, mixed mode)

7.安装maven

sudo apt install maven

验证

pi@sw1:~ $ mvn -v
Apache Maven 3.6.0
Maven home: /usr/share/maven
Java version: 11.0.5, vendor: Raspbian, runtime: /usr/lib/jvm/java-11-openjdk-armhf
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "4.19.75-v7+", arch: "arm", family: "unix"

如何查看本地maven仓库地址

mvn help:effective-settings

通过输出可以看到

如果你没有做任何特殊配置的话,
从Maven中心仓库下载到本地的jar包的默认存放在”${user.home}/.m2/repository”中,

apt 安装maven的主目录在
/etc/maven

为了以后快速下载我们还需要
修改maven国内镜像源
在~/.m2/settings.xml
内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <mirrors>
        <!-- 阿里云仓库 -->
        <mirror>
            <id>alimaven</id>
            <mirrorOf>central</mirrorOf>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
        </mirror>
    
        <!-- 中央仓库1 -->
        <mirror>
            <id>repo1</id>
            <mirrorOf>central</mirrorOf>
            <name>Human Readable Name for this Mirror.</name>
            <url>http://repo1.maven.org/maven2/</url>
        </mirror>
    
        <!-- 中央仓库2 -->
        <mirror>
            <id>repo2</id>
            <mirrorOf>central</mirrorOf>
            <name>Human Readable Name for this Mirror.</name>
            <url>http://repo2.maven.org/maven2/</url>
        </mirror>

<mirror>
            <id>nexus-aliyun</id>
            <mirrorOf>*</mirrorOf>
            <name>Nexus aliyun</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    	</mirror>
    </mirrors> 
</settings>

运行出错 libjffi-1.2.so 参考解决方案
https://blog.csdn.net/sinat_32676193/article/details/105182642

运行mvn有警告没有配置环境变量,这里设置 JAVA_HOME

tangyihao@sw1:~/.m2$ mvn -v
Warning: JAVA_HOME environment variable is not set.

tangyihao@sw1:~/.m2$ which java
/usr/bin/java
tangyihao@sw1:~/.m2$ ls -lrt /usr/bin/java
lrwxrwxrwx 1 root root 22 4月 26 22:57 /usr/bin/java -> /etc/alternatives/java
tangyihao@sw1:~/.m2$ ls -lrt /etc/alternatives/java
lrwxrwxrwx 1 root root 46 4月 26 22:57 /etc/alternatives/java -> /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
修改vim ~/.bashrc 添加以下:

export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export JRE_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$JAVA_HOME:$PATH

8.项目编译

程序里有自动上传镜像,所以编译之前要修改/etc/maven/settings.xml
配置ip和密码在services中

       <server>
        <id>10.168.3.157:5000</id>
        <username>tangyihao</username>
        <password>w5</password>
       </server>
  </servers>

将写好的Eureka项目上传到宿主机
tangyihao@sw1:~/microservice-mallmanagement$
执行编译

 mvn install

成功编译后输出

[INFO] Image c65fb725975c: Pushed
[INFO] 0.0.1-SNAPSHOT: digest: sha256:08baa8963520cdfcec5d8e2ecf3544de2bcd3f33007158413a5ffda7aec7aa7e size: 1999
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for microservice-mallmanagement 0.0.1-SNAPSHOT:
[INFO] 
[INFO] microservice-mallmanagement ........................ SUCCESS [ 11.390 s]
[INFO] microservice-eureka-server ......................... SUCCESS [02:09 min]
[INFO] microservice-gateway-zuul .......................... SUCCESS [01:06 min]
[INFO] microservice-orderservice .......................... SUCCESS [01:47 min]
[INFO] microservice-userservice ........................... SUCCESS [01:19 min]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

查看镜像 docker images

tangyihao@sw1:~$ docker images
REPOSITORY                                     TAG                 IMAGE ID            CREATED             SIZE
10.168.2.171:5000/microservice-userservice     0.0.1-SNAPSHOT      514421a8258c        About an hour ago   354MB
10.168.2.171:5000/microservice-orderservice    0.0.1-SNAPSHOT      27573eefc73c        About an hour ago   355MB
10.168.2.171:5000/microservice-gateway-zuul    0.0.1-SNAPSHOT      3e239ef1adaa        About an hour ago   350MB
10.168.2.171:5000/microservice-eureka-server   0.0.1-SNAPSHOT      5839a3f8568a        About an hour ago   351MB
mysql                                          <none>              afa411733b0c        4 hours ago         302MB
registry                                       2                   708bc6af7e5e        3 months ago        25.8MB
dockersamples/visualizer                       <none>              8dbf7c60cf88        2 years ago         148MB
java                                           8-jre               e44d62cf8862        3 years ago         311MB

9.非集群部署 (可选)

docker-compose up
docker-compose down

需要检查docker-compose的版本,教材使用v3版本

10.分布式 swarm部署

检查网络

docker network ls

注意一定要有网络 overlay

我的网络是所以特殊些

docker  network  create  -d  overlay  --subnet  10.168.0.0/24  microservice_net

分布式 swarm部署

docker stack deploy -c docker-compose-swarm.yml --with-registry-auth mallmanagement

11.部分查看命令

docker stack ps mallmanagement
查看具体服务状况
docker service logs -f mallmanagement_order-service
不截断输出 --no-trunc 参考 https://www.php.cn/manual/view/36121.html
docker stack ps --format “{{.Error}}” --no-trunc mallmanagement
检测集群服务启动情况
watch ‘docker service ls’
在这里插入图片描述

“测试示例集群部署”
docker service create --replicas 1 --name helloworld alpine ping docker.com
修改副本数量
docker service scale helloworld=5
自定义网络
docker network create --driver overlay my-multi-host-network
进入容器查看详情
docker service inspect jb9yh1qicsmc

http://10.168.2.171:8761/
http://10.168.2.171:8081/
http://10.168.2.171:8030/swagger-ui.html
http://10.168.2.171:7900/swagger-ui.html
微服务集群环境搭建步骤(Ubuntu+Eureka+Docker swarm compose dockerspace + Maven)_第1张图片

你可能感兴趣的:(微服务)