2023 - A new way to deploy blogs

2023 年 3 月 27 日 星期一(已编辑)
/ ,
41
这篇文章上次修改于 2024 年 3 月 28 日 星期四,可能部分内容已经不适用,如有疑问可询问作者。

2023 - A new way to deploy blogs

I migrated my code repository when I had some free time.

Prior to that, I had been using Coding to manage my private code for three reasons:

  1. I was accustomed to it from my previous company.

  2. The pushing and pulling speed of the code in China was notably fast.

  3. I could utilize private mirror repositories and receive several hundred minutes of free nodes for continuous deployment.

However, my interest waned after it became a paid service. Additionally, deploying on foreign servers proved to be sluggish. While I attempted to utilize my own nodes for deployment, their performance was subpar, possibly due to the inefficiency of foreign servers... perhaps.

Currently, I employ k3s to manage these servers, making mirror image deployment convenient. The deployment process is as follows:

Local -> Github -> Docker Hub -> Pod reset -> K3s pull

Building and Pushing Images

In the file .github/workflows/build-and-deploy.yml:

name: Build and Deploy

on:
  push:
    branches:
      - main

jobs:
  build-and-deploy:
    runs-on: ubuntu-22.04

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: '0.110.0'

      - name: Remove old public folder
        run: rm -rf public

      - name: Build website
        run: hugo --minify

      - name: Login to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }} # create a on docker

      - name: Build image and push
        uses: docker/build-push-action@v4
        with:
          context: .
          file: ./Dockerfile
          push: true
          tags: avacooper/blog:latest # your img path

      - name: Cleanup
        run: |
          docker system prune -af --volumes
          rm -rf ./public

The Public folder generated by Hugo contains numerous static files that can be directly utilized by nginx.

In the Dockerfile:

FROM nginx:alpine

COPY public/ /usr/share/nginx/html/

EXPOSE 80

K3S Configuration

As I had configured the certificate manager, I did not use LoadBalancer directly but instead employed nginx-ingress.

The configuration is as follows:

apiVersion: v1
kind: Service
metadata:
  name: org-yatu-blog
  labels:
    app: blog
spec:
  ports:
    - port: 80
      name: blog
      targetPort: 80
  selector:
    app: blog
  type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: blog
spec:
  replicas: 1
  selector:
    matchLabels:
      app: blog
  template:
    metadata:
      labels:
        app: blog
    spec:
      containers:
        - name: blog
          image: avacooper/blog:latest
          ports:
            - containerPort: 80
          imagePullPolicy: Always
    #   nodeSelector:
    #     nn: sp
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: org-yatu-blog-ingress
  annotations:
    cert-manager.io/cluster-issuer: "letsencrypt-prod"
    nginx.ingress.kubernetes.io/proxy-send-timeout: "3600"
    nginx.ingress.kubernetes.io/proxy-read-timeout: "3600"
spec:
  ingressClassName: nginx
  tls:
  - hosts: 
    - "*.yatu.org"
    - "blog.yatu.org"
    secretName: lf-ink-tls
  rules:
  - host: blog.yatu.org
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service: 
            name: org-yatu-blog
            port:
              number: 80

The drawback is that the image uploaded to Docker Hub is public. Consequently, I intend to purchase a larger server with private mirror deployment capabilities.

  • Loading...
  • Loading...
  • Loading...
  • Loading...
  • Loading...