1. DamonSet 方式部署 etcd
DaemonSet 的特性是在整个 Kubernetes 集群的所有节点上都启动一个副本,但在部署 etcd 时不能使用特性,这是需要结合 nodeSelector
将 DaemonSet 的 Pod 调度到固定的节点上,我们一般会选择固定的3台节点来调度 etcd 的 pod。
使用 DaemonSet 的好处是,不需要分别在3个节点上复制文件,部署相对简单。
在 Kubernetes 的 node 节点上配置 DaemonSet 调度匹配的 Label
kubectl label node 10.100.0.13 my-etcd=true
编辑 DaemonSet的 Yaml 文件
apiVersion: apps/v1
kind: DaemonSet
metadata:
labels:
app: my-etcd
name: my-etcd
namespace: kube-system
spec:
selector:
matchLabels:
app: my-etcd
template:
metadata:
labels:
app: my-etcd
spec:
containers:
- command:
- /usr/local/bin/etcd
- --advertise-client-urls=http://$(POD_IP):32379
- --auto-compaction-mode=periodic
- --auto-compaction-retention=1h
- --client-cert-auth=false
- --data-dir=/data/my-etcd-ds/data
- --initial-advertise-peer-urls=http://$(POD_IP):32380
- --initial-cluster-token=my-etcd
- --listen-client-urls=http://$(POD_IP):32379,http://127.0.0.1:32379
- --listen-peer-urls=http://$(POD_IP):32380
- --logger=zap
- --log-outputs=stderr,/data/my-etcd-ds/log/etcd.log
- --name=$(NODE_NAME)
- --peer-client-cert-auth=false
- --snapshot-count=10000
image: registry.cn-hangzhou.aliyuncs.com/cncfstack/mywiki-etcd:v1-20201229
env:
- name: ETCD_INITIAL_CLUSTER
value: "10.100.0.13=http://10.100.0.13:32380"
- name: ETCD_INITIAL_CLUSTER_STATE
value: new
- name: NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
livenessProbe:
exec:
command:
- /bin/sh
- -ec
- ETCDCTL_API=3 etcdctl --endpoints=http://[127.0.0.1]:32379 get foo
failureThreshold: 8
initialDelaySeconds: 15
timeoutSeconds: 15
name: my-etcd
resources:
limits:
cpu: "1"
memory: "2G"
volumeMounts:
- mountPath: /data/my-etcd-ds/data
name: etcd-data
- mountPath: /etc/localtime
name: localtime
- mountPath: /data/my-etcd-ds/log
name: etcd-log
ports:
- containerPort: 32379
hostPort: 32379
- containerPort: 32380
hostPort: 32380
hostNetwork: true
nodeSelector:
my-etcd: "true"
volumes:
- hostPath:
path: /data/my-etcd-ds/data
name: etcd-data
- hostPath:
path: /data/my-etcd-ds/log
name: etcd-log
- hostPath:
path: /etc/localtime
name: localtime
dnsPolicy: ClusterFirstWithHostNet
hostNetwork: true
tolerations:
- effect: NoSchedule
operator: Exists
- effect: NoExecute
operator: Exists
这里主要关注集群主食和的配置 ETCD_INITIAL_CLUSTER
,名称设定和主机 IP 地址一致。
- name: ETCD_INITIAL_CLUSTER
value: "10.100.0.13=http://10.100.0.13:32380"
- name: ETCD_INITIAL_CLUSTER_STATE
value: new
- name: NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
创建 kubernetes 资源
$ kubectl apply -f my-etcd-ds.yaml
daemonset.apps/my-etcd created
查看 pod 运行状态
$ kubectl -n kube-system get pod |grep my-etcd
my-etcd-hdqh6 1/1 Running 0 3m24s
查看成员信息
$ etcdctl --endpoints http://127.0.0.1:32379 member list -w table
+------------------+---------+-------------+--------------------------+--------------------------+------------+
| ID | STATUS | NAME | PEER ADDRS | CLIENT ADDRS | IS LEARNER |
+------------------+---------+-------------+--------------------------+--------------------------+------------+
| 3da87a463ee2e5a7 | started | 10.100.0.13 | http://10.100.0.13:32380 | http://10.100.0.13:32379 | false |
+------------------+---------+-------------+--------------------------+--------------------------+------------+
读写数据
$ etcdctl --endpoints http://127.0.0.1:32379 put dstest dsvalue
OK
$ etcdctl --endpoints http://127.0.0.1:32379 get dstest
dstest
dsvalue