1. standalone启动

  • 创建nacos-stand.yaml文件如下
---
apiVersion: v1
kind: Service
metadata:
  name: nacos
  namespace: nacos
  labels:
    app: nacos
spec:
  type: NodePort
  ports:
    - port: 8848
      name: server
      targetPort: 8848
      nodePort: 30042
    - port: 7848
      name: rpc
      targetPort: 7848
      nodePort: 30043
  selector:
    app: nacos
---
apiVersion: v1
kind: Service
metadata:
  name: nacos-headless
  namespace: nacos
  labels:
    app: nacos
spec:
  ports:
    - port: 8848
      name: server
      targetPort: 8848
    - port: 7848
      name: rpc
      targetPort: 7848
  clusterIP: None
  selector:
    app: nacos
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nacos-cm
  namespace: nacos
data:
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nacos
  namespace: 
spec:
  serviceName: nacos-headless
  replicas: 1
  template:
    metadata:
      labels:
        app: nacos
      annotations:
        pod.alpha.kubernetes.io/initialized: "true"
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: "app"
                    operator: In
                    values:
                      - nacos
              topologyKey: "kubernetes.io/hostname"
      containers:
        - name: nacos
          imagePullPolicy: Always
          image: nacos/nacos-server:latest
          resources:
            requests:
              memory: "2Gi"
              cpu: "500m"
          ports:
            - containerPort: 8848
              name: client-port
            - containerPort: 7848
              name: rpc
          env:
            - name: NACOS_REPLICAS
              value: "1"
            - name: PREFER_HOST_MODE
              value: "hostname"
            - name: TZ
              value: Asia/Shanghai
            - name: MODE
              value: standalone
          volumeMounts:
            - name: datadir
              mountPath: /home/nacos/data
            - name: logdir
              mountPath: /home/nacos/logs
  volumeClaimTemplates:
    - metadata:
        name: datadir
        annotations:
          volume.beta.kubernetes.io/storage-class: "nfs-client"
      spec:
        accessModes: [ "ReadWriteMany" ]
        resources:
          requests:
            storage: 10Gi
    - metadata:
        name: logdir
        annotations:
          volume.beta.kubernetes.io/storage-class: "nfs-client"
      spec:
        accessModes: [ "ReadWriteMany" ]
        resources:
          requests:
            storage: 10Gi
  selector:
    matchLabels:
      app: nacos

  • 启动
#kubectl create -f nacos-stand.yaml
  • 查看结果
[root@DoM01 nacos-test]# kubectl get all -n nacos
NAME          READY   STATUS    RESTARTS   AGE
pod/nacos-0   1/1     Running   0          9m35s

NAME                     TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)                         AGE
service/nacos            NodePort    10.1.85.209   <none>        8848:30042/TCP,7848:30043/TCP   153m
service/nacos-headless   ClusterIP   None          <none>        8848/TCP,7848/TCP               153m

NAME                     READY   AGE
statefulset.apps/nacos   2/2     153m

2. 使用mysql

2.1 安装mysql

  • 创建mysql.yml文件内容如下
apiVersion: v1
kind: ReplicationController
metadata:
  name: mysql
  namespace: nacos-test
  labels:
    name: mysql
spec:
  replicas: 1
  selector:
    name: mysql
  template:
    metadata:
      labels:
        name: mysql
    spec:
      containers:
      - name: mysql
        image: nacos/nacos-mysql:5.7
        ports:
        - containerPort: 3306
        volumeMounts:
        - name: nacos-mysql
          mountPath: /var/lib/mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "liubei@2020"
        - name: MYSQL_DATABASE
          value: "nacos"
        - name: MYSQL_USER
          value: "nacos"
        - name: MYSQL_PASSWORD
          value: "liubei@2020"
        - name: TZ
          value: Asia/Shanghai
      volumes:
      - name: nacos-mysql
        persistentVolumeClaim:
          claimName: nacos-mysql-pvc
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nacos-mysql-pvc
  namespace: nacos-test
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 20Gi
  storageClassName: nfs-client
---
apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: nacos-test
  labels:
    name: mysql
spec:
  ports:
  - port: 3306
    targetPort: 3306
  selector:
    name: mysql

  • 启动
#kubectl create -f mysql.yml

2.2 安装 nacos

  • 创建nacos.yml文件,内容如下

默认使用derby 数据库的持久化存储不需要了,都注释掉。

apiVersion: v1
kind: ReplicationController
metadata:
  name: mysql
  namespace: nacos-test
  labels:
    name: mysql
spec:
  replicas: 1
  selector:
    name: mysql
  template:
    metadata:
      labels:
        name: mysql
    spec:
      containers:
      - name: mysql
        image: nacos/nacos-mysql:5.7
        ports:
        - containerPort: 3306
        volumeMounts:
        - name: nacos-mysql
          mountPath: /var/lib/mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "liubei@2020"
        - name: MYSQL_DATABASE
          value: "nacos"
        - name: MYSQL_USER
          value: "nacos"
        - name: MYSQL_PASSWORD
          value: "liubei@2020"
        - name: TZ
          value: Asia/Shanghai
      volumes:
      - name: nacos-mysql
        persistentVolumeClaim:
          claimName: nacos-mysql-pvc
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nacos-mysql-pvc
  namespace: nacos-test
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 20Gi
  storageClassName: nfs-client
---
apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: nacos-test
  labels:
    name: mysql
spec:
  ports:
  - port: 3306
    targetPort: 3306
  selector:
    name: mysql
[root@DoM01 nacos]# cat nacos.yaml
---
apiVersion: v1
kind: Service
metadata:
  name: nacos
  namespace: nacos-test
  labels:
    app: nacos
spec:
  type: NodePort
  ports:
    - port: 8848
      name: server
      targetPort: 8848
      nodePort: 30042
    - port: 7848
      name: rpc
      targetPort: 7848
      nodePort: 30043
  selector:
    app: nacos
---
apiVersion: v1
kind: Service
metadata:
  name: nacos-headless
  namespace: nacos-test
  labels:
    app: nacos
#  annotations:
#    解决k8s的statefulset里的pod如果不ready就ping不通对方
#    service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
spec:
  ports:
    - port: 8848
      name: server
      targetPort: 8848
    - port: 7848
      name: rpc
      targetPort: 7848
  clusterIP: None
  selector:
    app: nacos
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nacos-cm
  namespace: nacos-test
data:
  mysql.db.name: "nacos"
  mysql.port: "3306"
  mysql.user: "nacos"
  mysql.password: "liubei@2020"
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nacos
  namespace: nacos-test
spec:
  serviceName: nacos-headless
  replicas: 1
  template:
    metadata:
      labels:
        app: nacos
      annotations:
        pod.alpha.kubernetes.io/initialized: "true"
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: "app"
                    operator: In
                    values:
                      - nacos
              topologyKey: "kubernetes.io/hostname"
#      serviceAccountName: nfs-client-provisioner
#      initContainers:
#        - name: peer-finder-plugin-install
#          image: nacos/nacos-peer-finder-plugin:1.0
#          imagePullPolicy: Always
#          volumeMounts:
#            - mountPath: "/home/nacos/plugins/peer-finder"
#              name: plugindir
      containers:
        - name: nacos
          imagePullPolicy: Always
          image: nacos/nacos-server:latest
          resources:
            requests:
              memory: "2Gi"
              cpu: "500m"
          ports:
            - containerPort: 8848
              name: client-port
            - containerPort: 7848
              name: rpc
          env:
            - name: NACOS_REPLICAS
              value: "1"
            - name: SERVICE_NAME
              value: "nacos-headless"
            - name: NACOS_AUTH_ENABLE
              value: "true"
            - name: DOMAIN_NAME
              value: "cluster.local"
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.namespace
            - name: MYSQL_SERVICE_DB_NAME
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.db.name
            - name: MYSQL_SERVICE_PORT
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.port
            - name: MYSQL_SERVICE_USER
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.user
            - name: MYSQL_SERVICE_PASSWORD
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.password
            - name: NACOS_SERVER_PORT
              value: "8848"
            - name: NACOS_APPLICATION_PORT
              value: "8848"
            - name: PREFER_HOST_MODE
              value: "hostname"
            - name: SPRING_DATASOURCE_PLATFORM
              value: "mysql"
            - name: TZ
              value: Asia/Shanghai
            - name: NACOS_SERVERS
              value: "nacos-0.nacos-headless.nacos.svc.cluster.local:8848 nacos-1.nacos-headless.nacos.svc.cluster.local:8848 nacos-2.nacos-headless.nacos.svc.cluster.local:8848"
          volumeMounts:
#            - name: plugindir
#              mountPath: /home/nacos/plugins/peer-finder
#            - name: datadir
#              mountPath: /home/nacos/data
            - name: logdir
              mountPath: /home/nacos/logs
  volumeClaimTemplates:
#    - metadata:
#        name: plugindir
#        annotations:
#          volume.beta.kubernetes.io/storage-class: "nfs-client"
#      spec:
#        accessModes: [ "ReadWriteMany" ]
#        resources:
#          requests:
#            storage: 10Gi
#    - metadata:
#        name: datadir
#        annotations:
#          volume.beta.kubernetes.io/storage-class: "nfs-client"
#      spec:
#        accessModes: [ "ReadWriteMany" ]
#        resources:
#          requests:
#            storage: 10Gi
    - metadata:
        name: logdir
        annotations:
          volume.beta.kubernetes.io/storage-class: "nfs-client"
      spec:
        accessModes: [ "ReadWriteMany" ]
        resources:
          requests:
            storage: 10Gi
  selector:
    matchLabels:
      app: nacos

  • 启动服务
#kubectl create -f mysql.yml

2.3 查看结果

[root@DoM01 nacos]# kubectl get all -n nacos-test
NAME              READY   STATUS    RESTARTS   AGE
pod/mysql-86tl9   1/1     Running   0          17m
pod/nacos-0       1/1     Running   0          16m

NAME                          DESIRED   CURRENT   READY   AGE
replicationcontroller/mysql   1         1         1       19m

NAME                     TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                         AGE
service/mysql            ClusterIP   10.1.204.122   <none>        3306/TCP                        20m
service/nacos            NodePort    10.1.1.160     <none>        8848:30042/TCP,7848:30043/TCP   15m
service/nacos-headless   ClusterIP   None           <none>        8848/TCP,7848/TCP               16m


NAME                     READY   AGE
statefulset.apps/nacos   1/1     16m



Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐