阿里雲 Centos7 輕量伺服器 安裝 K8s 叢集---採坑
前言:集百家之言......
環境規劃:
作業系統:centos7.3
Kubernetes :1.10.7
Docker: 18.06.0-ce
Etcd: 3.0
CPU 1核+ 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
基礎環境:
關閉防火牆:
[[email protected] ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]#
[[email protected] ~]# systemctl disable firewalld
關閉selinux:
[[email protected] ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config
[[email protected] ~]# setenforce 0
setenforce: SELinux is disabled
[[email protected] ~]#
[[email protected] ~]#
關閉swap:
[[email protected] ~]# swapoff -a
[[email protected] ~]# free -m
total used free shared buff/cache available
Mem: 1838 1288 76 17 474 366
Swap: 0 0 0
[ [email protected] ~]#
設定k8s叢集主機名(你自己的叢集節點ip)(可有可無):
# echo '47.95.7.67 k8s-master
47.95.7.67 etcd
120.783.212 k8s-node-212
39.1.201.0 k8s-node-0' >> /etc/hosts
同步時間:
[[email protected] ~]# yum install ntpdate -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
base | 3.6 kB 00:00:00
docker-ce-stable | 2.9 kB 00:00:00
epel | 3.2 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/3): epel/x86_64/updateinfo | 933 kB 00:00:00
(2/3): updates/7/x86_64/primary_db | 6.0 MB 00:00:00
(3/3): epel/x86_64/primary | 3.6 MB 00:00:00
epel 12756/12756
Package ntpdate-4.2.6p5-28.el7.centos.x86_64 already installed and latest version
Nothing to do
[[email protected] ~]# ntpdate ntp.api.bz
1 Nov 12:22:26 ntpdate[27967]: the NTP socket is in use, exiting
[[email protected] ~]#
安裝Docker 方案一:
# yum install -y yum-utils device-mapper-persistent-data lvm2
如果提示container-selinux依賴問題,先安裝ce-17.03匹配版本:
# yum localinstall https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.3.ce-1.e17.noarch.rpm
如果已安裝docker-ce 18, 先解除安裝:
# yum remove docker container-selinux
# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
檢視當前所有docker 版本
# yum list docker-ce.x86_64 --showduplicates |sort -r
目前docker 最大支援docker-ce-17.03, 所以要指定版本安裝
# yum install -y docker-ce
# systemctl enable docker & systemctl start docker
# vi /lib/systemd/system/docker.service
#找到ExecStart=xxx,在這行上面加入一行,內容如下:(k8s的網路需要)
ExecStartPost=/sbin/iptables -I FORWARD -s 0.0.0.0/0 -j ACCEPT
啟動服務
# systemctl daemon-reload
# service docker start
安裝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}
自籤TLS證書:
在master上面操作,即192.168.1.6
安裝證書生成工具cfssl:
不知道如何建立證書可以根據提示 生成模板然後修改:
cfssl print-defaults config >config.json
cfssl print-defaults csr >csr.json
生成我們需要的證書指令碼:
然後執行該指令碼
我們只需要生成的pem格式的證書,其他的可以刪掉了。
ls |grep -v pem|xargs -i rm {}
然後將我們證書拷貝到我們定義的地方:
cp server* ca* /opt/kubernetes/ssl/
部署etcd 儲存叢集:
首先我們在master上操作,即192.168.1.6
此處我們可以用的最新版的:3.2.12版本
wget https://github.com/etcd-io/etcd/releases/download/v3.2.12/etcd-v3.2.12-linux-amd64.tar.gz
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服務:
檢視/var/log/message
systemctl start etcd
#開機啟動
systemctl enable etcd
如果啟動的時候沒有退出命令互動介面,可以強制ctrl +c
如果報錯:journalctl -xe 檢視日誌 檢視/var/log/message。
說道說道阿里雲輕量伺服器:
錯誤一:systemd啟動etcd服務的時候出現錯誤:Failed at step CHDIR spawning /usr/bin/etcd: No such file or directory
解決辦法:etcd.service服務配置檔案中設定的工作目錄WorkingDirectory=/var/lib/etcd/必須存在,否則會報以上錯誤
錯誤二:systemd啟動etcd服務的時候出現錯誤:cannot assign requested address
解決辦法:更改 vim /opt/kubernetes/cfg/etcd ,繫結阿里雲的私網IP。
安全組:2379,2380,43944
錯誤三: fatal: parameter inet_interfaces: no local interface found for ::1
vi /etc/postfix/main.cf
發現配置為:
inet_interfaces = localhost
inet_protocols = all
改成:
inet_interfaces = all
inet_protocols = all
重新啟動就ok了
service postfix start
錯誤四:dial tcp 127.0.0.1:4001: getsockopt: connection refused
解決辦法:ETCD_LISTEN_CLIENT_URLS需要配置http://127.0.0.1:2379,特別注意,此處是http而不是https
以上錯誤,三臺伺服器都需要注意,切記,切記,切記。。。。。。。。。。
繼續啟動 systemctl start etcd 如果不退出,可以 ctrl + c ,然後 ps -ef|grep etcd 就可以看到了 etcd 程序。
每臺伺服器上安裝 rsync :
yum install rsync -y
把我們剛才的配置都copy 到其他機器上:
rsync -avzP /opt/kubernetes [email protected]:/opt
rsync -avzP /opt/kubernetes [email protected]:/opt
傳過去以後我們只需要變更/opt/kubernetes/cfg/etcd 配置檔案即可
需要改的地方:
vim /opt/kubernetes/cfg/etcd
注意:ETCD_NAME 記得修改
#資料目錄
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://阿里雲內網ip:2380"
ETCD_LISTEN_CLIENT_URLS="https://阿里雲內網ip:2379"
其他:外網ip。
上述 vim /usr/lib/systemd/system/etcd.service 該配置,在每個node 上生成,然後啟動。
注意 上圖中 那個 ETCD_NAME= 這個配置,分別是01,02,03.
測試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
#正常如下:
member 16d32cea662d494c is unreachable: no available published client urls
member 766bc6053ca9ca80 is healthy: got healthy result from https://39.1001.0:2379
member 8a6a38d26f51127e is healthy: got healthy result from https://47.957.67:2379
cluster is healthy
/******************************************************** ETCD 部署結束 ***********************************************************/
部署Flannel網路:
我們先在master上面操作,即192.168.1.6
下載二進位制包:
此處我們用的比較新的0.10版本
wget https://github.com/coreos/flannel/releases/download/v0.10.0/flannel-v0.10.0-linux-amd64.tar.gz
將解壓後得到的可執行檔案放入我們之定義的路徑下面:
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
方案一, 將上述操作檔案copy 到 其他 節點:
阿里雲 輕量伺服器會有部分檔案, copy 不過去,建議去各個節點重複上述操作。
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
/********************************************** flannel ping 不通,懷疑阿里雲的坑 ************************************/
建立Node節點kubeconfig檔案:
在Master上執行即192.168.1.6:
mkdir /opt/ssl
cd /opt/ssl
執行指令碼獲取我們所需的檔案,總共有三個:
1、TLS Bootstrapping Token
2、kubelet kubeconfig
3、kube-proxy kubeconfig
阿里雲伺服器 此處配置:export KUBE_APISERVER="https://192.168.1.6:6443" , 設定為阿里雲內網IP(外網ip 也行......)。
[[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=/opt/kubernetes/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=/opt/kubernetes/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=./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
#結果如下
[[email protected] ssl]# sh -x kubeconfig.sh
++ head -c 16 /dev/urandom
++ od -An -t x
++ tr -d ' '
+ export BOOTSTRAP_TOKEN=0115145c5898d710c8a887f32994b882
+ BOOTSTRAP_TOKEN=0115145c5898d710c8a887f32994b882
+ cat
+ export KUBE_APISERVER=https://47.95.237.67:6443
+ KUBE_APISERVER=https://47.95.237.67:6443
+ kubectl config set-cluster kubernetes --certificate-authority=/opt/kubernetes/ssl/ca.pem --embed-certs=true --server=https://47.95.237.67:6443 --kubeconfig=bootstrap.kubeconfig
Cluster "kubernetes" set.
+ kubectl config set-credentials kubelet-bootstrap --token=0115145c5898d710c8a887f32994b882 --kubeconfig=bootstrap.kubeconfig
User "kubelet-bootstrap" set.
+ kubectl config set-context default --cluster=kubernetes --user=kubelet-bootstrap --kubeconfig=bootstrap.kubeconfig
Context "default" modified.
+ kubectl config use-context default --kubeconfig=bootstrap.kubeconfig
Switched to context "default".
+ kubectl config set-cluster kubernetes --certificate-authority=/opt/kubernetes/ssl/ca.pem --embed-certs=true --server=https://47.95.237.67:6443 --kubeconfig=kube-proxy.kubeconfig
Cluster "kubernetes" set.
+ kubectl config set-credentials kube-proxy --client-certificate=./kube-proxy.pem --client-key=./kube-proxy-key.pem --embed-certs=true --kubeconfig=kube-proxy.kubeconfig
error: error reading client-certificate data from ./kube-proxy.pem: open ./kube-proxy.pem: no such file or directory
+ kubectl config set-context default --cluster=kubernetes --user=kube-proxy --kubeconfig=kube-proxy.kubeconfig
Context "default" modified.
+ kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig
Switched to context "default".
[[email protected] ssl]#
此時我們可以獲取到三個配置檔案,我們將其拷貝到指定目錄:
cp -rf token.csv bootstrap.kubeconfig kube-proxy.kubeconfig /opt/kubernetes/cfg/
獲取K8S二進位制包:
wget https://dl.k8s.io/v1.10.7/kubernetes-server-linux-amd64.tar.gz
我們先在master(192.168.1.6)上操作
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:
阿里雲伺服器此處配置 MASTER_ADDRESS=${1:-"192.168.1.6"} 配置為內網ip.
[[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=0.0.0.0 \\
--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 改成 阿里雲 內網IP。
./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"}
檢視狀態:
systemctl status kube-scheduler
systemctl status kube-controller-manager
systemctl status kube-apiserver
要是都是非 綠色的 activity .則通過日誌檢視錯誤原因 :
journalctl -u kube-apiserver
journalctl -u kube-controller-manager
journalctl -u kube-controller-manager
執行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節點上面去
cd /opt/ssl/
rsync -avPz bootstrap.kubeconfig kube-proxy.kubeconfig [email protected]:/opt/kubernetes/cfg/
rsync -avPz bootstrap.kubeconfig kube-proxy.kubeconfig [email protected]:/opt/kubernetes/cfg/
#注意這兩個檔案的大小
total 16
-rw------- 1 root root 2174 Nov 8 10:57 bootstrap.kubeconfig
-rwxrwxrwx 1 root root 1554 Nov 8 10:57 kubeconfig.sh
-rw------- 1 root root 2092 Nov 8 10:57 kube-proxy.kubeconfig
-rw-r--r-- 1 root root 84 Nov 8 10:57 token.csv
在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
編輯安裝指令碼:
kubelet.sh 中 NODE_ADDRESS=${1:-"192.168.1.6"} , 配置為阿里雲外網ip, 注意 該處是 主節點的Ip
[[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
執行指令碼安裝:
阿里雲伺服器,此 ip 192.168.1.7, 為阿里雲內網ip.
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改成node03本機ip:
./kubelet.sh 192.168.1.8 10.10.10.2
./proxy.sh 192.168.1.8
到此叢集的安裝結束,我們測試叢集通不通
查詢叢集狀態:
[[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"}