在开始写之前我们先来简单了解一下kubernetes的 StatefulSets,大致概括为,有状态的集合,管理所有有状态的服务,以下是官网上StatefulSets的几个特性
1.稳定,独特的网络标识符。
2.稳定,持久的存储。
3.有序,优雅的部署和扩展。
4.有序的自动滚动更新。
我们在idea的Eureka-server服务中创建application.yaml,我们的目的是让A注册进B,B也注册进A,那么我们需要创建两个yaml
贴出示例yaml
spring:
application:
name: eureka-server
server:
port: 8761 #eureka默认端口号8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://server2:8760/eureka
instance:
hostname: server1
server:
enable-self-preservation: false
接下来我们接着贴出第二个yaml
spring:
application:
name: eureka-server
server:
port: 8760 #eureka默认端口号8760
eureka:
client:
# register-with-eureka: false
# fetch-registry: false
service-url:
defaultZone: http://server3:8762/eureka
instance:
hostname: server2
server:
enable-self-preservation: false
好然后直接启动eureka服务,这个时候我们需要注意的是在Edit Configurations上勾选如图,便于我们二次启动
最后在我们两次都启动过eureka服务之后,我们在网页上直接访问applocation.yaml的端口号localhost:8761
然后同样的访问:8760的端口号这样我们就会发现,第一个yaml注册到第二个yaml里面,…
ok 接下来我们在eureka-server的目录下创建eureka.yaml文件,并且配置容器
apiVersion: v1
kind: Service
metadata:
name: eureka-service
labels:
app: eureka-server
spec:
selector:
app: eureka-server
type: NodePort
ports:
- port: 8761
nodePort: 30001
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: eureka-server
labels:
app: eureka-server
spec:
replicas: 2
selector:
matchLabels:
app: eureka-server
serviceName: eureka-service
template:
metadata:
labels:
app: eureka-server
spec:
volumes:
- name: jar-location
hostPath:
path: /root/apps/eureka-server-0.0.1-SNAPSHOT.jar
containers:
- name: eureka-container
image: java
imagePullPolicy: IfNotPresent
workingDir: /apps
env:
- name: NODE
value: "eureka-server-0.eureka-service.default.svc.cluster.local"
- name: NODE1
value: "eureka-server-1.eureka-service.default.svc.cluster.local"
# resources:
# requests:
# memory: "256m"
# limits:
# memory: "256m"
volumeMounts:
- mountPath: /apps/eureka-server-0.0.1-SNAPSHOT.jar
name: jar-location
command:
- "java"
- "-jar"
- "eureka-server-0.0.1-SNAPSHOT.jar"
然后我们更改application.yaml的配置
spring:
application:
name: eureka-server
server:
port: 8761 #eureka默认端口号8761
eureka:
client:
#register-with-eureka: false
## fetch-registry: false
service-url:
defaultZone: http://${NODE:locahost}:8761/eureka,http://${NODE1:locahost}:8761/eureka
instance:
# hostname: ${NODE1_HOST_NAME: server1}
server:
enable-self-preservation: false
接下来就是打开Gradle的Tasks的build 进行打包项目为jar,然后把jar包上传到finalshell的服务器上进行部署,并且创建Eureka.yaml文件
apiVersion: v1
kind: Service
metadata:
name: eureka-service
labels:
app: eureka-server
spec:
selector:
app: eureka-server
type: NodePort
ports:
- port: 8761
nodePort: 30001
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: eureka-server
labels:
app: eureka-server
spec:
replicas: 2
selector:
matchLabels:
app: eureka-server
serviceName: eureka-service
template:
metadata:
labels:
app: eureka-server
spec:
volumes:
- name: jar-location
hostPath:
path: /root/apps/eureka-server-0.0.1-SNAPSHOT.jar
containers:
- name: eureka-container
image: java
imagePullPolicy: IfNotPresent
workingDir: /apps
env:
- name: NODE
value: "eureka-server-0.eureka-service.default.svc.cluster.local"
- name: NODE1
value: "eureka-server-1.eureka-service.default.svc.cluster.local"
# resources:
# requests:
# memory: "256m"
# limits:
# memory: "256m"
volumeMounts:
- mountPath: /apps/eureka-server-0.0.1-SNAPSHOT.jar
name: jar-location
command:
- "java"
- "-jar"
- "eureka-server-0.0.1-SNAPSHOT.jar"
接下来我们就在finalshell上传项目的jar包以及yaml文件到服务器
1.kubectl detele -f eureka.yaml
2. kubectl create -f eureka.yaml
3 kubectl get svc
然后docker-demo-service已经占用我们的30001的端口了,
删除
重新get svc就能看到name变成了eureka-service的名字
ok 这个时候已经差不多就剩启动容器了
4.查看状态
kubectl get pod
5.启动容器
kubectl logs eureka-server-0
curl http://localhost:30001