溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2021-07-28 18:51:47 來源:億速云 閱讀:207 作者:chen 欄目:云計算

這篇文章主要介紹“如何基于k8s的Ingress部署hexo博客”,在日常操作中,相信很多人在如何基于k8s的Ingress部署hexo博客問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何基于k8s的Ingress部署hexo博客”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

注:kuberntes版本為1.15

什么是 Ingress

Ingress 是一個提供對外服務(wù)的路由和負(fù)載均衡器,其本質(zhì)是個nginx控制器服務(wù)。

k8s文檔上Ingress經(jīng)典數(shù)據(jù)鏈路圖:

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

對博客進(jìn)行改造

構(gòu)建Dockefile

先容器化整個Hexo項目,構(gòu)建Dockefile,這里采用nginx + 靜態(tài)資源的形式部署(主要為了節(jié)約內(nèi)存CPU):

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

# 裝載編譯后的文件對外訪問
COPY ./public /usr/share/nginx/html
構(gòu)建Deployment

構(gòu)建一個Deployment服務(wù)將其部署上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
構(gòu)建Service暴露服務(wù)端口

構(gòu)建一個Service暴露統(tǒng)一的服務(wù)端口:

apiVersion: v1
kind: Service
metadata:
  name: static-blog
spec:
  selector:
    webtype: staticblog
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80 # deployment的端口,

這里創(chuàng)建一個名稱為 "static-blog" 的 Service 對象,它會將請求代理到使用 TCP 端口 targetPort,并且具有標(biāo)簽 "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

構(gòu)建Ingress服務(wù)

最后一步,構(gòu)建Ingress服務(wù)對外部提供服務(wù)和反向代理:

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

完成!

構(gòu)建HTTPS網(wǎng)站

用secret類型對象保存密鑰數(shù)據(jù)

Secret 對象類型用來保存敏感信息,例如密碼、OAuth 令牌和 ssh key,其中 ssh key 就是一個經(jīng)典的應(yīng)用。

Secret 參數(shù)用例:

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]

創(chuàng)建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

到此,關(guān)于“如何基于k8s的Ingress部署hexo博客”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI