• 【k8s学习2】二进制文件方式安装 Kubernetes之etcd集群部署


    (一)准备工作

    (1.1)准备3台centos7 系统的服务器,master最低要求2核1G内存。

    (1.2)修改服务器的hostname

    192.168.52.21

    hostname  master01

    echo "master01"  > /etc/hostname

    192.168.52.22

    hostname  master02

    echo "master02"  > /etc/hostname

    192.168.52.23

    hostname  node01

    echo "node01"  > /etc/hostname

    或者

    192.168.52.21

    hostnamectl  set-hostname master01

    使用这个命令会覆盖到dns配置所需要重新设置dns

    echo "nameserver 114.114.114.114">/etc/resolv.conf

    192.168.52.22

    hostnamectl  set-hostname master02

    echo "nameserver 114.114.114.114">/etc/resolv.conf

    192.168.52.23

    hostnamectl  set-hostname node01

    echo "nameserver 114.114.114.114">/etc/resolv.conf

    (1.3)设置本地hosts

    cat > /etc/hosts <

    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    master01 192.168.52.21
    master02 192.168.52.22
    node 192.168.52.23

    EOF

    (1.4)关闭防火墙

     systemctl stop firewalld
    systemctl disable firewalld

    (1.5)关闭selinux

    # 重启永久关闭selinux
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
    # 当前环境临时关闭selinux
    setenforce 0

     (1.6)关闭swap分区

    # 临时关闭swap
    swapoff -a
    # 永久关闭swap
    sed -ri 's/.*swap.*/#&/' /etc/fstab

    (1.7)将桥接的IPv4流量传递到iptables的链

     # 覆盖写入文件内容
    cat > /etc/sysctl.d/k8s.conf << EOF
    net.bridge.bridge-nf-call-ip6tables = 1
    net.bridge.bridge-nf-call-iptables = 1
    EOF
    # 配置生效
    sysctl --system

    (1.8)时间同步

     # 1、安装ntpdate
    yum install ntpdate -y
    # 2、手动同步时间
    ntpdate time.windows.com
    ntpdate ntp1.aliyun.com
    # 3、定时同步时间任务
    echo " */10 * * * * /usr/sbin/ntpdate -u ntp1.aliyun.com > /dev/null 2>&1 " \
    >> /var/spool/cron/root

    (1.9)升级systemd

    yum  -y update systemd

    (1.10) 安装一些基础包

    yum -y install gcc gcc-c++  ipvsadm  wget 

    (二)部署etcd集群

    (2.1)下载etcd二进制文件

    两台服务器都要下载部署etcd文件,步骤一样。

    官方参考文档

    Release v3.4.13 · etcd-io/etcd · GitHub

    linux部署脚本里面有下载地址,脚本如下

    https://storage.googleapis.com/etcd/v3.4.13/etcd-v3.4.13-linux-amd64.tar.gz

    https://github.com/etcd-io/etcd/releases/download/v3.4.13/etcd-v3.4.13-linux-amd64.tar.gz

    1. #官方文档的部署脚本
    2. ETCD_VER=v3.4.13
    3. # choose either URL
    4. GOOGLE_URL=https://storage.googleapis.com/etcd
    5. GITHUB_URL=https://github.com/etcd-io/etcd/releases/download
    6. DOWNLOAD_URL=${GOOGLE_URL}
    7. rm -f /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
    8. rm -rf /tmp/etcd-download-test && mkdir -p /tmp/etcd-download-test
    9. curl -L ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz -o /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
    10. tar xzvf /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz -C /tmp/etcd-download-test --strip-components=1
    11. rm -f /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
    12. /tmp/etcd-download-test/etcd --version
    13. /tmp/etcd-download-test/etcdctl version

    解压后将etcd 和 /etcdctl 都复制到/usr/bin 目录

    将etcd 部署为systemd的服务,创建配置文件。

     cat > /usr/lib/systemd/system/etcd.service <

    [Unit]
    Description=etcd key-value store
    Documentation=https://github.com/etcd-io/etcd
    After=network.target
    
    [Service]
    EnvironmentFile=/etc/etcd/etcd.conf
    ExecStart=/usr/bin/etcd
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    

    EOF

     (2.2)配置etcd证书

       2.2.1、创建根证书

    所有的证书全部在mater01节点创建,然后拷贝到master02对应的目录。

    由于我们测试环境没有统一的ca认证,所以需要使用自签名证书来完成安全配置,etcd 和 Kubernets需要继续根证书来创建自己的ca证书。根证书即签发机构。

     mkdir -p  /etc/kubernetes/pki

    cd  /etc/kubernetes/pki
    openssl genrsa -out ca.key 2048

    openssl req -x509 -new -nodes -key ca.key -subj "/CN=192.168.52.21" -days 36500 -out ca.crt

     2.2.2、创建etcd的服务端ca证书

    所有的证书全部在mater01节点创建,然后拷贝到master02对应的目录。

    先创建一个x509 v3配置文件

    mkdir -p /root/etcd/

    cd  /root/etcd/

    cat > etcd_ssl.cnf <

    [ req ]

    req_extensions = v3_req

    distinguished_name = req_distinguished_name

    [ req_distinguished_name ]

    [ v3_req ]

    basicConstraints = CA:FALSE

    keyUsage = nonRepudiation, digitalSignature, keyEncipherment

    subjectAltName = @alt_names

    [ alt_names ]

    IP.1 = 192.168.52.21 

    IP.2 = 192.168.52.22

    EOF

    下面开始根据配置文件创建etcd服务端CA证书,注意如果创建根证书那一步忘记这边就会出错。

    cd /root/etcd/

    openssl genrsa -out etcd_server.key 2048

    openssl req -new -key etcd_server.key -config etcd_ssl.cnf -subj "/CN=etcd-server" -out etcd_server.csr

    openssl x509 -req -in etcd_server.csr -CA /etc/kubernetes/pki/ca.crt -CAkey /etc/kubernetes/pki/ca.key -CAcreateserial -days 36500 -extensions v3_req -extfile etcd_ssl.cnf -out etcd_server.crt

    mkdir -p /etc/etcd/pki

    cp etcd_server.crt     /etc/etcd/pki/

    cp etcd_server.key   /etc/etcd/pki/

    2.2.3、创建etcd客户端ca证书

    所有的证书全部在mater01节点创建,然后拷贝到master02对应的目录。

    这个主要是给后续kube-apiserver链接etcd时使用。

    openssl genrsa -out etcd_client.key 2048

    openssl req -new -key etcd_client.key -config etcd_ssl.cnf -subj "/CN=etcd-client" -out etcd_client.csr

    openssl x509 -req -in etcd_client.csr -CA /etc/kubernetes/pki/ca.crt -CAkey /etc/kubernetes/pki/ca.key -CAcreateserial -days 36500 -extensions v3_req -extfile etcd_ssl.cnf -out etcd_client.crt

    cp etcd_client.key /etc/etcd/pki/
    cp etcd_client.crt /etc/etcd/pki/

    (2.3)etcd配置文件

    前面我们将etcd设置为systemd服务时,指定了环境变量文件EnvironmentFile=/etc/etcd/etcd.conf

    所以我们现在要创建一个/etc/etcd/etcd.conf配置文件。内容如下

    #192.168.52.21 的/etc/etcd/etcd.conf

    ETCD_NAME=etcd1
    ETCD_DATA_DIR=/etc/etcd/data

    ETCD_CERT_FILE=/etc/etcd/pki/etcd_server.crt
    ETCD_KEY_FILE=/etc/etcd/pki/etcd_server.key
    ETCD_TRUSTED_CA_FILE=/etc/kubernetes/pki/ca.crt
    ETCD_CLIENT_CERT_AUTH=true
    ETCD_LISTEN_CLIENT_URLS=https://192.168.52.21:2379
    ETCD_ADVERTISE_CLIENT_URLS=https://192.168.52.21:2379
    ETCD_PEER_CERT_FILE=/etc/etcd/pki/etcd_server.crt
    ETCD_PEER_KEY_FILE=/etc/etcd/pki/etcd_server.key
    ETCD_PEER_TRUSTED_CA_FILE=/etc/kubernetes/pki/ca.crt
    ETCD_LISTEN_PEER_URLS=https://192.168.52.21:2380
    ETCD_INITIAL_ADVERTISE_PEER_URLS=https://192.168.52.21:2380

    ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster
    ETCD_INITIAL_CLUSTER="etcd1=https://192.168.52.21:2380,etcd2=https://192.168.52.22:2380"
    ETCD_INITIAL_CLUSTER_STATE=new

      #192.168.52.22的/etc/etcd/etcd.conf

    ETCD_NAME=etcd2
    ETCD_DATA_DIR=/etc/etcd/data

    ETCD_CERT_FILE=/etc/etcd/pki/etcd_server.crt
    ETCD_KEY_FILE=/etc/etcd/pki/etcd_server.key
    ETCD_TRUSTED_CA_FILE=/etc/kubernetes/pki/ca.crt
    ETCD_CLIENT_CERT_AUTH=true
    ETCD_LISTEN_CLIENT_URLS=https://192.168.52.22:2379
    ETCD_ADVERTISE_CLIENT_URLS=https://192.168.52.22:2379
    ETCD_PEER_CERT_FILE=/etc/etcd/pki/etcd_server.crt
    ETCD_PEER_KEY_FILE=/etc/etcd/pki/etcd_server.key
    ETCD_PEER_TRUSTED_CA_FILE=/etc/kubernetes/pki/ca.crt
    ETCD_LISTEN_PEER_URLS=https://192.168.52.22:2380
    ETCD_INITIAL_ADVERTISE_PEER_URLS=https://192.168.52.22:2380

    ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster
    ETCD_INITIAL_CLUSTER="etcd1=https://192.168.52.21:2380,etcd2=https://192.168.52.22:2380"
    ETCD_INITIAL_CLUSTER_STATE=new

     其中

    ETCD_LISTEN_CLIENT_URLS /ETCD_ADVERTISE_CLIENT_URLS 是为客户端提供服务的监听url

    ETCD_LISTEN_PEER_URLS/ETCD_INITIAL_ADVERTISE_PEER_URLS为本集群其他节点提供服务的监听url

    ETCD_INITIAL_CLUSTER 是集群全部节点endpoint列表。

    ETCD_INITIAL_CLUSTER_STATE 为初始集群状态,新建为new,已存在时为existing

    ETCD_CLIENT_CERT_AUTH 是否启用客户端证书认证。

    记忆的小技巧,带CLIENT是客户端配置,PEER是集群之间的配置,不带这两个的是服务端配置

    如果上述步骤都做好了的话就可以直接启动了。

    (2.4)启动etcd机器并验证集群状态

    启动:

    systemctl restart etcd 

    systemctl enable etcd

    验证:

    etcdctl --cacert=/etc/kubernetes/pki/ca.crt --cert=/etc/etcd/pki/etcd_client.crt --key=/etc/etcd/pki/etcd_client.key --endpoints=https://192.168.52.21:2379,https://192.168.52.22:2379 endpoint health

     以上启用了https的etcd节点部署完成。下一步是部署kubernetes服务,下一篇再记录。

  • 相关阅读:
    Nomad系列-Nomad网络模式
    本地缓存 guava
    【Xshell】ssh连接卡在To escape to local shell, press ‘Ctrl+Alt+]‘处理办法[亲测好使]
    Docker容器镜像
    SQL语句实现模糊查询
    Spring 控制反转和依赖注入
    分享 6 个 Vue3 开发必备的 VSCode 插件
    2_dataset, dataloader
    使用QGIS转换矢量数据投影
    css问题
  • 原文地址:https://blog.csdn.net/qq_26711103/article/details/126562791