WordPress 示例中:
使用 StatefulSet 编排运行 MySQL,实例数为 1:
部署思路:
创建 Kubernetes 集群
创建一个 MySQL 镜像
创建一个 PVC(Persistent Volume Claim),用于存储 MySQL 数据
创建一个 Service,用于提供 MySQL 服务
创建一个 StatefulSet,用于运行 MySQL 实例
具体操作步骤:
创建 PVC:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
复制代码
创建 Service:
apiVersion: v1
kind: Service
metadata:
name: mysql
labels:
app: mysql
spec:
ports:
- name: mysql
port: 3306
targetPort: 3306
selector:
app: mysql
复制代码
创建 StatefulSet:
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
serviceName: mysql
replicas: 1
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
value: "password"
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumeClaimTemplates:
- metadata:
name: mysql-persistent-storage
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
复制代码
换成使用 Operator 编排运行 MySQL,实例数为 1+:
部署思路:
具体操作步骤:
安装 MySQL Operator:
kubectl create namespace mysql-operator
helm repo add presslabs https://presslabs.github.io/charts
helm install mysql-operator presslabs/mysql-operator --namespace mysql-operator
复制代码
创建 MySQLCluster 对象:
apiVersion: mysql.presslabs.org/v1alpha1
kind: MySQLCluster
metadata:
name: mysqlcluster
spec:
replicas: 1
复制代码
将 MySQL 以传统模型的主从复制的形式运行于 Kubernetes 外部,让运行在 Kubernetes 集群上的 WordPress 去访问外部的 MySQL 服务:
部署思路:
具体操作步骤:
在 Kubernetes 集群外部搭建 MySQL 主从复制集群,具体操作步骤可以参考 MySQL 官方文档:https://dev.mysql.com/doc/refman/8.0/en/replication.html
在 Kubernetes 内部创建一个 Service:
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
type: ExternalName
externalName: <mysql-master-ip-address>
ports:
- port: 3306
name: mysql
复制代码
WordPress 实例扩展至多个,测试应用是否工作正常:
部署思路:
具体操作步骤:
扩展 WordPress Deployment 的副本数:
kubectl scale deployment wordpress --replicas=3
复制代码
创建一个 Service:
apiVersion: v1
kind: Service
metadata:
name: wordpress
spec:
ports:
- name: http
port: 80
targetPort: 80
selector:
app: wordpress
type: LoadBalancer
复制代码
Nginx 实例扩展至多个,测试应用是否工作正常;额外为 Nginx 添加 HTTPS 虚拟主机:
部署思路:
扩展 Nginx Deployment 的副本数
创建一个 ConfigMap
配置 HTTPS 证书和 Nginx 配置文件
创建一个 Secret,用于存储 HTTPS 证书
修改 Nginx 配置文件,添加 HTTPS 虚拟主机
创建一个 Service,用于提供 Nginx 服务
具体操作步骤:
扩展 Nginx Deployment 的副本数:
kubectl scale deployment nginx --replicas=3
复制代码
创建一个 ConfigMap,用于存储 HTTPS 证书和 Nginx 配置文件:
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
data:
default.conf: |
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://wordpress;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
ssl.conf: |
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/nginx/ssl/cert.pem;
ssl_certificate_key /etc/nginx/ssl/key.pem;
location / {
proxy_pass http://wordpress;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
复制代码
创建一个 Secret,用于存储 HTTPS 证书:
kubectl create secret tls tls-secret --cert=cert.pem --key=key.pem
复制代码
修改 Nginx 配置文件,添加 HTTPS 虚拟主机:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
volumeMounts:
- name: nginx-config
mountPath: /etc/nginx/conf.d
- name: tls-secret
mountPath: /etc/nginx/ssl
readOnly: true
ports:
- name: http
containerPort: 80
- name: https
containerPort: 443
volumes:
- name: nginx-config
configMap:
name: nginx-config
- name: tls-secret
secret:
secretName: tls-secret
复制代码
创建一个 Service,用于提供 Nginx 服务:
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
ports:
- name: http
port: 80
targetPort: 80
- name: https
port: 443
targetPort: 443
selector:
app: nginx
type: LoadBalancer
复制代码
注意:以上操作仅供参考,具体操作可能需要根据实际情况进行调整。
评论