我的 K8S 架构搭建 之旅

不问归期 提交于 2020-03-24 12:24:31

一、总体框架图:
我的 K8S 架构搭建 之旅
Master组件:
nkube-apiserver
Kubernetes API,集群的统一入口,各组件协调者,以HTTP API提供接口服务,所有对象资源的增删改查和监听操作都交给APIServer处理后再提交给Etcd存储。
nkube-controller-manager
处理集群中常规后台任务,一个资源对应一个控制器,而ControllerManager就是负责管理这些控制器的。
nkube-scheduler
根据调度算法为新创建的Pod选择一个Node节点。

Node组件:
nkubelet
kubelet是Master在Node节点上的Agent,管理本机运行容器的生命周期,比如创建容器、Pod挂载数据卷、
下载secret、获取容器和节点状态等工作。kubelet将每个Pod转换成一组容器。
nkube-proxy
在Node节点上实现Pod网络代理,维护网络规则和四层负载均衡工作。
ndocker或rocket/rkt
运行容器。

第三方服务:
netcd
分布式键值存储系统。用于保持集群状态,比如Pod、Service等对象信息。

二、部署的步骤:
1、环境规划
2、安装Docker
3、自签TLS证书
4、部署Etcd集群
5、部署Flannel网络
6、创建Node节点kubeconfig文件
7、获取K8S二进制包
8、运行Master组件
9、运行Node组件
10、查询集群状态
11、启动一个测试示例

1、环境规划:
我的 K8S 架构搭建 之旅

#统一时区和时间信息
timedatectl set-local-rtc 1 
timedatectl set-timezone Asia/Shanghai 
timedatectl status 

#修改主机名
hostnamectl --static set-hostname docker1
hostnamectl status

2、安装Docker

#CentOS7
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 makecache fast
yum install -y docker-ce
systemctl start docker
docker run hello-world
docker version
echo '{
  "registry-mirrors": [ "https://registry.docker-cn.com";]
}' > /etc/docker/daemon.json
systemctl restart docker

3、自签证书:

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

mkdir -p /opt/kubernetes/{bin,cfg,ssl}          #三台docker宿主机上都要执行
cd /opt/kubernetes/ssl
cat > ca-config.json <<EOF
{
  "signing": {
    "default": {
      "expiry": "87600h"
    },
    "profiles": {
      "kubernetes": {
         "expiry": "87600h",
         "usages": [
            "signing",
            "key encipherment",
            "server auth",
            "client auth"
        ]
      }
    }
  }
}
EOF

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

cfssl gencert -initca ca-csr.json | cfssljson -bare ca -

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

cat > server-csr.json <<EOF
{
    "CN": "kubernetes",
    "hosts": [
      "127.0.0.1",
      "192.168.0.195",
      "192.168.0.78",
      "192.168.0.101",
      "10.10.10.1",
      "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"
        }
    ]
}
EOF

cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare server

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

cat > admin-csr.json <<EOF
{
  "CN": "admin",
  "hosts": [],
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "L": "BeiJing",
      "ST": "BeiJing",
      "O": "system:masters",
      "OU": "System"
    }
  ]
}
EOF

cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes admin-csr.json | cfssljson -bare admin

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

cat > kube-proxy-csr.json <<EOF
{
  "CN": "system:kube-proxy",
  "hosts": [],
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "L": "BeiJing",
      "ST": "BeiJing",
      "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

4、部署ETCD集群:

#前提是已经rz etcd的二进制执行文件到/opt/kubernetes/bin/目录下 
[root@docker3 ~]# ls /opt/kubernetes/bin/
etcd  etcdctl 

#修改配置文件
[root@docker3 ~]# cat /opt/kubernetes/cfg/etcd
#[Member]
ETCD_NAME="etcd03"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.0.195:2380"
ETCD_LISTEN_CLIENT_URLS="https://192.168.0.195:2379"

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

#修改启动文件
[root@docker3 ~]# cat /usr/lib/systemd/system/etcd.service
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
Type=notify
EnvironmentFile=-/opt/kubernetes/cfg/etcd
ExecStart=/opt/kubernetes/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} \
--initial-cluster-state=new \
--cert-file=/opt/kubernetes/ssl/server.pem \
--key-file=/opt/kubernetes/ssl/server-key.pem \
--peer-cert-file=/opt/kubernetes/ssl/server.pem \
--peer-key-file=/opt/kubernetes/ssl/server-key.pem \
--trusted-ca-file=/opt/kubernetes/ssl/ca.pem \
--peer-trusted-ca-file=/opt/kubernetes/ssl/ca.pem
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

#记得直接scp证书到别的node节点!!!  第二次安装的经验

#启动和开机启动
systemctl start etcd
systemctl enable etcd

#验证下运行情况
[root@docker3 ~]# netstat -lntup|grep etcd
tcp        0      0 192.168.0.195:2379      0.0.0.0:*               LISTEN      18038/etcd          
tcp        0      0 127.0.0.1:2379          0.0.0.0:*               LISTEN      18038/etcd          
tcp        0      0 192.168.0.195:2380      0.0.0.0:*               LISTEN      18038/etcd          
[root@docker3 ~]#
[root@docker3 ~]# systemctl status etcd
● etcd.service - Etcd Server
   Loaded: loaded (/usr/lib/systemd/system/etcd.service; disabled; vendor preset: disabled)
   Active: activating (start) since Thu 2018-02-01 14:40:44 CST; 3s ago
Main PID: 18272 (etcd)
   Memory: 14.8M
   CGroup: /system.slice/etcd.service
           └─18272 /opt/kubernetes/bin/etcd --name=etcd03 --data-dir=/var/lib/etcd/default.etcd --listen-peer-urls=https://192.168.0.195:2380 ...

Feb 01 14:40:46 docker3 etcd[18272]: request cluster ID mismatch (got 3b51f2169c0d39ba want ee2e162b8c4b542b)
Feb 01 14:40:47 docker3 etcd[18272]: request sent was ignored (cluster ID mismatch: peer[9c97b9b3d6c86056]=3b51f2169c0d39ba, local=ee2...c4b542b)
Feb 01 14:40:47 docker3 etcd[18272]: request sent was ignored (cluster ID mismatch: peer[ea36423e7d6e2441]=3b51f2169c0d39ba, local=ee2...c4b542b)
Feb 01 14:40:47 docker3 etcd[18272]: request sent was ignored (cluster ID mismatch: peer[9c97b9b3d6c86056]=3b51f2169c0d39ba, local=ee2...c4b542b)
Feb 01 14:40:47 docker3 etcd[18272]: request cluster ID mismatch (got 3b51f2169c0d39ba want ee2e162b8c4b542b)
Feb 01 14:40:47 docker3 etcd[18272]: request sent was ignored (cluster ID mismatch: peer[ea36423e7d6e2441]=3b51f2169c0d39ba, local=ee2...c4b542b)
Feb 01 14:40:47 docker3 etcd[18272]: request cluster ID mismatch (got 3b51f2169c0d39ba want ee2e162b8c4b542b)
Feb 01 14:40:47 docker3 etcd[18272]: request cluster ID mismatch (got 3b51f2169c0d39ba want ee2e162b8c4b542b)
Feb 01 14:40:47 docker3 etcd[18272]: request cluster ID mismatch (got 3b51f2169c0d39ba want ee2e162b8c4b542b)
Feb 01 14:40:47 docker3 etcd[18272]: request cluster ID mismatch (got 3b51f2169c0d39ba want ee2e162b8c4b542b)
Hint: Some lines were ellipsized, use -l to show in full.

#查看etcd集群状态:
[root@docker3 ~]# cd /opt/kubernetes/ssl
[root@docker3 ssl]# ls
ca-key.pem  ca.pem  server-key.pem  server.pem
[root@docker3 ssl]# /opt/kubernetes/bin/etcdctl \
> --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem \
> --endpoints="https://192.168.0.195:2379,https://192.168.0.78:2379,https://192.168.0.101:2379" \
> cluster-health
member 1867c607b632ef74 is healthy: got healthy result from https://192.168.0.78:2379
member 4f5978bb4e5f8a9e is unreachable: no available published client urls      #报错 其中master的etcd不能用啊 ???
member f948418339a94dd3 is healthy: got healthy result from https://192.168.0.101:2379
cluster is healthy    #但是其他两个节点又没有问题,我日!
[root@docker3 ssl]# netstat -lntup|grep etcd
tcp        0      0 192.168.0.195:2379      0.0.0.0:*               LISTEN      19136/etcd          
tcp        0      0 127.0.0.1:2379          0.0.0.0:*               LISTEN      19136/etcd          
tcp        0      0 192.168.0.195:2380      0.0.0.0:*               LISTEN      19136/etcd

#检测是否正常的语句
/opt/kubernetes/bin/etcdctl \
--ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem \
--endpoints="https://192.168.0.195:2379,https://192.168.0.78:2379,https://192.168.0.101:2379" \
cluster-health

#第二次安装完美成功!
[root@docker3 ssl]# /opt/kubernetes/bin/etcdctl \
> --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem \
> --endpoints="https://192.168.0.195:2379,https://192.168.0.78:2379,https://192.168.0.101:2379" \
> cluster-health
member 1867c607b632ef74 is healthy: got healthy result from https://192.168.0.78:2379
member 4f5978bb4e5f8a9e is healthy: got healthy result from https://192.168.0.195:2379
member f948418339a94dd3 is healthy: got healthy result from https://192.168.0.101:2379
cluster is healthy

5、部署flanneld网络

1)写入分配的子网段到etcd,供flanneld使用

/opt/kubernetes/bin/etcdctl \

--ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem \
--endpoints="https://192.168.0.211:2379,https://192.168.0.212:2379,https://192.168.0.213:2379" \
set /coreos.com/network/config '{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}'
2)下载二进制包

wget https://github.com/coreos/flannel/releases/download/v0.9.1/flannel-v0.9.1-linux-amd64.tar.gz

3)配置Flannel cfg
4)systemd管理Flannel
5)配置Docker启动指定子网段
6)启动

#前提是已经rz flanneld 的二进制执行文件到/opt/kubernetes/bin/目录下 
[root@docker3 ~]# ls /opt/kubernetes/bin/
etcd  etcdctl  flanneld  mk-docker-opts.sh

#写入分配的子网段到etcd,供flanneld使用
cd /opt/kubernetes/ssl
/opt/kubernetes/bin/etcdctl \
--ca-file=ca.pem --cert-file=server.pem  --key-file=server-key.pem \
--endpoints="https://192.168.0.195:2379,https://192.168.0.78:2379,https://192.168.0.101:2379" \
set /coreos.com/network/config '{ "Network":  "172.17.0.0/16", "Backend": {"Type":  "vxlan"}}'

#配置Flannel cfg
cat <<EOF >/opt/kubernetes/cfg/flanneld
FLANNEL_OPTIONS="--etcd-endpoints=https://192.168.0.195:2379,https://192.168.0.78:2379,https://192.168.0.101:2379 -etcd-cafile=/opt/kubernetes/ssl/ca.pem -etcd-certfile=/opt/kubernetes/ssl/server.pem -etcd-keyfile=/opt/kubernetes/ssl/server-key.pem"
EOF

#systemd管理Flannel
cat <<EOF >/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              
#问题1:我这里如果\\$FLANNEL_OPTIONS  就可以启动        \$FLANNEL_OPTIONS 反而报错 可能和配置文件没有写正确有关系把!   第二次安装还是需要注释掉才能正常启动   2018年2月5日 16:16:34 经过阿良哥指导是因为$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
EOF

#配置Docker启动指定子网段
cat <<EOF >/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
EOF

#启动
systemctl daemon-reload
systemctl enable flanneld
systemctl restart flanneld
systemctl restart docker

#查看etcd存储的信息
[root@docker3 ssl]# /opt/kubernetes/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem  --key-file=server-key.pem --endpoints="https://192.168.0.195:2379,https://192.168.0.78:2379,https://192.168.0.101:2379" ls /coreos.com/network/subnets
/coreos.com/network/subnets/172.17.47.0-24
/coreos.com/network/subnets/172.17.92.0-24
[root@docker3 ssl]# /opt/kubernetes/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem  --key-file=server-key.pem --endpoints="https://192.168.0.195:2379,https://192.168.0.78:2379,https://192.168.0.101:2379" get /coreos.com/network/subnets/172.17.47.0-24
{"PublicIP":"192.168.0.101","BackendType":"vxlan","BackendData":{"VtepMAC":"fe:f4:f3:14:cd:6a"}}
[root@docker3 ssl]# /opt/kubernetes/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem  --key-file=server-key.pem --endpoints="https://192.168.0.195:2379,https://192.168.0.78:2379,https://192.168.0.101:2379" get /coreos.com/network/subnets/172.17.92.0-24
{"PublicIP":"192.168.0.78","BackendType":"vxlan","BackendData":{"VtepMAC":"0a:75:1e:ab:a7:e0"}}

6、创建Node节点所需的kubeconfig文件

#前提是已经rz flanneld 的二进制执行文件到/opt/kubernetes/bin/目录下 
[root@docker3 bin]# ls /opt/kubernetes/bin/
etcd  etcdctl  flanneld  kubectl  mk-docker-opts.sh
[root@docker3 bin]# chmod +x kubectl

# 创建 TLS Bootstrapping Token
export BOOTSTRAP_TOKEN=$(head -c 16 /dev/urandom | od -An -t x | tr -d ' ')
cat > token.csv <<EOF
${BOOTSTRAP_TOKEN},kubelet-bootstrap,10001,"system:kubelet-bootstrap"
EOF

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

# 创建kubelet bootstrapping kubeconfig
export KUBE_APISERVER="https://192.168.0.195:6443"

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

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

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

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

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

# 创建kube-proxy kubeconfig文件
/opt/kubernetes/bin/kubectl config set-cluster kubernetes \
  --certificate-authority=./ca.pem \
  --embed-certs=true \
  --server=${KUBE_APISERVER} \
  --kubeconfig=kube-proxy.kubeconfig

/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

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

/opt/kubernetes/bin/kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig

7、获取K8S二进制包

[root@docker3 bin]# ls /opt/kubernetes/bin
etcd  etcdctl  flanneld  kubectl  master.zip  mk-docker-opts.sh
[root@docker2 ssl]# ls /opt/kubernetes/bin
etcd  etcdctl  flanneld  kubectl  mk-docker-opts.sh  node.zip
[root@docker1 ssl]# ls /opt/kubernetes/bin
etcd  etcdctl  flanneld  kubectl  mk-docker-opts.sh  node.zip

8、运行Master组件

#解压master.zip  运行apiserver.sh如下      并执行kube-apiserver的启动
export MASTER_ADDRESS=${1:-"192.168.0.195"}
export ETCD_SERVERS=${2:-"https://192.168.0.195:2379,https://192.168.0.78:2379,https://192.168.0.101: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=/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/kubernetes/ssl/ca.pem \\
--etcd-certfile=/opt/kubernetes/ssl/server.pem \\
--etcd-keyfile=/opt/kubernetes/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

#保证把token.csv文件放到/opt/kubernetes/cfg/目录下!!!
[root@docker3 cfg]# cp /opt/kubernetes/ssl/token.csv /opt/kubernetes/cfg/
[root@docker3 cfg]# ls /opt/kubernetes/cfg/
etcd  flanneld  kube-apiserver  token.csv

systemctl daemon-reload
systemctl enable kube-apiserver
systemctl restart kube-apiserver

#运行controller-manager.sh如下      并执行kube-controller-manager 的启动
export MASTER_ADDRESS=${1:-"127.0.0.1"}

cat <<EOF >/opt/kubernetes/cfg/kube-controller-manager

KUBE_CONTROLLER_MANAGER_OPTS="--logtostderr=true \\
--v=4 \\
--master=${MASTER_ADDRESS}:8080 \\
--leader-elect=true \\
--address=127.0.0.1 \\
--service-cluster-ip-range=10.10.10.0/24 \\
--cluster-name=kubernetes \\
--cluster-signing-cert-file=/opt/kubernetes/ssl/ca.pem \\
--cluster-signing-key-file=/opt/kubernetes/ssl/ca-key.pem  \\
--service-account-private-key-file=/opt/kubernetes/ssl/ca-key.pem \\
--root-ca-file=/opt/kubernetes/ssl/ca.pem"

EOF

cat <<EOF >/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
EOF

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

#运行controller-manager.sh如下      并执行kube-controller-manager 的启动
export MASTER_ADDRESS=${1:-"127.0.0.1"}

cat <<EOF >/opt/kubernetes/cfg/kube-scheduler
KUBE_SCHEDULER_OPTS="--logtostderr=true \\
--v=4 \\
--master=${MASTER_ADDRESS}:8080 \\
--leader-elect"
EOF

cat <<EOF >/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
EOF

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

echo 'export PATH=$PATH:/opt/kubernetes/bin/' >>/etc/profile
. /etc/profile

#很重要的一步:执行绑定node角色用户
[root@docker3 ssl]# kubectl create clusterrolebinding kubelet-bootstrap --clusterrole=system:node-bootstrapper --user=kubelet-bootstrap
clusterrolebinding "kubelet-bootstrap" created

#验证下:
[root@docker3 bin]# kubectl get cs
NAME                 STATUS    MESSAGE              ERROR
controller-manager   Healthy   ok                   
scheduler            Healthy   ok                   
etcd-2               Healthy   {"health": "true"}   
etcd-0               Healthy   {"health": "true"}   
etcd-1               Healthy   {"health": "true"}

9、运行Node组件

#拷贝第六步 产生 的  node节点所需的kubeconfig文件到各node节点的/opt/kubernetes/cfg/目录下
[root@docker3 ssl]# cd /opt/kubernetes/ssl/
[root@docker3 ssl]# scp kube-proxy.kubeconfig bootstrap.kubeconfig 192.168.0.78:/opt/kubernetes/cfg/
root@192.168.0.78's password:
kube-proxy.kubeconfig                                                                                    100% 6295     5.8MB/s   00:00    
bootstrap.kubeconfig                                                                                     100% 2189     2.3MB/s   00:00    
[root@docker3 ssl]# scp kube-proxy.kubeconfig bootstrap.kubeconfig 192.168.0.101:/opt/kubernetes/cfg/
root@192.168.0.101's password:
kube-proxy.kubeconfig                                                                                    100% 6295   933.6KB/s   00:00    
bootstrap.kubeconfig                                                                                     100% 2189   206.9KB/s   00:00

#解压node.zip  
[root@docker1 bin]# unzip node.zip
[root@docker1 bin]# chmod +x kubelet kube-proxy
[root@docker1 bin]# ls
etcd  etcdctl  flanneld  kubectl  kubelet  kubelet.sh  kube-proxy  mk-docker-opts.sh  node.zip  proxy.sh

#node1节点运行kubelet组件
export NODE_ADDRESS=${1:-"192.168.0.101"}
export DNS_SERVER_IP=${2:-"10.10.10.2"}

cat <<EOF >/opt/kubernetes/cfg/kubelet
KUBELET_OPTS="--logtostderr=true \\
--v=4 \\
--address=${NODE_ADDRESS} \\
--hostname-override=${NODE_ADDRESS} \\
--kubeconfig=/opt/kubernetes/cfg/kubelet.kubeconfig \\
--experimental-bootstrap-kubeconfig=/opt/kubernetes/cfg/bootstrap.kubeconfig \\
--cert-dir=/opt/kubernetes/ssl \\
--allow-privileged=true \\
--cluster-dns=${DNS_SERVER_IP} \\
--cluster-domain=cluster.local \\
--fail-swap-on=false \\
--pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0"
EOF

cat <<EOF >/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
EOF

systemctl daemon-reload
systemctl enable kubelet
systemctl restart kubelet

#node1节点运行kube-proxy组件
export NODE_ADDRESS=${1:-"192.168.0.101"}

cat <<EOF >/opt/kubernetes/cfg/kube-proxy
KUBE_PROXY_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=${NODE_ADDRESS} \
--kubeconfig=/opt/kubernetes/cfg/kube-proxy.kubeconfig"
EOF

cat <<EOF >/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
EOF

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

10、查询集群状态

[root@docker3 ~]# kubectl get node
NAME            STATUS    ROLES     AGE       VERSION
192.168.0.101   Ready     <none>    1d        v1.9.0
192.168.0.78    Ready     <none>    1d        v1.9.0
[root@docker3 ~]# kubectl get cs
NAME                 STATUS    MESSAGE              ERROR
controller-manager   Healthy   ok                   
scheduler            Healthy   ok                   
etcd-0               Healthy   {"health": "true"}   
etcd-1               Healthy   {"health": "true"}   
etcd-2               Healthy   {"health": "true"}  

11、启动一个测试示例

 kubectl run nginx --image=nginx  --replicas=3
 kubectl get pod
 kubectl expose deployment nginx --port=88  --target-port=80 --type=NodePort
 kubectl get svc nginx

问题1的解决:
我的 K8S 架构搭建 之旅
我的 K8S 架构搭建 之旅

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!