---
# System prepended metadata

title: 雲端通訊整合實務(10/27)
tags: [docker]

---

# 雲端通訊整合實務(10/27)
###### tags: `docker`


## 將 iris 部署在 docker
在iris的資料夾下，分別建立Dockerfile：
```
FROM nitincypher/docker-ubuntu-python-pip

COPY ./requirements.txt /app/requirements.txt

WORKDIR /app

RUN pip install -r requirements.txt

COPY server.py /app

COPY train_model.py /app

CMD python /app/train_model.py && python /app/server.py
```
以及requirements.txt：
```
sklearn
flask
```
編輯完後，執行：
```
docker build -t iris:1.0 .
```
> docker build -t 自定義鏡像名稱:版本
> .(當前路徑下)

* 一般來說，下載docker images可以使用兩種方式：

1. 一種是利用Dockerfile來建立images的腳本，並且使用：
`docker build -t images:version`
3. 一種是直接在dockerhub上進行`docker pull images`的動作

啟動iris鏡像：
```
docker run -itd --name iris -p 5000:5000 iris:1.0
```

結果如下：
```
root@vm2:~/iris# docker run -itd --name iris -p 5000:5000 iris:1.0
e1092a4d4e913a4d49c466b04759befaaf3bf1dd706e6b29e3f7571a763e61b8
root@vm2:~/iris# docker ps -a
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS                    NAMES
e1092a4d4e91        iris:1.0               "/bin/sh -c 'python …"   3 seconds ago       Up 2 seconds        0.0.0.0:5000->5000/tcp   iris
df07f926fe4d        635b36f4d89f           "/usr/local/bin/kube…"   11 seconds ago      Up 10 seconds                                k8s_kube-proxy_kube-proxy-h82nz_kube-system_6e5db04a-3257-4279-bb52-b99bd8a2e9a5_0
b3069b0e2612        k8s.gcr.io/pause:3.2   "/pause"                 11 seconds ago      Up 11 seconds                                k8s_POD_kube-proxy-h82nz_kube-system_6e5db04a-3257-4279-bb52-b99bd8a2e9a5_0
```
基本上這樣就完成了部署在docker的動作了，接著你就可以用`python client.py`測試：

![](https://i.imgur.com/ILNXNSq.png)

## 利用gitlab進行自動化部屬

首先，在gitlab的官網上註冊一組帳號：
```
https://gitlab.com/users/sign_in?__cf_chl_jschl_tk__=32cc64420b45aa45ee47d5f3f971c3dab2725987-1606998192-0-AcqZY6w6ZENMV8ZedyRISQD0pgZLrUtHa4OdJfVe-AA6NQVg3pEYIhX6KAwtbF0IuzhdY20p9-Ya-Um_gVTSIWTetpO1wv4WU_52fb7dcroU1HiSPKSl-EO0_ci7blPGexWnrzCgMG4tmgm5wFuSwWK8K8WUs4MkYqMRyV76H-91Kp6Mkz97yKzR4ouVEyP1B7f4vN02It64LT5s8_I0aCkbzLinr8DPihjEFTa6HWU6hI4R4h0Vh-NFV-MASGgu_vQ0CEdB1fGbwHbDD-OjGByldKL72oMNFxqVUYV26vowhzlYFAfO5WUxDm508A0y_E2I3INuQhRanRgDzif-Vc3Ah0iT9e7A2LToaTvgIFndPVF6wKA5_VpepAqlCEnKjA
```

在gitlab上建立ssh keys：
![](https://i.imgur.com/roDAyMk.png)

在vm2上，我們可以直接在`.ssh`下查看：
```
root@vm2:~# cd .ssh/
root@vm2:~/.ssh# ls
id_rsa  id_rsa.pub  known_hosts
root@vm2:~/.ssh# pwd
/root/.ssh
root@vm2:~/.ssh# 
```
```
root@vm2:~/.ssh# cat id_rsa.pub
...
ssh-rsa AAAAB3NzaC1yc
...
```
或是直接創建一個新的：
```
root@vm2:~# cd .ssh/
root@vm2:~/.ssh# ls
id_rsa  id_rsa.pub  known_hosts
root@vm2:~/.ssh# rm -rf *
root@vm2:~/.ssh# cd ..
root@vm2:~# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:KRuvEIVrTkU4wWUOpNUxAz0guvz9bGXOOaEV+RpiaBI root@vm2
The key's randomart image is:
+---[RSA 2048]----+
|  .oBOB.         |
| . ++*+o         |
|. . ..+.  .      |
|.. E +   +       |
|..  * + S o      |
|  .=.+ B * .     |
|   .=.o X =      |
|     .o+ *       |
|      oo  .      |
+----[SHA256]-----+
```

> 金鑰的部分盡量是自己保管好，以防造成使用者資料外洩。

將ssh key丟到gitlab的網頁上，結果可以看到如下：
1. vm1:
 
![](https://i.imgur.com/PS4e9wt.png)
2. vm2:

![](https://i.imgur.com/vZrqZ5D.png)

在gitlab創建一個project：

![](https://i.imgur.com/NPPoKRq.png)
當你創建完成後，會看到以下畫面：

![](https://i.imgur.com/Srk8Yj6.png)

在vm2上，我們進行一些gitlab設置以及將iris的檔案push到gitlab上：
```
git config --global user.name "110510549"
git config --global user.email "cf337188@gmail.com"

git init
git remote add origin git@gitlab.com:110510549/mytest.git
git add .
git commit -m "Initial commit"
git push -u origin master
```

結果可以看到：
```
root@vm2:~# cd iris/
root@vm2:~/iris# ls
client.py  Dockerfile  model.pkl  requirements.txt  server.py  train_model.py
root@vm2:~/iris# git config --global user.name "110510549"
root@vm2:~/iris# git config --global user.email "cf337188@gmail.com"
root@vm2:~/iris# 
root@vm2:~/iris# git init
Reinitialized existing Git repository in /root/iris/.git/
root@vm2:~/iris# git remote add origin git@gitlab.com:110510549/mytest.git
fatal: remote origin already exists.
root@vm2:~/iris# git add .
root@vm2:~/iris# git commit -m "Initial commit"
[master 02520d6] Initial commit
 1 file changed, 244 insertions(+), 244 deletions(-)
 rewrite model.pkl (66%)
root@vm2:~/iris# git push -u origin master
The authenticity of host 'gitlab.com (172.65.251.78)' can't be established.
ECDSA key fingerprint is SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitlab.com,172.65.251.78' (ECDSA) to the list of known hosts.
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 740 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To git@gitlab.com:110510549/myiris.git
   0d6af5b..02520d6  master -> master
Branch master set up to track remote branch master from origin.
root@vm2:~/iris# 
```
將檔案push上去後可以到創建的project上查看：

![](https://i.imgur.com/nCL9Z8k.png)

接著vm1上，安裝gitlab-runner：
```
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
```
開啟gitlab-runner權限：
```
sudo chmod +x /usr/local/bin/gitlab-runner
```
創建一個gitlab CI user：
```
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
```
新增一個user名叫gitlab-runner，並且讓它可以運行docker：
```
usermod -aG docker gitlab-runner
```
此時可以做一個小測試，開啟一個新的終端機，並且分別測試`su - user`以及`su - gitlab-runner`，其結果如下：
```
root@vm1:~/iris# su - user
user@vm1:~$ docker ps -a
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/json?all=1: dial unix /var/run/docker.sock: connect: permission denied
user@vm1:~$ exit
logout
root@vm1:~/iris# su - gitlab-runner
gitlab-runner@vm1:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
b8f4ca51448e        iris                "/bin/sh -c 'python …"   13 minutes ago      Up 13 minutes       0.0.0.0:5000->5000/tcp   iris
becebb97850a        httpd               "httpd-foreground"       5 hours ago         Up 5 hours          0.0.0.0:8080->80/tcp     laughing_tereshkova
gitlab-runner@vm1:~$ 
```
接著就可以進行gitlab-runner註冊：
```
root@vm1:~/iris# gitlab-runner register
Runtime platform                                    arch=amd64 os=linux pid=19817 revision=ece86343 version=13.5.0
Running in system-mode.                            
                                                   
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
https://gitlab.com/
Please enter the gitlab-ci token for this runner:
suDx1sXf9FX5uu1h4AQs
Please enter the gitlab-ci description for this runner:
[vm1]: mininet-wifi2020
Please enter the gitlab-ci tags for this runner (comma separated):
mininet-wifi2020
Registering runner... succeeded                     runner=suDx1sXf
Please enter the executor: docker-ssh, ssh, virtualbox, docker-ssh+machine, custom, docker, parallels, shell, docker+machine, kubernetes:
shell
```
註冊完後，結果可以從**settings → CI/CD → runner**中看到：
![](https://i.imgur.com/mhPJKzx.png)

**其中在 vm2 中，需要更改`.gitlab-ci.yml`的 tags，例如：`mininet-wifi2020`**

![](https://i.imgur.com/F4zuTCX.png)

確定runner啟動後，可以到CI/CD中查看jobs是否正常啟動：
![](https://i.imgur.com/LzA4fMz.png)

當出現**Job succeeded**，代表你已經成功自動化部屬到vm1上了：
![](https://i.imgur.com/GDkMIfO.png)

你可以到vm1上查看，並且進行iris測試：
![](https://i.imgur.com/VzZPDYJ.png)


## Reference
[上課影片](https://drive.google.com/drive/folders/1oM_ejAeSIhGbDGAVBDnVRv4DuoXaDwYw?usp=sharing)