先决条件:
这里的存储使用的是storageClass,所以要预先将storageClass(nfs)部署完成详情参见:
k8s-StoargClass的使用-基于nfs-CSDN博客
因为nacos数据存储依赖于mysql.所以要预先部署MySQL,然后再部署nacos
部署mysql使用的pvc
[root@master /devops/nacos/mysql]$cat mysql-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: mysql-pvcnamespace: default
spec:storageClassName: managed-nfs-storageaccessModes:- ReadWriteManyresources:requests:storage: 10G
验证pv/pvc是否创建绑定成功
[root@master /devops/nacos/mysql]$kubectl get pv|grep mysql
pvc-e68d14f7-f76c-42ba-b5bb-141277022bfa 10G RWX Delete Bound default/mysql-pvc managed-nfs-storage 4d1h[root@master /devops/nacos/mysql]$kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mysql-pvc Bound pvc-e68d14f7-f76c-42ba-b5bb-141277022bfa 10G RWX managed-nfs-storage 4d1h
部署mysql服务,(这里所有的服务使用的都是默认命名空间,如果需要命名空间隔离的话自行创建即可.)
[root@master /devops/nacos/mysql]$cat mysql-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: mysqllabels:name: mysql
spec:replicas: 1selector:matchLabels:name: mysqltemplate:metadata:labels:name: mysqlspec:containers:- name: mysqlimage: nacos/nacos-mysql:5.7ports:- containerPort: 3306volumeMounts:- name: mysql-datamountPath: /var/lib/mysqlenv:- name: MYSQL_ROOT_PASSWORDvalue: "root"- name: MYSQL_DATABASEvalue: "nacos_devtest"- name: MYSQL_USERvalue: "nacos"- name: MYSQL_PASSWORDvalue: "nacos"volumes:- name: mysql-datapersistentVolumeClaim:claimName: mysql-pvc
---
apiVersion: v1
kind: Service
metadata:name: mysqllabels:name: mysql
spec:type: NodePortports:- port: 3306targetPort: 3306selector:name: mysql
这里部署的事单点MySQL服务.数据持久化使用的是预先创建好的storageClass,预先给nacos使用的用户是nacos,
查看部署结果
[root@master /zpf/devops/nacos/load/nacos-k8s/deploy/mysql]$kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
mysql 1/1 1 1 4d
查看svc并进行连接mysq服务进行验证
[root@master /devops/nacos/mysql]$kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql NodePort 10.233.33.184 <none> 3306:31549/TCP 4d
使用第三方工具链接mysql,验证MySQL可用性.
部署nacos,这个在官网上就有相关部署方式,
Kubernetes Nacos
[root@master /devops/nacos/nacos]$cat nacos-quick-start.yaml
---
apiVersion: v1
kind: Service
metadata:name: nacos-headlesslabels:app: nacos-headless
spec:type: ClusterIP #指定svcip类型clusterIP: None #不分配clusterIp地址ports:- port: 8848name: servertargetPort: 8848- port: 9848name: client-rpctargetPort: 9848- port: 9849name: raft-rpctargetPort: 9849## 兼容1.4.x版本的选举端口- port: 7848name: old-raft-rpctargetPort: 7848selector:app: nacos
---
apiVersion: v1
kind: ConfigMap
metadata:name: nacos-cm
data:mysql.host: "mysql"mysql.db.name: "nacos_devtest"mysql.port: "3306"mysql.user: "nacos"mysql.password: "nacos"
---
apiVersion: apps/v1
kind: StatefulSet
metadata:name: nacos
spec:serviceName: nacos-headlessreplicas: 2template:metadata:labels:app: nacosannotations:pod.alpha.kubernetes.io/initialized: "true"spec:affinity:podAntiAffinity:requiredDuringSchedulingIgnoredDuringExecution:- labelSelector:matchExpressions:- key: "app"operator: Invalues:- nacostopologyKey: "kubernetes.io/hostname"containers:- name: nacosimagePullPolicy: Alwaysimage: nacos/nacos-server:latestresources:requests:memory: "2Gi"cpu: "500m"ports:- containerPort: 8848name: client- containerPort: 9848name: client-rpc- containerPort: 9849name: raft-rpc- containerPort: 7848name: old-raft-rpcenv:- name: NACOS_REPLICASvalue: "2"- name: MYSQL_SERVICE_HOSTvalueFrom:configMapKeyRef:name: nacos-cmkey: mysql.host- name: MYSQL_SERVICE_DB_NAMEvalueFrom:configMapKeyRef:name: nacos-cmkey: mysql.db.name- name: MYSQL_SERVICE_PORTvalueFrom:configMapKeyRef:name: nacos-cmkey: mysql.port- name: MYSQL_SERVICE_USERvalueFrom:configMapKeyRef:name: nacos-cmkey: mysql.user- name: MYSQL_SERVICE_PASSWORDvalueFrom:configMapKeyRef:name: nacos-cmkey: mysql.password- name: SPRING_DATASOURCE_PLATFORMvalue: "mysql"- name: NACOS_SERVER_PORTvalue: "8848"- name: NACOS_APPLICATION_PORTvalue: "8848"- name: PREFER_HOST_MODEvalue: "hostname"- name: NACOS_SERVERSvalue: "nacos-0.nacos-headless.default.svc.cluster.local:8848 nacos-1.nacos-headless.default.svc.cluster.local:8848" #这里是高可用使用的地址名称,在部署集群的时候根据自己的实际情况进行修改.selector:matchLabels:app: nacos
查看部署结果
[root@master /devops/nacos/mysql]$kubectl get sts
NAME READY AGE
jenkins 1/1 13d
nacos 2/2 4d
redis-master 1/1 6d23h
redis-replicas 3/3 6d23h
rocketmq-broker-master 1/1 6d3h
rocketmq-broker-replica-id1 1/1 6d3h
rocketmq-nameserver 1/1 6d3h
[root@master /devops/nacos/mysql]$kubectl get po |grep nacos
nacos-0 1/1 Running 0 4d
nacos-1 1/1 Running 0 4d
[root@master /devops/nacos/mysql]$kubectl get svc |grep nacos
nacos-headless ClusterIP None <none> 8848/TCP,9848/TCP,9849/TCP,7848/TCP 4d
[root@master /devops/nacos/mysql]$kubectl get ing|grep nacos
nacos-headless <none> nacos-web.nacos-demo.com 192.168.75.40 80 4d2h
从页面进行查看部署结果.这里说明下,端口号为31324是网关的对外暴漏端口号.
部署成功.