搭建一个完整的Kubernetes集群---安装etcd集群

**1. 自签Etcd SSL证书
**
服务器规划与组件安装

10.100.97.78 master1
kube-apiserver
kube-controller-manager
kube-scheduler
etcd

10.100.97.79 node1
kubelet
kube-proxy
docker
etcd

10.100.97.55 node2
kubelet
kube-proxy
docker
etcd

下载cfssl工具,用于生成自签证书

wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64
wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64
chmod +x cfssl_linux-amd64 cfssljson_linux-amd64 cfssl-certinfo_linux-amd64
mv cfssl_linux-amd64 /usr/bin/cfssl
mv cfssljson_linux-amd64 /usr/bin/cfssljson
mv cfssl-certinfo_linux-amd64 /usr/bin/cfssl-certinfo

创建证书json文件

mkdir etcd_ca
cd etcd_ca

创建 Etcd根证书配置文件

cat > ca-config.json <<EOF
{
  "signing": {
    "default": {
      "expiry": "87600h"
    },
    "profiles": {
      "www": {
         "expiry": "87600h",
         "usages": [
            "signing",
            "key encipherment",
            "server auth",
            "client auth"
        ]
      }
    }
  }
}
EOF

创建 Etcd根证书请求文件

cat > ca-csr.json <<EOF
{
    "CN": "etcd CA",
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "L": "Shanghai",
            "ST": "Shanghai"
        }
    ]
}
EOF

创建 Etcd服务证书请求文件

cat > server-csr.json <<EOF
{
    "CN": "etcd",
    "hosts": [
        "10.100.97.78",
        "10.100.97.79",
        "10.100.97.55"
        ],
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "L": "BeiJing",
            "ST": "BeiJing"
        }
    ]
}
EOF

├── ca-config.json
├── ca-csr.json
└── server-csr.json

生成 Etcd根证书

cfssl gencert -initca ca-csr.json | cfssljson -bare ca -

搭建一个完整的Kubernetes集群---安装etcd集群

生成 Etcd服务证书

cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | cfssljson -bare server

搭建一个完整的Kubernetes集群---安装etcd集群

2.部署三个Etcd节点
下载etcd二进制包

wget https://github.com/etcd-io/etcd/releases/download/v3.3.18/etcd-v3.3.18-linux-amd64.tar.gz"
mkdir /opt/etcd/{bin,cfg,ssl} -p
tar zxvf etcd-v3.3.18-linux-amd64.tar.gz
mv etcd-v3.3.18-linux-amd64/{etcd,etcdctl} /opt/etcd/bin/
cp ~/etcd_ca/*.pem /opt/etcd/ssl/

创建启动eted配置文件

vim etcd.service 

[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
EnvironmentFile=/opt/etcd/cfg/etcd
ExecStart=/opt/etcd/bin/etcd \
--name=${ETCD_NAME} \
--data-dir=${ETCD_DATA_DIR} \
--listen-peer-urls=${ETCD_LISTEN_PEER_URLS} \
--listen-client-urls=${ETCD_LISTEN_CLIENT_URLS},http://127.0.0.1:2379 \
--advertise-client-urls=${ETCD_ADVERTISE_CLIENT_URLS} \
--initial-advertise-peer-urls=${ETCD_INITIAL_ADVERTISE_PEER_URLS} \
--initial-cluster=${ETCD_INITIAL_CLUSTER} \
--initial-cluster-token=${ETCD_INITIAL_CLUSTER_TOKEN} \
--initial-cluster-state=new \
--cert-file=/opt/etcd/ssl/server.pem \
--key-file=/opt/etcd/ssl/server-key.pem \
--peer-cert-file=/opt/etcd/ssl/server.pem \
--peer-key-file=/opt/etcd/ssl/server-key.pem \
--trusted-ca-file=/opt/etcd/ssl/ca.pem \
--peer-trusted-ca-file=/opt/etcd/ssl/ca.pem
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target

上面启动报错( conflicting environment variable "ETCD_NAME" is shadowed by corresponding command-line flag (either unset environment variable or disable flag)),可去掉NAME

[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
Type=notify
EnvironmentFile=/opt/etcd/cfg/etcd.conf
ExecStart=/opt/etcd/bin/etcd \
--cert-file=/opt/etcd/ssl/server.pem \
--key-file=/opt/etcd/ssl/server-key.pem \
--peer-cert-file=/opt/etcd/ssl/server.pem \
--peer-key-file=/opt/etcd/ssl/server-key.pem \
--trusted-ca-file=/opt/etcd/ssl/ca.pem \
--peer-trusted-ca-file=/opt/etcd/ssl/ca.pem
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

创建etcd配置文件

#[Member]
ETCD_NAME="etcd01"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://10.100.97.78:2380"    
ETCD_LISTEN_CLIENT_URLS="https://10.100.97.78:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://10.100.97.78:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://10.100.97.78:2379"
ETCD_INITIAL_CLUSTER="etcd01=https://10.100.97.78:2380,etcd02=https://10.100.97.79:2380,etcd03=https://10.100.97.55:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"



#参数说明
ETCD_NAME 节点名称
ETCD_DATA_DIR 数据目录
ETCD_LISTEN_PEER_URLS 集群通信监听地址
ETCD_LISTEN_CLIENT_URLS 客户端访问监听地址
ETCD_INITIAL_ADVERTISE_PEER_URLS 集群通告地址
ETCD_ADVERTISE_CLIENT_URLS 客户端通告地址
ETCD_INITIAL_CLUSTER 集群节点地址
ETCD_INITIAL_CLUSTER_TOKEN 集群Token
ETCD_INITIAL_CLUSTER_STATE 加入集群的当前状态,new是新集群,existing表示加入已有集群

分别拷贝到另外二个节点

scp -r /opt/etcd/ root@10.100.97.79:/opt/
scp -r /opt/etcd/ root@10.100.97.55:/opt/

scp /usr/local/src/etcd.service root@10.100.97.79:/usr/lib/systemd/system
scp /usr/local/src/etcd.service root@10.100.97.55:/usr/lib/systemd/system

搭建一个完整的Kubernetes集群---安装etcd集群

登陆二个节点修改配置文件 名称和IP

搭建一个完整的Kubernetes集群---安装etcd集群

systemctl daemon-reload
systemctl start etcd
systemctl enable etcd 

 /opt/etcd/bin/etcdctl --ca-file=/opt/etcd/ssl/ca.pem --cert-file=/opt/etcd/ssl/server.pem --key-file=/opt/etcd/ssl/server-key.pem --endpoints="https://10.100.97.78:2379,https://10.100.97.79:2379,https://10.100.97.55:2379" cluster-health

搭建一个完整的Kubernetes集群---安装etcd集群

上一篇:Kubernetes中用Helm安装Ceph存储服务


下一篇:VSTO之旅系列(四):创建Word解决方案