文章大纲:
1、搭建nfs存储
2、创建PV、PVC
3、确认pv及pvc的状态
4、k8s集群内安装MySQL5.7并使数据持久化
5、创建Apollo
6、访问Apollo
7、手动删除MySQL容器,验证sql数据是否还在以及Apollo服务能否正常运行
1.搭建nfs存储
[root@master ~]# yum -y install nfs-utils
[root@master ~]# systemctl enable rpcbind
[root@master ~]# mkdir -p /nfs/data
[root@master ~]# vim /etc/exports
/nfs/data *(rw,sync,no_root_squash)
[root@master ~]# systemctl start nfs-server
[root@master ~]# systemctl enable nfs-server
[root@master ~]# showmount -e
Export list for master:
/nfs/data *
把需要导入Apollo库的sql提前准备好,存放到共享目录/nfs/data/下
2.创建PV、PVC
apiVersion: v1
kind: PersistentVolume
metadata:
name: model
namespace: public-service
labels:
pv: model1
spec:
storageClassName: ml
accessModes:
- ReadWriteOnce
capacity:
storage: 5Gi
persistentVolumeReclaimPolicy: Retain
nfs:
path: /nfs/data
server: 192.168.3.81
#volumeMode: Filesystem
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: model
namespace: public-service
spec:
storageClassName: ml
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
selector:
matchLabels:
pv: model1
3.确认PV及PVC状态
[root@k8s-master yml]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
model 5Gi RWO Retain Bound public-service/model ml 19h
[root@k8s-master yml]# kubectl get pvc
No resources found in default namespace.
[root@k8s-master yml]# kubectl get pvc -n public-service
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
model Bound model 5Gi RWO ml 19h
[root@k8s-master yml]#
4.k8s集群内安装MySQL5.7并使数据持久化
#apiVersion: v1
#kind: ConfigMap
#metadata:
# name: model-db-config
# namespace: public-service
# labels:
# app: model-db
#data:
# my.cnf: |-
# [client]
# default-character-set=utf8mb4
# [mysql]
# default-character-set=utf8mb4
# [mysqld]
# max_connections = 2000
# secure_file_priv=/var/lib/mysql
# sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
namespace: public-service
spec:
selector:
matchLabels:
app: mysql
replicas: 1
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env: #以下是设置MySQL数据库的密码
- name: MYSQL_ROOT_PASSWORD
value: WQF1314wqf@
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql #MySQL容器的数据都是存在这个目录的,要对这个目录做数据持久化
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: model
---
apiVersion: v1
kind: Service
metadata:
name: mysql
namespace: public-service
spec:
type: NodePort
ports:
- port: 3306
targetPort: 3306
nodePort: 31306
selector:
app: mysql
[root@k8s-master yml]# kubectl get pod -n public-service
NAME READY STATUS RESTARTS AGE
mysql-64c9f78cfd-bjhml 1/1 Running 0 17h
导入sql
修改Apolloconfig.sql的eureka地址为http://192.168.3.81:8080/eureka/
进入MySQL容器
mysql -uroot -p < apolloconfigdb.sql
mysql -uroot -p < apolloportaldb.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 873
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, 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 |
| ApolloConfigDB |
| ApolloPortalDB |
| mysql |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.01 sec)
5.创建Apollo.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: apollo-configservice
namespace: public-service
data:
application-github.properties: |
spring.datasource.url = jdbc:mysql://192.168.3.81:31306/ApolloConfigDB?characterEncoding=utf8
spring.datasource.username = root
spring.datasource.password = WQF1314wqf@
apollo.config-service.url = http://apollo-configservice.public-service:8080
apollo.admin-service.url = http://apollo-adminservice.public-service:8090
---
apiVersion: v1
kind: ConfigMap
metadata:
name: apollo-adminservice
namespace: public-service
data:
application-github.properties: |
spring.datasource.url = jdbc:mysql://192.168.3.81:31306/ApolloConfigDB?characterEncoding=utf8
spring.datasource.username = root
spring.datasource.password = WQF1314wqf@
---
apiVersion: v1
kind: ConfigMap
metadata:
name: apollo-portal
namespace: public-service
data:
application-github.properties: |
spring.datasource.url = jdbc:mysql://192.168.3.81:31306/ApolloPortalDB?characterEncoding=utf8
spring.datasource.username = root
spring.datasource.password = WQF1314wqf@
apollo.portal.envs = dev
apollo-env.properties: |
dev.meta = http://apollo-configservice:8080
---
apiVersion: v1
kind: Service
metadata:
name: apollo-configservice
namespace: public-service
labels:
app: apollo-configservice
spec:
type: NodePort
selector:
app: apollo-configservice
ports:
- name: http
protocol: TCP
port: 8080
targetPort: 8080
nodePort: 30080
---
apiVersion: v1
kind: Service
metadata:
name: apollo-adminservice
namespace: public-service
labels:
app: apollo-adminservice
spec:
type: NodePort
selector:
app: apollo-adminservice
ports:
- name: http
protocol: TCP
port: 8090
targetPort: 8090
nodePort: 30090
---
#apiVersion: extensions/v1beta1
#kind: Ingress
#metadata:
# name: apollo-portal
# namespace: public-service
# labels:
# app: apollo-portal
#spec:
# rules:
# - host: apollo.wqf.com
# http:
# paths:
# - path: /
# backend:
# serviceName: apollo-portal
# servicePort: 8070
#
---
apiVersion: v1
kind: Service
metadata:
name: apollo-portal
namespace: public-service
labels:
app: apollo-portal
spec:
type: NodePort
selector:
app: apollo-portal
sessionAffinity: ClientIP
ports:
- name: http
protocol: TCP
port: 8070
targetPort: 8070
nodePort: 30070
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: apollo-configservice
namespace: public-service
labels:
app: apollo-configservice
spec:
replicas: 1
selector:
matchLabels:
app: apollo-configservice
template:
metadata:
labels:
app: apollo-configservice
spec:
containers:
- name: apollo-configservice
image: apolloconfig/apollo-configservice:latest
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 8080
protocol: TCP
env:
- name: SPRING_PROFILES_ACTIVE
value: "github,kubernetes"
#resources:
# limits:
# cpu: "1000m"
# memory: "1024Mi"
# requests:
# cpu: "1000m"
# memory: "1024Mi"
volumeMounts:
- name: apollo-configservice-config
mountPath: /apollo-configservice/config/application-github.properties
subPath: application-github.properties
volumes:
- name: apollo-configservice-config
configMap:
name: apollo-configservice
items:
- key: application-github.properties
path: application-github.properties
defaultMode: 420
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: apollo-adminservice
namespace: public-service
labels:
app: apollo-adminservice
spec:
replicas: 1
selector:
matchLabels:
app: apollo-adminservice
template:
metadata:
labels:
app: apollo-adminservice
spec:
containers:
- name: apollo-adminservice
image: apolloconfig/apollo-adminservice:latest
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 8090
protocol: TCP
env:
- name: SPRING_PROFILES_ACTIVE
value: "github,kubernetes"
#resources:
# limits:
# cpu: "1000m"
# memory: "1024Mi"
# requests:
# cpu: "1000m"
# memory: "1024Mi"
volumeMounts:
- name: apollo-adminservice-config
mountPath: /apollo-adminservice/config/application-github.properties
subPath: application-github.properties
volumes:
- name: apollo-adminservice-config
configMap:
name: apollo-adminservice
items:
- key: application-github.properties
path: application-github.properties
defaultMode: 420
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: apollo-portal
namespace: public-service
labels:
app: apollo-portal
spec:
replicas: 1
selector:
matchLabels:
app: apollo-portal
template:
metadata:
labels:
app: apollo-portal
spec:
containers:
- name: apollo-portal
image: apolloconfig/apollo-portal:latest
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 8070
protocol: TCP
env:
- name: SPRING_PROFILES_ACTIVE
value: "github,auth"
resources:
limits:
cpu: "1000m"
memory: "1024Mi"
requests:
cpu: "1000m"
memory: "1024Mi"
volumeMounts:
- name: apollo-portal-config
mountPath: /apollo-portal/config/application-github.properties
subPath: application-github.properties
- name: apollo-portal-config
mountPath: /apollo-portal/config/apollo-env.properties
subPath: apollo-env.properties
volumes:
- name: apollo-portal-config
configMap:
name: apollo-portal
items:
- key: application-github.properties
path: application-github.properties
- key: apollo-env.properties
path: apollo-env.properties
defaultMode: 420
[root@k8s-master yml]# kubectl apply -f apollo.yaml
configmap/apollo-configservice unchanged
configmap/apollo-adminservice unchanged
configmap/apollo-portal unchanged
service/apollo-configservice unchanged
service/apollo-adminservice unchanged
service/apollo-portal unchanged
deployment.apps/apollo-configservice unchanged
deployment.apps/apollo-adminservice unchanged
deployment.apps/apollo-portal configured
[root@k8s-master yml]#
确认Apollo状态
[root@k8s-master yml]# kubectl get pod -n public-service
NAME READY STATUS RESTARTS AGE
apollo-adminservice-749d849b94-q9sds 1/1 Running 0 3h9m
apollo-configservice-b6998cb9d-cl2hj 1/1 Running 0 3h9m
apollo-portal-766c576c89-nhgbk 1/1 Running 0 3h9m
mysql-64c9f78cfd-bjhml 1/1 Running 0 17h
[root@k8s-master yml]# kubectl get svc -n public-service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
apollo-adminservice NodePort 10.109.3.146 <none> 8090:30090/TCP 3h10m
apollo-configservice NodePort 10.109.131.180 <none> 8080:30080/TCP 3h10m
apollo-portal NodePort 10.104.83.28 <none> 8070:30070/TCP 3h10m
mysql NodePort 10.108.104.65 <none> 3306:31306/TCP 18h
6.访问Apollo
192.168.3.81:30070
7、手动删除MySQL容器,验证sql数据是否还在以及Apollo服务能否正常运行
[root@k8s-master yml]# kubectl delete -f mysql.yaml
deployment.apps "mysql" deleted
service "mysql" deleted
[root@k8s-master yml]# kubectl apply -f mysql.yaml
deployment.apps/mysql created
service/mysql created
[root@k8s-master yml]# kubectl get pod -n public-service -w
NAME READY STATUS RESTARTS AGE
apollo-adminservice-749d849b94-q9sds 1/1 Running 0 3h27m
apollo-configservice-b6998cb9d-cl2hj 1/1 Running 0 3h27m
apollo-portal-766c576c89-nhgbk 1/1 Running 0 3h27m
mysql-64c9f78cfd-dbbpf 0/1 ContainerCreating 0 32s
mysql-64c9f78cfd-dbbpf 1/1 Running 0 38s
^C[root@k8s-master yml]# ^C
[root@k8s-master yml]# kubectl get pod -n public-service
NAME READY STATUS RESTARTS AGE
apollo-adminservice-749d849b94-q9sds 1/1 Running 0 3h27m
apollo-configservice-b6998cb9d-cl2hj 1/1 Running 0 3h27m
apollo-portal-766c576c89-nhgbk 1/1 Running 0 3h27m
mysql-64c9f78cfd-dbbpf 1/1 Running 0 45s
[root@k8s-master yml]#
再次访问Apollo验证高可用