github.com/qsunny/k8s@v0.0.0-20220101153623-e6dca256d5bf/nacos/README.md (about)

     1  # Kubernetes Nacos
     2  
     3  
     4  This project contains a Nacos Docker image meant to facilitate the deployment of [Nacos](https://nacos.io) on [Kubernetes](https://kubernetes.io/) via StatefulSets.
     5  
     6  [中文文档](https://github.com/nacos-group/nacos-k8s/blob/master/README-CN.md)
     7  
     8  # Tips
     9  If you are using **Nacos** version 1.1.4 or lower,, please refer to this [Tag](https://github.com/nacos-group/nacos-k8s/tree/v1.1.4)
    10  
    11  It is recommended to deploy Nacos in Kubernetes using [Nacos Operator](operator/README.md).
    12  
    13  # Quick Start
    14  
    15  * **Clone Project**
    16  
    17  
    18  ```shell
    19  git clone https://github.com/nacos-group/nacos-k8s.git
    20  ```
    21  
    22  
    23  
    24  * **Simple Start**
    25  
    26  > If you want to start Nacos without NFS, but **emptyDirs will possibly result in a loss of data**. as follows:
    27  
    28  ```shell
    29  cd nacos-k8s
    30  chmod +x quick-startup.sh
    31  ./quick-startup.sh
    32  ```
    33  
    34  
    35  
    36  * **Testing**
    37  
    38    * **Service registration**
    39  
    40    ```powershell
    41    curl -X PUT 'http://cluster-ip:8848/nacos/v1/ns/instance?serviceName=nacos.naming.serviceName&ip=20.18.7.10&port=8080'
    42    ```
    43  
    44  
    45  
    46    * **Service discovery**
    47  
    48    ```powershell
    49    curl -X GET 'http://cluster-ip:8848/nacos/v1/ns/instances?serviceName=nacos.naming.serviceName'
    50    ```
    51  
    52  
    53  
    54    * **Publish config**
    55  
    56    ```powershell
    57    curl -X POST "http://cluster-ip:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test&content=helloWorld"
    58    ```
    59  
    60  
    61  
    62    * **Get config**
    63  
    64    ```powershell
    65    curl -X GET "http://cluster-ip:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test"
    66    ```
    67  
    68  
    69  
    70  
    71  # Advanced
    72  
    73  ## Tips
    74  If you use a custom database, please initialize the database script yourself first.
    75  <https://github.com/alibaba/nacos/blob/develop/distribution/conf/nacos-mysql.sql>
    76  
    77  
    78  > In advanced use, the cluster is automatically scaled and data is persisted, but [PersistentVolumeClaims](https://kubernetes.io/docs/concepts/storage/persistent-volumes/#persistentvolumeclaims) must be deployed. In this example, NFS is used.
    79  >
    80  
    81  
    82  
    83  ## Deploy NFS
    84  
    85  * Create Role 
    86  
    87  ```shell
    88  kubectl create -f deploy/nfs/rbac.yaml
    89  ```
    90  
    91  > If your K8S namespace is not default, execute the following script before creating RBAC
    92  
    93  
    94  ```shell
    95  # Set the subject of the RBAC objects to the current namespace where the provisioner is being deployed
    96  $ NS=$(kubectl config get-contexts|grep -e "^\*" |awk '{print $5}')
    97  $ NAMESPACE=${NS:-default}
    98  $ sed -i '' "s/namespace:.*/namespace: $NAMESPACE/g" ./deploy/nfs/rbac.yaml
    99  
   100  ```
   101  
   102  
   103  
   104  * Create `ServiceAccount` And deploy `NFS-Client Provisioner`
   105  
   106  ```shell
   107  kubectl create -f deploy/nfs/deployment.yaml
   108  ```
   109  
   110  
   111  
   112  * Create NFS StorageClass
   113  
   114  ```shell
   115  kubectl create -f deploy/nfs/class.yaml
   116  ```
   117  
   118  
   119  
   120  * Verify that NFS is working
   121  
   122  ```shell
   123  kubectl get pod -l app=nfs-client-provisioner
   124  ```
   125  
   126  
   127  
   128  ## Deploy database
   129  
   130  
   131  * Deploy mysql
   132  
   133  ```shell
   134  
   135  cd nacos-k8s
   136  
   137  kubectl create -f deploy/mysql/mysql-nfs.yaml
   138  ```
   139  
   140  
   141  
   142  
   143  
   144  
   145  
   146  * Verify that Database is working
   147  
   148  ```shell
   149  
   150  kubectl get pod 
   151  NAME                         READY   STATUS    RESTARTS   AGE
   152  mysql-gf2vd                        1/1     Running   0          111m
   153  
   154  ```
   155  
   156  
   157  
   158  ## Deploy Nacos 
   159  
   160  
   161  
   162  
   163  
   164  * Modify  **deploy/nacos/nacos-pvc-nfs.yaml**
   165  
   166  ```yaml
   167  data:
   168    mysql.db.name: "db name"
   169    mysql.port: " db port"
   170    mysql.user: " db username"
   171    mysql.password: " db password"
   172  ```
   173  
   174  
   175  
   176  * Create Nacos
   177  
   178  ``` shell
   179  kubectl create -f nacos-k8s/deploy/nacos/nacos-pvc-nfs.yaml
   180  ```
   181  
   182  
   183  
   184  * Verify that Nacos is working
   185  
   186  ```shell
   187  kubectl get pod -l app=nacos
   188  
   189  
   190  NAME      READY   STATUS    RESTARTS   AGE
   191  nacos-0   1/1     Running   0          19h
   192  nacos-1   1/1     Running   0          19h
   193  nacos-2   1/1     Running   0          19h
   194  ```
   195  
   196  
   197  
   198  
   199  
   200  ## Scale Testing
   201  
   202  * Use [`kubectl exec`](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands/#exec) to get the cluster config of the Pods in the `nacos` StatefulSet.
   203  
   204  ```powershell
   205  for i in 0 1; do echo nacos-$i; kubectl exec nacos-$i cat conf/cluster.conf; done
   206  ```
   207  
   208  The StatefulSet controller provides each Pod with a unique hostname based on its ordinal index. The hostnames take the form of `<statefulset name>-<ordinal index>`. Because the `replicas` field of the `nacos` StatefulSet is set to `2`, In the cluster file only two nacos address
   209  
   210  
   211  
   212  ![k8s](/images/k8s.gif)
   213  
   214  
   215  
   216  * Use kubectl to scale StatefulSets
   217  
   218  ```bash
   219  kubectl scale sts nacos --replicas=3
   220  ```
   221  
   222  ![scale](/images/scale.gif)
   223  
   224  
   225  
   226  * Use [`kubectl exec`](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands/#exec) to get the cluster config of the Pods in the `nacos` StatefulSet after scale StatefulSets
   227  
   228  ```bash
   229  for i in 0 1 2; do echo nacos-$i; kubectl exec nacos-$i cat conf/cluster.conf; done
   230  ```
   231  
   232  ![get_cluster_after](/images/get_cluster_after.gif)
   233  
   234  
   235  
   236  * Use [`kubectl exec`](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands/#exec) to get the **state** of the Pods in the `nacos` StatefulSet after scale StatefulSets
   237  
   238  ```bash
   239  for i in 0 1 2; do echo nacos-$i; kubectl exec nacos-$i curl GET "http://localhost:8848/nacos/v1/ns/raft/state"; done
   240  ```
   241  
   242  You can find that the new node has joined the cluster
   243  
   244  # Prerequisites
   245  
   246  - Kubernetes Node configuration(for reference only)
   247  
   248  | Hostname   | Configuration                                                                    |
   249  | ---------- | -------------------------------------------------------------------------------- |                    
   250  | k8s-master | CentOS Linux release 7.4.1708 (Core) Single-core processor Mem 4G Cloud disk 40G |
   251  | node01     | CentOS Linux release 7.4.1708 (Core) Single-core processor Mem 4G Cloud disk 40G |
   252  | node02     | CentOS Linux release 7.4.1708 (Core) Single-core processor Mem 4G Cloud disk 40G |
   253  
   254  - Kubernetes version:**1.12.2+** 
   255  - NFS version:**4.1+** 
   256  
   257  
   258  
   259  
   260  # Limitations
   261  
   262  * Persistent Volumes must be used. emptyDirs will possibly result in a loss of data
   263  
   264  
   265  
   266  
   267  
   268  # Project directory
   269  
   270  | Directory Name   | Description                                |
   271  | ------ | ----------------------------------- |
   272  | plugin | Help Nacos cluster achieve automatic scaling in K8s |
   273  | deploy | Deploy the required files                     |
   274  
   275  
   276  
   277  # Configuration properties
   278  
   279  * nacos-pvc-nfs.yaml or nacos-quick-start.yaml 
   280  
   281  | Name                  | Required | Description                                    |
   282  | --------------------- | -------- | --------------------------------------- |
   283  | mysql.db.name  | Y       | database name                          |
   284  | mysql.port     | N       | database port                          |
   285  | mysql.user     | Y       | database username                        |
   286  | mysql.password | Y       | database password                       |
   287  | NACOS_REPLICAS        | Y       | The number of clusters must be consistent with the value of the replicas attribute |
   288  | NACOS_SERVER_PORT     | N       | Nacos port,default:8848 for Peer-finder plugin               |
   289  | NACOS_APPLICATION_PORT     | N       | Nacos port, default:8848           |
   290  | PREFER_HOST_MODE      | Y       | Enable Nacos cluster node domain name support               |
   291  
   292  
   293  
   294  * **nfs** deployment.yaml 
   295  
   296  | Name       | Required | Description                     |
   297  | ---------- | -------- | ------------------------ |
   298  | NFS_SERVER | Y       | NFS server address           |
   299  | NFS_PATH   | Y       | NFS server shared directory |
   300  | server     | Y       | NFS server address           |
   301  | path       | Y       | NFS server shared directory |
   302  
   303  
   304  
   305  * mysql yaml 
   306  
   307  | Name                       | Required | Description                                                        |
   308  | -------------------------- | -------- | ----------------------------------------------------------- |
   309  | MYSQL_ROOT_PASSWORD        | N       | Root password                                                    |
   310  | MYSQL_DATABASE             | Y       | Database Name                                     |
   311  | MYSQL_USER                 | Y       | Database Username                                     |
   312  | MYSQL_PASSWORD             | Y       | Database Password                                |
   313  | Nfs:server                 | Y       | NFS server address |
   314  | Nfs:path                   | Y       | NFS server shared path |
   315  
   316  
   317  
   318