mafeifan 2021-08-15 17:05:51 阅读数:777
打算把個人博客部署在k8s上面,記錄下過程。
博客是由vuepress搭建的,本質是個nodejs項目,托管在了github 執行npm run build
後會生成靜態資源
Dockerfile
和.dockerignore
Dockerfile
# build stage
FROM node:lts-alpine as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
# production stage
FROM nginx:stable-alpine as production-stage
COPY --from=build-stage /app/docs/.vuepress/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
複制代碼
.dockerignore
node_modules
.git
複制代碼
docker build . --file Dockerfile -t finleyma/blog-vuepres
docker push
複制代碼
打開 hub.docker.com/r/finleyma/… 成功啦
注意:我們已經提前在google cloud上建立好了包含3個工作節點的k8s集群
其中Deployment的replicas為3,因為我們的k8s集群有三個工作節點
Service的類型為LoadBalancer,這樣google cloud會自動幫我們創建負載均衡器並分配IP
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-blog-vuepress
labels:
app: blog-vuepress
spec:
replicas: 3
selector:
matchLabels:
app: blog-vuepress
template:
metadata:
labels:
app: blog-vuepress
spec:
containers:
- name: blog-vuepress
image: finleyma/blog-vuepress:latest
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: service-blog-vuepress
spec:
type: LoadBalancer
selector:
app: blog-vuepress
ports:
# 默認情况下,為了方便起見,`targetPort` 被設置為與 `port` 字段相同的值。
- protocol: TCP
## service暴露在cluster ip上的端口
port: 80
## targetPort是pod上的端口,Dockerfile中指定了80
targetPort: 80
複制代碼
kubectl describe svc service-blog-vuepress
注意LoadBalancer Ingress是暴露出來的可以外部訪問的IP啦
以上基本功能已經完成,接下來我們實現自動化
項目根目錄創建.github/workflows/cloud.yaml
# This is a basic workflow to help you get started with Actions
name: CI
# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
push:
branches: [ master ]
env:
CI: true
NODE: 14.x
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- uses: actions/[email protected]
- name: Install Node.js
uses: actions/[email protected]
with:
node-version: "${{ env.NODE }}"
- name: Install npm dependencies
run: npm ci
- name: Run build task
run: npm run build --if-present
- name: Build the Docker image
run: docker build . --file Dockerfile
- name: publish docker image
uses: elgohr/[email protected]
with:
name: finleyma/blog-vuepress
username: finleyma
password: ${{ secrets.DOCKER_HUB_PASSWORD }}
tags: "latest"
複制代碼
版权声明:本文为[mafeifan]所创,转载请带上原文链接,感谢。 https://gsmany.com/2021/08/20210815170542646A.html