目录
Consul集群搭建
Docker的安装
配置docker参数
Docker SDK for Python
结果展示:
矩阵乘法mpi
Consul集群搭建
安装并配置consul
首先去官网现在合适的consul包:https://www.consul.io/downloads.html
unzip consul_1.10.0_linux_amd64.zip
sudo mv consul /usr/local/bin
consul
建议命令行安装:
curl -fsSL
https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64]
https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update && sudo apt-get install consul
#输入consul,出现上面的内容证明安装成功
sudo ufw status #查看防火墙状态
ufw enable|disable #开启或者关闭防火墙
consul agent -server -bootstrap -data-dir /opt/consul -bind= IPAdress
#主节点
consul agent -data-dir /opt/consul -bind= IPAdress
#从节点
consul members
#查看集群
Docker的安装
方式一:
Ubutun 18.04 LTS
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release # 更新apt包列表,并安装依赖
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
#添加官方GPG密钥
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
#设置稳定仓库
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
#更新apt包列表,并安装docker Engine-Community
sudo docker run hello-world
#验证安装是否成功
方式二:
-
sudo apt-get remove docker docker-engine docker-ce docker.io #删除原有docker
-
sudo apt-get update #命令更新系统
-
wget -qO- https://get.docker.com/ | sh #安装
-
docker run hello-world
配置docker参数
sudo vim /lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2376 -H unix:///var/run/docker.sock --insecure-registry=192.168.43.250:5000 --cluster-store=consul://127.0.0.1:8500 --cluster-advertise=ens33:2376
systemctl daemon-reload
service docker restart
#重启服务
sudo docker network create -d overlay multi_host
#创建overlay网络
docker network ls
#查看网络情况
sudo apt-get install openssh-server
sudo service sshd start
#安装ssh
sudo docker ps -a #查看docker容器
sudo docker image ls#查看docker容器所使用镜像
sudo docker load< /home/hadoop/下载/V2_image.tar #导入镜像
sudo docker tag c71536617a3c hjh:z1 #给docker镜像命名 打标签
Docker SDK for Python
from tkinter import constants
import docker
import tkinter
import tkinter.messagebox
import tkinter as tk
from tkinter import ttk
from docker import client
'''
def tcp_node():
tcp_url=['tcp://192.168.43.3:2376','tcp://192.168.43.250:2376','tcp://192.168.43.192:2376']
node_all=[]
for m in tcp_url:
node=[]
try:
client_node = docker.DockerClient(base_url=m)
except:
print("连接方未响应")
n=len(client_node.containers.list(all))
for h in range(n):
node.append(client_node.containers.list(all)[h].names)
node_all.append(node)
'''
def docker_info():
text.delete('1.0','end')
position = docker_place.get()
if position == "0":
url=tcp_url[0]
elif position == '1':
url=tcp_url[1]
client1 = docker.DockerClient(base_url=url)
c1=client1.containers
c1_nodename=[]
c1_state=[]
c1_compose=[]
for i in c1.list(all):
c=c1.get(str(i.name))
c1_nodename.append(i.name)
c1_state.append([c.attrs['Config']['Image'],i.status])
for i in zip(c1_nodename,c1_state):
c1_compose.append(list(i))
str_aaa=''
for i in c1_compose:
str_aaa+='虚拟机名称: '+str(i[0])+' 镜像: '+str(i[1][0])+' 状态: '+str(i[1][1])+'\n'
str_aaa+=' '
text.insert('insert',str_aaa)
def docker_build():
position = docker_place.get()
if position == "0":
url=tcp_url[0]
elif position == '1':
url=tcp_url[1]
client_all= docker.DockerClient(base_url=url)
client_all.containers.run(image='hjh:z1',name=entryName.get(),tty=True,privileged=True,stdin_open=True,detach=True,network_mode='multi_host')
client_all.containers.get(entryName.get()).exec_run('/etc/init.d/ssh start')
tk.messagebox.showwarning("提示", '虚拟机创建成功')
def delete_docker1():
position = docker_place.get()
if position == "0":
url=tcp_url[0]
elif position == '1':
url=tcp_url[1]
client_all= docker.DockerClient(base_url=url)
container=client_all.containers.get(client_all.containers.get(entryName.get()).short_id)
container.remove(force=True)
tk.messagebox.showwarning("提示", '虚拟机删除成功')
def open_docker():
position = docker_place.get()
if position == "0":
url=tcp_url[0]
elif position == '1':
url=tcp_url[1]
client_all= docker.DockerClient(base_url=url)
container=client_all.containers.get(client_all.containers.get(entryName.get()).short_id)
container.start()
container.exec_run('/etc/init.d/ssh start')
tk.messagebox.showwarning("提示", '虚拟机成功开启')
#open_docker()
def close_docker():
position = docker_place.get()
if position == "0":
url=tcp_url[0]
elif position == '1':
url=tcp_url[1]
client_all= docker.DockerClient(base_url=url)
container=client_all.containers.get(client_all.containers.get(entryName.get()).short_id)
container.stop()
tk.messagebox.showwarning("提示", '虚拟机成功关闭')
def run_mpi():
position = docker_place.get()
if position == "0":
url=tcp_url[0]
elif position == '1':
url=tcp_url[1]
client1= docker.DockerClient(base_url='tcp://192.168.43.3:2376')
for i in client1.containers.list():
container1=client1.containers.get(entryName.get())
container1.exec_run('cd /home/mpich-3.2/examples')
container1.exec_run('mpiexec -n 8 -f /home/hostfile ./cpi')
tk.messagebox.showwarning("提示", 'MPI成功运行')
if __name__ == '__main__':
tcp_url=['tcp://192.168.43.3:2376','tcp://192.168.43.41:2376']
top=tk.Tk()
top.title("云计算课程设计")
entryName=tk.Entry(top,width=24)
entryName.grid(row=1,column=1,padx=2,sticky='W')
#top.geometry('602x')#窗体大小
lableTitle = tk.Label(top,bg='pink',fg="blue",font = 'Helvetica -24 bold',text="云 计 算 管 理 平 台",width=50)
lableTitle.grid(row=0,column=0,columnspan=2,ipady=10)
#-------------------------------
labelHello = tk.Label(top,fg='#c71585',font = 'Helvetica -18 bold', text = "请输入虚拟机的名称:",)
labelHello.grid(row=1,column=0,padx=0,pady=5,sticky='E')
lableTitle = tk.Label(top,bg='pink',fg="blue",font = 'Helvetica -24 bold',text="云 计 算 管 理 平 台",width=50)
lableTitle.grid(row=0,column=0,columnspan=2,ipady=10)
number2 = tk.StringVar()
docker_place = ttk.Combobox(top, width=12, textvariable=number2)
docker_place['values'] = ('0', '1')
docker_place.grid(row=1,column=1,padx=2,sticky='E')
docker_place.current(0)
position = docker_place.get()
if position == "0":
url=tcp_url[0]
elif position == '1':
url=tcp_url[1]
#-------操作------
create1 =tk.Button(top,bg='#00BFFF',fg='white',width=18,height=2,font = 'Helvetica -15 bold', text="显示虚拟机信息",command=docker_info)
create1.grid(row=2,column=1,padx=4,pady=4)
xianshi =tk.Button(top,bg='#00BFFF',fg='white',width=18,height=2,font = 'Helvetica -15 bold', text="创建虚拟机",command=docker_build)
xianshi.grid(row=2,column=0,padx=2,pady=4)
delete1 =tk.Button(top,bg='#00BFFF',fg='white',width=18,height=2,font = 'Helvetica -15 bold',text="删除虚拟机",command=delete_docker1)
delete1.grid(row=3,column=0,padx=2,pady=4)
open1= tk.Button(top,bg='#00BFFF',fg='white',width=18,height=2,font = 'Helvetica -15 bold', text="打开虚拟机",command=open_docker)
open1.grid(row=4,column=0,padx=2,pady=4)
close1 =tk.Button(top,bg='#00BFFF',fg='white',width=18,height=2,font = 'Helvetica -15 bold',text="关闭虚拟机",command=close_docker)
close1.grid(row=4,column=1,padx=4,pady=4,sticky='N')
mpi =tk.Button(top,bg='#00BFFF',fg='white',width=3,height=0,font = 'Helvetica -15 bold',text="MPI",command=run_mpi)
mpi.grid(row=4,column=1,padx=4,pady=4,sticky='E')
text = tk.Text(top,width = 50,height = 20)
text.grid(row=3,column=1,padx=4,pady=4)
top.mainloop()
结果展示:
在三台物理机进行并行测试
矩阵乘法mpi
#include <stdio.h>
#include "mpi.h"
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
//生成随机矩阵
int **generate_matrix(int size)
{
int num = 0,m;
int **matrix;
matrix = (int **)malloc(sizeof(int *) * size);
for(m = 0; m < size; m++)
matrix[m] = (int *)malloc(sizeof(int) * size);
int i,j;
srand(time(NULL) + rand());
for(i = 0; i < size; i++)
{
for(j = 0; j < size; j++)
{
matrix[i][j]= rand() % 20;
}
}
return matrix;
}
//输出矩阵
void print_matrx(int **a,int size)
{
int i,j;
for(i = 0; i < size; i++)
{
for(j = 0; j < size; j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\n");
}
//矩阵相乘
int * Multiplication(int **a,int b[],int size)
{
int *result;
result = (int *)malloc(sizeof(int) * size);
int i,m,n,sum = 0;
for(m = 0; m < size; m++)
{
for(n = 0; n < size; n++)
{
sum += a[n][m] * b[n];
}
result[m] = sum;
sum = 0;
}
return result;
}
int main(int argc,char **argv)
{
int size,rank,dest;
MPI_Comm comm = MPI_COMM_WORLD;
MPI_Status status;
MPI_Init(&argc,&argv);
MPI_Comm_size(comm,&size);
MPI_Comm_rank(comm,&rank);
int **matrix1;
int **matrix2;
int send_buff[size*size];
matrix1 = generate_matrix(size);
matrix2 = generate_matrix(size);
if(rank == 0)
{
printf("matrix1 is :\n");
print_matrx((int **)matrix1,size);
printf("matrix2 is :\n");
print_matrx((int **)matrix2,size);
int j,k,tmp = 0;
for(j = 0; j < size; j++)
for(k = 0; k < size; k++)
{
send_buff[tmp] = matrix1[j][k];
tmp++;
}
}
int rbuf[size];
int final_buff[size];
int *result;
result = (int *)malloc(sizeof(int) * size);
//Multiplication((int**)matrix,)
//分发列
MPI_Scatter(send_buff,size,MPI_INT,rbuf,size,MPI_INT,0,comm);
result = Multiplication((int **)matrix2,rbuf,size);
MPI_Barrier(comm);//等待所有进程计算结束
int *recv_buff;
if(rank == 0)
recv_buff = (int*)malloc(sizeof(int)*size*size);
MPI_Barrier(comm);
MPI_Gather(result,size,MPI_INT,recv_buff,size,MPI_INT,0,comm);//收集各列数据
//根进程进行输出
if(rank == 0)
{
printf("\nresult is :\n");
int m,n,tmp = 0;
for(m = 0; m < size; m++)
{
for(n = 0;n < size;n++)
{
printf("%d ",recv_buff[tmp]);
tmp++;
}
printf("\n");
}
printf("\n");
}
MPI_Finalize();
return 0;
}