Kubernetes二进制部署

关于kubernetes社区有很多解释,这里就不在过多叙述。

k8s基于etcd和flannel,所以我们先来把这两个服务做好

准备工作:配置域名解析,配置静态ip

操作系统:CentOS7

软件版本:Docker 19.03.9 Kubernetes 1.11.10

机器配置要求:视情况而定(越大越好)

服务器角色IP组件
k8s-master1192.168.119.144kube-apiserver,kube-controller-manager,kube-scheduler,etcd
k8s-node1192.168.119.145kubelet,kube-proxy,docker,flannel,etcd
k8s-node2192.168.119.146kubelet,kube-proxy,docker,flannel,etcd

三台机器都要做

192.168.119.144
# hostnamectl set-hostname k8s-master
# vim /etc/hosts
192.168.119.144 k8s-master
192.168.119.145 k8s-node01
192.168.119.146 k8s-node02

192.168.119.145
# hostnamectl set-hostname k8s-node01
# vim /etc/hosts
192.168.119.144 k8s-master
192.168.119.145 k8s-node01
192.168.119.146 k8s-node02

192.168.119.146
# hostnamectl set-hostname k8s-node02
# vim /etc/hosts
192.168.119.144 k8s-master
192.168.119.145 k8s-node01
192.168.119.146 k8s-node02

静态解析

主节点master
# cd /etc/sysconfig/network-scripts/
# cp ifcfg-ens33 ifcfg-ens33.bak		#做之前先备份
# vim ifcfg-ens33
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"     #修改为静态ip地址 static或none     动态获取IP地址是dhcp
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="c975809c-317c-4142-8f0a-978238b21e17"
DEVICE="ens33"
ONBOOT="yes"
IPADDR=192.168.119.144   	#指定ip地址
GATEWAY=192.168.119.2     	#指定网关
NETMASK=255.255.255.0       #设置子网掩码   还有一种方式
DNS1=8.8.8.8   		        #DNS设置为自己网关
# systemctl restart network		#重启网卡,加载网卡配置信息

# 其余两台一样

# 配置免密
ssh-keygen   #一直回车
ssh-copy-id k8s-master
ssh-copy-id k8s-node01
ssh-copy-id k8s-node01

一、生成自签名

我们使用cfssl来生成自签证书,任何机器都行,证书这块儿知道怎么生成、怎么用即可,暂且不用过多研究。

下载cfssl工具

由于国内网速下载太慢,需要翻墙。所以我已经提前下载下来了

# mkdir k8s-bao			#创建一个目录把自签名文件和包放到这个目录下
# cd k8s-bao/
# wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64^C
# wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64^C
# wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64^C
# rz				#上传文件
# ls				#可以看到上传的三个文件
# 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

生成Etcd证书:

创建一个文件夹etcd,创建这三个文件生成密匙的配置文件

# vim ca-config.json
{
  "signing": {
    "default": {
      "expiry": "87600h"
    },
    "profiles": {
      "www": {
         "expiry": "87600h",
         "usages": [
            "signing",
            "key encipherment",
            "server auth",
            "client auth"
        ]
      }
    }
  }
}
# vim ca-csr.json
{
    "CN": "etcd CA",
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "L": "Beijing",
            "ST": "Beijing"
        }
    ]
}
# vim server-csr.json
{
    "CN": "etcd",
    "hosts": [
    "192.168.119.144",
    "192.168.119.145",
    "192.168.119.146"
    ],
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "L": "BeiJing",
            "ST": "BeiJing"
        }
    ]
}

生成证书:

# cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
2019/11/05 21:28:30 [INFO] generating a new CA key and certificate from CSR
2019/11/05 21:28:30 [INFO] generate received request
2019/11/05 21:28:30 [INFO] received CSR
2019/11/05 21:28:30 [INFO] generating key: rsa-2048
2019/11/05 21:28:31 [INFO] encoded CSR
2019/11/05 21:28:31 [INFO] signed certificate with serial number 81435836147677695014359226267989679249541043678

# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | cfssljson -bare server
2019/11/05 21:29:06 [INFO] generate received request
2019/11/05 21:29:06 [INFO] received CSR
2019/11/05 21:29:06 [INFO] generating key: rsa-2048
2019/11/05 21:29:06 [INFO] encoded CSR
2019/11/05 21:29:07 [INFO] signed certificate with serial number 100762703930809881352937340626683611193591863095
2019/11/05 21:29:07 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable for
websites. For more information see the Baseline Requirements for the Issuance and Management
of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (https://cabforum.org);
specifically, section 10.2.3 ("Information Requirements").

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

二、部署Etcd

二进制包下载地址:https://github.com/coreos/etcd/releases/tag/v3.2.12

以下部署步骤在规划的三个etcd节点操作一样,唯一不同的是etcd配置文件中的服务器IP要写当前的:

解压二进制包:由于包已经提前下载好了,在这里不在下载。自己可以进去看一看

master:

# mkdir /opt/etcd/{bin,cfg,ssl} -p
# rz									#上传提前下载好的压缩包

# ls
anaconda-ks.cfg  etcd-v3.2.12-linux-amd64.tar.gz  k8s-bao  mysql-5.7.24  mysql-boost-5.7.24.tar.gz
# tar xf etcd-v3.2.12-linux-amd64.tar.gz
# mv etcd-v3.2.12-linux-amd64/{etcd,etcdctl} /opt/etcd/bin/

创建etcd配置文件:

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

#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS=https://192.168.119.144:2380
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.119.144:2379"
ETCD_INITIAL_CLUSTER="etcd01=https://192.168.119.144:2380,etcd02=https://192.168.119.145:2380,etcd03=https://192.168.119.146: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表示加入已有集群

systemd管理etcd:

# vim /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

node01:

# mkdir /opt/etcd/{bin,cfg,ssl} -p
# rz

# ls
anaconda-ks.cfg  etcd-v3.2.12-linux-amd64.tar.gz  k8s-bao  mysql-5.7.24  mysql-boost-5.7.24.tar.gz
# tar xf etcd-v3.2.12-linux-amd64.tar.gz
# mv etcd-v3.2.12-linux-amd64/{etcd,etcdctl} /opt/etcd/bin/

创建etcd配置文件:

# vim /opt/etcd/cfg/etcd
#[Member]
ETCD_NAME="etcd02"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS=https://192.168.119.145:2380
ETCD_LISTEN_CLIENT_URLS="https://192.168.119.145:2379"

#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS=https://192.168.119.145:2380
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.119.145:2379"
ETCD_INITIAL_CLUSTER="etcd01=https://192.168.119.144:2380,etcd02=https://192.168.119.145:2380,etcd03=https://192.168.119.146:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"

systemd管理etcd:

# vim /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

node02:

# mkdir /opt/etcd/{bin,cfg,ssl} -p
# rz

# ls
anaconda-ks.cfg  etcd-v3.2.12-linux-amd64.tar.gz  k8s-bao  mysql-5.7.24  mysql-boost-5.7.24.tar.gz
# tar xf etcd-v3.2.12-linux-amd64.tar.gz
# mv etcd-v3.2.12-linux-amd64/{etcd,etcdctl} /opt/etcd/bin/

创建etcd配置文件:

# vim /opt/etcd/cfg/etcd
#[Member]
ETCD_NAME="etcd03"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS=https://192.168.119.146:2380
ETCD_LISTEN_CLIENT_URLS="https://192.168.119.146:2379"

#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS=https://192.168.119.146:2380
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.119.146:2379"
ETCD_INITIAL_CLUSTER="etcd01=https://192.168.119.144:2380,etcd02=https://192.168.119.145:2380,etcd03=https://192.168.119.146:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"

systemd管理etcd:

# vim /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

把刚才生成的证书拷贝到配置文件中的位置:

# cd k8s-bao/
# ls
ca-config.json  ca.csr  ca-csr.json  ca-key.pem  ca.pem  server.csr  server-csr.json  server-key.pem  server.pem
# cp ca*pem server*pem /opt/etcd/ssl
# ls /opt/etcd/ssl
ca-key.pem  ca.pem  server-key.pem  server.pem

把刚刚的证书也要拷贝到node01、node02两个节点上:

# scp ca*pem server*pem k8s-node01:/opt/etcd/ssl
The authenticity of host 'k8s-node01 (192.168.119.145)' can't be established.
ECDSA key fingerprint is SHA256:7TA8Iz+ks7dTjI8yBsrw8vx+Dk3w2bSpVFzUVs6QNp4.
ECDSA key fingerprint is MD5:2a:26:a8:30:8d:43:03:04:9d:c0:4a:57:0d:8f:b0:81.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'k8s-node01,192.168.119.145' (ECDSA) to the list of known hosts.
root@k8s-node01's password: 
ca-key.pem                                                                                                           100% 1679   379.0KB/s   00:00    
ca.pem                                                                                                               100% 1265     1.2MB/s   00:00    
server-key.pem                                                                                                       100% 1675     1.3MB/s   00:00    
server.pem                                                                                                           100% 1338   883.0KB/s   00:00    

# scp ca*pem server*pem k8s-node02:/opt/etcd/ssl
The authenticity of host 'k8s-node02 (192.168.119.146)' can't be established.
ECDSA key fingerprint is SHA256:7TA8Iz+ks7dTjI8yBsrw8vx+Dk3w2bSpVFzUVs6QNp4.
ECDSA key fingerprint is MD5:2a:26:a8:30:8d:43:03:04:9d:c0:4a:57:0d:8f:b0:81.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'k8s-node02,192.168.119.146' (ECDSA) to the list of known hosts.
root@k8s-node02's password: 
ca-key.pem                                                                                                           100% 1679   642.0KB/s   00:00    
ca.pem                                                                                                               100% 1265    93.1KB/s   00:00    
server-key.pem                                                                                                       100% 1675   100.3KB/s   00:00    
server.pem                                                                                                           100% 1338    85.3KB/s   00:00    

去到两个node节点上查看一下是否发送过去:

# ls /opt/etcd/ssl/
ca-key.pem  ca.pem  server-key.pem  server.pem
# ls /opt/etcd/ssl/
ca-key.pem  ca.pem  server-key.pem  server.pem

三台etcd启动并设置开启启动:

# systemctl daemon-reload
# systemctl start etcd
# systemctl enable etcd
# systemctl stop firewalld		#起不来关闭防火墙

etcd部署完成后,检查etcd集群状态(3个节点都需要测试):

# /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.119.144:2379,https://192.168.119.144:2379,https://192.168.119.144:2379" \
cluster-health
输出一下信息为成功

member 7d5b4d9d0e873c35 is healthy: got healthy result from https://192.168.119.144:2379
member 94206f83b3600ff9 is healthy: got healthy result from https://192.168.119.146:2379
member e0f8a925b8fcbbb2 is healthy: got healthy result from https://192.168.119.145:2379
cluster is healthy

三个节点都要和图中一样,就说明etcd集群部署成功

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

三、部署docker

在两台node节点上部署

node01node02

下载地址:https://download.docker.com/linux/static/stable/x86_64/docker-19.03.9.tgz 以下在所有节点操作。这里采用二进制安装,用apt安装也一样。

# 解压安装
# wget https://download.docker.com/linux/static/stable/x86_64/docker-19.03.9.tgz
# tar zxvf docker-19.03.9.tgz
# mv docker/* /usr/bin

# systemd管理docker
# cat > /lib/systemd/system/docker.service << EOF
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target

[Service]
Type=notify
ExecStart=/usr/bin/dockerd
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
EOF

# 创建配置文件
# mkdir /etc/docker
# cat > /etc/docker/daemon.json << EOF
{
  "registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF
#registry-mirrors 阿里云镜像加速器
#启动并设置开机启动
# systemctl daemon-reload
# systemctl start docker
# systemctl enable docker

四、部署Flannel网络

主节点执行(master)

Falnnel要用etcd存储自身一个子网信息,所以要保证能成功连接Etcd,写入预定义子网段:

在主节点的/opt/etcd/ssl下执行

# cd /opt/etcd/ssl
# /opt/etcd/bin/etcdctl \
 --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem \
 --endpoints="https://192.168.119.144:2379,https://192.168.119.145:2379,https://192.168.119.146:2379" \
 set /coreos.com/network/config  '{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}'
# 输出一下为成功
{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}

开始部署:

两个node节点上部署就行

下载二进制包:wget https://github.com/coreos/flannel/releases/download/v0.11.0/flannel-v0.11.0-linux-amd64.tar.gz

包已经提前下载好了,我们上传就行了

node01

# rz

# ls
anaconda-ks.cfg           etcd-v3.2.12-linux-amd64.tar.gz     mysql-5.7.24
etcd-v3.2.12-linux-amd64  flannel-v0.11.0-linux-amd64.tar.gz  mysql-boost-5.7.24.tar.gz
# tar xf flannel-v0.11.0-linux-amd64.tar.gz
# mkdir -pv /opt/kubernetes/bin
mkdir: created directory ‘/opt/kubernetes’
mkdir: created directory ‘/opt/kubernetes/bin’
# cp flanneld mk-docker-opts.sh /opt/kubernetes/bin

配置flannel:

# mkdir -pv /opt/kubernetes/cfg/
# vim /opt/kubernetes/cfg/flanneld
FLANNEL_OPTIONS="--etcd-endpoints=https://192.168.119.144:2379,https://192.168.119.145:2379,https://192.168.119.146:2379 -etcd-cafile=/opt/etcd/ssl/ca.pem -etcd-certfile=/opt/etcd/ssl/server.pem -etcd-keyfile=/opt/etcd/ssl/server-key.pem"

systemd管理Flannel:

# vim /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/kubernetes/cfg/flanneld
ExecStart=/opt/kubernetes/bin/flanneld --ip-masq $FLANNEL_OPTIONS
ExecStartPost=/opt/kubernetes/bin/mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/subnet.env
Restart=on-failure

[Install]
WantedBy=multi-user.target

配置Docker启动指定子网段:

# cp /usr/lib/systemd/system/docker.service /usr/lib/systemd/system/docker.service.bak	#先备份
# vim /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

重启flannel和docker:

# systemctl daemon-reload
# systemctl start flanneld
# systemctl enable flanneld
Created symlink from /etc/systemd/system/multi-user.target.wants/flanneld.service to /usr/lib/systemd/system/flanneld.service.
# systemctl restart docker
# systemctl status flanneld

检查是否生效:

# ps -ef | grep docker
root       5172      1  0 11:22 ?        00:00:00 /usr/bin/dockerd --bip=172.17.77.1/24 --ip-masq=false --mtu=1450
root       5527   3028  0 11:25 pts/1    00:00:00 grep --color=auto docker
# ps -ef | grep flanneld
root       5063      1  0 11:22 ?        00:00:00 /opt/kubernetes/bin/flanneld --ip-masq --etcd-endpoints=https://192.168.119.144:2379,https://192.168.119.145:2379,https://192.168.119.146:2379 -etcd-cafile=/opt/etcd/ssl/ca.pem -etcd-certfile=/opt/etcd/ssl/server.pem -etcd-keyfile=/opt/etcd/ssl/server-key.pem
root       5535   3028  0 11:25 pts/1    00:00:00 grep --color=auto flanneld

node02

# rz

# ls
anaconda-ks.cfg           etcd-v3.2.12-linux-amd64.tar.gz     mysql-5.7.24
etcd-v3.2.12-linux-amd64  flannel-v0.11.0-linux-amd64.tar.gz  mysql-boost-5.7.24.tar.gz
# tar xf flannel-v0.11.0-linux-amd64.tar.gz
# mkdir -pv /opt/kubernetes/bin
mkdir: created directory ‘/opt/kubernetes’
mkdir: created directory ‘/opt/kubernetes/bin’
# mv flanneld mk-docker-opts.sh /opt/kubernetes/bin

配置flannel:

# mkdir -pv /opt/kubernetes/cfg/
# vim /opt/kubernetes/cfg/flanneld
FLANNEL_OPTIONS="--etcd-endpoints=https://192.168.119.144:2379,https://192.168.119.145:2379,https://192.168.119.146:2379 -etcd-cafile=/opt/etcd/ssl/ca.pem -etcd-certfile=/opt/etcd/ssl/server.pem -etcd-keyfile=/opt/etcd/ssl/server-key.pem"

systemd管理Flannel:

# vim /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/kubernetes/cfg/flanneld
ExecStart=/opt/kubernetes/bin/flanneld --ip-masq $FLANNEL_OPTIONS
ExecStartPost=/opt/kubernetes/bin/mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/subnet.env
Restart=on-failure

[Install]
WantedBy=multi-user.target

配置Docker启动指定子网段:

# cp /usr/lib/systemd/system/docker.service /usr/lib/systemd/system/docker.service.bak	#先备份
# vim /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

重启flannel和docker:

# systemctl daemon-reload
# systemctl start flanneld
# systemctl enable flanneld
Created symlink from /etc/systemd/system/multi-user.target.wants/flanneld.service to /usr/lib/systemd/system/flanneld.service.
# systemctl restart docker
# systemctl status flanneld

检查是否生效:

# ps -ef | grep docker
root       4990      1  0 11:22 ?        00:00:00 /usr/bin/dockerd --bip=172.17.39.1/24 --ip-masq=false --mtu=1450
root       5400   2565  0 11:26 pts/1    00:00:00 grep --color=auto docker
# ps -ef | grep flanneld
root       4880      1  0 11:22 ?        00:00:00 /opt/kubernetes/bin/flanneld --ip-masq --etcd-endpoints=https://192.168.119.144:2379,https://192.168.119.145:2379,https://192.168.119.146:2379 -etcd-cafile=/opt/etcd/ssl/ca.pem -etcd-certfile=/opt/etcd/ssl/server.pem -etcd-keyfile=/opt/etcd/ssl/server-key.pem
root       5414   2565  0 11:26 pts/1    00:00:00 grep --color=auto flanneld

注:

确保docker0与flannel.1在同一网段。

测试不同节点互通,在当前节点访问另一个Node节点docker0 IP

# ping 172.17.39.1			#测试
PING 172.17.39.1 (172.17.39.1) 56(84) bytes of data.
64 bytes from 172.17.39.1: icmp_seq=1 ttl=64 time=5.26 ms
64 bytes from 172.17.39.1: icmp_seq=2 ttl=64 time=0.636 ms
^C
--- 172.17.39.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.636/2.952/5.268/2.316 ms

到此flannel已经部署完成

五、在Master节点部署组件

在部署Kubernetes之前一定要确保etcd、flannel、docker是正常工作的,否则先解决问题再继续。

生成证书
# master节点操作--给api-server创建的证书。别的服务访问api-server的时候需要通过证书认证
# 创建CA证书:
# mkdir -p /opt/crt/
# cd /opt/crt/
# vim ca-config.json
{
  "signing": {
    "default": {
      "expiry": "87600h"
    },
    "profiles": {
      "kubernetes": {
         "expiry": "87600h",
         "usages": [
            "signing",
            "key encipherment",
            "server auth",
            "client auth"
        ]
      }
    }
  }
}

# vim ca-csr.json
{
    "CN": "kubernetes",
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "L": "Beijing",
            "ST": "Beijing",
            "O": "k8s",
            "OU": "System"
        }
    ]
}

# cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
2021/10/28 22:27:27 [INFO] generating a new CA key and certificate from CSR
2021/10/28 22:27:27 [INFO] generate received request
2021/10/28 22:27:27 [INFO] received CSR
2021/10/28 22:27:27 [INFO] generating key: rsa-2048
2021/10/28 22:27:28 [INFO] encoded CSR
2021/10/28 22:27:28 [INFO] signed certificate with serial number 135856481078140935697778098254036264503877987710
# 生成apiserver证书:
# vim server-csr.json
# cat server-csr.json
{
    "CN": "kubernetes",
    "hosts": [
      "10.0.0.1",         //这是后面dns要使用的虚拟网络的网关,不用改,就用这个切忌
      "127.0.0.1",
      "192.168.119.144",    // master的IP地址。
      "192.168.119.145",
      "192.168.119.146",
      "kubernetes",
      "kubernetes.default",
      "kubernetes.default.svc",
      "kubernetes.default.svc.cluster",
      "kubernetes.default.svc.cluster.local"
    ],
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "L": "BeiJing",
            "ST": "BeiJing",
            "O": "k8s",
            "OU": "System"
        }
    ]
}

# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare server
2021/10/28 22:29:14 [INFO] generate received request
2021/10/28 22:29:14 [INFO] received CSR
2021/10/28 22:29:14 [INFO] generating key: rsa-2048
2021/10/28 22:29:15 [INFO] encoded CSR
2021/10/28 22:29:15 [INFO] signed certificate with serial number 244523529920785501907218511377720961377232223794
2021/10/28 22:29:15 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable for
websites. For more information see the Baseline Requirements for the Issuance and Management
of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (https://cabforum.org);
specifically, section 10.2.3 ("Information Requirements").
# 生成kube-proxy证书:
# vim kube-proxy-csr.json
# cat kube-proxy-csr.json
{
  "CN": "system:kube-proxy",
  "hosts": [],
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "L": "BeiJing",
      "ST": "BeiJing",
      "O": "k8s",
      "OU": "System"
    }
  ]
}

# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxy
2021/10/28 22:30:09 [INFO] generate received request
2021/10/28 22:30:09 [INFO] received CSR
2021/10/28 22:30:09 [INFO] generating key: rsa-2048
2021/10/28 22:30:09 [INFO] encoded CSR
2021/10/28 22:30:09 [INFO] signed certificate with serial number 684273386493845273424213219416293589958592990485
2021/10/28 22:30:09 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable for
websites. For more information see the Baseline Requirements for the Issuance and Management
of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (https://cabforum.org);
specifically, section 10.2.3 ("Information Requirements").
# 最终生成以下证书文件:
# ls *pem
ca-key.pem  ca.pem  kube-proxy-key.pem  kube-proxy.pem  server-key.pem  server.pem
部署apiserver组件

下载二进制包:https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG-1.11.md
下载这个包(kubernetes-server-linux-amd64.tar.gz)就够了,包含了所需的所有组件。

# wget https://dl.k8s.io/v1.11.10/kubernetes-server-linux-amd64.tar.gz
# mkdir /opt/kubernetes/{bin,cfg,ssl,logs} -p
# tar zxvf kubernetes-server-linux-amd64.tar.gz
# cd kubernetes/server/bin
# cp kube-apiserver kube-scheduler kube-controller-manager kubectl /opt/kubernetes/bin

如下操作:
# cd /opt/crt/
# cp server.pem server-key.pem ca.pem ca-key.pem /opt/kubernetes/ssl/

创建token文件:
# cd /opt/kubernetes/cfg/
# vim token.csv
# 生产token:
# head -c 16 /dev/urandom | od -An -t x | tr -d ' '
# cat /opt/kubernetes/cfg/token.csv
c5318aa541007da86704bf922edf78c5,kubelet-bootstrap,10001,"system:kubelet-bootstrap"
第一列:随机字符串,自己可生成
第二列:用户名
第三列:UID
第四列:用户组

创建apiserver配置文件:
# pwd
/opt/kubernetes/cfg
# vim kube-apiserver
# cat kube-apiserver 
KUBE_APISERVER_OPTS="--logtostderr=true \
--v=4 \
--etcd-servers=https://192.168.119.144:2379,https://192.168.119.145:2379,https://192.168.119.146:2379 \
--bind-address=192.168.119.144 \  #master的ip地址,就是安装api-server的机器地址
--secure-port=6443 \
--advertise-address=192.168.119.144 \
--allow-privileged=true \
--service-cluster-ip-range=10.0.0.0/24 \    #这里就用这个网段切记不要修改
--enable-admission-plugins=NamespaceLifecycle,LimitRanger,ServiceAccount,ResourceQuota,NodeRestriction \
--authorization-mode=RBAC,Node \
--enable-bootstrap-token-auth \
--token-auth-file=/opt/kubernetes/cfg/token.csv \
--service-node-port-range=30000-50000 \
--tls-cert-file=/opt/kubernetes/ssl/server.pem  \
--tls-private-key-file=/opt/kubernetes/ssl/server-key.pem \
--client-ca-file=/opt/kubernetes/ssl/ca.pem \
--service-account-key-file=/opt/kubernetes/ssl/ca-key.pem \
--etcd-cafile=/opt/etcd/ssl/ca.pem \
--etcd-certfile=/opt/etcd/ssl/server.pem \
--etcd-keyfile=/opt/etcd/ssl/server-key.pem"

# 配置好前面生成的证书,确保能连接etcd。

# 参数说明:
--logtostderr 启用日志
--v 日志等级
--etcd-servers etcd集群地址
--bind-address 监听地址
--secure-port https安全端口
--advertise-address 集群通告地址
--allow-privileged 启用授权
--service-cluster-ip-range Service虚拟IP地址段
--enable-admission-plugins 准入控制模块
--authorization-mode 认证授权,启用RBAC授权和节点自管理
--enable-bootstrap-token-auth 启用TLS bootstrap功能,后面会讲到
--token-auth-file token文件
--service-node-port-range Service Node类型默认分配端口范围

# systemd管理apiserver:
# cd /usr/lib/systemd/system
# vim kube-apiserver.service
# cat /usr/lib/systemd/system/kube-apiserver.service 
[Unit]
Description=Kubernetes API Server
Documentation=https://github.com/kubernetes/kubernetes

[Service]
EnvironmentFile=-/opt/kubernetes/cfg/kube-apiserver
ExecStart=/opt/kubernetes/bin/kube-apiserver $KUBE_APISERVER_OPTS
Restart=on-failure

[Install]
WantedBy=multi-user.target


# 启动:
# systemctl daemon-reload
# systemctl enable kube-apiserver
# systemctl start kube-apiserver
# systemctl status kube-apiserver
部署schduler组件
# 创建schduler配置文件:
# vim  /opt/kubernetes/cfg/kube-scheduler
# cat /opt/kubernetes/cfg/kube-scheduler 
KUBE_SCHEDULER_OPTS="--logtostderr=true \
--v=4 \
--master=127.0.0.1:8080 \
--leader-elect"
# 参数说明:
--master 连接本地apiserver
--leader-elect 当该组件启动多个时,自动选举(HA)

# systemd管理schduler组件:
# cd /usr/lib/systemd/system/
# vim kube-scheduler.service
# cat /usr/lib/systemd/system/kube-scheduler.service 
[Unit]
Description=Kubernetes Scheduler
Documentation=https://github.com/kubernetes/kubernetes

[Service]
EnvironmentFile=-/opt/kubernetes/cfg/kube-scheduler
ExecStart=/opt/kubernetes/bin/kube-scheduler $KUBE_SCHEDULER_OPTS
Restart=on-failure

[Install]
WantedBy=multi-user.target

# 启动:
# systemctl daemon-reload
# systemctl enable kube-scheduler 
# systemctl start kube-scheduler 
部署controller-manager组件
# master节点操作:创建controller-manager配置文件:
# cd /opt/kubernetes/cfg/
# vim kube-controller-manager
# cat /opt/kubernetes/cfg/kube-controller-manager 
KUBE_CONTROLLER_MANAGER_OPTS="--logtostderr=true \
--v=4 \
--master=127.0.0.1:8080 \
--leader-elect=true \
--address=127.0.0.1 \
--service-cluster-ip-range=10.0.0.0/24 \    //这是后面dns要使用的虚拟网络,不用改,就用这个  切忌
--cluster-name=kubernetes \
--cluster-signing-cert-file=/opt/kubernetes/ssl/ca.pem \
--cluster-signing-key-file=/opt/kubernetes/ssl/ca-key.pem  \
--root-ca-file=/opt/kubernetes/ssl/ca.pem \
--service-account-private-key-file=/opt/kubernetes/ssl/ca-key.pem"

systemd管理controller-manager组件:
# cd /usr/lib/systemd/system/
[root@k8s-master1 system]# vim kube-controller-manager.service
# cat /usr/lib/systemd/system/kube-controller-manager.service 
[Unit]
Description=Kubernetes Controller Manager
Documentation=https://github.com/kubernetes/kubernetes

[Service]
EnvironmentFile=-/opt/kubernetes/cfg/kube-controller-manager
ExecStart=/opt/kubernetes/bin/kube-controller-manager $KUBE_CONTROLLER_MANAGER_OPTS
Restart=on-failure

[Install]
WantedBy=multi-user.target

# 启动:
# systemctl daemon-reload
# systemctl enable kube-controller-manager
# systemctl start kube-controller-manager

# 所有组件都已经启动成功,通过kubectl工具查看当前集群组件状态:
# /opt/kubernetes/bin/kubectl get cs
NAME                 STATUS    MESSAGE              ERROR
scheduler            Healthy   ok                   
controller-manager   Healthy   ok                   
etcd-2               Healthy   {"health": "true"}   
etcd-0               Healthy   {"health": "true"}   
etcd-1               Healthy   {"health": "true"}
如上输出说明组件都正常。

下面这些操作在master节点完成:

将kubelet-bootstrap用户绑定到系统集群角色

# /opt/kubernetes/bin/kubectl create clusterrolebinding kubelet-bootstrap \
  --clusterrole=system:node-bootstrapper \
  --user=kubelet-bootstrap
clusterrolebinding.rbac.authorization.k8s.io/kubelet-bootstrap created

指定apiserver 内网负载均衡地址:

# KUBE_APISERVER="https://192.168.119.144:6443"
# echo $KUBE_APISERVER
https://192.168.119.145:6443
# BOOTSTRAP_TOKEN=c5318aa541007da86704bf922edf78c5
# echo $BOOTSTRAP_TOKEN
674c457d4dcf2eefe4920d7dbb6b0ddc

设置集群参数:

# cd /opt/crt
# /opt/kubernetes/bin/kubectl config set-cluster kubernetes \
  --certificate-authority=./ca.pem \
  --embed-certs=true \
  --server=${KUBE_APISERVER} \
  --kubeconfig=bootstrap.kubeconfig
 Cluster "kubernetes" set.

设置客户端认证参数:

# /opt/kubernetes/bin/kubectl config set-credentials kubelet-bootstrap \
  --token=${BOOTSTRAP_TOKEN} \
  --kubeconfig=bootstrap.kubeconfig
User "kubelet-bootstrap" set.

设置上下文参数:

# /opt/kubernetes/bin/kubectl config set-context default \
  --cluster=kubernetes \
  --user=kubelet-bootstrap \
  --kubeconfig=bootstrap.kubeconfig
Context "default" created.

设置默认上下文:

# /opt/kubernetes/bin/kubectl config use-context default --kubeconfig=bootstrap.kubeconfig
Switched to context "default".

创建kube-proxy kubeconfig文件:

# /opt/kubernetes/bin/kubectl config set-cluster kubernetes \
  --certificate-authority=./ca.pem \
  --embed-certs=true \
  --server=${KUBE_APISERVER} \
  --kubeconfig=kube-proxy.kubeconfig
Cluster "kubernetes" set.
# /opt/kubernetes/bin/kubectl config set-credentials kube-proxy \
  --client-certificate=./kube-proxy.pem \
  --client-key=./kube-proxy-key.pem \
  --embed-certs=true \
  --kubeconfig=kube-proxy.kubeconfig
User "kube-proxy" set.
# /opt/kubernetes/bin/kubectl config set-context default \
  --cluster=kubernetes \
  --user=kube-proxy \
  --kubeconfig=kube-proxy.kubeconfig
Context "default" created.
# /opt/kubernetes/bin/kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig
Switched to context "default".

# ls
bootstrap.kubeconfig  ca-key.pem                              kube-proxy-csr.json    kubernetes                            server-key.pem
ca-config.json        ca.pem                                  kube-proxy-key.pem     kubernetes-server-linux-amd64.tar.gz  server.pem
ca.csr                --clusterrole=system:node-bootstrapper  kube-proxy.kubeconfig  server.csr                            --user=kubelet-bootstrap
ca-csr.json           kube-proxy.csr

接下来把bootstrap.kubeconfig kube-proxy.kubeconfig 这两个配置文件拷贝到两个node节点的/opt/kubernetes/cfg目录下。

# scp bootstrap.kubeconfig kube-proxy.kubeconfig 192.168.119.145:/opt/kubernetes/cfg/
root@192.168.119.145's password: 
bootstrap.kubeconfig                                                                                                 100% 2169    53.0KB/s   00:00    
kube-proxy.kubeconfig                                                                                                100% 6275   867.2KB/s   00:00    
# scp bootstrap.kubeconfig kube-proxy.kubeconfig 192.168.119.146:/opt/kubernetes/cfg/
root@192.168.119.146's password: 
bootstrap.kubeconfig                                                                                                 100% 2169    52.5KB/s   00:00    
kube-proxy.kubeconfig                                                                                                100% 6275     2.4MB/s   00:00    

##去两个node节点上查看
node01
# ls /opt/kubernetes/cfg
bootstrap.kubeconfig  flanneld  kube-proxy.kubeconfig
node02
# ls /opt/kubernetes/cfg
bootstrap.kubeconfig  flanneld  kube-proxy.kubeconfig

下面这些操作在node节点完成:

node01

# scp kubernetes-server-linux-amd64.tar.gz k8s-node1:/root/
# scp kubernetes-server-linux-amd64.tar.gz k8s-node2:/root/
# cd k8s/
# ls
kubernetes-server-linux-amd64.tar.gz
# tar xf kubernetes-server-linux-amd64.tar.gz
# ls
kubernetes  kubernetes-server-linux-amd64.tar.gz
# cd kubernetes/server/bin
# cp kubelet kube-proxy /opt/kubernetes/bin
# ls /opt/kubernetes/bin
flanneld  kubelet  kube-proxy  mk-docker-opts.sh
# vim /opt/kubernetes/cfg/kubelet
KUBELET_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.119.145 \
--kubeconfig=/opt/kubernetes/cfg/kubelet.kubeconfig \
--bootstrap-kubeconfig=/opt/kubernetes/cfg/bootstrap.kubeconfig \
--config=/opt/kubernetes/cfg/kubelet.config \
--cert-dir=/opt/kubernetes/ssl \
--pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64:3.0"  #这个镜像需要提前下载
# 请提前下载
# docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64:3.0	#拉取镜像
3.0: Pulling from google-containers/pause-amd64
a3ed95caeb02: Pull complete 
f11233434377: Pull complete 
Digest: sha256:3b3a29e3c90ae7762bdf587d19302e62485b6bef46e114b741f7d75dba023bd3
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0
registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0

# vim /opt/kubernetes/cfg/kubelet.config
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
address: 192.168.119.145
port: 10250
readOnlyPort: 10255
cgroupDriver: cgroupfs
clusterDNS: ["10.0.0.2"]
clusterDomain: cluster.local.
failSwapOn: false
authentication:
  anonymous:
    enabled: true
  webhook:
    enabled: false

systemd管理kubelet组件:

# vim /usr/lib/systemd/system/kubelet.service
[Unit]
Description=Kubernetes Kubelet
After=docker.service
Requires=docker.service

[Service]
EnvironmentFile=/opt/kubernetes/cfg/kubelet
ExecStart=/opt/kubernetes/bin/kubelet $KUBELET_OPTS
Restart=on-failure
KillMode=process

[Install]
WantedBy=multi-user.target

# systemctl daemon-reload
# systemctl enable kubelet
Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.
# systemctl start kubelet

node02

# ls /opt/kubernetes/cfg
bootstrap.kubeconfig  flanneld  kube-proxy.kubeconfig
# cd k8s-bao/
[root@k8s-node02 k8s-bao]# tar xf kubernetes-server-linux-amd64.tar.gz
[root@k8s-node02 k8s-bao]# ls
kubernetes  kubernetes-server-linux-amd64.tar.gz
[root@k8s-node02 k8s-bao]# cd kubernetes/server/bin
[root@k8s-node02 bin]# cp kubelet kube-proxy /opt/kubernetes/bin
[root@k8s-node02 bin]# ls /opt/kubernetes/bin
flanneld  kubelet  kube-proxy  mk-docker-opts.sh
[root@k8s-node02 bin]# vim /opt/kubernetes/cfg/kubelet
KUBELET_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.119.146 \
--kubeconfig=/opt/kubernetes/cfg/kubelet.kubeconfig \
--bootstrap-kubeconfig=/opt/kubernetes/cfg/bootstrap.kubeconfig \
--config=/opt/kubernetes/cfg/kubelet.config \
--cert-dir=/opt/kubernetes/ssl \
--pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0"

# docker load < pause-amd64-3.0.tar 
e17133b79956: Loading layer [==================================================>]  744.4kB/744.4kB
Loaded image: registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64:3.0

# vim /opt/kubernetes/cfg/kubelet.config
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
address: 192.168.119.146
port: 10250
readOnlyPort: 10255
cgroupDriver: cgroupfs
clusterDNS: ["10.0.0.2"]
clusterDomain: cluster.local.
failSwapOn: false
authentication:
  anonymous:
    enabled: true
  webhook:
    enabled: false

systemd管理kubelet组件:

# vim /usr/lib/systemd/system/kubelet.service
[Unit]
Description=Kubernetes Kubelet
After=docker.service
Requires=docker.service

[Service]
EnvironmentFile=/opt/kubernetes/cfg/kubelet
ExecStart=/opt/kubernetes/bin/kubelet $KUBELET_OPTS
Restart=on-failure
KillMode=process

[Install]
WantedBy=multi-user.target

# systemctl daemon-reload
# systemctl enable kubelet
Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.
# systemctl start kubelet

接下来来到主节点master上

在Master审批Node加入集群:

启动后还没加入到集群中,需要手动允许该节点才可以。
在Master节点查看请求签名的Node:

# /opt/kubernetes/bin/kubectl get csr
NAME                                                   AGE       REQUESTOR           CONDITION
node-csr-lTuyvQxRrphQ_w2K04KmuzTv05Z3oH2GbA7uAcuSGJY   4m        kubelet-bootstrap   Pending
node-csr-uQTSv6T8aiBfVV5cdqhRHGOkPWG5oGaZQAsz_mMNFjg   33s       kubelet-bootstrap   Pending
# /opt/kubernetes/bin/kubectl certificate approve node-csr-lTuyvQxRrphQ_w2K04KmuzTv05Z3oH2GbA7uAcuSGJY
certificatesigningrequest.certificates.k8s.io "node-csr-Wz34kfkC80BEYbOM5sZWG6H3HjwlnP-YvRQ55oq7uuU" approved
# /opt/kubernetes/bin/kubectl certificate approve node-csr-uQTSv6T8aiBfVV5cdqhRHGOkPWG5oGaZQAsz_mMNFjg
certificatesigningrequest.certificates.k8s.io "node-csr-dUk0eW_HEfBZiSpbTv9m6DhewxhIbO1HGSAbu9EnMvY" approved

现在我们回到两个node节点上

node01

# vim /opt/kubernetes/cfg/kube-proxy
KUBE_PROXY_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.119.145 \
--cluster-cidr=10.0.0.0/24 \
--kubeconfig=/opt/kubernetes/cfg/kube-proxy.kubeconfig"

systemd管理kube-proxy组件:

# vim /usr/lib/systemd/system/kube-proxy.service
[Unit]
Description=Kubernetes Proxy
After=network.target

[Service]
EnvironmentFile=-/opt/kubernetes/cfg/kube-proxy
ExecStart=/opt/kubernetes/bin/kube-proxy $KUBE_PROXY_OPTS
Restart=on-failure

[Install]
WantedBy=multi-user.target

node02

# vim /opt/kubernetes/cfg/kube-proxy
KUBE_PROXY_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.119.146 \
--cluster-cidr=10.0.0.0/24 \
--kubeconfig=/opt/kubernetes/cfg/kube-proxy.kubeconfig"

systemd管理kube-proxy组件:

# vim /usr/lib/systemd/system/kube-proxy.service
[Unit]
Description=Kubernetes Proxy
After=network.target

[Service]
EnvironmentFile=-/opt/kubernetes/cfg/kube-proxy
ExecStart=/opt/kubernetes/bin/kube-proxy $KUBE_PROXY_OPTS
Restart=on-failure

[Install]
WantedBy=multi-user.target

下面分别把两个节点上的服务起来:

#node01
# systemctl daemon-reload
# systemctl enable kube-proxy
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-proxy.service to /usr/lib/systemd/system/kube-proxy.service.
# systemctl start kube-proxy
# systemctl status kube-proxy
#node02
# systemctl daemon-reload
# systemctl enable kube-proxy
Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.
# systemctl start kube-proxy
# systemctl status kube-proxy

到此我们集群就搭建完毕

我们到主节点master上来查看一下:

# /opt/kubernetes/bin/kubectl get node
NAME              STATUS    ROLES     AGE       VERSION
192.168.119.145   Ready     <none>    15m       v1.11.0-alpha.1
192.168.119.146   Ready     <none>    14m       v1.11.0-alpha.1
# /opt/kubernetes/bin/kubectl get cs
NAME                 STATUS    MESSAGE              ERROR
controller-manager   Healthy   ok                   
scheduler            Healthy   ok                   
etcd-0               Healthy   {"health": "true"}   
etcd-1               Healthy   {"health": "true"}   
etcd-2               Healthy   {"health": "true"}

这时候我们创建一个测试实例,判断集群是否正常工作:

以zookeeper为例,请提前下好镜像包

# 导入zookeeper镜像包
# docker load < soft/zookeeper.tar
# /opt/kubernetes/bin/kubectl create deployment zookeeper --image=10.0.42.61:1121/blockchain/zookeeper:3.4.9
deployment.apps/zookeeper created
# /opt/kubernetes/bin/kubectl expose deployment zookeeper --port=2181
service "zookeeper" exposed
# /opt/kubernetes/bin/kubectl get pods
NAME                         READY     STATUS    RESTARTS   AGE
zookeeper-5ccb766d76-tqcgg   1/1       Running   0          48m


# /opt/kubernetes/bin/kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP        7h
zookeeper    ClusterIP   10.0.0.215   <none>        2181/TCP       49m

我们找到部署在哪个node上登入zookeeper测试

# kubectl get pods -o wide
NAME                         READY     STATUS    RESTARTS   AGE       IP            NODE
zookeeper-5ccb766d76-tqcgg   1/1       Running   0          50m       172.17.15.2   192.168.119.145

可以看到把zookeeper分配到了145,也就是node1

# docker exec -it f58664122d9c bash
# 进入到容器之后我们创建一个列表
# ./bin/zkCli.sh
# ls /
[zookeeper]
# create /test "12345"
Created /test
# ls /
[zookeeper, test]
# get /test
123456
cZxid = 0x2
ctime = Thu Oct 28 17:26:52 GMT 2021
mZxid = 0x2
mtime = Thu Oct 28 17:26:52 GMT 2021
pZxid = 0x2
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0

到此kubernetes部署完成

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐