1. 程式人生 > >二進位制安裝kubernetes 1.10 最新版(上)

二進位制安裝kubernetes 1.10 最新版(上)

1、環境規劃
2、安裝Docker
3、自籤TLS證書
4、部署Etcd叢集
5、部署Flannel網路
6、建立Node節點kubeconfig檔案
7、獲取K8S二進位制包
8、執行Master元件
9、執行Node元件
10、查詢叢集狀態
11、啟動一個測試示例
12、部署Web UI (Dashboard)

1、環境規劃:

作業系統:centos7.4
Kubernetes :1.10.7
Docker: 18.06.0-ce
Etcd: 3.0
CPU 2核+ 2G記憶體+
master
192.168.1.6 kube-apiserver kube-controller-manager kube-scheduler flannel etcd
node01
192.168.1.7 kubelet kube-proxy docker flannel etcd
node02
192.168.1.8 kubelet kube-proxy docker flannel etcd
注意有iptables的注意新增規則內網互通:
iptables -I INPUT -s 192.168.1.0/24 -j ACCEPT

2、安裝docker

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
 cat << EOF > /etc/docker/daemon.json
{
"registry-mirrors": [ "https://registry.docker-cn.com"]
}
EOF

systemctl start docker
systemctl enable docker

建立k8s的目錄:

 mkdir -p /opt/kubernetes/{bin,cfg,ssl}

3、自籤TLS證書

用到證書的地方
這裡寫圖片描述

在master上面操作,即192.168.1.6
安裝證書生成工具cfssl:

cd /opt/ssl
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 *
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

不知道如何建立證書可以根據提示 生成模板然後修改:
cfssl print-defaults config >config.json
cfssl print-defaults csr >csr.json

生成我們需要的證書指令碼;

[[email protected] ssl]# cat certificate.sh
#證書根機構
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": "hangzhou",
            "ST": "hangzhou",
      	    "O": "k8s",
            "OU": "System"
        }
    ]
}
EOF

#用cfssl生成證書

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

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

#用於api http通訊的證書資訊
cat > server-csr.json <<EOF
{
    "CN": "kubernetes",
    "hosts": [
      "127.0.0.1",
      "192.168.1.6",
      "192.168.1.7",
      "192.168.1.8",
      "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

#生成server證書
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

然後執行該指令碼
我們只需要生成的pem格式的證書,其他的可以刪掉了。

ls |grep -v pem|xargs -i rm {}

然後將我們證書拷貝到我們定義的地方:

cp server* ca* /opt/kubernetes/ssl/

4、部署etcd 儲存叢集

tar xvf etcd-v3.2.12-linux-amd64.tar.gz
cd etcd-v3.2.12-linux-amd64

將我們需要的可執行檔案拷貝到我們自定義的地方

cp etcd  /opt/kubernetes/bin/
cp etcdctl /opt/kubernetes/bin/

編輯etcd配置檔案:

vim /opt/kubernetes/cfg/etcd

#[Member]
ETCD_NAME="etcd01"
#資料目錄
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.1.6:2380"
ETCD_LISTEN_CLIENT_URLS="https://192.168.1.6:2379"

#節點資訊
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.1.6:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.1.6:2379"
ETCD_INITIAL_CLUSTER="etcd01=https://192.168.1.6:2380,etcd02=https://192.168.1.7:2380,etcd03=https://192.168.1.8:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"

將etcd配置到系統環境中

vim /usr/lib/systemd/system/etcd.service


[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
Type=notify
EnvironmentFile=-/opt/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

啟動etcd服務:

systemctl start etcd

如果啟動的時候沒有退出命令互動介面,可以強制ctrl +c
如果報錯:檢視/var/log/message
開機啟動:

systemctl enable etcd

此時master上面etcd配置完畢,我們將其配置拷貝到其他2臺機器上面(192.168.1.7,192.168.1.8)

rsync -avzP /opt/kubernetes [email protected]:/opt
rsync -avzP /opt/kubernetes [email protected]:/opt

傳過去以後我們只需要變更/opt/kubernetes/cfg/etcd 配置檔案即可
需要改的地方:
這裡寫圖片描述
分別啟動 192.168.1.7和192.168.1.8上的etcd

測試etcd:

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.1.6:2379,https://192.168.1.7:2379,https://192.168.1.8:2379" cluster-health

我們也可以將etcdctl寫入bash裡面,方便使用
如果出現:8月 20 17:51:06 es1 etcd[2068]: request sent was ignored (cluster ID mismatch: peer[5fe38a6e135d0fde]=cdf818194e3a8c32, local=b37e2d2fbf0626a5)
類似錯誤,我們需要刪除etcd的資料目錄即可解決。rm -rf /var/lib/etcd/*

5、部署Flannel網路

將解壓後得到的可執行檔案放入我們之定義的路徑下面

cp flanneld mk-docker-opts.sh /opt/kubernetes/bin/

配置配置檔案:
直接在命令列 將配置檔案利用EOF寫進去

#寫flanneld配置檔案
cat <<EOF >/opt/kubernetes/cfg/flanneld

FLANNEL_OPTIONS="--etcd-endpoints=https://192.168.1.6:2379,https://192.168.1.7:2379,https://192.168.1.8:2379 \
-etcd-cafile=/opt/kubernetes/ssl/ca.pem \
-etcd-certfile=/opt/kubernetes/ssl/server.pem \
-etcd-keyfile=/opt/kubernetes/ssl/server-key.pem"

EOF

#寫入 flanneld系統配置檔案
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
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

#寫入分配的子網段到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.1.6:2379,https://192.168.1.7:2379,https://192.168.1.8:2379" set /coreos.com/network/config '{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}'

測試:

/opt/kubernetes/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.1.6:2379,https://192.168.1.7:2379,https://192.168.1.8:2379" get /coreos.com/network/config

systemctl enable flanneld
systemctl start flanneld

如果報錯請檢視:/var/log/message

檢視flanneld分配的網路:

cat /run/flannel/subnet.env

編輯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 restart docker

檢視ifconfig 中 會出現一個flannel 網路,並且flannel和docker0 網路段會相同

設定完成後將配置檔案分發到node02,和node03上即192.168.1.7和192.168.1.8

rsync -avzP /opt/kubernetes/bin/flanneld mk-docker-opts.sh [email protected]:/opt/kubernetes/bin/
rsync -avzP /opt/kubernetes/cfg/flanneld [email protected]:/opt/kubernetes/cfg/
rsync -avzP /usr/lib/systemd/system/flanneld.service [email protected]:/usr/lib/systemd/system/

rsync -avzP /opt/kubernetes/bin/flanneld mk-docker-opts.sh [email protected]:/opt/kubernetes/bin/
rsync -avzP /opt/kubernetes/cfg/flanneld [email protected]:/opt/kubernetes/cfg/
rsync -avzP /usr/lib/systemd/system/flanneld.service [email protected]:/usr/lib/systemd/system/

然後將docker系統配置檔案(即/usr/lib/systemd/system/docker.servic)改成和 master(192.168.1.6)一樣,
然後啟動

systemctl daemon-reload
systemctl enable flanneld
systemctl start flanneld
systemctl restart docker

檢視ifconfig 是否有flanneld,docker0網路是否和flanneld一致,
在master上ping node02上的docker0閘道器,如果能通的話證明Ok
如下:

[[email protected] ssl]# /opt/kubernetes/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.1.6:2379,https://192.168.1.7:2379,https://192.168.1.8:2379" ls /coreos.com/network/subnets
/coreos.com/network/subnets/172.17.41.0-24
/coreos.com/network/subnets/172.17.96.0-24
/coreos.com/network/subnets/172.17.98.0-24

[[email protected] ssl]# ping 172.17.41.1
PING 172.17.41.1 (172.17.41.1) 56(84) bytes of data.
64 bytes from 172.17.41.1: icmp_seq=1 ttl=64 time=0.454 ms

6、建立Node節點kubeconfig檔案

在Master上執行即192.168.1.6

cd /opt/ssl

執行指令碼獲取我們所需的檔案,總共有三個:
1、TLS Bootstrapping Token
2、kubelet kubeconfig
3、kube-proxy kubeconfig

[[email protected] ssl]# cat kubeconfig.sh
# 建立 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.1.6: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

執行該指令碼:

sh -x kubeconfig.sh

此時我們可以獲取到三個配置檔案,我們將其拷貝到指定目錄:

 cp -rf token.csv bootstrap.kubeconfig kube-proxy.kubeconfig /opt/kubernetes/cfg/

7、獲取K8S二進位制包,

8、執行Master元件

1.10包地址:

master需要三個元件:kube-apiserver kube-controller-manager kube-scheduler
我們將其拿出來 放入指定目錄:

tar xvf kubernetes-server-linux-amd64.tar.gz
cp kubernetes/server/bin/kube-scheduler ./
cp kubernetes/server/bin/kube-controller-manager ./
cp kubernetes/server/bin/kube-apiserver ./
[[email protected] kubernetes]# ls
apiserver.sh  controller-manager.sh  kube-apiserver  kube-controller-manager  kubectl  kube-scheduler  master.zip  scheduler.sh

mv kube-apiserver kube-controller-manager kube-scheduler /opt/kubernetes/bin/
賦予許可權
chmod +x /opt/kubernetes/bin/*


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

其中的*.sh檔案是我們自定義的指令碼,幫助我們安裝。
安裝kube-apiserver

[[email protected] kubernetes]# cat apiserver.sh
#!/bin/bash

MASTER_ADDRESS=${1:-"192.168.1.6"}
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=/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

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

執行指令碼:

./apiserver.sh 192.168.1.6 https://192.168.1.6:2379,https://192.168.1.7:2379,https://192.168.1.8:2379

安裝:controller-manager
編寫安裝指令碼:

[[email protected] master_pkg]# cat controller-manager.sh
#!/bin/bash

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 127.0.0.1
ps uxa |grep controller-manager

安裝scheduler:

編寫安裝指令碼:
[[email protected] master_pkg]# cat scheduler.sh
#!/bin/bash

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

執行指令碼安裝啟動:

./scheduler.sh 127.0.0.1
ps aux |grep scheduler

到這裡我們可以測試叢集大概狀態了

[[email protected] master_pkg]# 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"}

9、執行node元件

首先我們需要在master上面生成一個角色用於node上證書繫結認證
在master上面操作(192.168.1.6)
建立認證使用者

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

如果建立錯誤我們可以先刪除再建立:
刪除: kubectl delete clusterrolebinding kubelet-bootstrap
將在master上面生成的 bootstrap.kubeconfig ,kube-proxy.kubeconfig檔案傳到node節點上面去

rsync -avPz bootstrap.kubeconfig kube-proxy.kubeconfig [email protected]:/opt/kubernetes/cfg/
rsync -avPz bootstrap.kubeconfig kube-proxy.kubeconfig [email protected]:/opt/kubernetes/cfg/

在node上操作(192.168.1.7)
將我們需要的檔案(kubelet ,kube-proxy)拿出來,檔案都在我們下載的二進位制包中,其中*.sh為我們自定義的指令碼

[[email protected] node_pkg]# ls
kubelet  kubelet.sh  kube-proxy   proxy.sh
chmod +x *.sh

mv kubelet kube-proxy /opt/kubernetes/bin/
chmod +x /opt/kubernetes/bin/*

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

安裝kubelet
編輯安裝指令碼:

[[email protected] node_pkg]# cat kubelet.sh
#!/bin/bash

NODE_ADDRESS=${1:-"192.168.1.6"}
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

執行指令碼安裝:

sh ./kubelet.sh 192.168.1.7 10.10.10.2

啟動出錯檢視:/var/log/message

安裝kube-proxy:
編寫安裝指令碼:

[[email protected] node_pkg]# cat proxy.sh
#!/bin/bash

NODE_ADDRESS=${1:-"192.168.1.7"}

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

啟動指令碼:

sh ./proxy.sh 192.168.1.7
ps aux |grep proxy
報錯檢視:/var/log/message
journalctl -f -t kube-proxy     
journalctl -u kube-proxy

node02沒有出錯的話我們就去node03上操作(192.168.1.8)
同樣的操作:
只是將腳本里面對應的ip改成本機ip

./kubelet.sh 192.168.1.8 10.10.10.2
./proxy.sh 192.168.1.8

到此叢集的安裝結束,我們測試叢集通不通

10、查詢叢集狀態:

[[email protected] ~]# kubectl get csr
NAME                                                   AGE       REQUESTOR           CONDITION
node-csr-0EfKRr3ZU_UVFi5O8XLASGNxTzMrIsxMHg8oHJnY3JA   4m        kubelet-bootstrap   Pending
node-csr-MTGLph0ohPwDJK6FtjJagnkjo3utvYjL1N52nBA1xRU   43m       kubelet-bootstrap   Pending

可以看到,節點 是處於等待狀態

我們檢視節點接入情況:

[[email protected] ~]# kubectl get node
No resources found.

目前沒有節點加入,我們將節點加入:

kubectl certificate approve node-csr-0EfKRr3ZU_UVFi5O8XLASGNxTzMrIsxMHg8oHJnY3JA
kubectl certificate approve node-csr-MTGLph0ohPwDJK6FtjJagnkjo3utvYjL1N52nBA1xRU

騷等片刻,我們發現節點已經加入進來:

[[email protected] ~]#  kubectl get node
NAME          STATUS     ROLES     AGE       VERSION
192.168.1.7   NotReady   <none>    2s         v1.10.7

192.168.1.8   Ready      <none>    1m         v1.10.7

檢視叢集狀態:( kubectl get componentstatus)

[[email protected] ~]#  kubectl get cs
NAME                 STATUS    MESSAGE              ERROR
controller-manager   Healthy   ok
scheduler            Healthy   ok
etcd-1               Healthy   {"health": "true"}
etcd-2               Healthy   {"health": "true"}
etcd-0               Healthy   {"health": "true"}