天天看點

基于 k8s 的 Ingress 快速部署 hexo 部落格

原文作者:sugar_lover

原文連結:

https://developer.aliyun.com/article/726331?spm=a2c6h.12873581.0.0.5729115eLl6St3&groupCode=cloudnative

更多雲原生技術資訊可關注

阿裡巴巴雲原生技術圈

簡介:很多人說19年是原生雲的元年,之前沒感覺,這次在雲栖大會看到大家對原生雲的熱情,相信确實在進入原生雲推廣爆發期,就像15年的大資料技術的基礎hadoop一樣,k8s作為CNCF基金會的第一個暖化出來的項目,是不可撼動的基礎,是以為了讓大家快速熟悉k8s的基本操作,通過記錄hexo在k8s的部署來讓大家對k8s有個初步的影響。

對部落格進行原生雲改造

kubernetes概念

Kubernetes是用于容器化應用的高可用、自動彈性擴充和編排管理的架構。由于基于容器系統,Kubernetes可以輕松的提供内部雲、公有雲和混合雲支撐,輕松遷移工作負載。Kubernetes是基于谷歌公司在運作生産負載上的 15 年經驗打造,可以輕松支撐上千節點的運作。

搭建環境

搭建k8s環境,常用的有一下幾種方法:

(1)使用阿裡雲kubernetes叢集

基于 k8s 的 Ingress 快速部署 hexo 部落格

具體使用流程可以參見阿裡雲的幫助文檔:

https://help.aliyun.com/document_detail/86745.html?spm=5176.2020520152.0.0.2d5916ddXjQdeP

(2)使用kubeadm進行安裝

可以參考本人另一篇部落格:

【kubernetes部署:基于kubeadm的國内鏡像源安裝】

(3)使用rancher快速搭建

基于 k8s 的 Ingress 快速部署 hexo 部落格

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

阿裡巴巴雲原生 關注微服務、Serverless、容器、Service Mesh 等技術領域、聚焦雲原生流行技術趨勢、雲原生大規模的落地實踐,做最懂雲原生開發者的技術圈。”

繼續閱讀