先决条件:
这里的存储使用的是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-pvc
namespace: default
spec:
storageClassName: managed-nfs-storage
accessModes:
- ReadWriteMany
resources:
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: mysql
labels:
name: mysql
spec:
replicas: 1
selector:
matchLabels:
name: mysql
template:
metadata:
labels:
name: mysql
spec:
containers:
- name: mysql
image: nacos/nacos-mysql:5.7
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-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"
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: mysql-pvc
---
apiVersion: v1
kind: Service
metadata:
name: mysql
labels:
name: mysql
spec:
type: NodePort
ports:
- port: 3306
targetPort: 3306
selector:
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-headless
labels:
app: nacos-headless
spec:
type: ClusterIP #指定svcip类型
clusterIP: None #不分配clusterIp地址
ports:
- port: 8848
name: server
targetPort: 8848
- port: 9848
name: client-rpc
targetPort: 9848
- port: 9849
name: raft-rpc
targetPort: 9849
## 兼容1.4.x版本的选举端口
- port: 7848
name: old-raft-rpc
targetPort: 7848
selector:
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-headless
replicas: 2
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
- containerPort: 9848
name: client-rpc
- containerPort: 9849
name: raft-rpc
- containerPort: 7848
name: old-raft-rpc
env:
- name: NACOS_REPLICAS
value: "2"
- name: MYSQL_SERVICE_HOST
valueFrom:
configMapKeyRef:
name: nacos-cm
key: mysql.host
- 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: SPRING_DATASOURCE_PLATFORM
value: "mysql"
- name: NACOS_SERVER_PORT
value: "8848"
- name: NACOS_APPLICATION_PORT
value: "8848"
- name: PREFER_HOST_MODE
value: "hostname"
- name: NACOS_SERVERS
value: "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是网关的对外暴漏端口号.
部署成功.