一.知识点:

1.headless services

  NOTE:: 我们在k8s上运行etcd集群,集群间通告身份是使用dns,不能使用pod ip,因为如果pod被重构了ip会变,在这种场景中不能直接使用k8s 的service,因为在集群环境中我们需要直接将service name映射到pod ip,而不是 service ip,这样我们才能完成集群间的身份验证。

2.env

NOTE: pod还没启动之前怎么能知道pod的ip呢?那我们启动etcd时绑定网卡的ip该怎样拿到呢?这时就可以用env将pod ip 以变量方式传给etcd,这样当pod启动后,etcd就通过env拿到了pod ip,  从而绑定到container 的ip

二.架构:

采用三节点的etcd:

分别为:etcd1,etcd2,etcd3

etcd持久数据采用nfs

etcd1.yml

apiVersion: apps/v1
kind: Deployment
metadata:
name: etcd1
labels:
name: etcd1
spec:
replicas:
selector:
matchLabels:
app: etcd1
template:
metadata:
labels:
app: etcd1
spec:
containers:
- name: etcd1
image: myetcd
imagePullPolicy: IfNotPresent
volumeMounts:
- mountPath: /data
name: etcd-data
env:
- name: host_ip
valueFrom:
fieldRef:
fieldPath: status.podIP
command: ["/bin/sh","-c"]
args:
- /tmp/etcd
--name etcd1
--initial-advertise-peer-urls http://${host_ip}:2380
--listen-peer-urls http://${host_ip}:2380
--listen-client-urls http://${host_ip}:2379,http://127.0.0.1:2379
--advertise-client-urls http://${host_ip}:2379
--initial-cluster-token etcd-cluster-
--initial-cluster etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380
--initial-cluster-state new
--data-dir=/data volumes:
- name: etcd-data
nfs:
server: 192.168.85.139
path: /data/v1

etcd2.yml

apiVersion: apps/v1
kind: Deployment
metadata:
name: etcd2
labels:
name: etcd2
spec:
replicas:
selector:
matchLabels:
app: etcd2
template:
metadata:
labels:
app: etcd2
spec:
containers:
- name: etcd2
image: myetcd
imagePullPolicy: IfNotPresent
volumeMounts:
- mountPath: /data
name: etcd-data
env:
- name: host_ip
valueFrom:
fieldRef:
fieldPath: status.podIP command: ["/bin/sh","-c"]
args:
- /tmp/etcd
--name etcd2
--initial-advertise-peer-urls http://${host_ip}:2380
--listen-peer-urls http://${host_ip}:2380
--listen-client-urls http://${host_ip}:2379,http://127.0.0.1:2379
--advertise-client-urls http://${host_ip}:2379
--initial-cluster-token etcd-cluster-
--initial-cluster etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380
--initial-cluster-state new
--data-dir=/data volumes:
- name: etcd-data
nfs:
server: 192.168.85.139
path: /data/v2

etcd3.yml

apiVersion: apps/v1
kind: Deployment
metadata:
name: etcd3
labels:
name: etcd3
spec:
replicas:
selector:
matchLabels:
app: etcd3
template:
metadata:
labels:
app: etcd3
spec:
containers:
- name: etcd3
image: myetcd
imagePullPolicy: IfNotPresent
volumeMounts:
- mountPath: /data
name: etcd-data
env:
- name: host_ip
valueFrom:
fieldRef:
fieldPath: status.podIP
command: ["/bin/sh","-c"]
args:
- /tmp/etcd
--name etcd3
--initial-advertise-peer-urls http://${host_ip}:2380
--listen-peer-urls http://${host_ip}:2380
--listen-client-urls http://${host_ip}:2379,http://127.0.0.1:2379
--advertise-client-urls http://${host_ip}:2379
--initial-cluster-token etcd-cluster-
--initial-cluster etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380
--initial-cluster-state new
--data-dir=/data volumes:
- name: etcd-data
nfs:
server: 192.168.85.139
path: /data/v3

etcd1-svc.yml

apiVersion: v1
kind: Service
metadata:
name: etcd1
spec:
clusterIP: None
ports:
- name: client
port:
targetPort:
- name: message
port:
targetPort:
selector:
app: etcd1

etcd2-svc.yml

apiVersion: v1
kind: Service
metadata:
name: etcd2
spec:
clusterIP: None
ports:
- name: client
port:
targetPort:
- name: message
port:
targetPort:
selector:
app: etcd2

etcd3-svc.yml

apiVersion: v1
kind: Service
metadata:
name: etcd3
spec:
clusterIP: None
ports:
- name: client
port:
targetPort:
- name: message
port:
targetPort:
selector:
app: etcd3

在任意节点查看etcd状态:

[root@node1 test]# kubectl exec etcd1-79bdcb47c9-fwqps -- /tmp/etcdctl cluster-health
member 876043ef79ada1ea is healthy: got healthy result from http://10.244.1.113:2379
member 99eab3685d8363a1 is healthy: got healthy result from http://10.244.1.111:2379
member dcb68c82481661be is healthy: got healthy result from http://10.244.1.112:2379

提供外部访问:

apiVersion: v1
kind: Service
metadata:
name: etcd-external
spec:
type: NodePort
ports:
- name: client
port:
targetPort:
nodePort:
- name: message
port:
targetPort:
nodePort:
selector:
app: etcd1

最新文章

  1. 如何在xml文件中加注释或取消注释
  2. linux下svn命令使用大全
  3. 性能检测工具介绍-Linux系统命令行
  4. 【JAVA】通过HttpClient发送HTTP请求的方法
  5. CH round #55 Streaming #6
  6. 一、XML语法
  7. HTML5 input控件 placeholder属性
  8. runtime的基本应用
  9. enumerateObjectsUsingBlock VS for(... in ...)
  10. groovy : 正則表達式
  11. java复习笔记
  12. JAVAFX之tableview界面实时刷新导致的内存溢出(自己挖的坑,爬着也要出来啊0.0)
  13. DSAPI 键盘鼠标钩子
  14. Chapter_9 DP : uva1347 tour (bitonic tour)
  15. 关于图片的Base64编码
  16. 一键搭建LNMP脚本
  17. Hbase记录-Hbase调优参数
  18. 22. SpringBoot 集成 Mybatis
  19. 如何把手机app的视频下载到手机上?网页上的视频怎么下载?
  20. iOS 检测网络状态 自动判断 认为提示网络改变

热门文章

  1. dockerfile运行mysql并初始化数据
  2. js和jquery通过this获取html标签中的属性值[转藏]
  3. [LeetCode] 257. Binary Tree Paths 二叉树路径
  4. 使用ObjectARX 2012向导生成的自定义实体类无法捕捉的问题解决方式
  5. celery无法启动的问题 SyntaxError: invalid syntax
  6. c++ std::cout重定向到文件
  7. Spring Cloud--Hystrix服务熔断(线程隔离/服务降级)代码实现
  8. Spring系列(三):Spring IoC源码解析
  9. numpy模块之axis(转)
  10. Oracle scott解锁 以及连接数据库