helm リベンジ
リベンジの前に
ここを読んだ。
準備
前回の失敗は persistent volume を確保していないことが原因だった。
ここ を見ると stable/mysql
の persistence.size:8Gi RAW
とあるので8G用意しておく。storageclass は指定しない。
my-pv.yaml
apiVersion: v1 kind: PersistentVolume metadata: name: my-pv labels: type: local spec: capacity: storage: 8Gi accessModes: - ReadWriteOnce hostPath: path: "/mnt/data"
apply
$ kubectl apply -f my-pv.yaml persistentvolume/my-pv created $ kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE my-pv 8Gi RWO Retain Available 9s
helm install
helm install
$ helm install stable/mysql $helm install stable/mysql NAME: messy-waterbuffalo LAST DEPLOYED: Thu Aug 15 19:49:26 2019 NAMESPACE: default STATUS: DEPLOYED RESOURCES: ==> v1/ConfigMap NAME DATA AGE messy-waterbuffalo-mysql-test 1 0s ==> v1/PersistentVolumeClaim NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE messy-waterbuffalo-mysql Bound my-pv 8Gi RWO 0s ==> v1/Pod(related) NAME READY STATUS RESTARTS AGE messy-waterbuffalo-mysql-6c85b99b48-zldhx 0/1 Init:0/1 0 0s ==> v1/Secret NAME TYPE DATA AGE messy-waterbuffalo-mysql Opaque 2 0s ==> v1/Service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE messy-waterbuffalo-mysql ClusterIP 10.97.225.217 <none> 3306/TCP 0s ==> v1beta1/Deployment NAME READY UP-TO-DATE AVAILABLE AGE messy-waterbuffalo-mysql 0/1 1 0 0s NOTES: MySQL can be accessed via port 3306 on the following DNS name from within your cluster: messy-waterbuffalo-mysql.default.svc.cluster.local To get your root password run: MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default messy-waterbuffalo-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo) To connect to your database: 1. Run an Ubuntu pod that you can use as a client: kubectl run -i --tty ubuntu --image=ubuntu:16.04 --restart=Never -- bash -il 2. Install the mysql client: $ apt-get update && apt-get install mysql-client -y 3. Connect using the mysql cli, then provide your password: $ mysql -h messy-waterbuffalo-mysql -p To connect to your database directly from outside the K8s cluster: MYSQL_HOST=127.0.0.1 MYSQL_PORT=3306 # Execute the following command to route the connection: kubectl port-forward svc/messy-waterbuffalo-mysql 3306 mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}
kubectl で確認
$ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 15d messy-waterbuffalo-mysql ClusterIP 10.97.225.217 <none> 3306/TCP 4m15s $ kubectl get deploy NAME READY UP-TO-DATE AVAILABLE AGE messy-waterbuffalo-mysql 1/1 1 1 4m23s $ kubectl get po NAME READY STATUS RESTARTS AGE messy-waterbuffalo-mysql-6c85b99b48-zldhx 1/1 Running 0 4m27s
node22 の /mnt/data
にそれっぽいのができている。
$ ls -al /mnt/data total 188496 drwxr-xr-x. 5 999 render 4096 Aug 15 19:49 . drwxr-xr-x. 3 root root 4096 Aug 15 19:49 .. -rw-r-----. 1 999 render 56 Aug 15 19:49 auto.cnf -rw-r-----. 1 999 render 1329 Aug 15 19:49 ib_buffer_pool -rw-r-----. 1 999 render 50331648 Aug 15 19:49 ib_logfile0 -rw-r-----. 1 999 render 50331648 Aug 15 19:49 ib_logfile1 -rw-r-----. 1 999 render 79691776 Aug 15 19:49 ibdata1 -rw-r-----. 1 999 render 12582912 Aug 15 19:49 ibtmp1 drwxr-x---. 2 999 render 4096 Aug 15 19:49 mysql drwxr-x---. 2 999 render 4096 Aug 15 19:49 performance_schema drwxr-x---. 2 999 render 12288 Aug 15 19:49 sys
helm status を確認。mysql-client のインストール方法まで載っている。kubectl port-forward
そんなのが!
$ helm status messy-waterbuffalo LAST DEPLOYED: Thu Aug 15 19:49:26 2019 NAMESPACE: default STATUS: DEPLOYED RESOURCES: ==> v1/ConfigMap NAME DATA AGE messy-waterbuffalo-mysql-test 1 7m13s ==> v1/PersistentVolumeClaim NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE messy-waterbuffalo-mysql Bound my-pv 8Gi RWO 7m13s ==> v1/Pod(related) NAME READY STATUS RESTARTS AGE messy-waterbuffalo-mysql-6c85b99b48-zldhx 1/1 Running 0 7m13s ==> v1/Secret NAME TYPE DATA AGE messy-waterbuffalo-mysql Opaque 2 7m13s ==> v1/Service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE messy-waterbuffalo-mysql ClusterIP 10.97.225.217 <none> 3306/TCP 7m13s ==> v1beta1/Deployment NAME READY UP-TO-DATE AVAILABLE AGE messy-waterbuffalo-mysql 1/1 1 1 7m13s NOTES: MySQL can be accessed via port 3306 on the following DNS name from within your cluster: messy-waterbuffalo-mysql.default.svc.cluster.local To get your root password run: MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default messy-waterbuffalo-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo) To connect to your database: 1. Run an Ubuntu pod that you can use as a client: kubectl run -i --tty ubuntu --image=ubuntu:16.04 --restart=Never -- bash -il 2. Install the mysql client: $ apt-get update && apt-get install mysql-client -y 3. Connect using the mysql cli, then provide your password: $ mysql -h messy-waterbuffalo-mysql -p To connect to your database directly from outside the K8s cluster: MYSQL_HOST=127.0.0.1 MYSQL_PORT=3306 # Execute the following command to route the connection: kubectl port-forward svc/messy-waterbuffalo-mysql 3306 mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}
pod に入ってmysqlを確認
mysql が動いていました。
$ kubectl get po NAME READY STATUS RESTARTS AGE messy-waterbuffalo-mysql-6c85b99b48-zldhx 1/1 Running 0 41m $ kubectl exec -it messy-waterbuffalo-mysql-6c85b99b48-zldhx sh # mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 499 Server version: 5.7.14 MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) mysql> use mysql Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select user, host from user order by user, host; +------+------+ | user | host | +------+------+ | root | % | +------+------+ 1 row in set (0.00 sec)
k8sクラスタ外から
サービス確認
$ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 16d messy-waterbuffalo-mysql ClusterIP 10.97.225.217 <none> 3306/TCP 38m
nodeport 作ってみる
apiVersion: v1 kind: Service metadata: name: np spec: selector: app: mysql type: NodePort ports: - port: 3306 nodePort: 33306
$ kubectl apply -f np.yaml service/np created $ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 16d messy-waterbuffalo-mysql ClusterIP 10.97.225.217 <none> 3306/TCP 53m np NodePort 10.104.38.32 <none> 3306:32306/TCP 8s
クラスタ外からは接続に失敗。service復習しないと。。。
$ mysql -h 192.168.0.21 --port=32306 -u root -p ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.0.21' (111)
お掃除
$ kubectl delete -f np.yaml service "np" deleted $ helm ls NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE messy-waterbuffalo 1 Thu Aug 15 19:49:26 2019 DEPLOYED mysql-1.3.0 5.7.14 default $ helm delete --purge messy-waterbuffalo release "messy-waterbuffalo" deleted $ ls my-pv.yaml np.yaml $ kubectl delete -f my-pv.yaml persistentvolume "my-pv" deleted
とりあえず
とりあえず helm install
できた。install したものは helm delete
でアンインストールできることも分かった。
最初runningまでいかなかったが、それも一発で消せた。
このくらいならまだ kubectl apply -f
kubectl delete -f
できそうだが、helm は複雑なサービスを構成できそうなのはわかった。