概要
前回の記事(概要編)の続きです。
この記事では実際にデプロイして動かす部分を説明します。
実際にデプロイを行うためs3及びmysqlサーバーを準備してください。
動作環境
Kubernetes及びetcdサーバーの分散が完了している状態(記事はこちら)を想定しています。
なお、mysqlサーバー上でtestユーザーとtestdbデータベースを作成しFileHashテーブルを作成してください。
SQL
CREATE TABLE `FileHash` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`HostName` text,
`FileName` varchar(256) DEFAULT NULL,
`HashMethod` char(10) DEFAULT NULL,
`Hash` text,
`Date` datetime DEFAULT NULL,
PRIMARY KEY (`ID`)
)
サンプルコード
サンプルコードはGithub上で公開しています。よろしければご覧ください。
https://github.com/mmitti/kube-filehash-sample
etcdサーバーにデータを登録
以下etcdのキーのプリフィックスを/testとし、ノード(192.168.137.21)上でetcdサーバーが動いているとします。
etcdctl --endpoints http://192.168.137.21:2379 mkdir test
etcdctl --endpoints http://192.168.137.21:2379 mkdir test/req
etcdctl --endpoints http://192.168.137.21:2379 mkdir test/wip
etcdctl --endpoints http://192.168.137.21:2379 mkdir test/done
etcdctl --endpoints http://192.168.137.21:2379 set test/conf/mysql_host mysqlのホスト名
etcdctl --endpoints http://192.168.137.21:2379 set test/conf/mysql_name dbの名
etcdctl --endpoints http://192.168.137.21:2379 set test/conf/mysql_pass mysqlのパスワード
etcdctl --endpoints http://192.168.137.21:2379 set test/conf/mysql_user mysqlのユーザー名
etcdctl --endpoints http://192.168.137.21:2379 set test/conf/s3_bucket s3のバケット名
etcdctl --endpoints http://192.168.137.21:2379 set test/conf/s3_key s3のアクセスキーID
etcdctl --endpoints http://192.168.137.21:2379 set test/conf/s3_secret s3のシークレットアクセスキー
Podの配置
以下のyamlを読み込ませれば配置できます。なおconfigmapのapiはmasterのIPアドレスを指定してください。
kind: ConfigMap
apiVersion: v1
metadata:
name: myfilehash-system-config
namespace: default
data:
conf-path: "/dat/conf.json" #init-podで生成するコンフィグファイル
dl-path: "/dat/files/" #s3からダウンロードしたファイルの一時置き場
s3-fin: "/dat/s3fin" #ダウンロード終了を示すファイル
hash-fin: "/dat/hashfin" #ダウンロード終了を示すファイル
api: "http://192.168.137.20:8080/" #kubernetesが動いているURL
namespace: "default" #kubernetesのネームスペース
etcd-port: "2379" #etcdを動かすポート番号
etcd-prefix: "/test" #etcdのキーのプリフィックス
---
kind: ReplicationController
apiVersion: v1
metadata:
name: myfilehash-system
labels:
app: myfilehash-system
spec:
replicas: 3
selector:
app: myfilehash-system
template:
metadata:
name: myfilehash-system
labels:
app: myfilehash-system
spec:
containers:
- name: init
image: mmitti/init-pod
volumeMounts:
- name: data
mountPath: /dat
env:
- name: API
valueFrom:
configMapKeyRef:
name: myfilehash-system-config
key: api
- name: NAMESPACE
valueFrom:
configMapKeyRef:
name: myfilehash-system-config
key: namespace
- name: ETCD_PORT
valueFrom:
configMapKeyRef:
name: myfilehash-system-config
key: etcd-port
- name: ETCD_PREFIX
valueFrom:
configMapKeyRef:
name: myfilehash-system-config
key: etcd-prefix
- name: CONF_PATH
valueFrom:
configMapKeyRef:
name: myfilehash-system-config
key: conf-path
- name: DL_PATH
valueFrom:
configMapKeyRef:
name: myfilehash-system-config
key: dl-path
- name: S3_FIN
valueFrom:
configMapKeyRef:
name: myfilehash-system-config
key: s3-fin
- name: HASH_FIN
valueFrom:
configMapKeyRef:
name: myfilehash-system-config
key: hash-fin
- name: file-hasher
image: mmitti/file-hasher2
volumeMounts:
- name: data
mountPath: /dat
env:
- name: DL_PATH
valueFrom:
configMapKeyRef:
name: myfilehash-system-config
key: dl-path
- name: CONF_PATH
valueFrom:
configMapKeyRef:
name: myfilehash-system-config
key: conf-path
- name: S3_FIN
valueFrom:
configMapKeyRef:
name: myfilehash-system-config
key: s3-fin
- name: HASH_FIN
valueFrom:
configMapKeyRef:
name: myfilehash-system-config
key: hash-fin
- name: s3-fetch-tool
image: mmitti/s3-fetch-tool2
volumeMounts:
- name: data
mountPath: /dat
env:
- name: DL_PATH
valueFrom:
configMapKeyRef:
name: myfilehash-system-config
key: dl-path
- name: CONF_PATH
valueFrom:
configMapKeyRef:
name: myfilehash-system-config
key: conf-path
- name: S3_FIN
valueFrom:
configMapKeyRef:
name: myfilehash-system-config
key: s3-fin
volumes:
- name: data
emptyDir: {}
---
続けてyamlファイルを読み込ませます。
kubectl create -f etcd-myfilehash_system.yaml
この時点で動作確認をします。
$ kubectl get po -l app=myfilehash-system
NAME READY STATUS RESTARTS AGE
myfilehash-system-grnzy 3/3 Running 0 26s
myfilehash-system-tzrfl 3/3 Running 0 26s
myfilehash-system-utanj 3/3 Running 0 26s
まだジョブを登録していないので処理は走りません
ジョブの登録
s3に01フォルダーが有りその中にファイル名が16進数を文字列化したものをアップロードしてあるとします。
自作の登録ツールを使うことでetcdにジョブを登録します。(etcdサーバーやプリフィックスなどは環境変数で指定してください。)
export ETCD_PORT=2379
export ETCD_PREFIX=/test
export ETCD_SRV=192.168.137.21
例えばMD5でs3上の01フォルダー内のファイルを20分割して処理する場合は
python3 add_job.py MD5 20 01/
で登録できます。
この後開いているPodが順に処理を始めます。
参考
各コンテナの詳細については詳細編を御覧ください。