Kubernetes 实战【OpenEBS+MetalLB+Mysql+Wordpress】-2024.1.08-部署成功-数据库连不上
部署 MetalLB
https://www.cnblogs.com/birkhoffxia/articles/17949510
部署 OpenEBC
https://www.cnblogs.com/birkhoffxia/articles/17943626
额外部署多路读写 nfs-operator
#为了多路读写
kubectl apply -f https://openebs.github.io/charts/nfs-operator.yaml
#openebs-nfs-provisioner-5b595f4798-pg529 1/1 Running 0 32s
[root@k8s-master01 ~]# kubectl get pods -n openebs
NAME READY STATUS RESTARTS AGE
jiva-operator-54b95c8f45-jq2fg 1/1 Running 0 7m15s
openebs-jiva-csi-controller-0 5/5 Running 0 7m15s
openebs-jiva-csi-node-564dk 3/3 Running 0 7m15s
openebs-jiva-csi-node-7ns7x 3/3 Running 0 7m15s
openebs-jiva-csi-node-lf4wc 3/3 Running 0 7m15s
openebs-localpv-provisioner-6787b599b9-5zvwn 1/1 Running 0 46m
openebs-ndm-cluster-exporter-7bfd5746f4-l54qt 1/1 Running 0 46m
openebs-ndm-n62qp 1/1 Running 0 46m
openebs-ndm-node-exporter-9j6ww 1/1 Running 0 46m
openebs-ndm-node-exporter-g8dz7 1/1 Running 0 46m
openebs-ndm-node-exporter-qfchc 1/1 Running 0 46m
openebs-ndm-operator-845b8858db-fvgnq 1/1 Running 0 46m
openebs-ndm-vxmcx 1/1 Running 0 46m
openebs-ndm-zzfq6 1/1 Running 0 46m
openebs-nfs-provisioner-5b595f4798-pg529 1/1 Running 0 32s
部署 Mysql
*由于后续的wordpress是部署在blog名称空间下 所以mysql也需要部署在blog名称空间下
1、configmap配置
kubectl create namespace blog
vim 01-configmap-mysql.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql
data:
primary.cnf: |
# Apply this config only on the primary.
[mysql]
default-character-set=utf8mb4
[mysqld]
log-bin
character-set-server=utf8mb4
[client]
default-character-set=utf8mb4
replica.cnf: |
# Apply this config only on replicas.
[mysql]
default-character-set=utf8mb4
[mysqld]
super-read-only
character-set-server=utf8mb4
[client]
default-character-set=utf8mb4
[root@k8s-master01 mysql]# kubectl apply -f 01-configmap-mysql.yaml -n blog
2、配置无头服务和读服务
vim 02-services-mysql.yaml
# Headless service for stable DNS entries of StatefulSet members.
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
ports:
- name: mysql
port: 3306
clusterIP: None
selector:
app: mysql
---
# Client service for connecting to any MySQL instance for reads.
# For writes, you must instead connect to the primary: mysql-0.mysql.
apiVersion: v1
kind: Service
metadata:
name: mysql-read
labels:
app: mysql
spec:
ports:
- name: mysql
port: 3306
selector:
app: mysql
[root@k8s-master01 mysql]# kubectl apply -f 02-services-mysql.yaml -n blog
service/mysql created
service/mysql-read created
[root@k8s-master01 mysql]# kubectl get svc -n blog
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d
mysql ClusterIP None <none> 3306/TCP 3s
mysql-read ClusterIP 10.100.197.164 <none> 3306/TCP 3s
3、配置Statefulset
[root@k8s-master01 ~]# kubectl get sc
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
openebs-device openebs.io/local Delete WaitForFirstConsumer false 54m
openebs-hostpath openebs.io/local Delete WaitForFirstConsumer false 54m
openebs-jiva-csi jiva.csi.openebs.io Delete Immediate true 12m
openebs-rwx openebs.io/nfsrwx Delete Immediate false 8m25s
# storageClassName: "nfs-csi" 改为 storageClassName: "openebs-hostpath" 或者 storageClassName: "openebs-jiva-csi"
vim 03-statefulset-mysql.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
serviceName: mysql
replicas: 3
template:
metadata:
labels:
app: mysql
spec:
initContainers:
- name: init-mysql
image: mysql:5.7
command:
- bash
- "-c"
- |
set -ex
# Generate mysql server-id from pod ordinal index.
[[ $(cat /proc/sys/kernel/hostname) =~ -([0-9]+)$ ]] || exit 1
ordinal=${BASH_REMATCH[1]}
echo [mysqld] > /mnt/conf.d/server-id.cnf
# Add an offset to avoid reserved server-id=0 value.
echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
# Copy appropriate conf.d files from config-map to emptyDir.
if [[ $ordinal -eq 0 ]]; then
cp /mnt/config-map/primary.cnf /mnt/conf.d/
else
cp /mnt/config-map/replica.cnf /mnt/conf.d/
fi
volumeMounts:
- name: conf
mountPath: /mnt/conf.d
- name: config-map
mountPath: /mnt/config-map
- name: clone-mysql
image: ikubernetes/xtrabackup:1.0
command:
- bash
- "-c"
- |
set -ex
# Skip the clone if data already exists.
[[ -d /var/lib/mysql/mysql ]] && exit 0
# Skip the clone on primary (ordinal index 0).
[[ $(cat /proc/sys/kernel/hostname) =~ -([0-9]+)$ ]] || exit 1
ordinal=${BASH_REMATCH[1]}
[[ $ordinal -eq 0 ]] && exit 0
# Clone data from previous peer.
ncat --recv-only mysql-$(($ordinal-1)).mysql 3307 | xbstream -x -C /var/lib/mysql
# Prepare the backup.
xtrabackup --prepare --target-dir=/var/lib/mysql
volumeMounts:
- name: data
mountPath: /var/lib/mysql
subPath: mysql
- name: conf
mountPath: /etc/mysql/conf.d
containers:
- name: mysql
image: mysql:5.7
env:
- name: LANG
value: "C.UTF-8"
- name: MYSQL_ALLOW_EMPTY_PASSWORD
value: "1"
ports:
- name: mysql
containerPort: 3306
volumeMounts:
- name: data
mountPath: /var/lib/mysql
subPath: mysql
- name: conf
mountPath: /etc/mysql/conf.d
livenessProbe:
exec:
command: ["mysqladmin", "ping"]
initialDelaySeconds: 30
periodSeconds: 10
timeoutSeconds: 5
readinessProbe:
exec:
# Check we can execute queries over TCP (skip-networking is off).
command: ["mysql", "-h", "127.0.0.1", "-e", "SELECT 1"]
initialDelaySeconds: 5
periodSeconds: 2
timeoutSeconds: 1
- name: xtrabackup
image: ikubernetes/xtrabackup:1.0
ports:
- name: xtrabackup
containerPort: 3307
command:
- bash
- "-c"
- |
set -ex
cd /var/lib/mysql
# Determine binlog position of cloned data, if any.
if [[ -f xtrabackup_slave_info && "x$(<xtrabackup_slave_info)" != "x" ]]; then
# XtraBackup already generated a partial "CHANGE MASTER TO" query
# because we're cloning from an existing replica. (Need to remove the tailing semicolon!)
cat xtrabackup_slave_info | sed -E 's/;$//g' > change_master_to.sql.in
# Ignore xtrabackup_binlog_info in this case (it's useless).
rm -f xtrabackup_slave_info xtrabackup_binlog_info
elif [[ -f xtrabackup_binlog_info ]]; then
# We're cloning directly from primary. Parse binlog position.
[[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
rm -f xtrabackup_binlog_info xtrabackup_slave_info
echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\
MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in
fi
# Check if we need to complete a clone by starting replication.
if [[ -f change_master_to.sql.in ]]; then
echo "Waiting for mysqld to be ready (accepting connections)"
until mysql -h 127.0.0.1 -e "SELECT 1"; do sleep 1; done
echo "Initializing replication from clone position"
mysql -h 127.0.0.1 \
-e "$(<change_master_to.sql.in), \
MASTER_HOST='mysql-0.mysql', \
MASTER_USER='root', \
MASTER_PASSWORD='', \
MASTER_CONNECT_RETRY=10; \
START SLAVE;" || exit 1
# In case of container restart, attempt this at-most-once.
mv change_master_to.sql.in change_master_to.sql.orig
fi
# Start a server to send backups when requested by peers.
exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c \
"xtrabackup --backup --slave-info --stream=xbstream --host=127.0.0.1 --user=root"
volumeMounts:
- name: data
mountPath: /var/lib/mysql
subPath: mysql
- name: conf
mountPath: /etc/mysql/conf.d
volumes:
- name: conf
emptyDir: {}
- name: config-map
configMap:
name: mysql
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: "openebs-hostpath"
resources:
requests:
storage: 10Gi
[root@k8s-master01 mysql]# kubectl apply -f 03-statefulset-mysql.yaml -n blog
[root@k8s-master01 mysql]# kubectl get pods -n blog
NAME READY STATUS RESTARTS AGE
mysql-0 2/2 Running 0 3m50s
mysql-1 2/2 Running 0 2m56s
mysql-2 2/2 Running 0 2m1s
[root@k8s-master01 mysql]# kubectl get pvc -n blog
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
data-mysql-0 Bound pvc-b12a2489-9de0-46b7-a9be-ca56b65fc914 10Gi RWO openebs-hostpath 3m58s
data-mysql-1 Bound pvc-7c17cb17-fec3-4150-b89a-dff6faca7af9 10Gi RWO openebs-hostpath 3m4s
data-mysql-2 Bound pvc-14e384f4-df45-4d4c-8dbf-e677136bf856 10Gi RWO openebs-hostpath 2m9s
[root@k8s-master01 mysql]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-14e384f4-df45-4d4c-8dbf-e677136bf856 10Gi RWO Delete Bound default/data-mysql-2 openebs-hostpath 110s
pvc-7c17cb17-fec3-4150-b89a-dff6faca7af9 10Gi RWO Delete Bound default/data-mysql-1 openebs-hostpath 2m56s
pvc-b12a2489-9de0-46b7-a9be-ca56b65fc914 10Gi RWO Delete Bound default/data-mysql-0 openebs-hostpath 3m50s
4、测试主从服务
[root@k8s-master01 mysql]# kubectl exec -it mysql-0 bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
Defaulted container "mysql" out of: mysql, xtrabackup, init-mysql (init), clone-mysql (init)
bash-4.2# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 191
Server version: 5.7.44-log MySQL Community Server (GPL)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> CREATE DATABASE database_name;
Query OK, 1 row affected (0.00 sec)
mysql> CREATE DATABASE CloudNative;
Query OK, 1 row affected (0.00 sec)
mysql> CREATE USER 'xks'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> use CloudNative;
Database changed
mysql> CREATE TABLE PB (
-> id INT PRIMARY KEY,
-> name VARCHAR(100),
-> age INT
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
# 从节点 mysql-1 \ mysql-2 是一样的 数据都同步过来了
[root@k8s-master01 mysql]# kubectl exec -it mysql-1 bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
Defaulted container "mysql" out of: mysql, xtrabackup, init-mysql (init), clone-mysql (init)
bash-4.2# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 400
Server version: 5.7.44 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+------------------------+
| Database |
+------------------------+
| information_schema |
| CloudNative |
| database_name |
| mysql |
| performance_schema |
| sys |
| xtrabackup_backupfiles |
+------------------------+
7 rows in set (0.00 sec)
mysql> use CloudNative;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-----------------------+
| Tables_in_CloudNative |
+-----------------------+
| PB |
+-----------------------+
1 row in set (0.00 sec)
5、测试 无头服务
[root@k8s-master01 mysql]# kubectl get pods -o wide | grep mysql -n blog
mysql-0 2/2 Running 0 22m 172.16.58.205 k8s-node02 <none> <none>
mysql-1 2/2 Running 0 21m 172.16.85.204 k8s-node01 <none> <none>
mysql-2 2/2 Running 0 20m 172.16.32.132 k8s-master01 <none> <none>
[root@k8s-master01 mysql]# kubectl get svc -n blog
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d
mysql ClusterIP None <none> 3306/TCP 24m
mysql-read ClusterIP 10.100.197.164 <none> 3306/TCP 24m
#查询mysql-0/1/2 解析 会解析到自己IP地址
[root@k8s-master01 mysql]# kubectl run client-$RANDOM --image ikubernetes/admin-box:v1.2 -it --rm --restart=Never --command -- /bin/bash
root@client-1588 /# nslookup mysql-0.mysql.blog
Server: 10.96.0.10
Address: 10.96.0.10#53
Name: mysql-0.mysql.blog.svc.cluster.local
Address: 172.16.58.205
root@client-1588 /# nslookup mysql-1.mysql.blog
Server: 10.96.0.10
Address: 10.96.0.10#53
Name: mysql-1.mysql.blog.svc.cluster.local
Address: 172.16.85.204
root@client-1588 /# nslookup mysql-2.mysql.blog
Server: 10.96.0.10
Address: 10.96.0.10#53
Name: mysql-2.mysql.blog.svc.cluster.local
Address: 172.16.32.132
#查询mysql无头服务名是 解析 会解析到对应后端3个IP地址
root@client-1588 /# nslookup mysql.blog
Server: 10.96.0.10
Address: 10.96.0.10#53
Name: mysql.blog.svc.cluster.local
Address: 172.16.58.205
Name: mysql.blog.svc.cluster.local
Address: 172.16.85.204
Name: mysql.blog.svc.cluster.local
Address: 172.16.32.132
#查询mysql-read 因为是普通service 还是会解析到service clusterip
root@client-26698 /# nslookup mysql-read.blog
Server: 10.96.0.10
Address: 10.96.0.10#53
Name: mysql-read.blog.svc.cluster.local
Address: 10.100.197.164
WordPress
1、创建命名空间blog
vim 01-namespace-blog.yaml
apiVersion: v1
kind: Namespace
metadata:
creationTimestamp: null
name: blog
spec: {}
status: {}
kubectl apply -f 01-namespace-blog.yaml
[root@k8s-master01 wordPress]# kubectl get ns | grep blog
blog Active 12s
2、创建mysql secret
echo wordpress | base64
d29yZHByZXNzCg==
echo blog | base64
YmxvZwo=
echo sheca | base64
c2hlY2EK
[root@k8s-master01 wordPress]# cat 02-mysql-secret.yaml
apiVersion: v1
data:
wordpress.db: d29yZHByZXNzCg==
wordpress.password: c2hlY2EK
wordpress.username: YmxvZwo=
kind: Secret
metadata:
creationTimestamp: null
name: mysql-secret
namespace: blog
kubectl apply -f 02-mysql-secret.yaml
3、创建service 类型为LoadBalancer
#创建service 类型为LoadBalancer type: LoadBalancer
vim 05-wordpress-service.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: wordpress
name: wordpress
namespace: blog
spec:
ports:
- name: 80-80
port: 80
protocol: TCP
targetPort: 80
selector:
app: wordpress
type: LoadBalancer
kubectl apply -f 05-wordpress-service.yaml
[root@k8s-master01 wordPress]# kubectl get svc -A | grep wordpress
blog wordpress LoadBalancer 10.99.201.27 192.168.40.52 80:31431/TCP 15s
4、可选:可以修改 openebs-rwx storageclass后端为jiva 多因子复制卷
[root@k8s-master01 wordPress]# kubectl get sc
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
openebs-device openebs.io/local Delete WaitForFirstConsumer false 116m
openebs-hostpath openebs.io/local Delete WaitForFirstConsumer false 116m
openebs-jiva-csi jiva.csi.openebs.io Delete Immediate true 74m
openebs-rwx openebs.io/nfsrwx Delete Immediate false 69m
[root@k8s-master01 wordPress]# kubectl get sc openebs-rwx -o yaml > old-openebs-rwx.yaml
[root@k8s-master01 wordPress]# cp old-openebs-rwx.yaml openebs-rwx.yaml
#修改 value: "openebs-hostpath" 为 value: "openebs-jiva-csi"
[root@k8s-master01 wordPress]# vim openebs-rwx.yaml
- name: BackendStorageClass
value: "openebs-jiva-csi"
[root@k8s-master01 wordPress]# kubectl delete sc openebs-rwx
storageclass.storage.k8s.io "openebs-rwx" deleted
[root@k8s-master01 wordPress]# kubectl apply -f openebs-rwx.yaml
storageclass.storage.k8s.io/openebs-rwx created
[root@k8s-master01 wordPress]# kubectl get sc
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
openebs-device openebs.io/local Delete WaitForFirstConsumer false 116m
openebs-hostpath openebs.io/local Delete WaitForFirstConsumer false 116m
openebs-jiva-csi jiva.csi.openebs.io Delete Immediate true 75m
openebs-rwx openebs.io/nfsrwx Delete Immediate false 5s
[root@k8s-master01 wordPress]# kubectl get sc openebs-rwx -o yaml
- name: BackendStorageClass
value: "openebs-jiva-csi"
4、创建PVC 使用多路读写
#
vim 06-pvc-wordpress.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: wordpress-pvc
namespace: blog
spec:
accessModes: ["ReadWriteMany"]
volumeMode: Filesystem
resources:
requests:
storage: 5Gi
storageClassName: openebs-rwx
kubectl apply -f 06-pvc-wordpress.yaml
[root@k8s-master01 wordPress]# kubectl get pvc -n blog
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
wordpress-pvc Bound pvc-4f51d2eb-dc08-437b-b675-d7d4b1cea9f8 5Gi RWX openebs-rwx 15s
5、创建wordpress db、username、password
[root@k8s-master01 wordPress]# kubectl exec -it mysql-0 bash
bash-4.2# mysql
CREATE DATABASE wordpress;
CREATE USER blog@'%' IDENTIFIED BY 'sheca';
GRANT ALL PRIVILEGES ON wordpress.* TO blog@'%';
flush privileges;
6、部署wordpress 应用
vim 07-deployment-wordpress.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: wordpress
name: wordpress
namespace: blog
spec:
replicas: 2
selector:
matchLabels:
app: wordpress
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
template:
metadata:
labels:
app: wordpress
spec:
containers:
- image: wordpress:6-apache
name: wordpress
env:
- name: WORDPRESS_DB_HOST
value: mysql-0.mysql
- name: WORDPRESS_DB_NAME
valueFrom:
secretKeyRef:
name: mysql-secret
key: wordpress.db
- name: WORDPRESS_DB_USER
valueFrom:
secretKeyRef:
name: mysql-secret
key: wordpress.username
- name: WORDPRESS_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secret
key: wordpress.password
volumeMounts:
- name: data
mountPath: /var/www/html/
volumes:
- name: data
persistentVolumeClaim:
claimName: wordpress-pvc
kubectl apply -f 07-deployment-wordpress.yaml
[root@k8s-master01 wordPress]# kubectl get pods -n blog
NAME READY STATUS RESTARTS AGE
wordpress-584df8f6bc-4lpdm 1/1 Running 0 2m8s
wordpress-584df8f6bc-m99qz 1/1 Running 0 2m8s
#192.168.40.52
[root@k8s-master01 wordPress]# kubectl get svc -n blog
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
wordpress LoadBalancer 10.99.201.27 192.168.40.52 80:31431/TCP 12m

浙公网安备 33010602011771号