1. 程式人生 > 實用技巧 >kubernetes 環境安裝Nacos叢集

kubernetes 環境安裝Nacos叢集

kubernetes 環境安裝Nacos叢集

1.nfs伺服器配置

#所有k8s節點執行
yum install -y nfs-utils


mkdir -p /usr/local/nacos/nfs-share
mkdir -p /usr/local/nacos/mysql-slave
mkdir -p /usr/local/nacos/mysql-master

chmod a+rw /usr/local/nacos

#新增nacos、mysql-master 、mysql-slave 持久化資料夾
vi /etc/exports
/usr/local/nacos/mysql-master  *(insecure,rw,async,no_root_squash)
/usr/local/nacos/mysql-slave  *(insecure,rw,async,no_root_squash)
/usr/local/nacos/nfs-share  *(insecure,rw,async,no_root_squash)

systemctl restart rpcbind.service
systemctl restart nfs-server.service

showmount -e kipnode1

2.部署nfs

下載nacos k8s部署程式碼

git clone https://github.com/nacos-group/nacos-k8s.git

建立nacos 名稱空間

kubectl create namespace nacos

建立角色

cd nacos-k8s
NS=$(kubectl config get-contexts|grep -e "^\*" |awk '{print $5}')
NAMESPACE=${NS:-nacos}
sed -i'' "s/namespace:.*/namespace: $NAMESPACE/g" ./deploy/nfs/rbac.yaml
kubectl create -f deploy/nfs/rbac.yaml

建立 ServiceAccount 和部署 NFS-Client Provisioner

修改deployment.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-client-provisioner
---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: nfs-client-provisioner
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: nfs-client-provisioner
    spec:
      serviceAccount: nfs-client-provisioner
      containers:
      - name: nfs-client-provisioner
        image: quay.io/external_storage/nfs-client-provisioner:latest
        volumeMounts:
        - name: nfs-client-root
          mountPath: /persistentvolumes
        env:
        - name: PROVISIONER_NAME
          value: fuseim.pri/ifs
        - name: NFS_SERVER
          value: kipnode1
        - name: NFS_PATH
          value: /usr/local/nacos/nfs-share
      volumes:
      - name: nfs-client-root
        nfs:
          server: kipnode1
          path: /usr/local/nacos/nfs-share
kubectl create -f deploy/nfs/deployment.yaml -n nacos

建立 NFS StorageClass

kubectl create -f deploy/nfs/class.yaml -n nacos

驗證NFS部署成功

kubectl get pod -l app=nfs-client-provisioner -n nacos

3.部署資料庫

部署主庫

修改 mysql-master-nfs.yaml nfs位置

apiVersion: v1
kind: ReplicationController
metadata:
  name: mysql-master
  labels:
    name: mysql-master
spec:
  replicas: 1
  selector:
    name: mysql-master
  template:
    metadata:
      labels:
        name: mysql-master
    spec:
      containers:
      - name: master
        image: nacos/nacos-mysql-master:latest
        ports:
        - containerPort: 3306
        volumeMounts:
        - name: mysql-master-data
          mountPath: /var/lib/mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "root"
        - name: MYSQL_DATABASE
          value: "nacos_devtest"
        - name: MYSQL_USER
          value: "nacos"
        - name: MYSQL_PASSWORD
          value: "nacos"
        - name: MYSQL_REPLICATION_USER
          value: 'nacos_ru'
        - name: MYSQL_REPLICATION_PASSWORD
          value: 'nacos_ru'
      volumes:
      - name: mysql-master-data
        nfs:
          server: kipnode1
          path: /usr/local/nacos/mysql-master
---
apiVersion: v1
kind: Service
metadata:
  name: mysql-master
  labels:
    name: mysql-master
spec:
  ports:
  - port: 3306
    targetPort: 3306
  selector:
    name: mysql-master
kubectl create -f deploy/mysql/mysql-master-nfs.yaml -n nacos

部署從庫

修改 mysql-slave-nfs.yaml nfs位置

apiVersion: v1
kind: ReplicationController
metadata:
  name: mysql-slave
  labels:
    name: mysql-slave
spec:
  replicas: 1
  selector:
    name: mysql-slave
  template:
    metadata:
      labels:
        name: mysql-slave
    spec:
      containers:
      - name: slave
        image: nacos/nacos-mysql-slave:latest
        ports:
        - containerPort: 3306
        volumeMounts:
        - name: mysql-slave-data
          mountPath: /var/lib/mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "root"
        - name: MYSQL_REPLICATION_USER
          value: 'nacos_ru'
        - name: MYSQL_REPLICATION_PASSWORD
          value: 'nacos_ru'
      volumes:
      - name: mysql-slave-data
        nfs:
          server: kipnode1
          path: /usr/local/nacos/mysql-slave
---
apiVersion: v1
kind: Service
metadata:
  name: mysql-slave
  labels:
    name: mysql-slave
spec:
  ports:
  - port: 3306
    targetPort: 3306
  selector:
    name: mysql-slave
kubectl create -f deploy/mysql/mysql-slave-nfs.yaml -n nacos

4.部署Nacos

修改deploy/nacos/nacos-pvc-nfs.yaml 和資料庫配置檔案保持一致即可

data:
  mysql.master.db.name: "主庫名稱"
  mysql.master.port: "主庫埠"
  mysql.slave.port: "從庫埠"
  mysql.master.user: "主庫使用者名稱"
  mysql.master.password: "主庫密碼"
kubectl create -f deploy/nacos/nacos-pvc-nfs.yaml -n nacos

使用kubectl scale 對Nacos動態擴容

kubectl scale sts nacos --replicas=3 -n nacos

5.建立ingress

vi ingress/nacos-ingress.yaml

---
# 建立Ingress,定義訪問規則 
apiVersion: extensions/v1beta1 
kind: Ingress
metadata:
  name: nacos-headless
spec: 
 rules:
  - host: demo.nacos.com
    http:
      paths:
      - path: /
        backend:
          serviceName: nacos-headless
          servicePort: 8848 
kubectl create -f nacos-ingress.yaml -n nacos

nacos後臺管理訪問地址:

http://demo.nacos.com/nacos

本文由部落格群發一文多發等運營工具平臺 OpenWrite 釋出