1. Preparation
K8s cluster environment. See the setup tutorial: Tencent Cloud Lighthouse cross-region Kubernetes cluster tutorial. For a cluster management panel, refer to "Installation and usage of Kubernetes cluster management panel - Qingyang's blog".
2. Deployment overview
This guide is based on a Kubernetes cluster and assumes a cluster management panel such as Kuboard is available. The steps below describe deploying a Typecho blog on Kubernetes.
The Docker images referenced in this guide:
- MySQL: mysql - Official Image | Docker Hub
- typecho: rehiy/typecho - Docker Image | Docker Hub
3. Create the MySQL database
3.1 MySQL configuration file
apiVersion: apps/v1 kind: Deployment metadata: labels: app: &name mysql # Deployment label, used to identify the resource as the mysql application name: *name # Deployment name is mysql namespace: default # Namespace where the Deployment resides is default spec: replicas: 1 # Number of Deployment replicas is 1 selector: matchLabels: app: *name template: metadata: labels: app: *name spec: affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchFields: - key: metadata.name operator: In values: - vm-4-13-ubuntu # Select node named vm-4-13-ubuntu as the deployment target containers: # Container definition - name: *name # Container name is mysql image: mysql:latest # Use the latest MySQL image ports: # Container port mapping - containerPort: 3306 # Expose container port 3306 env: # Environment variable settings - name: MYSQL_ROOT_PASSWORD # MySQL root user password value: typecho@123 - name: MYSQL_DATABASE # MySQL database name value: typecho - name: MYSQL_USER # MySQL user name value: typecho - name: MYSQL_PASSWORD # MySQL user password value: typecho@123 volumeMounts: # Volume mounts for the container - name: db mountPath: /var/lib/mysql # Mount the volume to /var/lib/mysql volumes: - name: db hostPath: path: /var/lib/mysql # Host path /var/lib/mysql as the volume path --- apiVersion: v1 kind: Service metadata: name: mysql # Service name is mysql namespace: default # Service namespace is default spec: type: ClusterIP # Service type is ClusterIP for internal cluster use selector: app: mysql ports: - name: db-port protocol: TCP port: 3306 # Service port 3306 targetPort: 3306 # Forward to Pod port 3306
After modifying the YAML above as needed, paste it into your cluster panel and submit.
Once applied, the MySQL service will be created. Click the service in the cluster panel to view details.
4. Create Typecho
4.1 Typecho configuration file
kind: Deployment # Create a Deployment resource apiVersion: apps/v1 metadata: name: &name myblog # Define scalar anchor 'myblog' and reference it in the name field namespace: default labels: app: *name spec: selector: matchLabels: app: *name template: metadata: labels: app: *name spec: affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchFields: - key: metadata.name operator: In values: - vm-4-13-ubuntu # Select node named vm-4-13-ubuntu as the deployment target containers: - name: typecho # Define a container named typecho image: rehiy/typecho # Use the rehiy/typecho image ports: - containerPort: 80 # Expose port 80 - containerPort: 443 # Expose port 443 volumeMounts: - name: *name subPath: usr # Mount subpath 'usr' in the container mountPath: /var/www/default/usr # Mount to /var/www/default/usr volumes: - name: *name hostPath: path: /srv/myblog # Host path /srv/myblog as the volume path type: DirectoryOrCreate # Create directory if it does not exist --- kind: Service # Create a Service resource apiVersion: v1 metadata: name: &name myblog namespace: default labels: app: *name spec: selector: app: *name ports: - name: http port: 80 targetPort: 80 - name: https port: 443 targetPort: 443 --- kind: Ingress # Create an Ingress resource apiVersion: networking.k8s.io/v1 metadata: name: &name myblog namespace: default annotations: traefik.ingress.kubernetes.io/router.entrypoints: web,websecure # Traefik router entrypoints configuration spec: rules: - host: blog.eg.cn # Host for the Ingress http: paths: - path: / pathType: Prefix backend: service: name: *name port: name: http tls: - secretName: default # TLS secret used for HTTPS access
4.2 Apply the Typecho configuration
Apply the Typecho YAML in the same way as the MySQL configuration: modify as needed, paste into the cluster panel, and submit.
Testing
Point the domain to the pod IP and access the domain in a browser. If the page loads successfully, the blog has been created; then follow the on-screen prompts to complete installation.
5. Summary
Kubernetes provides high availability by automatically managing and scheduling container instances to keep applications available across the cluster. It can monitor and automatically recover failed containers to improve the stability and reliability of the blog. Its autoscaling features can adjust application instances according to load: when traffic increases, Kubernetes can scale out instances to meet demand, and scale in when traffic decreases to reduce resource usage.
ALLPCB