环境准备:
ip | 服务 |
---|---|
192.168.2.10(server1) | docker、k8s |
192.168.2.20(server2) | docker、k8s |
192.168.2.30(server3) | docker、k8s |
k8s集群如何搭建
如果是刚开机的k8s集群的主机开机后启动服务(集群还是ready)
代码语言:javascript复制[root@server1 ~]# systemctl enable kubelet.service && systemctl start kubelet.service
[root@server1 ~]# systemctl stop firewalld && systemctl start docker.service
开始我把防火墙都关了不然端口太多
代码语言:javascript复制systemctl stop firewalld
server1:pull所需要的mysql镜像
代码语言:javascript复制[root@server1 ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
8559a31e96f4: Pull complete
d51ce1c2e575: Pull complete
c2344adc4858: Pull complete
fcf3ceff18fc: Pull complete
16da0c38dc5b: Pull complete
b905d1797e97: Pull complete
4b50d1c6b05c: Pull complete
d85174a87144: Pull complete
a4ad33703fa8: Pull complete
f7a5433ce20d: Pull complete
3dcd2a278b4a: Pull complete
Digest: sha256:32f9d9a069f7a735e28fd44ea944d53c61f990ba71460c5c183e610854ca4854
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
server1:pull所需要的nginx和php的整合镜像
代码语言:javascript复制[root@server1 ~]# docker pull richarvey/nginx-php-fpm
Using default tag: latest
latest: Pulling from richarvey/nginx-php-fpm
aad63a933944: Pulling fs layer
b61c449d5d91: Pulling fs layer
3fde16e1397a: Pulling fs layer
b1096698ab2a: Pulling fs layer
96de990b7ad3: Pulling fs layer
c280bfe25221: Pulling fs layer
02be9679a029: Pulling fs layer
01973f657634: Pulling fs layer
75924d0578e0: Pulling fs layer
7545938f30ed: Pull complete
267be130ac8a: Pull complete
5b9ce6473ee0: Pull complete
0cb267b5005b: Pull complete
0bbcede612f0: Pull complete
6f23dfb3d18c: Pull complete
363732275cc7: Pull complete
679aa7f9f360: Pull complete
d47507c4f094: Pull complete
17ba4fac2074: Pull complete
b16f52630a15: Pull complete
e8c7df728273: Pull complete
3b3bc6c69299: Pull complete
d0f1d1b1ad30: Pull complete
b8af2e53bd85: Pull complete
7e439df1bb98: Pull complete
f28361c951da: Pull complete
126a49c3e514: Pull complete
39838375a23b: Pull complete
1ca506cbf594: Pull complete
Digest: sha256:8b7c47b940fd79b5764ec12fdfbc7a1a198889316347963c9e9bd1aa78eec098
Status: Downloaded newer image for richarvey/nginx-php-fpm:latest
docker.io/richarvey/nginx-php-fpm:latest
NFS
server2下载nfs
代码语言:javascript复制[root@server1 ~]# yum -y install nfs-utils
[root@server2 ~]# mkdir -p /data/k8s/{web,db}
[root@server2 ~]# vim /etc/exports
[root@server2 ~]# cat /etc/exports
/data/k8s 192.168.2.0/24(rw,sync,no_root_squash)
[root@server2 ~]# exportfs -rv
exporting 192.168.2.0/24:/data/k8s
[root@server2 ~]# systemctl start nfs
[root@server2 ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
server1:
代码语言:javascript复制[root@server1 ~]# mkdir -p /home/k8s/lnmp/mysql
[root@server1 ~]# cd /home/k8s/lnmp/mysql
[root@server1 mysql]# kubectl create secret generic mysql-pass --from-literal=password=123.com
secret/mysql-pass created
[root@server1 mysql]# vim mysql-pv.yml
#内容:
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteMany
nfs:
path: /data/k8s/db
server: 192.168.2.20
[root@server1 mysql]# kubectl apply -f mysql-pv.yml
persistentvolume/mysql-pv created
[root@server1 mysql]# vim mysql-pvc.yml
#内容:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-claim
labels:
app: discuz
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 10Gi
[root@server1 mysql]# kubectl apply -f mysql-pvc.yml
persistentvolumeclaim/mysql-claim created
[root@server1 mysql]# vim mysql-dp.yml
#内容:
apiVersion: apps/v1
kind: Deployment
metadata:
name: dz-mysql
labels:
app: discuz
spec:
selector:
matchLabels:
app: discuz
tier: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: discuz
tier: mysql
spec:
imagePullSecrets:
- name: my-secret
containers:
- image: mysql:5.7
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password
ports:
- containerPort: 3306
name: dz-mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-claim
[root@server1 mysql]# kubectl apply -f mysql-dp.yml
deployment.apps/dz-mysql created
[root@server1 mysql]# vim mysql-svc.yml
#内容:
apiVersion: v1
kind: Service
metadata:
name: dz-mysql
labels:
app: discuz
spec:
ports:
- port: 3306
selector:
app: discuz
tier: mysql
[root@server1 mysql]# kubectl apply -f mysql-svc.yml
service/dz-mysql created
[root@server1 mysql]# kubectl get pv,pvc
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/mysql-pv 10Gi RWX Retain Bound default/mysql-claim 17m
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/mysql-claim Bound mysql-pv 10Gi RWX 8m6s
[root@server1 mysql]# vim web-pv.yml
#内容:
apiVersion: v1
kind: PersistentVolume
metadata:
name: web-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteMany
nfs:
path: /data/k8s/web
server: 192.168.2.20
[root@server1 mysql]# kubectl apply -f web-pv.yml
persistentvolume/web-pv created
[root@server1 mysql]# vim web-pvc.yml
#内容:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: web-claim
labels:
app: discuz
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 10Gi
[root@server1 mysql]# kubectl apply -f web-pvc.yml
persistentvolumeclaim/web-claim created
[root@server1 mysql]# vim web-dp.yml
#内容:
apiVersion: apps/v1
kind: Deployment
metadata:
name: dz-web
labels:
app: discuz
spec:
replicas: 1
selector:
matchLabels:
app: discuz
tier: nginx-php
template:
metadata:
labels:
app: discuz
tier: nginx-php
spec:
imagePullSecrets:
- name: my-secret
containers:
- image: richarvey/nginx-php-fpm
name: dz-web
ports:
- containerPort: 9000
- containerPort: 80
name: dz-web
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/www/html
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: web-claim
[root@server1 mysql]# kubectl apply -f web-dp.yml
deployment.apps/dz-web created
[root@server1 mysql]# vim web-svc.yml
#内容:
apiVersion: v1
kind: Service
metadata:
name: dz-web
labels:
app: discuz
spec:
type: NodePort
ports:
- port: 80
nodePort: 30001
selector:
app: discuz
tier: nginx-php
[root@server1 mysql]# kubectl apply -f web-svc.yml
service/dz-web created
#如果下一步操作显示0/1别着急先往下做(等3-5分钟即可)
[root@server1 mysql]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
dz-mysql-5dcd86b5c8-qgqbm 1/1 Running 0 22m 10.244.1.24 server2 <none> <none>
dz-web-68959dc478-879qr 1/1 Running 0 6m43s 10.244.2.28 server3 <none> <none>
[root@server1 mysql]# kubectl get pv,pvc
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/mysql-pv 10Gi RWX Retain Bound default/mysql-claim 33m
persistentvolume/web-pv 10Gi RWX Retain Bound default/web-claim 11m
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/mysql-claim Bound mysql-pv 10Gi RWX 23m
persistentvolumeclaim/web-claim Bound web-pv 10Gi RWX 5m22s
server2:
代码语言:javascript复制[root@server2 data]# git clone https://gitee.com/ComsenzDiscuz/DiscuzX.git
[root@server2 data]# mv DiscuzX/upload/* k8s/web/
[root@server2 data]# ls
DiscuzX k8s
[root@server2 data]# cd k8s/web
[root@server2 web]# ls
admin.php data m source
api favicon.ico member.php static
api.php forum.php misc.php template
archiver group.php plugin.php uc_client
config home.php portal.php uc_server
connect.php index.php robots.txt
crossdomain.xml install search.php
[root@server2 web]# chmod -R 777 /data/k8s/web/
server1:
代码语言:javascript复制[root@server1 mysql]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
dz-mysql-5dcd86b5c8-qgqbm 1/1 Running 0 40m 10.244.1.24 server2 <none> <none>
dz-web-68959dc478-879qr 1/1 Running 0 24m 10.244.2.28 server3 <none> <none>
[root@server1 mysql]# kubectl exec -it dz-mysql-5dcd86b5c8-qgqbm -- mysql -uroot -p123.com
mysql> create database hy;
Query OK, 1 row affected (0.00 sec)
mysql> grant all on hy.* to 'hy'@'%' identified by '123.com';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> exit
Bye
验证:IP:30001 下面的步骤跟着走就行了!