1.Kubernetes架构与组件示意图

在这里插入图片描述

2.部署准备

准备三台虚拟机,master的cpu给大点

操作系统:
    CentOS7
软件版本:
    Docker	最新
    Kubernetes	1.11
master:
192.168.13.141  kube-apiserver,kube-controller-manager,kube-scheduler,etcd
node1:
192.168.13.142  kubelet,kube-proxy,docker,flannel,etcd
node2:
192.168.13.143  kubelet,kube-proxy,docker,flannel,etcd

三台机器需要关闭防火墙,selinux,互相解析

3.部署k8s集群(二进制方式)
1.部署Etcd集群

使用cfssl来生成自签证书,任何机器都行,这里在master上进行(一台机器就行)

下载cfssl工具:
[root@master ~]# wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64
[root@master ~]# wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
[root@master ~]# wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64
[root@master ~]# chmod +x cfssl_linux-amd64 cfssljson_linux-amd64 cfssl-certinfo_linux-amd64 
下面这些路径不要随意改,后面的配置文件中写的都是绝对路径
[root@master ~]# mv cfssl_linux-amd64 /usr/local/bin/cfssl
[root@master ~]# mv cfssljson_linux-amd64 /usr/local/bin/cfssljson
[root@master ~]# mv cfssl-certinfo_linux-amd64 /usr/bin/cfssl-certinfo
[root@master ~]# mkdir ssl  #创建一个目录来存放配置文件
[root@master ~]# cd ssl
创建以下三个文件:
[root@master ssl]# vim ca-config.json
{
  "signing": {
    "default": {
      "expiry": "87600h"
    },
    "profiles": {
      "www": {
         "expiry": "87600h",
         "usages": [
            "signing",
            "key encipherment",
            "server auth",
            "client auth"
        ]
      }
    }
  }
}

[root@master ssl]# cat ca-csr.json
{
    "CN": "etcd CA",
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "L": "Beijing",
            "ST": "Beijing"
        }
    ]
}

[root@master ssl]# cat server-csr.json
{
    "CN": "etcd",
    "hosts": [
    "192.168.13.141",      
    "192.168.13.142",
    "192.168.13.143"
    ],
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "L": "BeiJing",
            "ST": "BeiJing"
        }
    ]
}
=============================================
    "hosts": [
    "192.168.13.141",      
    "192.168.13.142",
    "192.168.13.143"
    ],
#etcd集群的ip地址
==============================================
生成证书:
[root@master ssl]# cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
[root@master ssl]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | cfssljson -bare server
[root@master ssl]# ls *pem
ca-key.pem  ca.pem  server-key.pem  server.pem
有以上这四个.pem文件证书就算生成了
安装Etcd:
二进制包下载地址:
https://github.com/etcd-io/etcd/releases/download/v3.2.12/etcd-v3.2.12-linux-amd64.tar.gz
以下部署步骤在规划的三个etcd节点操作一样,唯一不同的是etcd配置文件中的服务器IP要写当前本机的:
解压二进制包:
# mkdir /opt/etcd/{bin,cfg,ssl} -p    //bin里面存放的是可执行文件,cfg配置文件,ssl  证书
# tar zxvf 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.13.141:2380"
ETCD_LISTEN_CLIENT_URLS="https://192.168.13.141:2379"

#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.13.141:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.13.141:2379"
ETCD_INITIAL_CLUSTER="etcd01=https://192.168.13.141:2380,etcd02=https://192.168.13.142:2380,etcd03=https://192.168.13.143:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
=====================================================
* ETCD_NAME 节点名称    (master 是etcd01 node1是etcd02 node2是etcd03)
* ETCD_DATA_DIR 数据目录 (不用修改)
* ETCD_LISTEN_PEER_URLS 集群通信监听地址   (修改为本机ip)
* ETCD_LISTEN_CLIENT_URLS 客户端访问监听地址   (修改为本机ip)
* ETCD_INITIAL_ADVERTISE_PEER_URLS 集群通告地址  (修改为本机ip)
* ETCD_ADVERTISE_CLIENT_URLS 客户端通告地址    (修改为本机ip)
* ETCD_INITIAL_CLUSTER 集群节点地址    (跟上面节点名称要一一对应)
* ETCD_INITIAL_CLUSTER_TOKEN 集群Token
* ETCD_INITIAL_CLUSTER_STATE (加入集群的当前状态,new是新集群,existing表示加入已有集群,因为我们是新创建的所以设置为new,否则用existing)
=====================================================
systemd管理etcd:(方便使用systemctl启动,而不是用一长串的绝对路径启动)
# 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

把刚才生成的证书拷贝到配置文件中的位置:(对应上面systemd管理中我们写的路径)
# cp ca*pem server*pem /opt/etcd/ssl
因为证书我们是在master上配置的,所以我们需要拷贝给node节点上
[root@master ssl]# scp ca*pem server*pem node1:/opt/etcd/ssl/
[root@master ssl]# scp ca*pem server*pem node2:/opt/etcd/ssl/

启动并设置开启启动:
# systemctl start etcd
# systemctl enable etcd

都部署完成后,检查etcd集群状态:
# /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.13.141:2379,https://192.168.13.141:2379,https://192.168.13.141:2379" \
cluster-health
注意:一般像这么长的命令我都是写到一个.sh文件中,然后执行就行了(不要少复制了)
[root@master ~]# sh test.sh 
member 31050b4572314124 is healthy: got healthy result from https://192.168.13.141:2379
member ab245929487fc48c is healthy: got healthy result from https://192.168.13.143:2379
member f5f039265354b7b1 is healthy: got healthy result from https://192.168.13.142:2379
cluster is healthy
如果输出以上信息,就表示etcd集群部署成功
2.在Node节点安装Docker

docker安装详解

只在两个node节点上安装

# yum install -y yum-utils device-mapper-persistent-data lvm2
# yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
# yum install docker-ce -y
# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://bc437cce.m.daocloud.io
# systemctl start docker
# systemctl enable docker
3.部署Flannel网络
Falnnel要用etcd存储自身一个子网信息,所以要保证能成功连接Etcd,写入预定义子网段:
在master节点执行以下命令
[root@master ~]# cd /opt/etcd/ssl
[root@master ssl]# /opt/etcd/bin/etcdctl \
--ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem \
--endpoints="https://192.168.13.141:2379,https://192.168.13.142:2379,https://192.168.13.143:2379" \
set /coreos.com/network/config  '{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}'
以下部署步骤在规划的每个node节点都操作。
下载二进制包:
# wget https://github.com/coreos/flannel/releases/download/v0.10.0/flannel-v0.10.0-linux-amd64.tar.gz
# tar zxvf flannel-v0.10.0-linux-amd64.tar.gz
# mkdir -pv /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.13.141:2379,https://192.168.13.142:2379,https://192.168.13.143: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启动指定子网段:
# 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
# systemctl restart docker
检查是否生效:(确保docker0与flannel.1在同一网段。)
[root@node2 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:cc:02:29 brd ff:ff:ff:ff:ff:ff
    inet 192.168.13.143/24 brd 192.168.13.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::d059:defe:bc38:137e/64 scope link 
       valid_lft forever preferred_lft forever
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP 
    link/ether 02:42:ce:fa:ff:66 brd ff:ff:ff:ff:ff:ff
    inet 172.17.69.1/24 brd 172.17.69.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:ceff:fefa:ff66/64 scope link 
       valid_lft forever preferred_lft forever
4: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN 
    link/ether ce:eb:c2:70:84:a2 brd ff:ff:ff:ff:ff:ff
    inet 172.17.69.0/32 scope global flannel.1
       valid_lft forever preferred_lft forever
    inet6 fe80::cceb:c2ff:fe70:84a2/64 scope link 
       valid_lft forever preferred_lft forever
[root@node1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:27:d1:4f brd ff:ff:ff:ff:ff:ff
    inet 192.168.13.142/24 brd 192.168.13.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::4e07:7b5b:8315:984a/64 scope link 
       valid_lft forever preferred_lft forever
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP 
    link/ether 02:42:f4:33:f5:e0 brd ff:ff:ff:ff:ff:ff
    inet 172.17.91.1/24 brd 172.17.91.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:f4ff:fe33:f5e0/64 scope link 
       valid_lft forever preferred_lft forever
4: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN 
    link/ether 42:de:f7:5d:a5:74 brd ff:ff:ff:ff:ff:ff
    inet 172.17.91.0/32 scope global flannel.1
       valid_lft forever preferred_lft forever
    inet6 fe80::40de:f7ff:fe5d:a574/64 scope link 
       valid_lft forever preferred_lft forever
       
测试不同节点互通,在当前节点访问另一个Node节点docker0 IP:
(一个node节点去ping另一个node节点的docker0的IP,能通就说明Flannel部署成功)
[root@node1 ~]# ping 172.17.69.1    
PING 172.17.69.1 (172.17.69.1) 56(84) bytes of data.
64 bytes from 172.17.69.1: icmp_seq=1 ttl=64 time=0.625 ms
64 bytes from 172.17.69.1: icmp_seq=2 ttl=64 time=0.391 ms
64 bytes from 172.17.69.1: icmp_seq=3 ttl=64 time=0.400 ms

4.apiserver等组件的配置

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

master节点:
创建CA证书:
[root@master ~]# mkdir k8s        #同样创建证书文件存放目录
[root@master ~]# cd k8s
[root@master k8s]# vim ca-config.json
{
  "signing": {
    "default": {
      "expiry": "87600h"
    },
    "profiles": {
      "kubernetes": {
         "expiry": "87600h",
         "usages": [
            "signing",
            "key encipherment",
            "server auth",
            "client auth"
        ]
      }
    }
  }
}

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

生成apiserver证书:
[root@master k8s]# vim server-csr.json
{
    "CN": "kubernetes",
    "hosts": [
      "10.0.0.1",
      "127.0.0.1",
      "192.168.13.141",
      "192.168.13.142",
      "192.168.13.143",
      "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"
        }
    ]
}
=============================================
"10.0.0.1",   //这个是后边dns要用的虚拟网络的网关,不用改,就用这个--service-cluster-ip-range=10.0.0.0/24 \ 切忌
      "127.0.0.1",
      "192.168.13.141",    #k8s集群的ip,也就是三台虚拟机的ip
      "192.168.13.142",
      "192.168.13.143",
=============================================


生成kube-proxy证书:
[root@master k8s]# vim 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"
    }
  ]
}

[root@master k8s]# cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
[root@master k8s]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare server
[root@master k8s]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxy
最终生成以下证书文件:
[root@master k8s]# 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)就够了,包含了所需的所有组件。
[root@master ~]# mkdir /opt/kubernetes/{bin,cfg,ssl} -pv
[root@master ~]# tar zxvf kubernetes-server-linux-amd64.tar.gz
[root@master ~]# cd kubernetes/server/bin
[root@master ~]# cp kube-apiserver kube-scheduler kube-controller-manager kubectl /opt/kubernetes/bin
从生成证书的机器拷贝证书到master(因为是在一台机器上的 所以直接复制就行)
[root@master ~]# cd k8s
[root@master k8s]# cp server.pem  server-key.pem ca.pem ca-key.pem /opt/kubernetes/ssl/
创建token文件,后面会用到:
[root@master ~]# vim /opt/kubernetes/cfg/token.csv
674c457d4dcf2eefe4920d7dbb6b0ddc,kubelet-bootstrap,10001,"system:kubelet-bootstrap"
第一列:随机字符串,自己可生成(最好不要改,后面会用到这串数字)
第二列:用户名
第三列:UID
第四列:用户组

创建apiserver配置文件:
[root@master ~]# vim /opt/kubernetes/cfg/kube-apiserver 

KUBE_APISERVER_OPTS="--logtostderr=true \
--v=4 \
--etcd-servers=https://192.168.13.141:2379,https://192.168.13.142:2379,https://192.168.13.143:2379 \
--bind-address=192.168.13.141 \
--secure-port=6443 \
--advertise-address=192.168.13.141 \
--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"
============================================================
参数说明:
* --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:
[root@master ~]# vim /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

启动:
[root@master ~]# systemctl daemon-reload
[root@master ~]# systemctl enable kube-apiserver
[root@master ~]# systemctl start kube-apiserver

部署schduler组件
创建schduler配置文件:
[root@master ~]# vim /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组件:
[root@master ~]# vim 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

启动:
[root@master ~]# systemctl daemon-reload
[root@master ~]# systemctl enable kube-scheduler 
[root@master ~]# systemctl start kube-scheduler 

部署controller-manager组件
创建controller-manager配置文件:
[root@master ~]# 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 \
--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组件:
[root@master ~]# 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

启动:
[root@master ~]# systemctl daemon-reload
[root@master ~]# systemctl enable kube-controller-manager
[root@master ~]# systemctl start kube-controller-manager

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

在Node节点部署组件
Master apiserver启用TLS认证后,Node节点kubelet组件想要加入集群,必须使用CA签发的有效证书才能与apiserver通信,当Node节点很多时,签署证书是一件很繁琐的事情,因此有了TLS Bootstrapping机制,kubelet会以一个低权限用户自动向apiserver申请证书,kubelet的证书由apiserver动态签署。

master节点部署
将kubelet-bootstrap用户绑定到系统集群角色
[root@master ~]# /opt/kubernetes/bin/kubectl create clusterrolebinding kubelet-bootstrap \
  --clusterrole=system:node-bootstrapper \
  --user=kubelet-bootstrap

创建kubeconfig文件:
在生成kubernetes证书的目录下执行以下命令生成kubeconfig文件:
[root@master ~]# cd /k8s
指定apiserver 内网负载均衡地址
[root@master k8s]# KUBE_APISERVER="https://10.206.176.19:6443"
[root@master k8s]# BOOTSTRAP_TOKEN=674c457d4dcf2eefe4920d7dbb6b0ddc   #这个和上面创建token文件的一致

设置集群参数
[root@master k8s]# /opt/kubernetes/bin/kubectl config set-cluster kubernetes \
  --certificate-authority=./ca.pem \
  --embed-certs=true \
  --server=${KUBE_APISERVER} \
  --kubeconfig=bootstrap.kubeconfig

设置客户端认证参数
[root@master k8s]# /opt/kubernetes/bin/kubectl config set-credentials kubelet-bootstrap \
  --token=${BOOTSTRAP_TOKEN} \
  --kubeconfig=bootstrap.kubeconfig

设置上下文参数
[root@master k8s]# /opt/kubernetes/bin/kubectl config set-context default \
  --cluster=kubernetes \
  --user=kubelet-bootstrap \
  --kubeconfig=bootstrap.kubeconfig

设置默认上下文
[root@master k8s]# /opt/kubernetes/bin/kubectl config use-context default --kubeconfig=bootstrap.kubeconfig

创建kube-proxy kubeconfig文件

[root@master k8s]# /opt/kubernetes/bin/kubectl config set-cluster kubernetes \
  --certificate-authority=./ca.pem \
  --embed-certs=true \
  --server=${KUBE_APISERVER} \
  --kubeconfig=kube-proxy.kubeconfig

[root@master k8s]# /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

[root@master k8s]# /opt/kubernetes/bin/kubectl config set-context default \
  --cluster=kubernetes \
  --user=kube-proxy \
  --kubeconfig=kube-proxy.kubeconfig

[root@master k8s]# /opt/kubernetes/bin/kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig

[root@master k8s]# ls    #会多出下面两个.kubeconfig文件
bootstrap.kubeconfig  kube-proxy.kubeconfig

将这两个文件拷贝到Node节点/opt/kubernetes/cfg目录下。 !!!!不能忽略
[root@master k8s]# scp bootstrap.kubeconfig  kube-proxy.kubeconfig node1:/opt/kubernetes/cfg
[root@master k8s]# scp bootstrap.kubeconfig  kube-proxy.kubeconfig node2:/opt/kubernetes/cfg

部署kubelet组件
将前面下载的二进制包中的kubelet和kube-proxy拷贝到/opt/kubernetes/bin目录下。 
(master节点)
cd /root/kubernetes/server/bin/
scp kubelet kube-proxy node1:/opt/kubernetes/bin
scp kubelet kube-proxy node2:/opt/kubernetes/bin
----------------------下面这些操作在所有node节点完成:---------------------------
创建kubelet配置文件:
# vim /opt/kubernetes/cfg/kubelet
KUBELET_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.13.142 \
--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"
===============================================================
参数说明:
* --hostname-override 在集群中显示的主机名(你改成node1,node2也可以)
* --kubeconfig 指定kubeconfig文件位置,会自动生成
* --bootstrap-kubeconfig 指定刚才生成的bootstrap.kubeconfig文件
* --cert-dir 颁发证书存放位置
* --pod-infra-container-image 管理Pod网络的镜像
===============================================================
其中/opt/kubernetes/cfg/kubelet.config配置文件如下:
# vim /opt/kubernetes/cfg/kubelet.config
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
address: 192.168.13.142
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
# systemctl start kubelet

master节点:
在Master审批Node加入集群:
启动后还没加入到集群中,需要手动允许该节点才可以。
在Master节点查看请求签名的Node:
[root@master ~]# /opt/kubernetes/bin/kubectl get csr
找到NAME
[root@master ~]# /opt/kubernetes/bin/kubectl certificate approve 刚刚获取到的NAME
[root@master ~]# /opt/kubernetes/bin/kubectl get node


node节点:
部署kube-proxy组件
创建kube-proxy配置文件:
# vim /opt/kubernetes/cfg/kube-proxy
KUBE_PROXY_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.13.142 \
--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

启动:
# systemctl daemon-reload
# systemctl enable kube-proxy
# systemctl start kube-proxy

master节点:
查看集群状态
[root@master ~]# /opt/kubernetes/bin/kubectl get node
NAME             STATUS    ROLES     AGE       VERSION
192.168.13.142   Ready     <none>    1h        v1.11.0
192.168.13.143   Ready     <none>    1h        v1.11.0
[root@master ~]# /opt/kubernetes/bin/kubectl get cs
NAME                 STATUS    MESSAGE              ERROR
scheduler            Healthy   ok                   
controller-manager   Healthy   ok                   
etcd-1               Healthy   {"health": "true"}   
etcd-2               Healthy   {"health": "true"}   
etcd-0               Healthy   {"health": "true"}   
运行一个测试示例
如果你网速不行的话可以先去node节点上拉取nginx镜像
或者配置镜像加速器,要不然可能会失败

docker加速器

比如这样,没有Running就是失败
在这里插入图片描述

master节点:
创建一个Nginx Web,判断集群是否正常工作:
[root@master ~]# /opt/kubernetes/bin/kubectl run nginx --image=nginx --replicas=3
查看Pod,Service:(创建过后先查看,等全部都Running再弄端口)
[root@master ~]# /opt/kubernetes/bin/kubectl get pods 
NAME                     READY     STATUS    RESTARTS   AGE
nginx-64f497f8fd-r2sx8   1/1       Running   0          39m
nginx-64f497f8fd-v77z7   1/1       Running   0          1h
nginx-64f497f8fd-v7glf   1/1       Running   0          1h
[root@master ~]# /opt/kubernetes/bin/kubectl expose deployment nginx --port=88 --target-port=80 --type=NodePort
[root@master ~]# /opt/kubernetes/bin/kubectl get svc  查看端口
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP        2h
nginx        NodePort    10.0.0.73    <none>        88:48236/TCP   1h

此时打开浏览器,访问node节点加端口48236
http://192.168.13.142:48236  能访问到nginx界面就算成功

在这里插入图片描述

4.可视化界面Dashboard
master节点:
部署Dashboard(Web UI)
部署UI有三个文件:
* dashboard-deployment.yaml           // 部署Pod,提供Web服务
* dashboard-rbac.yaml                 // 授权访问apiserver获取信息
* dashboard-service.yaml              // 发布服务,提供对外访问
首先我们创建这三个文件的存放目录(自定义)
[root@master ~]# mkdir /opt/da_yaml
[root@master ~]# cd /opt/da_yaml
[root@master da_yaml]# vim dashboard-deployment.yaml
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: kubernetes-dashboard
  namespace: kube-system
  labels:
    k8s-app: kubernetes-dashboard
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: Reconcile
spec:
  selector:
    matchLabels:
      k8s-app: kubernetes-dashboard
  template:
    metadata:
      labels:
        k8s-app: kubernetes-dashboard
      annotations:
        scheduler.alpha.kubernetes.io/critical-pod: ''
    spec:
      serviceAccountName: kubernetes-dashboard
      containers:
      - name: kubernetes-dashboard
        image: registry.cn-hangzhou.aliyuncs.com/kube_containers/kubernetes-dashboard-amd64:v1.8.1 
        resources:
          limits:
            cpu: 100m
            memory: 300Mi
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 9090
          protocol: TCP
        livenessProbe:
          httpGet:
            scheme: HTTP
            path: /
            port: 9090
          initialDelaySeconds: 30
          timeoutSeconds: 30
      tolerations:
      - key: "CriticalAddonsOnly"
        operator: "Exists"
        
[root@master da_yaml]# vim dashboard-rbac.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    k8s-app: kubernetes-dashboard
    addonmanager.kubernetes.io/mode: Reconcile
  name: kubernetes-dashboard
  namespace: kube-system
---

kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: kubernetes-dashboard-minimal
  namespace: kube-system
  labels:
    k8s-app: kubernetes-dashboard
    addonmanager.kubernetes.io/mode: Reconcile
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
  - kind: ServiceAccount
    name: kubernetes-dashboard
    namespace: kube-system
    
[root@master da_yaml]# vim dashboard-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: kubernetes-dashboard
  namespace: kube-system
  labels:
    k8s-app: kubernetes-dashboard
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: Reconcile
spec:
  type: NodePort
  selector:
    k8s-app: kubernetes-dashboard
  ports:
  - port: 80
    targetPort: 9090

创建:
[root@master da_yaml]# /opt/kubernetes/bin/kubectl create -f dashboard-rbac.yaml
[root@master da_yaml]# /opt/kubernetes/bin/kubectl create -f dashboard-deployment.yaml
[root@master da_yaml]# /opt/kubernetes/bin/kubectl create -f dashboard-service.yaml
等待数分钟,查看资源状态:
[root@master da_yaml]# /opt/kubernetes/bin/kubectl get all -n kube-system
NAME                                       READY     STATUS    RESTARTS   AGE
pod/kubernetes-dashboard-d9545b947-tmdns   1/1       Running   0          49m

NAME                           TYPE       CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
service/kubernetes-dashboard   NodePort   10.0.0.149   <none>        80:37938/TCP   1h

NAME                                   DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/kubernetes-dashboard   1         1         1            1           1h

NAME                                             DESIRED   CURRENT   READY     AGE
replicaset.apps/kubernetes-dashboard-d9545b947   1         1         1         1h
查看访问端口:
[root@master da_yaml]# /opt/kubernetes/bin/kubectl get svc -n kube-system 
NAME                   TYPE       CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
kubernetes-dashboard   NodePort   10.0.0.149   <none>        80:37938/TCP   1h
打开浏览器访问http://192.168.13.142:37938/

在这里插入图片描述

Logo

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

更多推荐