目錄
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;
}