欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

kubernetes部署-ETCD(一)

程序员文章站 2022-07-13 21:11:41
...
kubernetes部署-ETCD
 

规划

1台controller:  10.167.130.205
2台node: 10.167.130.206,210
 

IP地址 主机名
10.167.130.205 node
10.167.130.206 node1
10.167.130.210 node2
节点 组件
node etcd,kube-apiserver,flanneld,docker,kube-scheduler,kube-controller-manager
node1 etcd,kube-proxy,kubelet,flanneld,docker
node2 etcd,kube-proxy,kubelet,flanneld,docker

准备工作基础环境配置

关防火墙

systemctl stop firewalld && systemctl disable firewalld
setenforce 0 #临时生效

vi /etc/selinux/config

SELINUX=disabled

vim /etc/hosts #修改3台主机的host

10.167.130.205  node
10.167.130.206  node01
10.167.130.210  node02

关闭swap

swapoff -a && sysctl -w vm.swappiness=0

vi /etc/fstab

#UUID=7bff6243-324c-4587-b550-55dc34018ebf swap                    swap    defaults        0 0
cat << EOF | tee /etc/sysctl.d/k8s.conf
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

应用系统配置

sysctl -p /etc/sysctl.d/k8s.conf

安装软件包以及配置安装源

mkdir  /data/docker

#安装必须的包。yum-utils提供了yum-config-manager实用程序,并且device-mapper-persistent-data和lvm2需要devicemapper存储驱动程序。
yum install -y yum-utils device-mapper-persistent-data lvm2
#使用以下命令设置稳定存储库。 您始终需要稳定的存储库,即使您也想从边缘或测试存储库安装构建。
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum -y install docker-ce
docker version

systemctl enable docker.service    
systemctl start docker.service

mkdir -p /etc/docker

tee /etc/docker/daemon.json <<-'EOF'
{"registry-mirrors":["https://registry.docker-cn.com"],"graph": "/data/docker"}
EOF

systemctl daemon-reload
systemctl restart docker

ETCD配置

node node1 node2
10.167.130.205 10.167.130.206 10.167.130.210

软件包安装证书生成工具cfssl:

mkdir /data/ssl -p

cd /data/
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/local/bin/cfssl
mv cfssljson_linux-amd64 /usr/local/bin/cfssljson
mv cfssl-certinfo_linux-amd64 /usr/bin/cfssl-certinfo

cd /data/ssl/

二进制包下载地址:https://github.com/etcd-io/etcd/releases/tag/v3.3.10
下载 etcd-v3.3.10-linux-amd64.tar.gz

生成etc集群所需要的key

mkdir /data/ssl/etcd
cd /data/ssl/etcd

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

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

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

sh etcd.sh

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

# ls *pem
ca-key.pem  ca.pem  server-key.pem  server.pem

部署etcd

3个节点 都操作


mkdir /data/src/
cd /data/src/

mkdir /opt/etcd/{bin,cfg,ssl} -p
tar xf etcd-v3.3.10-linux-amd64.tar.gz
mv etcd-v3.3.10-linux-amd64/{etcd,etcdctl} /opt/etcd/bin/

vim /opt/etcd/cfg/etcd   
# 3台机器 ETCD_NAME 名字不一样  本机IP不一样 
#[Member]
ETCD_NAME="node"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://10.167.130.205:2380" #更改为本机地址
ETCD_LISTEN_CLIENT_URLS="https://10.167.130.205:2379" #更改为本机地址

#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://10.167.130.205:2380" #更改为本机地址
ETCD_ADVERTISE_CLIENT_URLS="https://10.167.130.205:2379" #更改为本机地址
ETCD_INITIAL_CLUSTER="node=https://10.167.130.205:2380,node1=https://10.167.130.206:2380,node2=https://10.167.130.210: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表示加入已有集群


vim   /usr/lib/systemd/system/etcd.service   #添加etcd服务文件

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

[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





cp /data/ssl/etcd/ca*pem  /opt/etcd/ssl/   
cp /data/ssl/etcd/server*pem  /opt/etcd/ssl/
#拷贝生成的etcd证书文件到etcd工作目录里



scp  /data/ssl/etcd/ca*pem  [email protected]:/opt/etcd/ssl/
scp  /data/ssl/etcd/ca*pem  [email protected]:/opt/etcd/ssl/
scp  /data/ssl/etcd/server*pem  [email protected]:/opt/etcd/ssl/
scp  /data/ssl/etcd/server*pem  [email protected]:/opt/etcd/ssl/
scp /usr/lib/systemd/system/etcd.service [email protected]:/usr/lib/systemd/system/etcd.service
scp /usr/lib/systemd/system/etcd.service [email protected]:/usr/lib/systemd/system/etcd.service



systemctl daemon-reload
systemctl enable etcd
systemctl start etcd

检查查看集群状态:

cd /data/ssl/etcd/
/opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://10.167.130.205:2379,https://10.167.130.206:2379,https://10.167.130.210:2379" cluster-health
member 65f49728d3d54972 is healthy: got healthy result from https://192.168.100.100:2379
member d94ba21c17c75ffb is healthy: got healthy result from https://192.168.100.101:2379
member dc51f874259f7894 is healthy: got healthy result from https://192.168.100.102:2379
cluster is healthy

# 如果有问题第一步先看日志:/var/log/message 或 journalctl -u etcd

此处etcd集群遇到了一个问题 205在线显示正常 206 210 在线就显示不正常 这两组一直抢占
最后发现是之前有装过etcd 默认的数据文件default.etcd 已经生成了 咱们从新配置的etcd数据文件被命名成了default.etcd1 我们配置文件又指定的default.etcd 所以会有问题

[[email protected] etcd]# ls
default.etcd default.etcd1
[[email protected] etcd]# pwd
/var/lib/etcd