天天看点

基于 K8s 的 Ingress 快速部署 hexo 博客对博客进行原生云改造构建HTTPS网站

原文作者:sugar_lover

原文链接:

https://developer.aliyun.com/article/726331

对博客进行原生云改造

kubernetes 概念

Kubernetes 是用于容器化应用的高可用、自动弹性扩展和编排管理的框架。由于基于容器系统,Kubernetes 可以轻松的提供内部云、公有云和混合云支撑,轻松迁移工作负载。Kubernetes 是基于谷歌公司在运行生产负载上的 15 年经验打造,可以轻松支撑上千节点的运行。

搭建环境

搭建 k8s 环境,常用的有一下几种方法:

(1)使用阿里云 kubernetes 集群

基于 K8s 的 Ingress 快速部署 hexo 博客对博客进行原生云改造构建HTTPS网站

具体使用流程可以参见阿里云的帮助文档:

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

(2)使用kubeadm进行安装

可以参考另一篇博客:

【kubernetes 部署:基于 kubeadm 的国内镜像源安装】

(3)使用 rancher 快速搭建

基于 K8s 的 Ingress 快速部署 hexo 博客对博客进行原生云改造构建HTTPS网站

搭建 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 等技术领域、聚焦云原生流行技术趋势、云原生大规模的落地实践,做最懂云原生开发者的技术公众号。”

继续阅读