基於k8s的Ingress部署hexo博客(http和https)

注:kuberntes版本為1.15

什麼是 Ingress

Ingress 是一個提供對外服務的路由和負載均衡器,其本質是個nginx控制器服務。

k8s文檔上Ingress經典數據鏈路圖:

 internet
|
[ Ingress ]
--|-----|--
[ Services ]

對博客進行改造

基於k8s的Ingress部署hexo博客(http和https)

構建Dockefile

先容器化整個Hexo項目,構建Dockefile,這裡採用nginx + 靜態資源的形式部署(主要為了節約內存CPU):

FROM nginx:1.13.0-alpine
LABEL maintainer="hexo-shikanon-blog <shikanon>"
# 裝載編譯後的文件對外訪問
COPY ./public /usr/share/nginx/html
/<shikanon>

構建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 <none> 443/TCP 10d static-blog ClusterIP 10.13.83.44 <none> 80/TCP 8h/<none>/<none>

測試端口是否可以訪問:

$ 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服務對外部提供服務和反向代理:

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

完成!

基於k8s的Ingress部署hexo博客(http和https)

構建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

基於k8s的Ingress部署hexo博客(http和https)


分享到:


相關文章: