k8s部署Apollo

文章大纲:

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验证高可用
在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Apollo on k8s是指在Kubernetes(简称k8s)上部署和运行Apollo配置中心。Apollo是携程框架部门开发的一款开源配置管理平台,用于集中管理和动态配置应用程序的配置信息。Kubernetes是一个开源的容器编排平台,用于自动化部署、扩展和管理容器化应用程序。 将Apollo配置中心部署Kubernetes集群上可以带来以下好处: 1. 弹性伸缩:Kubernetes可以根据应用程序的负载情况自动调整Apollo实例的数量,以满足不同规模的应用需求。 2. 高可用性:Kubernetes提供了故障恢复和自动重启的机制,可以确保Apollo配置中心的高可用性。 3. 灵活性:Kubernetes支持多种部署策略,可以根据需要选择合适的部署方式,如单节点、多节点、分布式等。 4. 简化管理:Kubernetes提供了丰富的管理工具和API,可以方便地进行配置、监控和扩展等操作。 要在Kubernetes部署Apollo配置中心,通常需要进行以下步骤: 1. 创建Kubernetes集群:可以使用云服务提供商(如AWS、Azure、GCP)或自建集群。 2. 编写Apollo配置文件:根据应用程序的需求,编写Apollo的配置文件,包括应用信息、环境配置、数据库连接等。 3. 创建Kubernetes资源:使用Kubernetes的资源定义文件(如Deployment、Service、ConfigMap)创建Apollo的相关资源。 4. 部署Apollo配置中心:使用Kubernetes的命令或管理工具,将Apollo配置中心部署Kubernetes集群中。 5. 验证和测试:确保Apollo配置中心在Kubernetes上正常运行,并进行相关的验证和测试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值