在 k8s 上部署 etcd 集群,需要注意以下几个步骤:
- 创建 etcd 镜像:可以通过 Dockerfile 创建 etcd 的镜像。例如:
FROM quay.io/coreos/etcd:v3.5.0
CMD ["/usr/local/bin/etcd"]
- 创建 etcd 服务:在 k8s 中创建 etcd 服务,可以使用 StatefulSet 或 Deployment 等控制器。例如:
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: etcd-cluster
spec:
serviceName: etcd
replicas: 3
selector:
matchLabels:
app: etcd
template:
metadata:
labels:
app: etcd
spec:
containers:
- name: etcd
image: <etcd-image>
command:
- /usr/local/bin/etcd
args:
- --name=$(hostname)
- --initial-advertise-peer-urls=http://$(hostname):2380
- --listen-peer-urls=http://0.0.0.0:2380
- --advertise-client-urls=http://$(hostname):2379
- --listen-client-urls=http://0.0.0.0:2379
- --initial-cluster-state=new
- --initial-cluster-token=etcd-cluster
- --initial-cluster=<etcd-hostname-1>=http://<etcd-ip-1>:2380,<etcd-hostname-2>=http://<etcd-ip-2>:2380,<etcd-hostname-3>=http://<etcd-ip-3>:2380
volumeMounts:
- name: etcd-data
mountPath: /var/lib/etcd
volumes:
- name: etcd-data
emptyDir: {}
- 验证 etcd 集群:可以在 k8s 上运行一个测试的 pod,验证 etcd 集群是否正常工作。例如:
apiVersion: v1
kind: Pod
metadata:
name: etcd-test
spec:
containers:
- name: etcd-test
image: quay.io/coreos/etcd:v3.5.0
command:
- /bin/sh
- -c
- |
while true; do
etcdctl --endpoints=http://etcd-0.etcd:2379,http://etcd-1.etcd:2379,http://etcd-2.etcd:2379 put foo bar;
sleep 10;
done
以上是部署 etcd 集群的基本步骤,在实际部署中,还需要根据具体情况进行调整。