WordPress 從 GKE 搬家到 DigitalOcean

在一開始想說要研究 K8S 又剛好要寫個 Blog,
因此把機器放在 GKE 所以就用了官方提供的範例腳本把 WordPress 架設完成,
但是因為 GKE 需要用到負載平衡這部分一個月大概需要 10 美金到 20 美金,
不過我這邊沒有這麼大的流量就想說省點錢,
就開始搬家計畫拉!

首先要搬家就要先備份資料,
因為對於 K8S 的不熟,
一開始還直接到機器把掛載的資料夾拉出來 XD,
想想真的滿蠢的 …
在這邊教大家用比較簡單的方法備份機器

  1. 首先先使用指令 kubectl get all 找到你的 Service,這邊範例是 wordpress-0000000000-AAAAA

    clarence$ kubectl get all
    NAME READY STATUS RESTARTS AGE
    pod/mysql-0000000000-AAAAA 1/1 Running 0 6d
    pod/wordpress-0000000000-AAAAA 1/1 Running 0 6d
    NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    service/kubernetes ClusterIP 10.0.0.0 <none> 443/TCP 1y
    service/mysql ClusterIP 10.0.0.0 <none> 3306/TCP 1y
    service/wordpress LoadBalancer 10.0.0.0 35.197.0.0 80:31737/TCP 1y
    NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
    deployment.apps/mysql 1 1 1 1 1y
    deployment.apps/wordpress 1 1 1 1 1y
    NAME DESIRED CURRENT READY AGE
    replicaset.apps/mysql-0000000000 1 1 1 1y
    replicaset.apps/wordpress-0000000000 1 1 1 1y
    view raw bash-1.sh hosted with ❤ by GitHub

  2. WordPress 備份資料到本地

    kubectl cp wordpress-0000000000-AAAAA:/var/www/html/ Downloads/var/www/html/
    view raw bash-2.sh hosted with ❤ by GitHub

  3. 把 mysql 做一個反向連線到本地

    kubectl port-forward service/mysql 3306:3306
    view raw bash-3.sh hosted with ❤ by GitHub


    PS: 版主在這邊遇到一個 mysqldump throws: Unknown table ‘COLUMN_STATISTICS’ in information_schema (1109) 的問題,詳細可以看一下 https://serverfault.com/questions/912162/mysqldump-throws-unknown-table-column-statistics-in-information-schema-1109
  4. 使用 mysqldump 把資料庫整個備份到本地

    mysqldump --column-statistics=0 -h 127.0.0.1 -uroot -p -A --default-character-set=utf8 > mysql.sql
    view raw bash-4.sh hosted with ❤ by GitHub


    到這邊備份就已經結束拉 ~
  5. 再來把新機器安裝好把需要安裝的東西裝進去

    clarence@ubuntu:~$ sudo apt update
    clarence@ubuntu:~$ sudo apt install nginx
    clarence@ubuntu:~$ sudo apt install mysql-server
    clarence@ubuntu:~$ sudo mysql_secure_installation
    clarence@ubuntu:~$ sudo apt install php-fpm php-mysql php-curl php-gd php-intl php-mbstring php-soap php-xml php-xmlrpc php-zip
    view raw bash-5.sh hosted with ❤ by GitHub

  6. 把 Nginx 設定好

    clarence@ubuntu:~$ sudo vim /etc/nginx/sites-available/blog.com.tw
    clarence@ubuntu:~$ sudo ln -s /etc/nginx/sites-available/blog.com.tw /etc/nginx/sites-enabled/
    clarence@ubuntu:~$ sudo nginx -t
    clarence@ubuntu:~$ sudo systemctl reload nginx
    clarence@ubuntu:~$ sudo chown -R www-data:www-data /var/www/wordpress
    view raw bash-6.sh hosted with ❤ by GitHub

  7. 修改一下 WordPress 的 DB 設定

    clarence@ubuntu:~$ vim wp-config.php
    /** MySQL hostname */
    define('DB_HOST', '127.0.0.1:3306');
    view raw bash-7.sh hosted with ❤ by GitHub

  8. 把 Mysql 資料庫推回去,順便檢查一下是否有這個資料庫

    clarence@ubuntu:~/Download$ sudo mysql < mysql.sql
    [sudo] password for clarence:
    clarence@ubuntu:~/Download$ sudo mysql
    Welcome to the MySQL monitor. Commands end with ; or \g.
    Your MySQL connection id is 1
    Server version: 5.7.25-0ubuntu0.18.04.2 (Ubuntu)
    Copyright (c) 2000, 2019, 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 |
    | #mysql50 |
    | mysql |
    | performance_schema |
    | sys |
    | wordpress |
    +---------------------+
    6 rows in set (0.00 sec)
    mysql> exit
    Bye
    view raw bash-8.sh hosted with ❤ by GitHub


    PS: 版主在這邊遇到一個 Mysql 升級的問題,所以需要把資料庫更新一下,大家可以嘗試一下,詳細的問題如:https://stackoverflow.com/questions/43846950/column-count-of-mysql-user-is-wrong-expected-42-found-44-the-table-is-probabl

基本上這樣應該就搞定拉 ~ 我當時在做的時候花了我快一天的時間,可見對 K8S 有多不熟 XD
以上經驗分享給大家 ~