搞了两天集群了,得搞点有实际用途的东西出来,所以决定搭建一个WordPress的博客。
这次的总体思路是使用WordPress的官方镜像跑WordPress,使用MySQL镜像跑MySQL,尽可能的保持简单。
具体规划是,使用longhorn提供底层的存储支撑,MySQL跑在MySQL的独立namespace里面,因为我打算把他做成公共的服务,WordPress跑在default命名空间中,并且同时部署两个节点。
文中的一些MySQL的基础操作,会忽略具体细节,毕竟这不在K3S讨论的范畴内。
首先,根据这篇文章介绍,如果需要ReadWriteMany (RWX) 支持,centos需要安装NFSv4 client:
yum install nfs-utils
把 https://raw.githubusercontent.com/longhorn/longhorn/v1.2.4/deploy/longhorn.yaml 下载到服务器上,通过kubectl apply -f longhorn.yaml
安装
通过ingress配置外网访问longhorn的dashboard,这里加了一个简单的登录。
运行命令(记得替换里面的用户名、密码):
USER=<USERNAME_HERE>; PASSWORD=<PASSWORD_HERE>; echo "${USER}:$(openssl passwd -stdin -apr1 <<< ${PASSWORD})" >> auth
kubectl -n longhorn-system create secret generic basic-auth --from-file=auth
新建文件longhorn-ingress.yam
:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: longhorn-ingress
namespace: longhorn-system
annotations:
kubernetes.io/ingress.class: nginx
# type of authentication
nginx.ingress.kubernetes.io/auth-type: basic
# prevent the controller from redirecting (308) to HTTPS
nginx.ingress.kubernetes.io/ssl-redirect: 'false'
# name of the secret that contains the user/password definitions
nginx.ingress.kubernetes.io/auth-secret: basic-auth
# message to display with an appropriate context why the authentication is required
nginx.ingress.kubernetes.io/auth-realm: 'Authentication Required'
# custom max body size for file uploading like backing image uploading
nginx.ingress.kubernetes.io/proxy-body-size: 10000m
spec:
rules:
- host: longhorn.example.com #注意把域名换一下
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: longhorn-frontend
port:
number: 80
解析好域名,就可以使用设置的用户名、密码登录了。
其实这里也可以不用密码,直接查看,但是毕竟公网访问,还是加个简单的密码比较好。
新建文件mysql.yaml
apiVersion: v1
kind: Namespace
metadata:
name: mysql
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
namespace: mysql
labels:
app: mysql
spec:
storageClassName: longhorn
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
namespace: mysql
labels:
app: mysql
spec:
selector:
matchLabels:
app: mysql
replicas: 1
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql
imagePullPolicy: IfNotPresent
volumeMounts:
- mountPath: /var/lib/mysql
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: root
volumes:
- name: mysql
persistentVolumeClaim:
claimName: mysql-pvc
---
apiVersion: v1
kind: Service
metadata:
name: mysql
namespace: mysql
spec:
selector:
app: mysql
ports:
- name: mysql
protocol: TCP
port: 3306
targetPort: 3306
运行命令:kubectl apply -f mysql.yaml
创建
这里需要说明的是,mysql-pvc默认需要2个副本,但是我们这里只有两个节点,所以longhorn的dashboard里面会报错,可以用longhorn的dashboard里面,手动改成2副本.
这里我就不改系统默认的设置了,因为我之后还是会再加节点进集群,改来改去麻烦。
首先需要在数据库中新建一个名为wordpress
的数据库,分配好用户名密码,这一部分涉及MySQL的基础知识,就不细说了,链接数据库的话,可以通过kubectl exec
这个命令直接进pod节点直接扣命令,也可以临时把mysql的服务类型设为nodeport,用gui工具连上去搞。
新建文件wordpress.yaml
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: blog-example-com
namespace: default
labels:
app: wordpress
spec:
secretName: blog-example-com-tls
issuerRef:
kind: ClusterIssuer
name: letsencrypt-issuer
dnsNames:
- blog.example.com
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: wordpress-pvc
namespace: default
labels:
app: wordpress
spec:
storageClassName: longhorn
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
namespace: default
labels:
app: wordpress
spec:
selector:
matchLabels:
app: wordpress
replicas: 2
template:
metadata:
labels:
app: wordpress
spec:
containers:
- name: wordpress
image: wordpress
env:
- name: WORDPRESS_DB_HOST
value: mysql.mysql.svc.cluster.local:3306
- name: WORDPRESS_DB_PASSWORD
value: wordpress
- name: WORDPRESS_DB_USER
value: wordpress
- name: WORDPRESS_DB_NAME
value: wordpress
ports:
- containerPort: 80
name: http
volumeMounts:
- mountPath: /var/www/html
name: wordpress
volumes:
- name: wordpress
persistentVolumeClaim:
claimName: wordpress-pvc
---
apiVersion: v1
kind: Service
metadata:
name: wordpress
namespace: default
labels:
app: wordpress
spec:
selector:
app: wordpress
type: ClusterIP
ports:
- name: http
protocol: TCP
port: 80
targetPort: http
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx
name: wordpress
namespace: default
labels:
app: wordpress
spec:
rules:
- host: blog.example.com
http:
paths:
- pathType: Prefix
path: /
backend:
service:
name: wordpress
port:
name: http
tls:
- hosts:
- blog.example.com
secretName: blog-example-com-tls
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。