环境

系统版本:
CentOS Linux release 7.4.1708 (Core)
docker: Docker version 1.13.1
etcd:etcd-v3.3.10-linux-amd64
flannel:flannel-v0.11.0-linux-amd64
服务器角色、IP、组件:
master
192.168.254.137 kube-apiserver,kube-controller-manager,kube-scheduler,etcd

node1
192.168.254.136 kubelet,kube-proxy,docker,flannel,etcd

node2
192.168.254.135 kubelet,kube-proxy,docker,flannel

创建:
mkdir /opt/etcd/{bin,cfg,ssl}
mkdir /opt/k8s/{bin,cfg,ssl}
bin存放可执行文件 cfg存放配置文件 ssl存放证书文件

注:机器资源有限,简洁实验配置过程

生成etcd证书

1、下载cfss工具,用来生成证书

curl -L https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 -o /usr/local/bin/cfssl
curl -L https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 -o /usr/local/bin/cfssljson
curl -L https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 -o /usr/local/bin/cfssl-certinfo
chmod  +x   /usr/local/bin/cfssl  cfssljson   cfssl-certinfo

2、生成etcd证书
为了方便 把这些文件都放在ssl里面

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 <<EOF
{
    "CN": "etcd CA",
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "L": "Beijing",
            "ST": "Beijing"
        }
    ]
}
EOF

注意:下面的host是etcd集群的节点地址
cat > server-csr.json <<EOF
{
    "CN": "etcd",
    "hosts": [
    "192.168.254.137",      
    "192.168.254.136",
    "192.168.254.135"
    ],
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "L": "BeiJing",
            "ST": "BeiJing"
        }
    ]
}
EOF

生成证书:
# cask 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集群

1、下载etcd包
可以从官网下载
https://github.com/etcd-io/etcd/releases
或者
http://swc.swcc.xyz/kubernetes/package/etcd-v3.2.12-linux-amd64.tar.gz?preview
以下部署步骤在规划的三个etcd节点操作一样,唯一不同的是etcd配置文件中的服务器IP要写当前的
2、加压缩,拷贝执行文件
etcd启动etcd的主要程序 etcdctl 管理etcd客户端命令

mv etcd-v3.3.2-linux-amd64/{etcd,etcdctl}  /opt/etcd/bin/cfg

3、增加配置项
(1)创建etcd配合文件

# cat /opt/etcd/cfg/etcd   
#[Member]
ETCD_NAME="etcd01"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.254.137:2380"
ETCD_LISTEN_CLIENT_URLS="https://192.168.254.137:2379"

#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.254.137:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.254.137:2379"
ETCD_INITIAL_CLUSTER="etcd01=https://192.168.254.137:2380,etcd02=https://192.168.254.136:2380,etcd03=https://192.168.254.135: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表示加入已有集群
 
(2)systemd管理etcd:
# cat /usr/lib/systemd/system/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  


这是一个节点部署完成了!!!

(4)为了简化操作,把所有文件都拷贝到其他要部署的etcd节点上
scp -r /opt/etcd/ root@192.168.254.135:/opt/
scp -r /opt/etcd/ root@192.168.254.136:/opt/

scp /usr/lib/systemd/system/etcd.service root@192.168.254.135:/usr/lib/systemd/system/
scp /usr/lib/systemd/system/etcd.service root@192.168.254.136:/usr/lib/systemd/system/

注意修改节点名称和ip

(5)各个节点启动etcd服务

systemctl daemon-reload    #这个是systemd的配置文件
systemctl enable etcd
systemctl restart etcd

(6)查看集群状态

# /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://192.168.254.137:2379,https://192.168.254.136:2379,https://192.168.254.135:2379" \
cluster-health

注意:
2379是数据之间的端口
2380是集群之间的端口

在node节点安装doker

yum -y install docker
systemctl start docker
systemctl enable docker

部署flannel网络

原理图:
在这里插入图片描述
1、写入预定义子网段
Falnnel要用etcd存储自身一个子网信息,所以要保证能成功连接Etcd

# /opt/etcd/bin/etcdctl \
--ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem \
--endpoints="https://192.168.254.137:2379,https://192.168.254.136:2379,https://192.168.254.135:2379" \
set /coreos.com/network/config  '{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}'

2、下载包,并将执行文件移动到自定义位置
http://swc.swcc.xyz/kubernetes/package/flannel-v0.10.0-linux-amd64.tar.gz?preview

tar xf flannel-v0.11.0-linux-amd64.tar.gz
mv flanneld mk-docker-opts.sh /opt/k8s/bin

3、添加、修改配置文件
(1)配置Flannel(注意ip地址):

# cat /opt/k8s/cfg/flanneld
FLANNEL_OPTIONS="--etcd-endpoints=https://192.168.254.137:2379,https://192.168.254.136:2379,https://192.168.254.136:2379 -etcd-cafile=/opt/etcd/ssl/ca.pem -etcd-certfile=/opt/etcd/ssl/server.pem -etcd-keyfile=/opt/etcd/ssl/server-key.pem"

(2)配置Flannel(不用再修改):

# cat /usr/lib/systemd/system/flanneld.service
[Unit]
Description=Flanneld overlay address etcd agent
After=network-online.target network.target
Before=docker.service

[Service]
Type=notify
EnvironmentFile=/opt/k8s/cfg/flanneld
ExecStart=/opt/k8s/bin/flanneld --ip-masq $FLANNEL_OPTIONS
ExecStartPost=/opt/k8s/bin/mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/subnet.env
Restart=on-failure

[Install]
WantedBy=multi-user.target

(3)配置docker启动指定子网段
# cat /usr/lib/systemd/system/docker.service 

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target

[Service]
Type=notify
EnvironmentFile=/run/flannel/subnet.env
ExecStart=/usr/bin/dockerd $DOCKER_NETWORK_OPTIONS
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s

[Install]
WantedBy=multi-user.target

EnvironmentFile=/run/flannel/subnet.env 【读取flannel生成的子网】
ExecStart=/usr/bin/dockerd $DOCKER_NETWORK_OPTIONS 【启动docker时引用的变量】

(4)重启flannel和docker

# systemctl daemon-reload
# systemctl start flanneld
# systemctl enable flanneld
# systemctl restart docker

(5)查看ip
docker的ip和flannel的ip在同一个网段
在这里插入图片描述
在一个节点上的flannel配置完成了!!!!

(5)为了减少配置可以将文件直接拷贝到其他节点上

scp -r /opt/k8s/ root@192.168.254.136:/opt/
scp -r /opt/k8s/ root@192.168.254.135:/opt/

scp /usr/lib/systemd/system/flanneld.service root@192.168.254.136:/usr/lib/systemd/system/flanneld.service
scp /usr/lib/systemd/system/flanneld.service root@192.168.254.135:/usr/lib/systemd/system/flanneld.service

scp /usr/lib/systemd/system/docker.service root@192.168.254.136:/usr/lib/systemd/system/docker.service
scp /usr/lib/systemd/system/docker.service root@192.168.254.135:/usr/lib/systemd/system/docker.service

(6)并在其他节点上

重启flannel和docker:
# systemctl daemon-reload
# systemctl start flanneld
# systemctl enable flanneld
# systemctl restart docker

到此,flannel节点集群配置完成!!!!!

(7)集群测试
node1节点上的ip
在这里插入图片描述
node2节点上的ip
在这里插入图片描述
不同节点的docker互通
在这里插入图片描述

Logo

更多推荐