kubernetes實戰-配置中心(二)交付apollo配置中心到k8s


apollo官網:官方地址

apollo架構圖:

 

 

 

apollo需要使用數據庫,這里使用mysql,注意版本需要在5.6以上:

本次環境mysql部署在10.4.7.11上,使用mariadb:10.1以上版本

# vi /etc/yum.repos.d/MariaDB.repo
[mariadb]
name = MariaDB
baseurl = https://mirrors.ustc.edu.cn/mariadb/yum/10.1/centos7-amd64/
gpgkey=https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1

導入key:

# rpm --import https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
# yum install MariaDB-server -y

簡單配置mysql:

# vi /etc/my.cnf.d/server.cnf
[mysqld]
character_set_server = utf8mb4
collation_server = utf8mb4_general_ci
init_connect = "SET NAMES 'utf8mb4'"
# vi /etc/my.cnf.d/mysql-clients.cnf
[mysql]
default-character-set = utf8mb4
# systemctl start mariadb
# systemctl enable mariadb
# mysqladmin -u root password 

登錄檢查字符集:

# mysql -uroot -p
> \s

 

 

 執行數據庫初始化腳本:configdb初始化腳本

下載腳本:

# wget https://raw.githubusercontent.com/ctripcorp/apollo/1.5.1/scripts/db/migration/configdb/V1.0.0__initialization.sql -O apolloconfig.sql

執行初始化腳本:

# mysql -uroot -p < apolloconfig.sql

檢查數據庫:

 

 

 給數據庫授權:

# grant INSERT,DELETE,UPDATE,SELECT on ApolloConfigDB.* to 'apolloconfig'@'10.4.7.%'  identified by "123456";

 

 

 修改初始化數據:

> update ServerConfig set Value='http://config.od.com/eureka' where Id=1;
也可以使用下面的sql:執行一個即可
 > update ApolloConfigDB.ServerConfig set ServerConfig.Value="http://config.od.com/eureka" where ServerConfig.Key="eureka.service.url";

添加config域名解析:

# vi /var/named/od.com.zone
# systemctl restart named

測試域名解析:

# dig  -t A config.od.com @10.4.7.11 +short

交付順序:

  1、apolloconfigservice

  2、adminservice

  3、portal

下載apolloconfigservice的包:放到200上,制作鏡像

# wget https://github.com/ctripcorp/apollo/releases/download/v1.5.1/apollo-configservice-1.5.1-github.zip
# mkdir /data/dockerfile/apollo-configservice
# unzip -o apollo-configservice-1.5.1-github.zip -d /data/dockerfile/apollo-configservice/
# cd /data/dockerfile/apollo-configservice/

修改連接數據庫配置:

# cd config
# vi application-github.properties

 

 

 添加mysql.od.com域名解析:

 

 

 修改啟動腳本:

# cd scripts/
# vi startup.sh

將官網上的startup.sh內容替換進來 腳本地址

 添加一行:

APOLLO_CONFIG_SERVICE_NAME=$(hostname -i)

自行優化JVM

添加執行權限:

# chmod u+x startup.sh

編寫dockerfile:  官方地址

# cd ..
# vi Dockerfile
FROM harbor.od.com/base/jre8:8u112

ENV VERSION 1.5.1

RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&\
    echo "Asia/Shanghai" > /etc/timezone

ADD apollo-configservice-${VERSION}.jar /apollo-configservice/apollo-configservice.jar
ADD config/ /apollo-configservice/config
ADD scripts/ /apollo-configservice/scripts

CMD ["/apollo-configservice/scripts/startup.sh"]
# docker build . -t harbor.od.com/infra/apollo-configservice:v1.5.1
# docker push harbor.od.com/infra/apollo-configservice:v1.5.1

編寫資源配置清單:

# cd /data/k8s-yaml/
# mkdir apollo-configservice
# cd apollo-configservice

1、cm.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: apollo-configservice-cm
  namespace: infra
data:
  application-github.properties: |
    # DataSource
    spring.datasource.url = jdbc:mysql://mysql.od.com:3306/ApolloConfigDB?characterEncoding=utf8
    spring.datasource.username = apolloconfig
    spring.datasource.password = 123456
    eureka.service.url = http://config.od.com/eureka
  app.properties: |
    appId=100003171

2、dp.yaml

kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: apollo-configservice
  namespace: infra
  labels: 
    name: apollo-configservice
spec:
  replicas: 1
  selector:
    matchLabels: 
      name: apollo-configservice
  template:
    metadata:
      labels: 
        app: apollo-configservice 
        name: apollo-configservice
    spec:
      volumes:
      - name: configmap-volume
        configMap:
          name: apollo-configservice-cm
      containers:
      - name: apollo-configservice
        image: harbor.od.com/infra/apollo-configservice:v1.5.1
        ports:
        - containerPort: 8080
          protocol: TCP
        volumeMounts:
        - name: configmap-volume
          mountPath: /apollo-configservice/config
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        imagePullPolicy: IfNotPresent
      imagePullSecrets:
      - name: harbor
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      securityContext: 
        runAsUser: 0
      schedulerName: default-scheduler
  strategy:
    type: RollingUpdate
    rollingUpdate: 
      maxUnavailable: 1
      maxSurge: 1
  revisionHistoryLimit: 7
  progressDeadlineSeconds: 600

3、svc.yaml

kind: Service
apiVersion: v1
metadata: 
  name: apollo-configservice
  namespace: infra
spec:
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 8080
  selector: 
    app: apollo-configservice

4、ingress.yaml

kind: Ingress
apiVersion: extensions/v1beta1
metadata: 
  name: apollo-configservice
  namespace: infra
spec:
  rules:
  - host: config.od.com
    http:
      paths:
      - path: /
        backend: 
          serviceName: apollo-configservice
          servicePort: 8080

應用資源配置清單:

# kubectl create -f http://k8s-yaml.od.com/apollo-configservice/cm.yaml
# kubectl create -f http://k8s-yaml.od.com/apollo-configservice/dp.yaml
# kubectl create -f http://k8s-yaml.od.com/apollo-configservice/svc.yaml
# kubectl create -f http://k8s-yaml.od.com/apollo-configservice/ingress.yaml

檢查啟動情況:

 

 

 

 

 

 

 

 

 需要等到eureka啟動以后才可以,接下來使用瀏覽器訪問config.od.com:

 

 

 

下載adminservice:官方地址 放在200上

# wget https://github.com/ctripcorp/apollo/releases/download/v1.5.1/apollo-adminservice-1.5.1-github.zip
# mkdir /data/dockerfile/apollo-adminservice
# unzip -o apollo-adminservice-1.5.1-github.zip -d /data/dockerfile/apollo-adminservice/
# cd /data/dockerfile/apollo-adminservice/

由於使用了configmap資源將配置文件掛載出來了,所以不在修改配置文件,如需修改配置文件,請參考部署apollo-configservice時候的修改方法:

修改startup.sh:  官方地址

將端口修改為:8080

添加以下一項配置:

APOLLO_ADMIN_SERVICE_NAME=$(hostname -i)

制作dockerfile:

# vi Dockerfile
FROM stanleyws/jre8:8u112

ENV VERSION 1.5.1

RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&\
    echo "Asia/Shanghai" > /etc/timezone

ADD apollo-adminservice-${VERSION}.jar /apollo-adminservice/apollo-adminservice.jar
ADD config/ /apollo-adminservice/config
ADD scripts/ /apollo-adminservice/scripts

CMD ["/apollo-adminservice/scripts/startup.sh"]

制作資源配置清單:

# mkdir /data/k8s-yaml/apollo-adminservice && cd /data/k8s-yaml/apollo-adminservice 

1、cm.yaml  

apiVersion: v1
kind: ConfigMap
metadata:
  name: apollo-adminservice-cm
  namespace: infra
data:
  application-github.properties: |
    # DataSource
    spring.datasource.url = jdbc:mysql://mysql.od.com:3306/ApolloConfigDB?characterEncoding=utf8
    spring.datasource.username = apolloconfig
    spring.datasource.password = 123456
    eureka.service.url = http://config.od.com/eureka
  app.properties: |
    appId=100003172

2、dp.yaml

kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: apollo-adminservice
  namespace: infra
  labels: 
    name: apollo-adminservice
spec:
  replicas: 1
  selector:
    matchLabels: 
      name: apollo-adminservice
  template:
    metadata:
      labels: 
        app: apollo-adminservice 
        name: apollo-adminservice
    spec:
      volumes:
      - name: configmap-volume
        configMap:
          name: apollo-adminservice-cm
      containers:
      - name: apollo-adminservice
        image: harbor.od.com/infra/apollo-adminservice:v1.5.1
        ports:
        - containerPort: 8080
          protocol: TCP
        volumeMounts:
        - name: configmap-volume
          mountPath: /apollo-adminservice/config
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        imagePullPolicy: IfNotPresent
      imagePullSecrets:
      - name: harbor
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      securityContext: 
        runAsUser: 0
      schedulerName: default-scheduler
  strategy:
    type: RollingUpdate
    rollingUpdate: 
      maxUnavailable: 1
      maxSurge: 1
  revisionHistoryLimit: 7
  progressDeadlineSeconds: 600

應用資源配置清單:

# kubectl create -f http://k8s-yaml.od.com/apollo-adminservice/cm.yaml
# kubectl create -f http://k8s-yaml.od.com/apollo-adminservice/dp.yaml

 

 

 通過日志檢查啟動情況:

 

 

 通過config.od.com檢查是否注冊到了eureka:

 

 

 已經順利的注冊到了注冊中心中。

 

接下來交付portal:官方地址  200上下載並制作鏡像

# wget https://github.com/ctripcorp/apollo/releases/download/v1.5.1/apollo-portal-1.5.1-github.zip
# mkdir /data/dockerfile/apollo-portal
# unzip -o apollo-portal-1.5.1-github.zip -d /data/dockerfile/apollo-portal/
# cd /data/dockerfile/apollo-portal/

由於portal使用的是另一個portaldb,我們需要在數據庫中新建portdb,並初始化:初始化腳本

在7-21上下載下來腳本:

# wget https://raw.githubusercontent.com/ctripcorp/apollo/1.5.1/scripts/db/migration/portaldb/V1.0.0__initialization.sql -O apollo-portal.sql
# mysql -uroot -p
]> source ./apollo-portal.sq

創建用戶並授權:

# grant INSERT,DELETE,UPDATE,SELECT on ApolloPortalDB.* to "apolloportal"@"10.4.7.%" identified by "123456";

修改數據庫:

創建部門:

]> update ServerConfig set Value='[{"orgId":"od01","orgName":"Linux學院"},{"orgId":"od02","orgName":"雲計算學院"},{"orgId":"od03","orgName":"Python學院"}]' where Id=2;

 

 由於使用concigmap資源,故之做介紹,不在這里修改:

配置portal meta serice:

這里列出的是支持的環境列表配置:

# vi /data/dockerfile/apollo-portal/config/apollo-env.properties

 

 修改startup.sh 官方地址

修改端口為8080

添加以下配置項:

APOLLO_PORTAL_SERVICE_NAME=$(hostname -i)

制作dockerfile:

# vi Dockerfile
FROM stanleyws/jre8:8u112

ENV VERSION 1.5.1

RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&\
    echo "Asia/Shanghai" > /etc/timezone

ADD apollo-portal-${VERSION}.jar /apollo-portal/apollo-portal.jar
ADD config/ /apollo-portal/config
ADD scripts/ /apollo-portal/scripts

CMD ["/apollo-portal/scripts/startup.sh"]
# docker build . -t harbor.od.com/infra/apollo-portal:v1.5.1
# docker push harbor.od.com/infra/apollo-portal:v1.5.1

編寫資源配置清單:

# mkdir /data/k8s-yaml/apollo-portal && cd /data/k8s-yaml/apollo-portal

1、cm.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: apollo-portal-cm
  namespace: infra
data:
  application-github.properties: |
    # DataSource
    spring.datasource.url = jdbc:mysql://mysql.od.com:3306/ApolloPortalDB?characterEncoding=utf8
    spring.datasource.username = apolloportal
    spring.datasource.password = 123456
  app.properties: |
    appId=100003173
  apollo-env.properties: |
    dev.meta=http://config.od.com

2、dp.yaml

kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: apollo-portal
  namespace: infra
  labels: 
    name: apollo-portal
spec:
  replicas: 1
  selector:
    matchLabels: 
      name: apollo-portal
  template:
    metadata:
      labels: 
        app: apollo-portal 
        name: apollo-portal
    spec:
      volumes:
      - name: configmap-volume
        configMap:
          name: apollo-portal-cm
      containers:
      - name: apollo-portal
        image: harbor.od.com/infra/apollo-portal:v1.5.1
        ports:
        - containerPort: 8080
          protocol: TCP
        volumeMounts:
        - name: configmap-volume
          mountPath: /apollo-portal/config
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        imagePullPolicy: IfNotPresent
      imagePullSecrets:
      - name: harbor
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      securityContext: 
        runAsUser: 0
      schedulerName: default-scheduler
  strategy:
    type: RollingUpdate
    rollingUpdate: 
      maxUnavailable: 1
      maxSurge: 1
  revisionHistoryLimit: 7
  progressDeadlineSeconds: 600

3、svc.yaml

kind: Service
apiVersion: v1
metadata: 
  name: apollo-portal
  namespace: infra
spec:
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 8080
  selector: 
    app: apollo-portal

4、ingress.yaml

kind: Ingress
apiVersion: extensions/v1beta1
metadata: 
  name: apollo-portal
  namespace: infra
spec:
  rules:
  - host: portal.od.com
    http:
      paths:
      - path: /
        backend: 
          serviceName: apollo-portal
          servicePort: 8080

應用資源配置清單:

# kubectl create -f http://k8s-yaml.od.com/apollo-portal/cm.yaml
# kubectl create -f http://k8s-yaml.od.com/apollo-portal/dp.yaml
# kubectl create -f http://k8s-yaml.od.com/apollo-portal/svc.yaml
# kubectl create -f http://k8s-yaml.od.com/apollo-portal/ingress.yaml

 

 添加域名解析:portal.od.com

 

 網頁訪問 portal.od.com

默認用戶名:apollo

默認密碼:   admin

 

 

到此,apollo的三個組件都已經交付到k8s里了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM