kubernetes集群:nacos搭建
根据官网搭建失败后(失败原因详细见https://www.cnblogs.com/zoujiaojiao/p/12558946.html)。参考http://www.fengzhihai.cn/2020/03/17/k8s-nacos/重新搭建了一遍。搭建k8s集群1个多月,一直在学习中。有不足的请看客指出。
文件说明
- 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=密码
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
- configmap.sh
-n kube-ops 是命名空间。
kubectl create configmap custom.properties --from-file=./custom.properties -n kube-ops
生成配置:# sh configmap.sh
- nacos-pvc.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: nacos-pv
spec:
capacity:
storage: 50Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
nfs:
server: 10.10.3.84 nfs服务器地址
path: /data/nacos nfs挂载的目录。一定要确保存在。
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: nacos-pvc
namespace: kube-ops
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 50Gi
创建pv 和 pvc :
# kubectl create -f nacos-pvc.yaml
查看:
# kubectl get pv -n kube-ops|grep nacos
# kubectl get pvc -n kube-ops|grep nacos
图片中,一部分pv是之前按照官网搭建时产生的。可以删除
删除方式:
确保pod都删除了。先删除pvc
# kubectl get pvc
# kubectl delete pvc datadir-nacos-0
再删除pv
# kubectl delete pv pvc-04b58b4c-06c7-431b-a0cc-e6c624dc6b9f
- nacos-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nacos
namespace: kube-ops
labels:
name: nacos
spec:
selector:
matchLabels:
name: nacos
template:
metadata:
name: nacos
labels:
name: nacos
spec:
containers:
- name: nacos-mysql
image: nacos/nacos-mysql:5.7
imagePullPolicy: IfNotPresent
env:
- name: MYSQL_USER
value: "root"
- name: MYSQL_DATABASE
value: "nacos"
- name: MYSQL_ROOT_PASSWORD
value: "密码"
volumeMounts:
- mountPath: /var/lib/mysql
name: nacos-pv
subPath: mysql
ports:
- containerPort: 3306
name: "mysql-port"
- name: 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: custom.properties
- name: nacostime
hostPath:
path: /etc/localtime
生成nacos的deployment
# kubectl create -f nacos-deployment.yaml
查看pod:
# kubectl describe pods nacos-9b8479dd5-jlpsz -n kube-ops
查看mysql pod的日志:
# kubectl logs nacos-9b8479dd5-jlpsz nacos-mysql -n kube-ops
查看nacos pod的日志:
# kubectl logs nacos-9b8479dd5-jlpsz nacos-server -n kube-ops
- nacos-service.yaml
apiVersion: v1
kind: Service
metadata:
name: nacos-service
namespace: kube-ops
spec:
# type: NodePort
ports:
- port: 3306
targetPort: 3306
name: "nacos-mysql-port"
- port: 8848
targetPort: 8848
name: "nacos-port"
selector:
name: nacos
生成service
# kubectl create -f nacos-service.yaml
- 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: kube-ops
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: nacos.vonedao.com
http:
paths:
- backend:
serviceName: nacos-service
servicePort: 8848
生成ingress:
# kubectl create -f nacos-ingress.yaml
验证
配置本地hosts。
访问http://nacos.vonedao.com/ 会报错。需要访问:http://nacos.vonedao.com/nacos