K8S部署Nacos配置中心


部署PVC

nacos-pvc.yaml文件内容如下

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name:  nacos-pvc
  namespace: devops #非业务类统一资源对象统一放在 devops命名空间下
  annotations:
    volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 30Gi

部署PVC

# kubectl  apply -f  nacos-pvc.yaml 
persistentvolumeclaim/nacos-pvc created

验证PVC

# kubectl  get pvc -n devops
NAME        STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS          AGE

 准备nacos需要的配置文件ConfigMap

 文件custom.properties内容如下

server.servlet.contextPath=/nacos
server.port=8848
db.num=1
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=nacos123
management.metrics.export.elastic.enabled=false
management.metrics.export.influx.enabled=false
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D %{User-Agent}i
server.tomcat.basedir=
nacos.security.ignore.urls=/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/**
nacos.core.auth.system.type=nacos
nacos.core.auth.enabled=true
nacos.core.auth.default.token.expire.seconds=18000
nacos.core.auth.default.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789
nacos.core.auth.caching.enabled=false
nacos.istio.mcp.server.enabled=false

 部署nacos-custom.properties

# kubectl create configmap nacos-custom.properties --from-file=./custom.properties -n devops
configmap/nacos-custom.properties created

 查看cm

# kubectl  get cm -n devops
NAME                      DATA   AGE
nacos-custom.properties   1      81s

 部署nacos-deployment

文件nacos-deployment.yaml文件内容如下

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nacos-deployment
  namespace: devops
spec:
  replicas: 1
  template:
    metadata:
      labels:
        service: nacos
        app: nacos
    spec:
      containers:
      #nacos-mysql
      - image: nacos/nacos-mysql:5.7
        imagePullPolicy: Always
        name: nacos-mysql
        env:
        - name: MYSQL_USER
          value: "root"
        - name: MYSQL_DATABASE
          value: "nacos"
        - name: MYSQL_ROOT_PASSWORD
          value: "nacos123"
        volumeMounts:
        - mountPath: /var/lib/mysql
          name: nacos-pv
          subPath: mysql
        ports:
        - containerPort: 3306
          name: "mysql-port"
      #nacos
      - image: nacos/nacos-server:latest
        imagePullPolicy: Always
        name: nacos-server
        env:
        - name: "PREFER_HOST_MODE"
          value: "hostname"
        - name: "MODE"
          value: "standalone"
        - name: "SPRING_DATASOURCE_PLATFORM"
          value: "mysql"
        - name: "MYSQL_SERVICE_HOST"
          value: "mysql"
        - name: "MYSQL_SERVICE_DB_NAME"
          value: "nacos_devtest"
        - name: "MYSQL_SERVICE_PORT"
          value: "3306"
        - name: "MYSQL_SERVICE_USER"
          value: "root"
        - name: "MYSQL_SERVICE_PASSWORD"
          value: "nacos123"
        ports:
        - containerPort: 8848
          name: "nacos-port"
        volumeMounts:
        - mountPath: /home/nacos/logs
          name: nacos-pv
          subPath: logs
        - name: nacos-config
          mountPath: /home/nacos/init.d/custom.properties
          subPath: custom.properties
        - mountPath: /etc/localtime
          name: nacostime
      volumes:
      - name: nacos-pv
        persistentVolumeClaim:
          claimName: nacos-pvc
      - name: nacos-config
        configMap:
          name: nacos-custom.properties
      - name: nacostime
        hostPath:
          path: /etc/localtime

部署nacos-deployment

# kubectl apply -f  nacos-deployment.yaml 
deployment.extensions/nacos-deployment created

查看nacos-deployment

需要等待一段时间,进行查看

# kubectl  get pod -n devops
NAME                                READY   STATUS              RESTARTS   AGE
nacos-deployment-57c8746979-qfk2x   0/2     ContainerCreating   0          54s

等待2分钟

# kubectl  get pod -n devops 
NAME                                READY   STATUS    RESTARTS   AGE
nacos-deployment-57c8746979-wl5x6   2/2     Running   0          2m33s

如果出现问题请用以下命令进行配错

#kubectl  describe  pod/nacos-deployment-57c8746979-wl5x6 -n devops

部署Nacos-svc

文件nacos-svc.yaml内容如下

apiVersion: v1
kind: Service
metadata:
  name: nacos-service
  namespace: devops
spec:
  type: NodePort
  ports:
    - port: 3306
      targetPort: 3306
      nodePort: 31255
      name: "nacos-mysql-port"
    - port: 8848
      targetPort: 8848
      nodePort: 31256
      name: "nacos-port"
  selector:
    app: nacos

部署nacos-svc

# kubectl apply -f nacos-svc.yaml 
service/nacos-service created

 

部署nacos-ingrss

文件nacos-ingress.yaml内容如下

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/proxy-body-size: "1024m"
    nginx.ingress.kubernetes.io/proxy-read-timeout: "600"
    nginx.ingress.kubernetes.io/proxy-send-timeout: "600"
  name: nacos
  namespace: devops
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: nacos.xxxx.cn
    http:
      paths:
      - backend:
          serviceName: nacos-service
          servicePort: 8848

部署nacos-ingress

# kubectl apply -f nacos-ingress.yaml 
ingress.extensions/nacos created

查看nacos-ingress

# kubectl get ingress -n devops
NAME    HOSTS                 ADDRESS          PORTS   AGE
nacos   nacos.star-xxxxx.cn   xxxx   80      65s

 

访问nacos配置中心

为了安全起见配置中心最好通过绑定(每个节点都要添加)hosts文件来实现访问

访问http://nacos.xxxx.cn/nacos/#/login 默认登录信息:nacos/nacos 

 

配置中心完成部署

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM