High Available Wordpress with Kubernetes <4>
看完 High Available Wordpress with Kubernetes 系列應該對 k8s 有個初步的認識
本篇要來加碼如何讓你架的 wordpress 看起來更有個人識別性及安全性
也就是說把你 wordpress 改成你自己的 domain
像是: blog.dennis.king wordpress.someone.me ... 之類的
除了方便人識別及好記之外
對 SEO 也有幫助
在開始之前
我們需要改一下我們之前的架構
在第二集中我們的對內流量是透過 LoadBalancer 的 Service
這個 Servce 會去建一個 TCP Load Balance in GCP
對我們來說建一個 HTTP Load Balance 會比較方便
可以支援 HTTPS 讓你的 wordpress 看起來更專業
所以本集要做的事有:
1. 申請一個 Domain (如果你目前沒有的話)
2. 改架構: 用 Ingress 取代 LoadBalancer Service
3. 加上 SSL 讓你的 wordpress 看起來更專業!
4. 用 Domain 取代 IP
----
如果你沒有自己的 Domain 的話
建議是去買一個
可以到 Gandi 或是 Godaddy 這類的服務買一個適合自己的域名
接下來我們要來改我們做好的架構
改成這樣
因為用 Ingress Controller 會在 GCP 上開一個 HTTP(S) 的 Load Balance
然後因為 Ingress 算是外部來源
所以需要再 k8s 內部開一個 NodePort Service 來服務外來的流量
我們可以把原本的 LoadBalancer Service 先移除
▶ kubectl get service wordpress
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
wordpress LoadBalancer 10.15.244.169 35.221.238.197 80:31446/TCP 5m
▶ kubectl delete svc wordpress
service "wordpress" deleted
再新增一個 NodePort Sercice
wp-service.yaml:
apiVersion: v1
kind: Service
metadata:
name: wp-service
labels:
app: wordpress
spec:
ports:
- port: 80
selector:
app: wordpress
tier: frontend
type: NodePort
別忘記 apply 唷
確認一下服務是否有起來
▶ kubectl get svc wp-service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
wp-service NodePort 10.15.245.122 <none> 80:30729/TCP 1h
接下來我們要做一個 Ingress Controller
wp-ingress.yaml:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: wp-ingress
spec:
backend:
serviceName: wp-service
servicePort: 80
tls:
- secretName: ssl-wp-dnhuang-me
在這裡我們指定了 tls 的檔案
這個憑證要怎麼取的呢
方式有很多種
有些 Domain name provider 有買 Domain 送一年免費憑證
就直接申請可以拿到一年免費憑證
我這邊是用另一個方法
用 let's encrypt 的方式拿到免費憑證
首先先在電腦安裝 certbot 這個指令
然後我們透過驗證 acme challenge 認證 dns 的方式取得憑證
我選用的 domain 是 wp.dnhuang.me
▶ sudo certbot certonly --manual --preferred-challenges dns -d wp.dnhuang.me
之後呢會出現一段提示
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for wp.dnhuang.me
-------------------------------------------------------------------------------
NOTE: The IP of this machine will be publicly logged as having requested this
certificate. If you're running certbot in manual mode on a machine that is not
your server, please ensure you're okay with that.
Are you OK with your IP being logged?
-------------------------------------------------------------------------------
(Y)es/(N)o: y
-------------------------------------------------------------------------------
Please deploy a DNS TXT record under the name
_acme-challenge.wp.dnhuang.me with the following value:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Before continuing, verify the record is deployed.
-------------------------------------------------------------------------------
Press Enter to Continue
Waiting for verification...
Cleaning up challenges
出現之後先別急著按 Enter
記得將你的 DNS 加上一個 TXT Record 來認證這個 Domain 是你所有的
通常 Domain Nmae Provider 都會提供 DNS 服務讓你設定
新增一個 TXT Record
_acme-challenge.wp. 指定成提示中的亂碼 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
之後你就可以按下 Enter 了!
沒意外的話就可以看到憑證產生了
接著我們要把憑證上傳到 Secret 中
▶ kubectl create secret tls ssl-wp-dnhuang-me --key privkey.pem --cert fullchain.pem
secret "ssl-wp-dnhuang-me" created
▶ kubectl get secret ssl-wp-dnhuang-me
NAME TYPE DATA AGE
ssl-wp-dnhuang-me kubernetes.io/tls 2 2h
接著我們就可以 apply 我們的 Ingress 了!
apply 之後過一下子我們就可以看到 新的 IP 了
目前 HTTP(S) 都已備妥了
只剩下把 Domain 指定到我們的 k8s 的 Ingress IP 上了
所以再到 Domain Provider 的 DNS 服務上
新增一個 A Record 將我們的 Domain 指過去
完成後就可以看到拉!
答拉~~~~ https://wp.dnhuang.me/
----
這邊記錄個小插曲
如果在 LoadBalancer Service 的時候已經開啟了 wordpress 服務的話
記得先到 wordpress 設定中更改你的 domain
以免跟我一樣改成 Ingress 後進不去 QQ
這裡也順便提供進不去的解決辦法
先進 mysql 的 pod
▶ kubectl get pod | grep wordpress-mysql
NAME READY STATUS RESTARTS AGE
wordpress-mysql-6dd894df8b-m9fxb 1/1 Running 0 6d
▶ kubectl exec -it wordpress-mysql-6dd894df8b-m9fxb bash
root@wordpress-mysql-6dd894df8b-m9fxb:/# mysql -p
Enter password:
進 mysql 之後下兩個 command 來更改
mysql> use wordpress
mysql> UPDATE wp_options SET option_value="https://wp.dnhuang.me" WHERE option_name="home";
mysql> UPDATE wp_options SET option_value="https://wp.dnhuang.me" WHERE option_name="siteurl";
High Available Wordpress with Kubernetes 到此告一個段落
希望對大家能有所幫助
感謝收看
查看原文 >>