k8s项目部署wordpress
四台虚拟机。
·
1 .项目部署
四台虚拟机
192.168.1.100 master
192.168.1.120 node01
192.168.1.111 node02
192.168.1.121 nfs
2.先做nfs(192.168.1.121)
[root@servera yum.repos.d]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
Device does not contain a recognized partition table
使用磁盘标识符 0x678eeead 创建新的 DOS 磁盘标签。
命令(输入 m 获取帮助):n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): e
分区号 (1-4,默认 1):
起始 扇区 (2048-20971519,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-20971519,默认为 20971519):
将使用默认值 20971519
分区 1 已设置为 Extended 类型,大小设为 10 GiB
命令(输入 m 获取帮助):n
Partition type:
p primary (0 primary, 1 extended, 3 free)
l logical (numbered from 5)
Select (default p): l
添加逻辑分区 5
起始 扇区 (4096-20971519,默认为 4096):
将使用默认值 4096
Last 扇区, +扇区 or +size{K,M,G} (4096-20971519,默认为 20971519):+2G
分区 5 已设置为 Linux 类型,大小设为 2 GiB
命令(输入 m 获取帮助):n
Partition type:
p primary (0 primary, 1 extended, 3 free)
l logical (numbered from 5)
Select (default p): l
添加逻辑分区 6
起始 扇区 (4200448-20971519,默认为 4200448):+2G
值超出范围。
起始 扇区 (4200448-20971519,默认为 4200448):
将使用默认值 4200448
Last 扇区, +扇区 or +size{K,M,G} (4200448-20971519,默认为 20971519):+2G
分区 6 已设置为 Linux 类型,大小设为 2 GiB
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@servera yum.repos.d]# partprobe /dev/sdb
[root@servera yum.repos.d]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos-root 253:0 0 18G 0 lvm /
└─centos-swap 253:1 0 1G 0 lvm [SWAP]
sdb 8:16 0 10G 0 disk
├─sdb1 8:17 0 1K 0 part
├─sdb5 8:21 0 2G 0 part
└─sdb6 8:22 0 2G 0 part
sr0 11:0 1 4.4G 0 rom /mnt/cdrom
[root@servera yum.repos.d]# mkdir -p /blog/master
[root@servera yum.repos.d]# mkdir -p /blog/slave
[root@servera yum.repos.d]# vim /etc/fstab
[root@servera yum.repos.d]# vim /etc/fstab
/dev/sdb5 /blog/master xfs defaults 0 0
/dev/sdb6 /blog/slave xfs defaults 0 0
[root@servera yum.repos.d]# mkfs -t xfs /dev/sdb5
meta-data=/dev/sdb5 isize=512 agcount=4, agsize=131072 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=524288, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@servera yum.repos.d]# mkfs -t xfs /dev/sdb6
meta-data=/dev/sdb6 isize=512 agcount=4, agsize=131072 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=524288, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@servera yum.repos.d]# mount -a
[root@servera yum.repos.d]# df -Th
文件系统 类型 容量 已用 可用 已用% 挂载点
devtmpfs devtmpfs 894M 0 894M 0% /dev
tmpfs tmpfs 910M 0 910M 0% /dev/shm
tmpfs tmpfs 910M 11M 900M 2% /run
tmpfs tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 18G 5.4G 13G 30% /
/dev/sr0 iso9660 4.4G 4.4G 0 100% /mnt/cdrom
/dev/sda1 xfs 1014M 187M 828M 19% /boot
tmpfs tmpfs 182M 12K 182M 1% /run/user/42
tmpfs tmpfs 182M 0 182M 0% /run/user/0
/dev/sdb5 xfs 2.0G 33M 2.0G 2% /blog/master
/dev/sdb6 xfs 2.0G 33M 2.0G 2% /blog/slave
[root@servera yum.repos.d]# vim /etc/exports
/blog/master *(rw,no_root_squash)
/blog/slave *(rw,no_root_squash)
[root@servera yum.repos.d]# exports -rav
bash: exports: 未找到命令...
[root@servera yum.repos.d]# exportfs -rav
exporting *:/blog/slave
exporting *:/blog/master
3.总体配置(master)
1.命名空间
[root@k8s-master opt]# ls
apr-1.5.2 apr-util-1.5.4 cni httpd-2.4.12
apr-1.5.2.tar.gz apr-util-1.5.4.tar.gz containerd httpd-2.4.12.ta
[root@k8s-master opt]# mkdir wordpress
[root@k8s-master opt]# cd wordpress/
[root@k8s-master wordpress]# ls
[root@k8s-master wordpress]#
[root@k8s-master wordpress]#
[root@k8s-master wordpress]# vim blog-namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
name: blog
2.主库的pvc
[root@k8s-master wordpress]# kubectl create -f blog-namespace.yaml
namespace/blog created
[root@k8s-master wordpress]# vim blog-master-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: blog-master-pv
namespace: blog
spec:
capacity:
storage: 2G
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Recycle
nfs:
server: "192.168.1.121"
path: "/blog/master"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: blog-master-pvc
namespace: blog
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 2G
3.主库的配置
vim blog-master-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: blog-master-config
namespace: blog
data:
master_my.cnf: |
[mysqld]
server_id=10
log_bin=master
[root@k8s-master wordpress]# kubectl create -f blog-master-pv.yaml
persistentvolume/blog-master-pv created
persistentvolumeclaim/blog-master-pvc created
[root@k8s-master wordpress]# kubectl create -f blog-master-config.yaml
configmap/blog-master-config created
[root@k8s-master wordpress]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS VOLUMEATTRIBUTESCLASS REASON AGE
blog-master-pv 2G RWX Recycle Bound blog/blog-master-pvc <unset> 21s
[root@k8s-master wordpress]# kubectl get cm -n blog
NAME DATA AGE
blog-master-config 1 24s
kube-root-ca.crt 1 28m
[root@k8s-master wordpress]# vim blog-master-db.yaml
4.主库的pod
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: blog-master-db
namespace: blog
spec:
replicas: 1
selector:
matchLabels:
app: blog-master-db
serviceName: "blog-master-db"
template:
metadata:
labels:
app: blog-master-db
spec:
containers:
- name: blog-master-db
image: mysql:5.7
imagePullPolicy: IfNotPresent
env:
- name: MYSQL_ROOT_PASSWORD
value: redhat
volumeMounts:
- name: mysql-data-volume
mountPath: /var/lib/mysql
- name: mysql-config-volume
mountPath: "/etc/mysql/conf.d/"
readOnly: true
volumes:
- name: mysql-data-volume
persistentVolumeClaim:
claimName: blog-master-pvc
- name: mysql-config-volume
configMap:
name: blog-master-config
---
apiVersion: v1
kind: Service
metadata:
name: blog-master-db
namespace: blog
spec:
ports:
- port: 3306
selector:
app: blog-master-db
5.进入主库,为主从做准备
[root@k8s-master wordpress]# kubectl create -f blog-master-db.yaml
statefulset.apps/blog-master-db created
service/blog-master-db created
[root@k8s-master wordpress]# kubectl get pod -n blog
NAME READY STATUS RESTARTS AGE
blog-master-db-0 1/1 Running 0 13s
[root@k8s-master wordpress]# kubectl exec -ti blog-master-db-0 bash -n blog
root@blog-master-db-0:/# mysql -uroot -predhat
mysql> show master status;
+---------------+----------+--------------+------------------+------ -------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Execu ted_Gtid_Set |
+---------------+----------+--------------+------------------+------ -------------+
| master.000003 | 154 | | | |
+---------------+----------+--------------+------------------+------ -------------+
1 row in set (0.00 sec)
mysql> create database blog charset utf8;
Query OK, 1 row affected (0.01 sec)
mysql> grant all on blog.* to 'buser'@'%' identified by 'redhat';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> grant replication slave on *.* to 'repluser'@'%' identified b y 'redhat';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
[root@k8s-master wordpress]# cp blog-master-pv.yaml blog-slave-pv.yaml
[root@k8s-master wordpress]# vim blog-slave-pv.yaml
6.为从库做pvc
apiVersion: v1
kind: PersistentVolume
metadata:
name: blog-slave-pv
namespace: blog
spec:
capacity:
storage: 2G
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Recycle
nfs:
server: "192.168.1.121"
path: "/blog/slave"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: blog-slave-pvc
namespace: blog
spec:
accessModes:
- ReadWriteMany
- resources:
requests:
storage: 2G
[root@k8s-master wordpress]# kubectl create -f blog-slave-pv.yaml
persistentvolume/blog-slave-pv created
persistentvolumeclaim/blog-slave-pvc created
[root@k8s-master wordpress]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS VOLUMEATTRIBUTESCLASS REASON AGE
blog-master-pv 2G RWX Recycle Bound blog/blog-master-pvc <unset> 76m
blog-slave-pv 2G RWX Recycle Bound blog/blog-slave-pvc <unset> 8s
[root@k8s-master wordpress]# cp blog-master-config.yaml blog-slave-config.yaml
[root@k8s-master wordpress]# vim blog-slave-config.yaml
7. 为从库做配置
apiVersion: v1
kind: ConfigMap
metadata:
name: blog-slave-config
namespace: blog
data:
slave_my.cnf: |
[mysqld]
server_id=11
log_bin=slave
[root@k8s-master wordpress]# kubectl create -f blog-slave-config.yaml
configmap/blog-slave-config created
[root@k8s-master wordpress]# kubectl get cm -n blog
NAME DATA AGE
blog-master-config 1 80m
blog-slave-config 1 14s
kube-root-ca.crt 1 109m
[root@k8s-master wordpress]# kubectl get cm -n blog
NAME DATA AGE
blog-master-config 1 80m
blog-slave-config 1 14s
kube-root-ca.crt 1 109m
[root@k8s-master wordpress]# cp blog-master-db.yaml blog-slave-db.yaml
[root@k8s-master wordpress]# vim blog-slave-db.yaml
8.为从库做pod
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: blog-slave-db
namespace: blog
spec:
replicas: 1
selector:
matchLabels:
app: blog-slave-db
serviceName: "blog-slave-db"
template:
metadata:
labels:
app: blog-slave-db
spec:
containers:
- name: blog-slave-db
image: mysql:5.7
imagePullPolicy: IfNotPresent
env:
- name: MYSQL_ROOT_PASSWORD
value: redhat
volumeMounts:
- name: mysql-data-volume
mountPath: /var/lib/mysql
- name: mysql-config-volume
mountPath: "/etc/mysql/conf.d/"
readOnly: true
volumes:
- name: mysql-data-volume
persistentVolumeClaim:
claimName: blog-slave-pvc
- name: mysql-config-volume
configMap:
name: blog-slave-config
---
apiVersion: v1
kind: Service
metadata:
name: blog-slave-db
namespace: blog
spec:
ports:
- port: 3306
selector:
app: blog-slave-db
9.进入从库,主从复制
[root@k8s-master wordpress]# kubectl create -f blog-slave-db.yaml
statefulset.apps/blog-slave-db created
service/blog-slave-db created
[root@k8s-master wordpress]# kubectl get pod -n blog NAME READY STATUS RESTARTS AGE
blog-master-db-0 1/1 Running 0 21m
blog-slave-db-0 1/1 Running 0 12s
[root@k8s-master wordpress]# kubectl exec -ti blog-slave-db-0 bash -n blog
root@blog-slave-db-0:/# mysql -uroot -predhat
mysql> CHANGE MASTER TO
-> MASTER_HOST="blog-master-db",
-> MASTER_USER="repluser",
-> MASTER_PASSWORD="redhat",
-> MASTER_LOG_FILE="master.000003",
-> MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: blog-master-db
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master.000003
Read_Master_Log_Pos: 1056
Relay_Log_File: blog-slave-db-0-relay-bin.000002
Relay_Log_Pos: 1219
Relay_Master_Log_File: master.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1056
Relay_Log_Space: 1436
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 10
Master_UUID: 27d1b57b-4da7-11ef-b12d-4eb06cca4568
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
[root@k8s-master wordpress]# vim blog.yaml
10.导入wordpress镜像顺便做pod
apiVersion: apps/v1
kind: Deployment
metadata:
name: blog
namespace: blog
spec:
replicas: 1
selector:
matchLabels:
app: blog
template:
metadata:
labels:
app: blog
spec:
containers:
- name: blog
image: wordpress
imagePullPolicy: IfNotPresent
env:
- name: WORDPRESS_DB_HOST
value: blog-master-db
- name: WORDPRESS_DB_USER
value: buser
- name: WORDPRESS_DB_PASSWORD
value: redhat
- name: WORDPRESS_DB_NAME
value: blog
- name: WORDPRESS_TABLE_PREFIX
value: wp_
---
apiVersion: v1
kind: Service
metadata:
name: blog
spec:
ports:
- port: 80
selector:
app: blog
[root@k8s-master wordpress]# kubectl create -f blog.yaml
deployment.apps/blog created
service/blog created
[root@k8s-master wordpress]# kubectl get pod -n blog
NAME READY STATUS RESTARTS AGE
blog-586b8b59c5-hcr65 1/1 Running 0 12s
blog-master-db-0 1/1 Running 0 45m
blog-slave-db-0 1/1 Running 0 24m
[root@k8s-master wordpress]# kubectl get svc -n blog
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
blog-master-db ClusterIP 10.96.98.77 <none> 3306/TCP 45m
blog-slave-db ClusterIP 10.96.83.109 <none> 3306/TCP 24m
[root@k8s-master wordpress]# vim blog-ingress.yaml
11.ingress配置
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: blog-ingress
namespace: blog
spec:
ingressClassName: nginx
rules:
- host: blog.linux.com
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: blog
port:
number: 80
[root@k8s-master wordpress]# kubectl create -f blog-ingress.yaml
ingress.networking.k8s.io/blog-ingress created
[root@k8s-master wordpress]# kubectl get ingress -n blog
NAME CLASS HOSTS ADDRESS PORTS AGE
blog-ingress nginx blog.linux.com 80 19s
更多推荐
已为社区贡献1条内容
所有评论(0)