原文作者:sugar_lover
原文連結:
https://developer.aliyun.com/article/726331對部落格進行原生雲改造
kubernetes 概念
Kubernetes 是用于容器化應用的高可用、自動彈性擴充和編排管理的架構。由于基于容器系統,Kubernetes 可以輕松的提供内部雲、公有雲和混合雲支撐,輕松遷移工作負載。Kubernetes 是基于谷歌公司在運作生産負載上的 15 年經驗打造,可以輕松支撐上千節點的運作。
搭建環境
搭建 k8s 環境,常用的有一下幾種方法:
(1)使用阿裡雲 kubernetes 叢集

具體使用流程可以參見阿裡雲的幫助文檔:
https://help.aliyun.com/document_detail/86745.html?spm=5176.2020520152.0.0.2d5916ddXjQdeP(2)使用kubeadm進行安裝
可以參考另一篇部落格:
【kubernetes 部署:基于 kubeadm 的國内鏡像源安裝】
(3)使用 rancher 快速搭建
搭建 Nginx 版
建構 Dockefile
先容器化整個 Hexo 項目,建構 Dockefile,這裡采用 nginx + 靜态資源的形式部署(主要為了節約記憶體 CPU):
FROM nginx:1.13.0-alpine
LABEL maintainer="hexo-shikanon-blog <[email protected]>"
# 裝載編譯後的檔案對外通路
COPY ./public /usr/share/nginx/html
建構Deployment
建構一個
Deployment
服務将其部署上 kubernetes:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-hexo-blog-delopyment
labels:
webtype: staticblog
spec:
replicas: 2
selector:
matchLabels:
webtype: staticblog
template:
metadata:
labels:
webtype: staticblog
function: blog
spec:
containers:
- name: hexo-blog
image: nginx-hexo-blog:0.0.1
ports:
- containerPort: 80
建構 Service 暴露服務端口
建構一個 Service 暴露統一的服務端口:
apiVersion: v1
kind: Service
metadata:
name: static-blog
spec:
selector:
webtype: staticblog
ports:
- protocol: TCP
port: 80
targetPort: 80 # deployment的端口,
這裡建立一個名稱為 "static-blog" 的 Service 對象,它會将請求代理到使用 TCP 端口 targetPort,并且具有标簽 "webtype: staticblog" 的 Pod 上。
檢視端口資訊:
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.13.0.1 443/TCP 10d
static-blog ClusterIP 10.13.83.44 80/TCP 8h
測試端口是否可以通路:
$ curl -I 10.13.83.44
HTTP/1.1 200 OK
Server: nginx/1.13.0
Date: Wed, 16 Oct 2019 16:51:13 GMT
Content-Type: text/html
Content-Length: 71636
Last-Modified: Mon, 29 Jul 2019 19:25:29 GMT
Connection: keep-alive
ETag: "5d3f4829-117d4"
Accept-Ranges: bytes
建構 Ingress 服務
Ingress 是一個提供對外服務的路由和負載均衡器,其本質是個 nginx 控制器服務。
k8s 文檔上 Ingress 經典資料鍊路圖:
internet
|
[ Ingress ]
--|-----|--
[ Services ]
最後一步,建構 Ingress 服務對外部提供服務和反向代理:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: reverse-proxy
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: www.shikanon.com
http:
paths:
- backend:
serviceName: static-blog
servicePort: 80
完成!
搭建 Nodejs 版
Node 版和 Nginx 版的主要差別是 dockefile 建構不同:
FORM node:8.16-onbuild
LABEL maintainer="hexo-shikanon-blog <[email protected]>"
# 将源碼檔案放入目錄
COPY . /app
# 安裝安裝包
npm install hexo -g --registry=https://registry.npm.taobao.org
CMD ["hexo","run","serve"]
建構HTTPS網站
用secret類型對象儲存密鑰資料
Secret 對象類型用來儲存敏感資訊,例如密碼、OAuth 令牌和 ssh key,其中 ssh key 就是一個經典的應用。
Secret 參數用例:
kubectl create secret -h
Create a secret using specified subcommand.
Available Commands:
docker-registry Create a secret for use with a Docker registry
generic Create a secret from a local file, directory or literal value
tls Create a TLS secret
Usage:
kubectl create secret [flags] [options]
建立
Secret
加密對象:
kubectl create secret tls shikanon-ssh-key-secret --cert=/home/shikanon/web/www/ssl/cert.pem --key=/home/shikanon/web/www/ssl/private.key
修改 Ingress:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: reverse-proxy
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: www.shikanon.com
http:
paths:
- backend:
serviceName: static-blog
servicePort: 80
tls:
- hosts:
- www.shikanon.com
secretName: shikanon-ssh-key-secret
注:一個 Ingress 隻能支援一個 tls
“阿裡巴巴雲原生微信公衆号(ID:Alicloudnative)關注微服務、Serverless、容器、Service Mesh 等技術領域、聚焦雲原生流行技術趨勢、雲原生大規模的落地實踐,做最懂雲原生開發者的技術公衆号。”