看完 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 到此告一个段落

希望对大家能有所帮助

感谢收看

查看原文 >>
相关文章