I plan to deploy my personal blog on K8S to document the process.
The blog is built by Vuepress and is essentially a NodeJS project hosted on Github that generates static resources after the NPM run build is executed
- First the project root directory is added
Dockerfile
and.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;"]
Copy the code
.dockerignore
node_modules
.git
Copy the code
- Package the Docker image and push it to the Docker Hub repository
docker build . --file Dockerfile -t finleyma/blog-vuepres
docker push
Copy the code
Open hub.docker.com/r/finleyma/… Success!
- Create kubernates. Yaml for the project root
Note: We have set up a K8S cluster with three working nodes on the Google Cloud in advance
The Deployment replicas is 3, because our K8S cluster has three working nodes
Service is of type LoadBalancer, so Google cloud will automatically create the LoadBalancer for us and assign the IP address
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:
# By default, 'targetPort' is set to the same value as the 'port' field for convenience.
- protocol: TCP
## Service exposes port on cluster IP
port: 80
## targetPort is the port on the pod and 80 is specified in the Dockerfile
targetPort: 80
Copy the code
- Wait a moment to view the service you just created
kubectl describe svc service-blog-vuepress
Note that the LoadBalancer Ingress is an exposed IP address that can be accessed externally
With the above basic functions completed, we can automate them
- The image is automatically created after the code is submitted
Create project root directory. Making/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/checkout@v2
- name: Install Node.js
uses: actions/setup-node@v1
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/Publish-Docker-Github-Action@master
with:
name: finleyma/blog-vuepress
username: finleyma
password: ${{ secrets.DOCKER_HUB_PASSWORD }}
tags: "latest"
Copy the code
- TODO Github Actions combined with Google Cloud
Github.com/actions/sta…