温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何基于k8s的Ingress部署hexo博客

发布时间:2021-07-28 18:51:47 来源:亿速云 阅读:190 作者:chen 栏目:云计算

这篇文章主要介绍“如何基于k8s的Ingress部署hexo博客”,在日常操作中,相信很多人在如何基于k8s的Ingress部署hexo博客问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何基于k8s的Ingress部署hexo博客”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

注:kuberntes版本为1.15

什么是 Ingress

Ingress 是一个提供对外服务的路由和负载均衡器,其本质是个nginx控制器服务。

k8s文档上Ingress经典数据链路图:

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

对博客进行改造

构建Dockefile

先容器化整个Hexo项目,构建Dockefile,这里采用nginx + 静态资源的形式部署(主要为了节约内存CPU):

FROM nginx:1.13.0-alpine
LABEL maintainer="hexo-shikanon-blog <shikanon@tensorbytes.com>"

# 装载编译后的文件对外访问
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 <none> 443/TCP 10d static-blog ClusterIP 10.13.83.44 <none> 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服务对外部提供服务和反向代理:

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

完成!

构建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博客”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI