K8S环境搭建

2020/11/23 K8SK8S

# 机器规划

机器 角色 软件
192.168.1.5 master docker,etcd,flanneld,kubelet,kube_proxy,kube-apiserver、kube-controller-manager、kube-scheduler
192.168.1.9 node docker,etcd,flanneld,kubelet,kube_proxy
192.168.1.16 node docker,etcd,flanneld,kubelet,kube_proxy

# 环境准备

1.关闭防火墙(三台机器同时进行):

systemctl stop firewalld && systemctl disable firewalld
1

2.关闭SELINUX(三台机器同时进行):

#将selinux改成disabled保存并退出
vi /etc/selinux/config
SELINUX=disabled
1
2
3

3.关闭Swap(三台机器同时进行):

swapoff -a && sysctl -w vm.swappiness=0
#将内容里面的UUID进行注释
vi /etc/fstab
#UUID=75db221b-5797-4f81-ac79-d75622cdf625 /boot
1
2
3
4

4.安装Docker(三台机器同时进行):

#运行该脚本就可以安装Docker18.09版本,若Docker版本太高,安装k8s会出现问题
#!/bin/bash
yum remove docker-*
yum update -y
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 -y install docker-ce-18.09.0 docker-ce-cli-18.09.0
systemctl start docker
systemctl enable docker
echo '{
"registry-mirrors": ["https://b24tskqv.mirror.aliyuncs.com"]
}' > /etc/docker/daemon.json
systemctl daemon-reload
systemctl restart docker
1
2
3
4
5
6
7
8
9
10
11
12
13
14

5.配置主机名和ip地址的对应关系:

vi /etc/hosts
#在该文件中添加(三台机器一起添加)
192.168.1.5 master
192.168.1.9 node1
192.168.1.16 node2
vi /etc/hostname
#配置主机名(分别添加)
在192.168.1.5中添加master,并将之前的内容删除
在192.168.1.9中添加node1,并将之前的内容删除
在192.168.1.16中添加node2,并将之前的内容删除
#执行完之后进行重启
reboot
1
2
3
4
5
6
7
8
9
10
11
12

6.免密码登录(三台机器两两配置免密码登录):

#三台机器一起执行
ssh-keygen -t rsa
#上面这条命令生成公钥和私钥(一路执行回车)
#将公钥拷贝给对方,实现免密码登录
ssh-copy-id -i .ssh/id_rsa.pub root@master
ssh-copy-id -i .ssh/id_rsa.pub root@node1
ssh-copy-id -i .ssh/id_rsa.pub root@node2
1
2
3
4
5
6
7

7.创建安装目录(三台机器同时执行):

mkdir -p /k8s/etcd/{bin,cfg,ssl}
mkdir -p /k8s/kubernetes/{bin,cfg,ssl}
1
2

8.安装包的下载链接:

Client Binaries
https://dl.k8s.io/v1.13.1/kubernetes-client-linux-amd64.tar.gz
Server Binaries
https://dl.k8s.io/v1.13.1/kubernetes-server-linux-amd64.tar.gz
Node Binaries
https://dl.k8s.io/v1.13.1/kubernetes-node-linux-amd64.tar.gz
etcd
https://github.com/etcd-io/etcd/releases/download/v3.3.10/etcd-v3.3.10-linux-amd64.tar.gz
flannel
https://github.com/coreos/flannel/releases/download/v0.10.0/flannel-v0.10.0-linux-amd64.tar.gz
1
2
3
4
5
6
7
8
9
10

# 安装kubernetes

# 安装及配置CFSSL

(在master节点执行然后进行拷贝):

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
1
2
3
4
5
6
7

# 创建认证证书:

# ETCD

  • 创建 ETCD 证书
cat << EOF | tee ca-config.json
{
  "signing": {
    "default": {
      "expiry": "87600h"
    },
    "profiles": {
      "www": {
         "expiry": "87600h",
         "usages": [
            "signing",
            "key encipherment",
            "server auth",
            "client auth"
        ]
      }
    }
  }
}
EOF
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  • 创建 ETCD CA 配置文件
cat << EOF | tee ca-csr.json
{
    "CN": "etcd CA",
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "L": "Shenzhen",
            "ST": "Shenzhen"
        }
    ]
}
EOF
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  • 创建 ETCD Server 证书
cat << EOF | tee server-csr.json
{
    "CN": "etcd",
    "hosts": [
    "192.168.1.5",
    "192.168.1.9",
    "192.168.1.16"
    ],
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "L": "Shenzhen",
            "ST": "Shenzhen"
        }
    ]
}
EOF
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  • 生成 ETCD CA 证书和私钥
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
1
2

# Kubernetes

  • 创建 Kubernetes CA 证书
cat << EOF | tee ca-config.json
{
  "signing": {
    "default": {
      "expiry": "87600h"
    },
    "profiles": {
      "kubernetes": {
         "expiry": "87600h",
         "usages": [
            "signing",
            "key encipherment",
            "server auth",
            "client auth"
        ]
      }
    }
  }
}
EOF
cat << EOF | tee ca-csr.json
{
    "CN": "kubernetes",
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "L": "Shenzhen",
            "ST": "Shenzhen",
            "O": "k8s",
            "OU": "System"
        }
    ]
}
EOF

cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  • 生成API_SERVER证书
cat << EOF | tee server-csr.json
{
    "CN": "kubernetes",
    "hosts": [
      "10.0.0.1",
      "127.0.0.1",
      "192.168.1.5",
      "kubernetes",
      "kubernetes.default",
      "kubernetes.default.svc",
      "kubernetes.default.svc.cluster",
      "kubernetes.default.svc.cluster.local"
    ],
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "L": "Shenzhen",
            "ST": "Shenzhen",
            "O": "k8s",
            "OU": "System"
        }
    ]
}
EOF

cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  • 创建 Kubernetes Proxy 证书
cat << EOF | tee kube-proxy-csr.json
{
  "CN": "system:kube-proxy",
  "hosts": [],
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "L": "Shenzhen",
      "ST": "Shenzhen",
      "O": "k8s",
      "OU": "System"
    }
  ]
}
EOF

cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# 部署ETCD

  • 解压安装文件
tar -xvf etcd-v3.3.10-linux-amd64.tar.gz
cd etcd-v3.3.10-linux-amd64/
cp etcd etcdctl /k8s/etcd/bin/
1
2
3
  • 创建etcd配置文件
vim /k8s/etcd/cfg/etcd

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

#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.1.5:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.1.5:2379"
ETCD_INITIAL_CLUSTER="etcd01=https://192.168.1.5:2380,etcd02=https://192.168.1.9:2380,etcd03=https://192.168.1.16:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  • 创建 etcd的 systemd unit 文件
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=/k8s/etcd/cfg/etcd
ExecStart=/k8s/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=/k8s/etcd/ssl/server.pem \
--key-file=/k8s/etcd/ssl/server-key.pem \
--peer-cert-file=/k8s/etcd/ssl/server.pem \
--peer-key-file=/k8s/etcd/ssl/server-key.pem \
--trusted-ca-file=/k8s/etcd/ssl/ca.pem \
--peer-trusted-ca-file=/k8s/etcd/ssl/ca.pem
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  • 拷贝证书文件
cp ca*pem server*pem /k8s/etcd/ssl
1
  • 启动ETCD服务
systemctl daemon-reload
systemctl enable etcd
systemctl start etcd
1
2
3
  • 将启动文件、配置文件拷贝到 节点1、节点2
cd /k8s/
scp -r etcd node1:/k8s/
scp -r etcd node2:/k8s/
scp /usr/lib/systemd/system/etcd.service  node1:/usr/lib/systemd/system/etcd.service
scp /usr/lib/systemd/system/etcd.service  node2:/usr/lib/systemd/system/etcd.service

#在node1上面操作
vim /k8s/etcd/cfg/etcd
#[Member]
ETCD_NAME="etcd02"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.1.9:2380"
ETCD_LISTEN_CLIENT_URLS="https://192.168.1.9:2379"

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

#在node2上面操作
vim /k8s/etcd/cfg/etcd

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

#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.1.16:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.1.16:2379"
ETCD_INITIAL_CLUSTER="etcd01=https://192.168.1.5:2380,etcd02=https://192.168.1.9:2380,etcd03=https://192.168.1.16:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  • 验证集群是否正常运行(在master上操作)

    执行:

    [root@master bin]# ./etcdctl --ca-file=/k8s/etcd/ssl/ca.pem --cert-file=/k8s/etcd/ssl/server.pem --key-file=/k8s/etcd/ssl/server-key.pem --endpoints="https://192.168.1.5:2379,\
    https://192.168.1.9:2379,\
    https://192.168.1.16:2379" cluster-health
    
    1
    2
    3

    结果:

    member 2b5e1efdc00a764e is healthy: got healthy result from https://192.168.1.9:2379
    member 97797244e0f60935 is healthy: got healthy result from https://192.168.1.5:2379
    member d8e778c20ef8bd26 is healthy: got healthy result from https://192.168.1.16:2379
    cluster is healthy
    #注意:启动节点失败将/var/lib/etcd的东西删掉
    #启动ETCD集群同时启动二个节点,启动一个节点集群是无法正常启动的;
    
    1
    2
    3
    4
    5
    6

# 部署Flannel网络

  • 向 etcd 写入集群 Pod 网段信息
cd /k8s/etcd/ssl/
/k8s/etcd/bin/etcdctl \
--ca-file=ca.pem --cert-file=server.pem \
--key-file=server-key.pem \
--endpoints="https://192.168.1.5:2379,\
https://192.168.1.9:2379,https://192.168.1.16:2379" \
set /coreos.com/network/config  '{ "Network": "172.18.0.0/16", "Backend": {"Type": "vxlan"}}'
1
2
3
4
5
6
7
  • 解压安装flannel
tar -zxvf flannel-v0.10.0-linux-amd64.tar.gz
mv flanneld mk-docker-opts.sh /k8s/kubernetes/bin/
1
2
  • 配置Flannel
vim /k8s/kubernetes/cfg/flanneld

FLANNEL_OPTIONS="--etcd-endpoints=https://192.168.1.5:2379,https://192.168.1.9:2379,https://192.168.1.16:2379 -etcd-cafile=/k8s/etcd/ssl/ca.pem -etcd-certfile=/k8s/etcd/ssl/server.pem -etcd-keyfile=/k8s/etcd/ssl/server-key.pem"
1
2
3
  • 创建 flanneld 的 systemd unit 文件
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=/k8s/kubernetes/cfg/flanneld
ExecStart=/k8s/kubernetes/bin/flanneld --ip-masq $FLANNEL_OPTIONS
ExecStartPost=/k8s/kubernetes/bin/mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/subnet.env
Restart=on-failure

[Install]
WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  • mk-docker-opts.sh 脚本将分配给 flanneld 的 Pod 子网网段信息写入 /run/flannel/docker 文件,后续 docker 启动时 使用这个文件中的环境变量配置 docker0 网桥;
  • flanneld 使用系统缺省路由所在的接口与其它节点通信,对于有多个网络接口(如内网和公网)的节点,可以用 -iface 参数指定通信接口,如上面的 eth0 接口;
  • flanneld 运行时需要 root 权限;
  • 配置Docker启动指定子网段
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  • 将flanneld systemd unit 文件到所有节点
cd /k8s/
scp -r kubernetes node1:/k8s/
scp -r kubernetes node2:/k8s/
scp /k8s/kubernetes/cfg/flanneld node1:/k8s/kubernetes/cfg/flanneld
scp /k8s/kubernetes/cfg/flanneld node2:/k8s/kubernetes/cfg/flanneld
scp /usr/lib/systemd/system/docker.service  node1:/usr/lib/systemd/system/docker.service
scp /usr/lib/systemd/system/docker.service  node2:/usr/lib/systemd/system/docker.service
scp /usr/lib/systemd/system/flanneld.service  node1:/usr/lib/systemd/system/flanneld.service
scp /usr/lib/systemd/system/flanneld.service node2:/usr/lib/systemd/system/flanneld.service

#启动服务(三个节点同时执行)
systemctl daemon-reload
systemctl start flanneld
systemctl enable flanneld
systemctl restart docker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 部署Master 节点

安装kube-apiserver,kube-scheduler,kube-controller-manager

# 环境准备

  • 将二进制文件解压拷贝到master 节点
tar -zxvf kubernetes-server-linux-amd64.tar.gz
cd kubernetes/server/bin/
cp kube-scheduler kube-apiserver kube-controller-manager kubectl /k8s/kubernetes/bin/
1
2
3
  • 拷贝认证
cp *pem /k8s/kubernetes/ssl/
1

# 部署 kube-apiserver 组件

  • 创建 TLS Bootstrapping Token
head -c 16 /dev/urandom | od -An -t x | tr -d ' '
8d38ad6d7df6b109901bac9d07aaaefe

vim /k8s/kubernetes/cfg/token.csv
8d38ad6d7df6b109901bac9d07aaaefe,kubelet-bootstrap,10001,"system:kubelet-bootstrap"
1
2
3
4
5
  • 创建apiserver配置文件

    vim /k8s/kubernetes/cfg/kube-apiserver
    
    KUBE_APISERVER_OPTS="--logtostderr=true \
    --v=4 \
    --etcd-servers=https://192.168.1.5:2379,https://192.168.1.9:2379,https://192.168.1.16:2379 \
    --bind-address=192.168.1.5 \
    --secure-port=6443 \
    --advertise-address=192.168.1.5 \
    --allow-privileged=true \
    --service-cluster-ip-range=10.0.0.0/24 \
    --enable-admission-plugins=NamespaceLifecycle,LimitRanger,SecurityContextDeny,ServiceAccount,ResourceQuota,NodeRestriction \
    --authorization-mode=RBAC,Node \
    --enable-bootstrap-token-auth \
    --token-auth-file=/k8s/kubernetes/cfg/token.csv \
    --service-node-port-range=30000-50000 \
    --tls-cert-file=/k8s/kubernetes/ssl/server.pem  \
    --tls-private-key-file=/k8s/kubernetes/ssl/server-key.pem \
    --client-ca-file=/k8s/kubernetes/ssl/ca.pem \
    --service-account-key-file=/k8s/kubernetes/ssl/ca-key.pem \
    --etcd-cafile=/k8s/etcd/ssl/ca.pem \
    --etcd-certfile=/k8s/etcd/ssl/server.pem \
    --etcd-keyfile=/k8s/etcd/ssl/server-key.pem"
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22

    创建apiserver.sh

    vim apiserver.sh
    #!/bin/bash
    MASTER_ADDRESS=${1:-"192.168.1.195"}
    ETCD_SERVERS=${2:-"http://127.0.0.1:2379"}
    
    cat <<EOF >/opt/kubernetes/cfg/kube-apiserver
    
    KUBE_APISERVER_OPTS="--logtostderr=true \\
    --v=4 \\
    --etcd-servers=${ETCD_SERVERS} \\
    --insecure-bind-address=127.0.0.1 \\
    --bind-address=${MASTER_ADDRESS} \\
    --insecure-port=8080 \\
    --secure-port=6443 \\
    --advertise-address=${MASTER_ADDRESS} \\
    --allow-privileged=true \\
    --service-cluster-ip-range=10.10.10.0/24 \\
    --admission-control=NamespaceLifecycle,LimitRanger,SecurityContextDeny,ServiceAccount,ResourceQuota,NodeRestriction \
    --authorization-mode=RBAC,Node \\
    --kubelet-https=true \\
    --enable-bootstrap-token-auth \\
    --token-auth-file=/k8s/kubernetes/cfg/token.csv \\
    --service-node-port-range=30000-50000 \\
    --tls-cert-file=/k8s/kubernetes/ssl/server.pem  \\
    --tls-private-key-file=//k8s/kubernetes/ssl/server-key.pem \\
    --client-ca-file=/k8s/kubernetes/ssl/ca.pem \\
    --service-account-key-file=/k8s/kubernetes/ssl/ca-key.pem \\
    --etcd-cafile=/k8s/etcd/ssl/ca.pem \\
    --etcd-certfile=/k8s/etcd/ssl/server.pem \\
    --etcd-keyfile=/k8s/etcd/ssl/server-key.pem"
    
    EOF
    
    cat <<EOF >/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
    EOF
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46

    执行执行

    ./apiserver.sh  192.168.1.5 https://192.168.1.5:2379,https://192.168.1.9:2379,https://192.168.1.16:2379
    
    1

    启动

    systemctl daemon-reload
    systemctl enable kube-apiserver
    systemctl restart kube-apiserver
    
    1
    2
    3
  • 创建 kube-apiserver systemd unit 文件

vim /usr/lib/systemd/system/kube-apiserver.service

[Unit]
Description=Kubernetes API Server
Documentation=https://github.com/kubernetes/kubernetes

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

[Install]
WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
11
12
13
  • 启动服务
systemctl daemon-reload
systemctl enable kube-apiserver
systemctl start kube-apiserver
1
2
3
  • 查看apiserver是否运行
ps -ef |grep kube-apiserver
1

# 部署 kube-scheduler

  • 创建kube-scheduler配置文件
vim  /k8s/kubernetes/cfg/kube-scheduler

KUBE_SCHEDULER_OPTS="--logtostderr=true --v=4 --master=127.0.0.1:8080 --leader-elect"
1
2
3
  • –address:在 127.0.0.1:10251 端口接收 http /metrics 请求;kube-scheduler 目前还不支持接收 https 请求;
  • –kubeconfig:指定 kubeconfig 文件路径,kube-scheduler 使用它连接和验证 kube-apiserver;
  • –leader-elect=true:集群运行模式,启用选举功能;被选为 leader 的节点负责处理工作,其它节点为阻塞状态;
  • 创建kube-scheduler systemd unit 文件
vim /usr/lib/systemd/system/kube-scheduler.service

[Unit]
Description=Kubernetes Scheduler
Documentation=https://github.com/kubernetes/kubernetes

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

[Install]
WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
11
12
13
  • 启动服务
systemctl daemon-reload
systemctl enable kube-scheduler.service
systemctl start kube-scheduler.service
1
2
3
  • 查看kube-scheduler是否运行
ps -ef |grep kube-scheduler
1

# 部署 kube-controller-manager

  • 创建kube-controller-manager配置文件
vim /k8s/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 \
--cluster-name=kubernetes \
--cluster-signing-cert-file=/k8s/kubernetes/ssl/ca.pem \
--cluster-signing-key-file=/k8s/kubernetes/ssl/ca-key.pem  \
--root-ca-file=/k8s/kubernetes/ssl/ca.pem \
--service-account-private-key-file=/k8s/kubernetes/ssl/ca-key.pem"
1
2
3
4
5
6
7
8
9
10
11
12
13
  • 创建kube-controller-manager systemd unit 文件
vim /usr/lib/systemd/system/kube-controller-manager.service

[Unit]
Description=Kubernetes Controller Manager
Documentation=https://github.com/kubernetes/kubernetes

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

[Install]
WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
11
12
13
  • 启动服务
systemctl daemon-reload
systemctl enable kube-controller-manager
systemctl restart kube-controller-manager
1
2
3
  • 查看kube-controller-manager是否启动
systemctl status kube-controller-manager
1
  • 将可执行文件路/k8s/kubernetes/ 添加到 PATH 变量中
vim /etc/profile
PATH=/k8s/kubernetes/bin:$PATH:$HOME/bin
source /etc/profile
1
2
3
  • 查看master集群状态
[root@master bin]# kubectl get cs,nodes

NAME                                 STATUS    MESSAGE             ERROR
componentstatus/scheduler            Healthy   ok
componentstatus/controller-manager   Healthy   ok
componentstatus/etcd-1               Healthy   {"health":"true"}
componentstatus/etcd-0               Healthy   {"health":"true"}
componentstatus/etcd-2               Healthy   {"health":"true"}

NAME                STATUS   ROLES    AGE     VERSION
node/192.168.1.16   Ready    node     3h42m   v1.13.1
node/192.168.1.5    Ready    master   3h50m   v1.13.1
node/192.168.1.9    Ready    node     3h50m   v1.13.1
1
2
3
4
5
6
7
8
9
10
11
12
13

# 部署 node 节点

安装 kubelet,kube-proxy

  • kublet 运行在每个 worker 节点上,接收 kube-apiserver 发送的请求,管理 Pod 容器,执行交互式命令,如exec、run、logs 等;
  • kublet 启动时自动向 kube-apiserver 注册节点信息,内置的 cadvisor 统计和监控节点的资源使用情况;
  • 为确保安全,本文档只开启接收 https 请求的安全端口,对请求进行认证和授权,拒绝未授权的访问(如apiserver、heapster)。
  • 将kubelet 二进制文件拷贝node节点
#在master节点上执行
cp kubelet kube-proxy /k8s/kubernetes/bin/
scp kubelet kube-proxy 192.168.1.9:/k8s/kubernetes/bin/
scp kubelet kube-proxy 192.168.1.16:/k8s/kubernetes/bin/
1
2
3
4
  • 创建 kubelet bootstrap kubeconfig 文件
#在master节点上执行
#注:将ca.pem,environment.sh,kube-proxy-key.pem,kube-proxy.pem四个文件放到一个目录下
vim  environment.sh
# 创建kubelet bootstrapping kubeconfig
BOOTSTRAP_TOKEN=8d38ad6d7df6b109901bac9d07aaaefe
KUBE_APISERVER="https://192.168.1.5:6443"
# 设置集群参数
kubectl config set-cluster kubernetes \
  --certificate-authority=./ca.pem \
  --embed-certs=true \
  --server=${KUBE_APISERVER} \
  --kubeconfig=bootstrap.kubeconfig

# 设置客户端认证参数
kubectl config set-credentials kubelet-bootstrap \
  --token=${BOOTSTRAP_TOKEN} \
  --kubeconfig=bootstrap.kubeconfig

# 设置上下文参数
kubectl config set-context default \
  --cluster=kubernetes \
  --user=kubelet-bootstrap \
  --kubeconfig=bootstrap.kubeconfig

# 设置默认上下文
kubectl config use-context default --kubeconfig=bootstrap.kubeconfig

#----------------------

# 创建kube-proxy kubeconfig文件

kubectl config set-cluster kubernetes \
  --certificate-authority=./ca.pem \
  --embed-certs=true \
  --server=${KUBE_APISERVER} \
  --kubeconfig=kube-proxy.kubeconfig

kubectl config set-credentials kube-proxy \
  --client-certificate=./kube-proxy.pem \
  --client-key=./kube-proxy-key.pem \
  --embed-certs=true \
  --kubeconfig=kube-proxy.kubeconfig

kubectl config set-context default \
  --cluster=kubernetes \
  --user=kube-proxy \
  --kubeconfig=kube-proxy.kubeconfig

kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  • 将bootstrap kubeconfig和kube-proxy.kubeconfig 文件拷贝到所有 nodes节点
cp bootstrap.kubeconfig kube-proxy.kubeconfig /k8s/kubernetes/cfg/
scp bootstrap.kubeconfig kube-proxy.kubeconfig node1:/k8s/kubernetes/cfg/
scp bootstrap.kubeconfig kube-proxy.kubeconfig node2:/k8s/kubernetes/cfg/
1
2
3
  • 创建kubelet 参数配置文件拷贝到所有 nodes节点
#在master上操作
vim /k8s/kubernetes/cfg/kubelet.config

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


#将参数配置文件拷贝到其他节点并且ip需要改成自己的ip
scp /k8s/kubernetes/cfg/kubelet.config node1:/k8s/kubernetes/cfg/
scp /k8s/kubernetes/cfg/kubelet.config node2:/k8s/kubernetes/cfg/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  • 创建kubelet配置文件
#在master上操作
vim /k8s/kubernetes/cfg/kubelet

KUBELET_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.1.5 \
--kubeconfig=/k8s/kubernetes/cfg/kubelet.kubeconfig \
--bootstrap-kubeconfig=/k8s/kubernetes/cfg/bootstrap.kubeconfig \
--config=/k8s/kubernetes/cfg/kubelet.config \
--cert-dir=/k8s/kubernetes/ssl \
--pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0"

#将文件拷贝到其他节点上并修改对应的ip
scp /k8s/kubernetes/cfg/kubelet node1:/k8s/kubernetes/cfg/kubelet
scp /k8s/kubernetes/cfg/kubelet node2:/k8s/kubernetes/cfg/kubelet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  • 创建kubelet systemd unit 文件
#在master上操作
vim /usr/lib/systemd/system/kubelet.service

[Unit]
Description=Kubernetes Kubelet
After=docker.service
Requires=docker.service

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

[Install]
WantedBy=multi-user.target

#将文件拷贝到其他节点上
scp /usr/lib/systemd/system/kubelet.service node1:/usr/lib/systemd/system/kubelet.service
scp /usr/lib/systemd/system/kubelet.service node2:/usr/lib/systemd/system/kubelet.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  • 将kubelet-bootstrap用户绑定到系统集群角色
#master节点上执行
kubectl create clusterrolebinding kubelet-bootstrap \
  --clusterrole=system:node-bootstrapper \
  --user=kubelet-bootstrap
#clusterrolebinding.rbac.authorization.k8s.io/kubelet-bootstrap created
1
2
3
4
5
  • 启动服务
#在两个节点上执行
systemctl daemon-reload
systemctl enable kubelet
systemctl restart kubelet
1
2
3
4
  • 查看 CSR 列表
kubectl get csr

NAME                                                   AGE   REQUESTOR           CONDITION
node-csr-HjaojHavfm-vFzime9FfPUB4rNCuBAgUiUR8GrjaKeM   10m   kubelet-bootstrap   Pending
node-csr-bfl0GRKg5hWleaClBFAcXVvNOvNhRUJ0f11J3vNN9rY   10m   kubelet-bootstrap   Pending
1
2
3
4
5
  • 手动 approve CSR 请求
kubectl certificate approve node-csr-HjaojHavfm-vFzime9FfPUB4rNCuBAgUiUR8GrjaKeM
kubectl certificate approve node-csr-bfl0GRKg5hWleaClBFAcXVvNOvNhRUJ0f11J3vNN9rY
1
2
  • 查看集群状态
kubectl get nodes
1

# 部署 kebue-proxy

  • 创建 kube-proxy文件
vim /k8s/kubernetes/cfg/kube-proxy
KUBE_PROXY_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.1.5 \
--cluster-cidr=10.0.0.0/24 \
--kubeconfig=/k8s/kubernetes/cfg/kube-proxy.kubeconfig"

#文件拷贝并且ip要改成自己的ip
scp /k8s/kubernetes/cfg/kube-proxy node1:/k8s/kubernetes/cfg/
scp /k8s/kubernetes/cfg/kube-proxy node2:/k8s/kubernetes/cfg/
1
2
3
4
5
6
7
8
9
10
  • 创建kube-proxy systemd unit 文件
vim /usr/lib/systemd/system/kube-proxy.service

[Unit]
Description=Kubernetes Proxy
After=network.target

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

[Install]
WantedBy=multi-user.target


#文件拷贝
scp /usr/lib/systemd/system/kube-proxy.service node1:/usr/lib/systemd/system/
scp /usr/lib/systemd/system/kube-proxy.service node2:/usr/lib/systemd/system/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  • 启动服务
systemctl daemon-reload
systemctl enable kube-proxy
systemctl restart kube-proxy
1
2
3
  • 打node 或者master 节点的标签
kubectl label node 192.168.1.5  node-role.kubernetes.io/master='master'
kubectl label node 192.168.1.9  node-role.kubernetes.io/node='node'
kubectl label node 192.168.1.16  node-role.kubernetes.io/node='node'
1
2
3