k8s:v1.13.0二进制部署 包括dashboard:v1.8 .3

网上很多k8s部署安装教程,但是由于文档和资料不全,安装部署遇到很多坑,本人亲自从零部署,故写下此文章记录,希望对有需要的朋友有帮助。
   部署环境如下:
   系统均为centos7,最小化安装

ip地址主机名cpu
192.168.1.200k8s-master4g
192.168.1.201node14g
192.168.1.202node24g

一.安装包下载
链接:https://pan.baidu.com/s/1wO6T7byhaJYBuu2JlhZvkQ
提取码:pm9u
版本:k8s v1.13

[root@k8s-master ~]# ls /usr/local/src/k8s1.13/
etcd-v3.3.10-linux-amd64            kubernetes-client-linux-amd64.tar.gz
etcd-v3.3.10-linux-amd64.tar.gz     kubernetes-node-linux-amd64.tar.gz
flannel-v0.10.0-linux-amd64.tar.gz  kubernetes-server-linux-amd64.tar.gz
kubernetes                          README.md

二.Kubernetes工作流程
Master节点:
Master节点上面主要由四个模块组成,APIServer,schedule,controller-manager,etcd

APIServer: APIServer负责对外提供RESTful的kubernetes API的服务,它是系统管理指令的统一接口,任何对资源的增删该查都要交给APIServer处理后再交给etcd,kubectl(kubernetes提供的客户端工具,该工具内部是对kubernetes API的调用)是直接和APIServer交互的。

schedule: schedule负责调度Pod到合适的Node上,如果把scheduler看成一个黑匣子,那么它的输入是pod和由多个Node组成的列表,输出是Pod和一个Node的绑定。 kubernetes目前提供了调度算法,同样也保留了接口。用户根据自己的需求定义自己的调度算法。

controller manager: 如果APIServer做的是前台的工作的话,那么controller manager就是负责后台的。每一个资源都对应一个控制器。而control manager就是负责管理这些控制器的,比如我们通过APIServer创建了一个Pod,当这个Pod创建成功后,APIServer的任务就算完成了。

etcd:etcd是一个高可用的键值存储系统,kubernetes使用它来存储各个资源的状态,从而实现了Restful的API。

Node节点:
每个Node节点主要由三个模板组成:kublet, kube-proxy

kube-proxy: 该模块实现了kubernetes中的服务发现和反向代理功能。kube-proxy支持TCP和UDP连接转发,默认基Round Robin算法将客户端流量转发到与service对应的一组后端pod。服务发现方面,kube-proxy使用etcd的watch机制监控集群中service和endpoint对象数据的动态变化,并且维护一个service到endpoint的映射关系,从而保证了后端pod的IP变化不会对访问者造成影响,另外,kube-proxy还支持session affinity。

kublet:kublet是Master在每个Node节点上面的agent,是Node节点上面最重要的模块,它负责维护和管理该Node上的所有容器,但是如果容器不是通过kubernetes创建的,它并不会管理。本质上,它负责使Pod的运行状态与期望的状态一致。

三.kubernetes安装配置
1.初始化系统环境(三台机器都要操作)
1.1 设置关闭防火墙和selinux(关闭是为了避免报莫名其妙的错误,部署好可以根据自己策略打开)

systemctl stop firewalld && systemctl disable firewalld
setenforce 0
vi /etc/selinux/config
SELINUX=disabled

1.2.设置ip路由转发

cat << EOF | tee /etc/sysctl.d/k8s.conf
net.ipv4.ip_forward = 1
EOF
sysctl -p /etc/sysctl.d/k8s.conf

1.3安装docker

yum-config-manager   --add-repo   https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install docker-ce -y
systemctl start docker && systemctl enable docker

1.4创建目录

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

1.5安装及配置CFSSL(以下操作主节点操作)
cfssl用来制作证书工具

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.6创建认证证书
创建目录存放ca文件

mkdir /usr/local/src/ca
cd  /usr/local/src/ca

创建 ETCD 证书:

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

创建 ETCD CA 配置文件:

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

创建etcd server证书:

#####里面ip换填成自己所有节点ip
cat << EOF | tee server-csr.json
{
    "CN": "etcd",
    "hosts": [
    "192.168.1.200",
    "192.168.1.201",
    "192.168.1.202"
    ],
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "L": "Shenzhen",
            "ST": "Shenzhen"
        }
    ]
}
EOF

生成 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

创建kubernetes 证书:

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 -

生成API_SERVER证书

####ip应该写全,多少个节点ip就写进去,不然集群启动会报错
cat << EOF | tee server-csr.json
{
    "CN": "kubernetes",
    "hosts": [
      "10.0.0.1",
      "127.0.0.1",
      "192.168.1.200",
      "192.168.1.202",
      "192.168.1.201",
      "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

创建 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

ssh认证:

# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:FQjjiRDp8IKGT+UDM+GbQLBzF3DqDJ+pKnMIcHGyO/o root@qas-k8s-master01
The key's randomart image is:
+---[RSA 2048]----+
|o.==o o. ..      |
|ooB+o+ o.  .     |
|B++@o o   .      |
|=X**o    .       |
|o=O. .  S        |
|..+              |
|oo .             |
|* .              |
|o+E              |
+----[SHA256]-----+

# ssh-copy-id 192.168.1.201
# ssh-copy-id 192.168.1.222

2.部署etcd

cd /usr/local/src/k8s1.13/
tar -xvf etcd-v3.3.10-linux-amd64.tar.gz
cd etcd-v3.3.10-linux-amd64/
cp etcd etcdctl /k8s/etcd/bin/

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.200:2380"
ETCD_LISTEN_CLIENT_URLS="https://192.168.1.200:2379"

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

创建 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

拷贝证书文件到目录:

cd /usr/local/src/ca/
cp ca*pem server*pem /k8s/etcd/ssl

启动etcd:

systemctl daemon-reload
systemctl enable etcd
systemctl start etcd

将etcd启动文件/配置文件scp到node1/node2:

cd /k8s/ 
scp -r etcd 192.168.1.201:/k8s/
scp -r etcd 192.168.1.202:/k8s/
scp /usr/lib/systemd/system/etcd.service  172.16.8.101:/usr/lib/systemd/system/etcd.service
scp /usr/lib/systemd/system/etcd.service  172.16.8.102:/usr/lib/systemd/system/etcd.service 

-------node1修改etcd文件
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.201:2380"
ETCD_LISTEN_CLIENT_URLS="https://192.168.1.201:2379"

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

-------node2修改etcd文件
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.202:2380"
ETCD_LISTEN_CLIENT_URLS="https://192.168.1.202:2379"

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

node1/node2同时启动etcd
注意:
启动ETCD集群同时启动二个节点,启动一个节点集群是无法正常启动的

systemctl daemon-reload
systemctl enable etcd
systemctl start etcd

master节点验证集群可用性:

[root@k8s-master ca]# /k8s/etcd/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.200:2379,\
> https://192.168.1.201:2379,\
> https://192.168.1.202:2379" cluster-health
member 60c9a074182650b3 is healthy: got healthy result from https://192.168.1.20                                                                                                                                                             1:2379
member af26e2bc543fbfba is healthy: got healthy result from https://192.168.1.20                                                                                                                                                             2:2379
member d5c79fd433825701 is healthy: got healthy result from https://192.168.1.20                                                                                                                                                             0:2379
cluster is healthy

3.部署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.200:2379,\
https://192.168.1.201:2379,https://192.168.1.202:2379" \
set /coreos.com/network/config  '{ "Network": "172.18.0.0/16", "Backend": {"Type": "vxlan"}}'
  • flanneld 当前版本 (v0.10.0) 不支持 etcd v3,故使用 etcd v2 API 写入配置 key 和网段数据;
  • 写入的 Pod 网段 ${CLUSTER_CIDR} 必须是 /16 段地址,必须与 kube-controller-manager 的 –cluster-cidr 参数值一致;

flannel安装配置:

[root@k8s-master ca]# cd /usr/local/src/k8s1.13/
tar -xvf flannel-v0.10.0-linux-amd64.tar.gz
mv flanneld mk-docker-opts.sh /k8s/kubernetes/bin/cal/src/k8s1.13

[root@k8s-master k8s1.13]# vim /k8s/kubernetes/cfg/flanneld
FLANNEL_OPTIONS="--etcd-endpoints=https://192.168.1.200:2379,https://192.168.1.201:2379,https://192.168.1.202:2379 -etcd-cafile=/k8s/etcd/ssl/ca.pem -etcd-certfile=/k8s/etcd/ssl/server.pem -etcd-keyfile=/k8s/etcd/ssl/server-key.pem"

创建 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
  • mk-docker-opts.sh 脚本将分配给 flanneld 的 Pod 子网网段信息写入 /run/flannel/docker 文件,后续 docker 启动时 使用这个文件中的环境变量配置 docker0 网桥;
  • flanneld 使用系统缺省路由所在的接口与其它节点通信,对于有多个网络接口(如内网和公网)的节点,可以用 -iface 参数指定通信接口,如上面的 eth0 接口;
  • flanneld 运行时需要 root 权限;

将flanneld systemd unit 文件到所有节点:

cd /k8s/
scp -r kubernetes 192.168.1.201:/k8s/
scp -r kubernetes 192.168.1.202:/k8s/
scp /k8s/kubernetes/cfg/flanneld 192.168.1.201:/k8s/kubernetes/cfg/flanneld
scp /k8s/kubernetes/cfg/flanneld 192.168.1.202:/k8s/kubernetes/cfg/flanneld
scp /usr/lib/systemd/system/docker.service  192.168.1.201:/usr/lib/systemd/system/docker.service 
scp /usr/lib/systemd/system/docker.service  192.168.1.202:/usr/lib/systemd/system/docker.service
scp /usr/lib/systemd/system/flanneld.service  192.168.1.201:/usr/lib/systemd/system/flanneld.service 
scp /usr/lib/systemd/system/flanneld.service  192.168.1.202:/usr/lib/systemd/system/flanneld.service 

-------三个节点都要启动服务
启动服务
systemctl daemon-reload
systemctl start flanneld
systemctl enable flanneld
systemctl restart docker

验证是否生效:

[root@k8s-master k8s1.13]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:de:1f:48 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.200/24 brd 192.168.1.255 scope global ens192
       valid_lft forever preferred_lft forever
    inet6 fe80::c56:5b4:beb4:2395/64 scope link
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
    link/ether 02:42:02:61:0c:d7 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
4: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN group default
    link/ether 16:88:79:cb:ca:0b brd ff:ff:ff:ff:ff:ff
    inet 172.18.40.0/32 scope global flannel.1
       valid_lft forever preferred_lft forever

4.部署master节点
kubernetes master 节点运行如下组件:

  • kube-apiserver
  • kube-scheduler
  • kube-controller-manager
  • kube-scheduler 和 kube-controller-manager 可以以集群模式运行,通过 leader 选举产生一个工作进程,其它进程处于阻塞模式。

部署准备:

cd /usr/local/src/k8s1.13
tar -xvf kubernetes-server-linux-amd64.tar.gz 
cd kubernetes/server/bin/
cp kube-scheduler kube-apiserver kube-controller-manager kubectl /k8s/kubernetes/bin/
---------拷贝认证
cd /usr/local/src/ca && cp *pem /k8s/kubernetes/ssl/

4.1 部署 kube-apiserver 组件
创建 TLS Bootstrapping Token:

# head -c 16 /dev/urandom | od -An -t x | tr -d ' '
2366a641f656a0a025abb4aabda4511b

vim /k8s/kubernetes/cfg/token.csv
2366a641f656a0a025abb4aabda4511b,kubelet-bootstrap,10001,"system:kubelet-bootstrap"

创建apiserver配置文件:

vim /k8s/kubernetes/cfg/kube-apiserver
KUBE_APISERVER_OPTS="--logtostderr=true \
--v=4 \
--etcd-servers=https://192.168.1.200:2379,https://192.168.1.201:2379,https://192.168.1.202:2379 \
--bind-address=192.168.1.200 \
--secure-port=6443 \
--advertise-address=192.168.1.200 \
--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"

创建 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

验证安装:

systemctl daemon-reload
systemctl enable kube-apiserver
systemctl restart kube-apiserver
[root@k8s-master k8s1.13]# ps -ef | grep kube-apiserver
root     19075  2556  0 17:30 pts/0    00:00:00 grep --color=auto kube-apiserver
root     31493     1  0 103 ?       00:13:30 /k8s/kubernetes/bin/kube-apiserver --logtostderr=true --v=4 --etcd-servers=https://192.168.1.200:2379,https://192.168.1.201:2379,https://192.168.1.202:2379 --bind-address=192.168.1.200 --secure-port=6443 --advertise-address=192.168.1.200 --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

4.2 部署kube-scheduler
创建kube-scheduler配置文件:

[root@k8s-master k8s1.13]# cat /k8s/kubernetes/cfg/kube-scheduler
KUBE_SCHEDULER_OPTS="--logtostderr=true --v=4 --master=127.0.0.1:8080 --leader-elect"

创建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.targetsystemctl daemon-reload


验证安装:

systemctl daemon-reload
systemctl enable kube-scheduler.service 
systemctl restart kube-scheduler.service

[root@k8s-master k8s1.13]# ps -ef | grep kube-scheduler
root     19813  2556  0 17:37 pts/0    00:00:00 grep --color=auto kube-scheduler
root     31867     1  0 103 ?       00:02:50 /k8s/kubernetes/bin/kube-scheduler --logtostderr=true --v=4 --master=127.0.0.1:8080 --leader-elect
[root@k8s-master k8s1.13]# systemctl status kube-scheduler
● kube-scheduler.service - Kubernetes Scheduler
   Loaded: loaded (/usr/lib/systemd/system/kube-scheduler.service; enabled; vendor preset: disabled)
   Active: active (running) since 四 2019-01-03 11:25:05 CST; 1 day 6h ago
     Docs: https://github.com/kubernetes/kubernetes
 Main PID: 31867 (kube-scheduler)
    Tasks: 7
   Memory: 34.0M
   CGroup: /system.slice/kube-scheduler.service
           └─31867 /k8s/kubernetes/bin/kube-scheduler --logtostderr=true --v=4 --master=127.0.0.1:8080 --leader-elect

104 17:32:20 k8s-master kube-scheduler[31867]: I0104 17:32:20.540291   31867 reflector.go:357] k8s.io/client-go/informers/factory.go:132: Watch close - *v1.StorageClass total 0 items received
104 17:33:27 k8s-master kube-scheduler[31867]: I0104 17:33:27.520213   31867 reflector.go:357] k8s.io/client-go/informers/factory.go:132: Watch close - *v1.ReplicationController total 0 items received
104 17:33:59 k8s-master kube-scheduler[31867]: I0104 17:33:59.522364   31867 reflector.go:357] k8s.io/client-go/informers/factory.go:132: Watch close - *v1.ReplicaSet total 0 items received
104 17:34:05 k8s-master kube-scheduler[31867]: I0104 17:34:05.520418   31867 reflector.go:357] k8s.io/client-go/informers/factory.go:132: Watch close - *v1.StatefulSet total 0 items received
104 17:35:18 k8s-master kube-scheduler[31867]: I0104 17:35:18.534253   31867 reflector.go:357] k8s.io/client-go/informers/factory.go:132: Watch close - *v1beta1.PodDisruptionBudget total 0 items received
104 17:35:22 k8s-master kube-scheduler[31867]: I0104 17:35:22.523677   31867 reflector.go:357] k8s.io/client-go/informers/factory.go:132: Watch close - *v1.PersistentVolume total 0 items received
104 17:36:04 k8s-master kube-scheduler[31867]: I0104 17:36:04.542313   31867 reflector.go:357] k8s.io/client-go/informers/factory.go:132: Watch close - *v1.Node total 87 items received
104 17:36:38 k8s-master kube-scheduler[31867]: I0104 17:36:38.528745   31867 reflector.go:357] k8s.io/client-go/informers/factory.go:132: Watch close - *v1.Service total 0 items received
104 17:36:46 k8s-master kube-scheduler[31867]: I0104 17:36:46.517256   31867 reflector.go:357] k8s.io/client-go/informers/factory.go:132: Watch close - *v1.PersistentVolumeClaim total 0 items received
104 17:37:43 k8s-master kube-scheduler[31867]: I0104 17:37:43.541372   31867 reflector.go:357] k8s.io/client-go/informers/factory.go:132: Watch close - *v1.StorageClass total 0 items received

4.3 部署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"

创建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

验证安装:

systemctl daemon-reload
systemctl enable kube-controller-manager
systemctl restart kube-controller-manager

[root@k8s-master k8s1.13]# systemctl status kube-controller-manager
● kube-controller-manager.service - Kubernetes Controller Manager
   Loaded: loaded (/usr/lib/systemd/system/kube-controller-manager.service; enabled; vendor preset: disabled)
   Active: active (running) since 四 2019-01-03 11:30:01 CST; 1 day 6h ago
     Docs: https://github.com/kubernetes/kubernetes
 Main PID: 32281 (kube-controller)
    Tasks: 6
   Memory: 76.9M
   CGroup: /system.slice/kube-controller-manager.service
           └─32281 /k8s/kubernetes/bin/kube-controller-manager --logtostderr=...

104 17:42:05 k8s-master kube-controller-manager[32281]: I0104 17:42:05.35...
104 17:42:08 k8s-master kube-controller-manager[32281]: I0104 17:42:08.38...
104 17:42:08 k8s-master kube-controller-manager[32281]: I0104 17:42:08.38...
104 17:42:08 k8s-master kube-controller-manager[32281]: I0104 17:42:08.38...
104 17:42:08 k8s-master kube-controller-manager[32281]: I0104 17:42:08.38...
104 17:42:08 k8s-master kube-controller-manager[32281]: I0104 17:42:08.38...
104 17:42:08 k8s-master kube-controller-manager[32281]: I0104 17:42:08.38...
104 17:42:10 k8s-master kube-controller-manager[32281]: I0104 17:42:10.33...
104 17:42:10 k8s-master kube-controller-manager[32281]: I0104 17:42:10.34...
104 17:42:10 k8s-master kube-controller-manager[32281]: I0104 17:42:1

自此master节点安装完毕
将k8s可执行文件写入到系统变量,三个节点都写入:


echo "export PATH=/k8s/kubernetes/bin:$PATH:$HOME/bin" >> /etc/profile
soure /etc/profile

验证master节点集群状态,是否安装成功:

[root@k8s-master k8s1.13]# kubectl get cs,node
NAME                                 STATUS    MESSAGE             ERROR
componentstatus/scheduler            Healthy   ok
componentstatus/controller-manager   Healthy   ok
componentstatus/etcd-0               Healthy   {"health":"true"}
componentstatus/etcd-1               Healthy   {"health":"true"}
componentstatus/etcd-2               Healthy   {"health":"true"}

5.部署node节点:
kubernetes work 节点运行如下组件:

  • docker 前面已经部署
  • kubelet
  • kube-proxy

部署 kubelet 组件:

  • kublet 运行在每个 worker 节点上,接收 kube-apiserver 发送的请求,管理 Pod 容器,执行交互式命令,如exec、run、logs 等;
  • kublet 启动时自动向 kube-apiserver 注册节点信息,内置的 cadvisor 统计和监控节点的资源使用情况;
  • 为确保安全,本文档只开启接收 https 请求的安全端口,对请求进行认证和授权,拒绝未授权的访问(如apiserver、heapster)。
cd /usr/local/src/k8s1.13/kubernetes/server/bin
cp kubelet kube-proxy /k8s/kubernetes/bin/
scp kubelet kube-proxy 192.168.1.201:/k8s/kubernetes/bin/
scp kubelet kube-proxy 192.168.1.202:/k8s/kubernetes/bin/

创建 kubelet bootstrap kubeconfig 文件:

#######下面涉及的key填写为自己实际存在的key,我这里是按照我安装流程下来key存在的路径
cd /usr/local/src/k8s1.13/kubernetes
vim  environment.sh
# 创建kubelet bootstrapping kubeconfig 
###TOKEN替换成上面自己生产的token码
BOOTSTRAP_TOKEN=f6dd2856e50ec60be3e91aca21817f90
KUBE_APISERVER="https://192.168.1.200:6443"
# 设置集群参数
kubectl config set-cluster kubernetes \
  --certificate-authority=/k8s/kuberbetes/ssl/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=/k8s/kuberbetes/ssl/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=/k8s/kuberbetes/ssl/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

将bootstrap.kubeconfig kube-proxy.kubeconfig拷贝到节点去:

cp bootstrap.kubeconfig kube-proxy.kubeconfig /k8s/kubernetes/cfg/
scp bootstrap.kubeconfig kube-proxy.kubeconfig 192.168.1.201:/k8s/kubernetes/cfg/
scp bootstrap.kubeconfig kube-proxy.kubeconfig 192.168.1.202:/k8s/kubernetes/cfg/

创建kubelet 参数配置文件拷贝到所有 nodes节点
创建 kubelet 参数配置模板文件:

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

创建kubelet配置文件:

vim /k8s/kubernetes/cfg/kubelet

KUBELET_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=172.16.8.100 \
--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"
``
创建kubelet systemd unit 文件
```java
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

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

kubectl create clusterrolebinding kubelet-bootstrap \
  --clusterrole=system:node-bootstrapper \
  --user=kubelet-bootstrap

将kubelet相关配置文件scp到节点1和节点2:

scp /k8s/kubernetes/cfg/kubelet.config /k8s/kubernetes/cfg/kubelet 192.168.1.201:/k8s/kubernetes/cfg
scp /k8s/kubernetes/cfg/kubelet.config /k8s/kubernetes/cfg/kubelet 192.168.1.202:/k8s/kubernetes/cfg  
scp /usr/lib/systemd/system/kubelet.service 192.168.1.201:/usr/lib/systemd/system/ 
scp /usr/lib/systemd/system/kubelet.service 192.168.1.202:/usr/lib/systemd/system/

node1 node2节点上面去修改配置文件参数(将ip修改为本身节点的实际ip)

#################将ip信息改为节点实际ip
vi /k8s/kubernetes/cfg/kubelet.config
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
address: 192.168.1.201
port: 10250
readOnlyPort: 10255
cgroupDriver: cgroupfs
clusterDNS: ["10.0.0.2"]
clusterDomain: cluster.local.
failSwapOn: false
authentication:
  anonymous:
    enabled: true

vi /k8s/kubernetes/cfg/kubelet    
KUBELET_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.1.201 \
--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"

启动服务(所有节点都启动)

systemctl daemon-reload
systemctl enable kubelet
systemctl restart kubelet

手动approve CSR 请求

# kubectl get csr
NAME                                                   AGE    REQUESTOR           CONDITION
node-csr-An1VRgJ7FEMMF_uyy6iPjyF5ahuLx6tJMbk2SMthwLs   39m    kubelet-bootstrap   Pending
node-csr-dWPIyP_vD1w5gBS4iTZ6V5SJwbrdMx05YyybmbW3U5s   5m5s   kubelet-bootstrap   Pending

# kubectl certificate approve node-csr-An1VRgJ7FEMMF_uyy6iPjyF5ahuLx6tJMbk2SMthwLs
certificatesigningrequest.certificates.k8s.io/node-csr-An1VRgJ7FEMMF_uyy6iPjyF5ahuLx6tJMbk2SMthwLs 

# kubectl certificate approve node-csr-dWPIyP_vD1w5gBS4iTZ6V5SJwbrdMx05YyybmbW3U5s  
certificatesigningrequest.certificates.k8s.io/node-csr-dWPIyP_vD1w5gBS4iTZ6V5SJwbrdMx05YyybmbW3U5s approved
[
# kubectl get csr
NAME                                                   AGE     REQUESTOR           CONDITION
node-csr-An1VRgJ7FEMMF_uyy6iPjyF5ahuLx6tJMbk2SMthwLs   41m     kubelet-bootstrap   Approved,Issued
node-csr-dWPIyP_vD1w5gBS4iTZ6V5SJwbrdMx05YyybmbW3U5s   7m32s   kubelet-bootstrap   Approved,Issued

查看集群状态:

# kubectl get nodes
NAME           STATUS   ROLES    AGE   VERSION
192.168.1.200   Ready    <none>   39m   v1.13.0
192.168.1.201   Ready    <none>   25s   v1.13.0
192.168.1.202   Ready    <none>   13s   v1.13.0

部署 kube-proxy 组件(master节点和node节点都需要,ip修改为自己的ip)
kube-proxy 运行在所有 node节点上,它监听 apiserver 中 service 和 Endpoint 的变化情况,创建路由规则来进行服务负载均衡。
创建 kube-proxy 配置文件:

vi /k8s/kubernetes/cfg/kube-proxy
KUBE_PROXY_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.1.200 \
--cluster-cidr=10.0.0.0/24 \
--kubeconfig=/k8s/kubernetes/cfg/kube-proxy.kubeconfig"

创建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

启动服务:

systemctl daemon-reload
systemctl enable kube-proxy
systemctl restart kube-proxy

标记master和node:

kubectl label node 192.168.1.200  node-role.kubernetes.io/master='master'
kubectl label node 192.168.1.201 node-role.kubernetes.io/node='node'
kubectl label node 192.168.1.202  node-role.kubernetes.io/node='node'

查看集群状态:

# kubectl get node,cs
NAME               STATUS   ROLES    AGE    VERSION
node/192.168.1.200   Ready    master   137m   v1.13.0
node/192.168.1.201  Ready    node     114m   v1.13.0
node/192.168.2.202  Ready    node     93m    v1.13.0

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

自此,k8s基础集群搭建完成。

二.部署dashboard
拉取所需的yaml文件

cd /usr/local/src/
git clone https://github.com/gh-Devin/kubernetes-dashboard.git
[root@k8s-master kubernetes-dashboard]# ls
heapster-rbac.yaml  kubernetes-dashboard-admin.rbac.yaml  README.md
heapster.yaml       kubernetes-dashboard.yaml

配置docker镜像源,以方便下载所需镜像源:
因为无法直接访问gcr.io下载镜像,所以需要配置一个国内的容器镜像加速器

配置一个阿里云的加速器:

登录 https://cr.console.aliyun.com/

在页面中找到并点击镜像加速按钮,即可看到属于自己的专属加速链接,选择Centos版本后即可看到配置方法。

vi /etc/docker/daemon.json
{
 "registry-mirrors": ["https://5ga2eg83.mirror.aliyuncs.com"]
}
systemctl restart docker

由于 kubernetes-dashboard-amd64:v1.8.3 镜像无法获取,我是通过外网下载之后导入本地然后上传到阿里镜像仓库:registry.cn-hangzhou.aliyuncs.com/gz/k8sui:v1.8.3
修改kubernetes.dashaboard.yaml 将里面的kubernetes-dashboard-amd64:v1.8.3 image地址换成阿里的

vi kubernetes-dashboard.yaml

 containers:
      - name: kubernetes-dashboard
        image: registry.cn-hangzhou.aliyuncs.com/gz/k8sui:v1.8.3
        ports:
        - containerPort: 9090
          protocol: TCP

创建pod:

cd /usr/local/src/kubernetes-dashboard
kubectl  -n kube-system create -f .
[root@k8s-master kubernetes-dashboard]# kubectl get svc,pod --all-namespaces
NAMESPACE     NAME                                    TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
default       service/kubernetes                      ClusterIP   10.0.0.1     <none>        443/TCP          12d
kube-system   service/heapster                        ClusterIP   10.0.0.18    <none>        80/TCP           11d
kube-system   service/kubernetes-dashboard-external   NodePort    10.0.0.114   <none>        9090:30090/TCP   11d

NAMESPACE     NAME                                        READY   STATUS    RESTARTS   AGE
kube-system   pod/heapster-6b58df9f59-w45m8               1/1     Running   0          11d
kube-system   pod/kubernetes-dashboard-5d49f788ff-pqs7x   1/1     Running   0          11d

查看kubernetes-dashboard的status是running,说明pod已经成功启动

二.部署dashboard
获取相关yarm文件

cd /usr/local/src/
git clone https://github.com/gh-Devin/kubernetes-dashboard.git
[root@k8s-master kubernetes-dashboard]# ls
heapster-rbac.yaml                    kubernetes-dashboard.bak
heapster.yaml                         kubernetes-dashboard.yaml
kubernetes-dashboard-admin.rbac.yaml  README.md

配置镜像源:
下载K8S相关镜像(Master节点操作)

因为无法直接访问gcr.io下载镜像,所以需要配置一个国内的容器镜像加速器

配置一个阿里云的加速器:

登录 https://cr.console.aliyun.com/

在页面中找到并点击镜像加速按钮,即可看到属于自己的专属加速链接,选择Centos版本后即可看到配置方法。

vi /etc/docker/daemon.json
{
 "registry-mirrors": ["https://5ga2eg83.mirror.aliyuncs.com"]
}

systemctl restart docker

由于kubernetes-dashboard-amd64:v1.8.3镜像国内无法获取,我是外网下载到本地上传到了阿里云私有仓库:
registry.cn-hangzhou.aliyuncs.com/gz/k8sui:v1.8.3

cd /usr/local/src/kubernetes-dashboard
vi kubernetes-dashboard.yaml
image: registry.cn-hangzhou.aliyuncs.com/gz/k8sui:v1.8.3

修改完毕,创建pod:

cd /usr/local/src/kubernetes-dashboard
kubectl  -n kube-system create -f .

查看pod情况:

[root@k8s-master kubernetes-dashboard]# kubectl get pod,svc -n kube-system -o wide
NAME                                        READY   STATUS    RESTARTS   AGE   IP           NODE            NOMINATED NODE   READINESS GATES
pod/heapster-6b58df9f59-w45m8               1/1     Running   0          11d   172.17.0.2   192.168.1.201   <none>           <none>
pod/kubernetes-dashboard-5d49f788ff-pqs7x   1/1     Running   0          11d   172.17.0.2   192.168.1.202   <none>           <none>

NAME                                    TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE   SELECTOR
service/heapster                        ClusterIP   10.0.0.18    <none>        80/TCP           11d   k8s-app=heapster
service/kubernetes-dashboard-external   NodePort    10.0.0.114   <none>        9090:30090/TCP   11d   k8s-app=kubernetes-dashboard

kubernetes-dashaboard的status是running,说明运行正常,分配到了192.168.1.202上面的30090端口
访问:https://192.168.1.202:30090
在这里插入图片描述
这里报错基本都是namespace 用户权限问题,解决如下:

vi kubernetes-dashboard.yaml
serviceAccountName: kubernetes-dashboard-admin
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: kubernetes-dashboard
  namespace: kube-system

[root@k8s-master kubernetes-dashboard]# kubectl apply -f kubernetes-dashboard.yaml -f kubernetes-dashboard-admin.rbac.yaml

访问:https://192.168.1.202:30090
在这里插入图片描述

Logo

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

更多推荐