Kubernetes1.12版本三節點詳細搭建

一. Kubernetes簡介
1.基本對象
Pod
Pod是最小部署單元,一個Pod有一個或多個容器組成,Pod中容器共享存儲和網絡,在同一臺Docker主機上運行。
Service
Service一個應用服務抽象,定義了Pod邏輯集合和訪問這個Pod集合的策略。
Service代理Pod集合對外表現是爲一個訪問入口,分配一個集羣IP地址,來自這個IP的請求將負載均衡轉發後端Pod中的容器。
Service經過Lable Selector選擇一組Pod提供服務。
Volume
數據卷,共享Pod中容器使用的數據。
Namespace
命名空間將對象邏輯上分配到不一樣Namespace,能夠是不一樣的項目、用戶等區分管理,並設定控制策略,從而實現多租戶。
命名空間也稱爲虛擬集羣。
Lable
標籤用於區分對象(好比Pod、Service),鍵/值對存在;每一個對象能夠有多個標籤,經過標籤關聯對象。node

2.Master組件
kube-apiserver
Kubernetes API,集羣的統一入口,各組件協調者,以HTTP API提供接口服務,全部對象資源的增刪改查和監聽操做都交給APIServer處理後再提交給Etcd存儲。
kube-controller-manager
處理集羣中常規後臺任務,一個資源對應一個控制器,而ControllerManager就是負責管理這些控制器的。
kube-scheduler
根據調度算法爲新建立的Pod選擇一個Node節點。linux

3.Node組件
kubelet
kubelet是Master在Node節點上的Agent,管理本機運行容器的生命週期,好比建立容器、Pod掛載數據卷、
下載secret、獲取容器和節點狀態等工做。kubelet將每一個Pod轉換成一組容器。
kube-proxy
在Node節點上實現Pod網絡代理,維護網絡規則和四層負載均衡工做。
docker
運行容器。nginx

4.第三方服務
etcd
分佈式鍵值存儲系統。用於保持集羣狀態,好比Pod、Service等對象信息。git

二. Kubernetes搭建環境
Kubernetes1.12版本三節點詳細搭建
Centos7系統
Mster節點 190.168.3.230
Node1節點 190.168.3.231
Node2節點 190.168.3.232
Etcd三節點高可用
搭建之間確保三臺主機時間同步,關閉selinux和firewalld
三.搭建Kubernetes
1.搭建etcd
自籤Etcd SSL證書,並在三節點搭建etcd集羣github

a.下載生成證書工具,並檢查安裝是否正常
vim cfssl.sh
curl -L https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 -o /usr/local/bin/cfssl
curl -L https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 -o /usr/local/bin/cfssljson
curl -L https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 -o /usr/local/bin/cfssl-certinfo
chmod +x /usr/local/bin/cfssl /usr/local/bin/cfssljson /usr/local/bin/cfssl-certinfo
Kubernetes1.12版本三節點詳細搭建算法

b.爲etcd生成證書
Kubernetes1.12版本三節點詳細搭建
[root@master etcd-cert]# vim etcd-cert.sh
cat > ca-config.json <<EOF
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"www": {
"expiry": "87600h",
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
EOFdocker

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

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

#-----------------------
cat > server-csr.json <<EOF
{
"CN": "etcd",
"hosts": [
"190.168.3.230",
"190.168.3.231",
"190.168.3.232"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing"
}
]
}
EOFvim

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

注:將server-csr.json 文件裏的hosts地址改成etcd三臺集羣的地址

執行ca證書腳本
Kubernetes1.12版本三節點詳細搭建

c.安裝master節點的etcd
下載etcd包
Kubernetes1.12版本三節點詳細搭建

建立etcd目錄,並將服務腳本拷貝到etcd的工做目錄下/opt/etcd/bin/
Kubernetes1.12版本三節點詳細搭建

建立etcd.sh 生成配置文件腳本
[root@master k8s]# vim etcd.sh
[root@master k8s]# chmod +x etcd.sh
#!/bin/bash

example: ./etcd.sh etcd01 192.168.1.10 etcd02=https://192.168.1.11:2380,etcd03=https://192.168.1.12:2380

ETCD_NAME=$1
ETCD_IP=$2
ETCD_CLUSTER=$3

WORK_DIR=/opt/etcd

cat <<EOF >$WORK_DIR/cfg/etcd
#[Member]
ETCD_NAME="${ETCD_NAME}"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://${ETCD_IP}:2380"
ETCD_LISTEN_CLIENT_URLS="https://${ETCD_IP}:2379"

#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://${ETCD_IP}:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://${ETCD_IP}:2379"
ETCD_INITIAL_CLUSTER="etcd01=https://${ETCD_IP}:2380,${ETCD_CLUSTER}"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
EOF

cat <<EOF >/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=${WORK_DIR}/cfg/etcd
ExecStart=${WORK_DIR}/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=${WORK_DIR}/ssl/server.pem \
--key-file=${WORK_DIR}/ssl/server-key.pem \
--peer-cert-file=${WORK_DIR}/ssl/server.pem \
--peer-key-file=${WORK_DIR}/ssl/server-key.pem \
--trusted-ca-file=${WORK_DIR}/ssl/ca.pem \
--peer-trusted-ca-file=${WORK_DIR}/ssl/ca.pem
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable etcd
systemctl restart etcd

[root@master k8s]# ./etcd.sh etcd01 190.168.3.230 etcd02=https://190.168.3.231:2380,etcd03=https://190.168.3.232:2380
Kubernetes1.12版本三節點詳細搭建

查看生成的etcd配置文件
Kubernetes1.12版本三節點詳細搭建

將以前生成的證書拷貝到/opt/etcd/ssl下
Kubernetes1.12版本三節點詳細搭建

拷貝完後啓動服務,第一次主節點啓動服務比較慢
[root@master k8s]# systemctl start etcd

查看mster節點的etcd已經啓動
Kubernetes1.12版本三節點詳細搭建
[root@master k8s]# systemctl enable etcd 開機自啓

d.安裝node節點的etcd
將主節點配置好的文件直接拷貝到其餘兩個節點上
Kubernetes1.12版本三節點詳細搭建

node1節點執行剛纔的etcd.sh腳本
[root@node1 ~]# ./etcd.sh etcd02 190.168.3.231 etcd01=https://190.168.3.230:2380,etcd03=https://190.168.3.232:2380
檢查node1下etcd的配置文件是否正確
[root@node1 ~]# vim /opt/etcd/cfg/etcd
Kubernetes1.12版本三節點詳細搭建
[root@node1 ~]# systemctl start etcd
[root@node1 ~]# systemctl enable etcd
Kubernetes1.12版本三節點詳細搭建

node2節點和node1節點同樣執行腳本
[root@node2 ~]# ./etcd.sh etcd03 190.168.3.232 etcd01=https://190.168.3.230:2380,etcd02=https://190.168.3.231:2380
Kubernetes1.12版本三節點詳細搭建
[root@node2 ~]# systemctl start etcd
[root@node2 ~]# systemctl enable etcd
Kubernetes1.12版本三節點詳細搭建

e.etcd三節點集羣安裝完成
查看集羣狀態
Kubernetes1.12版本三節點詳細搭建

[root@master ssl]# /opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://190.168.3.230:2379,https://190.168.3.231:2379,https://190.168.3.232:2379" cluster-health

member 25ff557ca2a90385 is healthy: got healthy result from https://190.168.3.230:2379
member e94bc10222c25d21 is healthy: got healthy result from https://190.168.3.231:2379
member f196ddd8571e9f64 is healthy: got healthy result from https://190.168.3.232:2379
cluster is healthy

2.node節點搭建docker
兩個節點作一樣的操做
在官網上下載最新版本
https://docs.docker.com/install/linux/docker-ce/centos/

安裝docker依賴
yum install -y yum-utils \
device-mapper-persistent-data \
lvm2

添加docker源倉庫
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo

安裝docker
yum install docker-ce -y

systemctl start docker
systemctl enable docker
Kubernetes1.12版本三節點詳細搭建

設置使用國內鏡像
https://www.daocloud.io/mirror

curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io
Kubernetes1.12版本三節點詳細搭建

3.部署flannel網絡
a.寫入子網到etcd中供flannel使用
進入到etcd-cert證書目錄
Kubernetes1.12版本三節點詳細搭建

[root@master etcd-cert]# /opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://190.168.3.230:2379,https://190.168.3.231:2379,https://190.168.3.232:2379" set /coreos.com/network/config '{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}'
{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}

b.下載部署flannel包並部署
建立kubernetes下的flannel目錄
[root@node1 ~]# mkdir -p /opt/kubernetes/{bin,cfg,ssl}
下載flannel包和編寫部署腳本
Kubernetes1.12版本三節點詳細搭建
將解壓後的flanneld mk-docker-opts.sh拷貝到/opt/kubernetes/bin下
Kubernetes1.12版本三節點詳細搭建
Kubernetes1.12版本三節點詳細搭建

查看flannel.sh腳本
[root@node1 ~]# vim flannel.sh

#!/bin/bash

ETCD_ENDPOINTS=${1:-"http://127.0.0.1:2379"}

cat <<EOF >/opt/kubernetes/cfg/flanneld

FLANNEL_OPTIONS="--etcd-endpoints=${ETCD_ENDPOINTS} \
-etcd-cafile=/opt/etcd/ssl/ca.pem \
-etcd-certfile=/opt/etcd/ssl/server.pem \
-etcd-keyfile=/opt/etcd/ssl/server-key.pem"

EOF

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
cat <<EOF >/usr/lib/systemd/system/dockerd.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 dockerd

執行flannel.sh腳本
[root@node1 ~]# ./flannel.sh

執行完修改配置文件爲如下參數
[root@node1 ~]# vim /opt/kubernetes/cfg/flanneld
Kubernetes1.12版本三節點詳細搭建

查看腳本生成的flanneld.service文件
Kubernetes1.12版本三節點詳細搭建

[root@node1 ~]# systemctl start flanneld
[root@node1 ~]# systemctl enable flanneld

查看生成的子網
Kubernetes1.12版本三節點詳細搭建

c.配置docker啓動時配置使用flannel子網
將如下的配置設置到docker開機啓動項中
[root@node1 ~]# cat flannel.sh
Kubernetes1.12版本三節點詳細搭建

EnvironmentFile=/run/flannel/subnet.env
ExecStart=/usr/bin/dockerd $DOCKER_NETWORK_OPTIONS

[root@node1 ~]# vim /usr/lib/systemd/system/docker.service
Kubernetes1.12版本三節點詳細搭建

查看設置結果
設置完後從新啓動,docker進入到flannel的子網了
Kubernetes1.12版本三節點詳細搭建

d.配置node2上的flannel網絡
將node1 下的/opt/kubernetes目錄直接拷貝到node2
Kubernetes1.12版本三節點詳細搭建

拷貝服務文件
Kubernetes1.12版本三節點詳細搭建

證書用的是以前etcd下ssl目錄裏的證書
[root@node2 ~]# systemctl start flanneld
[root@node2 ~]# systemctl enable flanneld

Kubernetes1.12版本三節點詳細搭建

[root@node2 ~]# vim /usr/lib/systemd/system/docker.service
Kubernetes1.12版本三節點詳細搭建

設置完後從新啓動,node2節點的docker進入到flannel的子網了
Kubernetes1.12版本三節點詳細搭建

e.測試兩個節點配置的網絡是否通
node1
Kubernetes1.12版本三節點詳細搭建
node2
Kubernetes1.12版本三節點詳細搭建

Kubernetes1.12版本三節點詳細搭建

兩個節點用容器測試
Kubernetes1.12版本三節點詳細搭建
Kubernetes1.12版本三節點詳細搭建
兩個不一樣節點的容器能夠通

4.部署master組件
自籤ssl證書
kube-apiserver
kube-controller-manager
kube-scheduler

a.生成自籤ssl證書
Kubernetes1.12版本三節點詳細搭建
Kubernetes1.12版本三節點詳細搭建
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": [
"10.0.0.1",
"127.0.0.1",
"190.168.3.250",
"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

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

"CN": "kubernetes",
"hosts": [
  "10.0.0.1",
  "127.0.0.1",
  "190.168.3.230",
  "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

執行建立腳本
[root@master k8s-cert]# chmod +x k8s-cert.sh
[root@master k8s-cert]# ./k8s-cert.sh
Kubernetes1.12版本三節點詳細搭建

b.kube-apiserver部署
[root@master k8s]# mkdir soft
[root@master k8s]# cd soft/
Kubernetes1.12版本三節點詳細搭建
Kubernetes1.12版本三節點詳細搭建

將須要的二進制命令拷貝到
[root@master bin]# cp kube-apiserver kubectl kube-controller-manager kube-scheduler /opt/kubernetes/bin/
Kubernetes1.12版本三節點詳細搭建

查看aipserver部署腳本
[root@master soft]# cat apiserver.sh
#!/bin/bash

MASTER_ADDRESS=$1
ETCD_SERVERS=$2

cat <<EOF >/opt/kubernetes/cfg/kube-apiserver

KUBE_APISERVER_OPTS="--logtostderr=true \
--v=4 \
--etcd-servers=${ETCD_SERVERS} \
--bind-address=${MASTER_ADDRESS} \
--secure-port=6443 \
--advertise-address=${MASTER_ADDRESS} \
--allow-privileged=true \
--service-cluster-ip-range=10.0.0.0/24 \
--enable-admission-plugins=NamespaceLifecycle,LimitRanger,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/etcd/ssl/ca.pem \
--etcd-certfile=/opt/etcd/ssl/server.pem \
--etcd-keyfile=/opt/etcd/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

將以前生成的master節點部署生成的證書拷貝到/opt/kubernetes/ssl/下
Kubernetes1.12版本三節點詳細搭建

設置aipserver啓動須要的token.csv文件
Kubernetes1.12版本三節點詳細搭建

[root@master soft]# vim /opt/kubernetes/cfg/token.csv
0767cd1f442ba2834a103fdc7cd3c7ce,kubelet-bootstrap,10001,"system:kubelet-bootstrap"
Kubernetes1.12版本三節點詳細搭建

生成aipserver服務文件
[root@master soft]# chmod +x apiserver.sh
[root@master soft]# ./apiserver.sh 190.168.3.230 https://190.168.3.230:2379,https://190.168.3.231:2379,https://190.168.3.232:2379

啓動apiserver服務
Kubernetes1.12版本三節點詳細搭建

c.部署啓動kube-controller-manager
查看controller-manager配置腳本
[root@master soft]# cat controller-manager.sh
#!/bin/bash

MASTER_ADDRESS=$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.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 \
--experimental-cluster-signing-duration=87600h0m0s"

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

啓動服務
Kubernetes1.12版本三節點詳細搭建
Kubernetes1.12版本三節點詳細搭建

d.kube-scheduler調度服務啓動
查看配置服務腳本
[root@master soft]# cat scheduler.sh
#!/bin/bash

MASTER_ADDRESS=$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

啓動服務
Kubernetes1.12版本三節點詳細搭建

e.查看mster部署結果是否正常
[root@master soft]# /opt/kubernetes/bin/kubectl get cs
Kubernetes1.12版本三節點詳細搭建

5.部署node組件

a.建立kubeconfig文件
將master kubernetes下的二進制命令kubelet kube-proxy傳給node1
Kubernetes1.12版本三節點詳細搭建

配置kubelet kube-proxy的環境變量
[root@master kubeconfig]# vim /etc/profile
Kubernetes1.12版本三節點詳細搭建
[root@master kubeconfig]# source /etc/profile

將以前生成的token寫入到kubeconfig.sh文件裏
Kubernetes1.12版本三節點詳細搭建

修改配置文件
[root@master kubeconfig]# vim kubeconfig.sh
Kubernetes1.12版本三節點詳細搭建

建立 TLS Bootstrapping Token

#BOOTSTRAP_TOKEN=$(head -c 16 /dev/urandom | od -An -t x | tr -d ' ')
#BOOTSTRAP_TOKEN=0fb61c46f8991b718eb38d27b605b008

#cat > token.csv <<EOF
#${BOOTSTRAP_TOKEN},kubelet-bootstrap,10001,"system:kubelet-bootstrap"
#EOF

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

APISERVER=$1
SSL_DIR=$2

建立kubelet bootstrapping kubeconfig

export KUBE_APISERVER="https://$APISERVER:6443"

BOOTSTRAP_TOKEN=0767cd1f442ba2834a103fdc7cd3c7ce

設置集羣參數

kubectl config set-cluster kubernetes \
--certificate-authority=$SSL_DIR/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=$SSL_DIR/ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=kube-proxy.kubeconfig

kubectl config set-credentials kube-proxy \
--client-certificate=$SSL_DIR/kube-proxy.pem \
--client-key=$SSL_DIR/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

生成配置文件
Kubernetes1.12版本三節點詳細搭建

Kubernetes1.12版本三節點詳細搭建

b.將kubelet-bootstrap用戶綁定到系統集羣角色
[root@master k8s-cert]# kubectl create clusterrolebinding kubelet-bootstrap --clusterrole=system:node-bootstrapper --user=kubelet-bootstrap

c.node1部署kubelet
Kubernetes1.12版本三節點詳細搭建

node節點下載node.zip
Kubernetes1.12版本三節點詳細搭建

node1 節點kubelet配置
查看配置腳本
[root@node1 ~]# cat kubelet.sh
#!/bin/bash

NODE_ADDRESS=$1
DNS_SERVER_IP=${2:-"10.0.0.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 \
--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"

EOF

cat <<EOF >/opt/kubernetes/cfg/kubelet.config

kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
address: ${NODE_ADDRESS}
port: 10250
cgroupDriver: cgroupfs
clusterDNS:

  • ${DNS_SERVER_IP}
    clusterDomain: cluster.local.
    failSwapOn: false

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

執行腳本
[root@node1 ~]# ./kubelet.sh 190.168.3.231 10.0.0.2
後面是加的是dns,後面會部署
Kubernetes1.12版本三節點詳細搭建

注:這一步記得前面要將kubelet-bootstrap用戶綁定到系統集羣角色

d.node1部署kube-proxy組件
Kubernetes1.12版本三節點詳細搭建
[root@node1 ~]# cat proxy.sh
#!/bin/bash

NODE_ADDRESS=$1

cat <<EOF >/opt/kubernetes/cfg/kube-proxy

KUBE_PROXY_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=${NODE_ADDRESS} \
--cluster-cidr=10.0.0.0/24 \
--proxy-mode=ipvs \
--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

執行腳本並檢查執行結果
Kubernetes1.12版本三節點詳細搭建

e.在主節點上檢查是否有節點的簽發證書請求
Kubernetes1.12版本三節點詳細搭建

贊成請求,加入集羣幾點
[root@master k8s-cert]# kubectl certificate approve node-csr-WLWkm04vtzQ8NPZ3giDV1PBNFxy-q2eQ1KCYdYLFHRE
Kubernetes1.12版本三節點詳細搭建

f.配置node2節點的kubelet和kube-proxy
因爲node1 下的 /opt/kubernetes目錄和node2一直,能夠直接拷過去
Kubernetes1.12版本三節點詳細搭建

切換到node2節點
Kubernetes1.12版本三節點詳細搭建
修改地址
[root@node2 cfg]# vim kubelet
Kubernetes1.12版本三節點詳細搭建
[root@node2 cfg]# vim kube-proxy
Kubernetes1.12版本三節點詳細搭建

將node1的服務文件拷貝到node2上
Kubernetes1.12版本三節點詳細搭建

在node2上刪除生成的證書
[root@node2 cfg]# rm -f /opt/kubernetes/ssl/*

啓動服務kubelet和kube-proxy服務
Kubernetes1.12版本三節點詳細搭建
啓動成功
Kubernetes1.12版本三節點詳細搭建

g.在master放行第二個節點
Kubernetes1.12版本三節點詳細搭建

四.運行實例測試
運行3個副本的nginx
[root@master ~]# kubectl run nginx --image=nginx --replicas=3
Kubernetes1.12版本三節點詳細搭建

Kubernetes1.12版本三節點詳細搭建

啓動服務,暴露80端口
[root@master ~]# kubectl expose deployment nginx --port=80 --target-port=80 --type=NodePort
Kubernetes1.12版本三節點詳細搭建

查看pod落在後端的地址
Kubernetes1.12版本三節點詳細搭建

在每一個節點均可以用集羣ip訪問
Kubernetes1.12版本三節點詳細搭建
Kubernetes1.12版本三節點詳細搭建

經過外網訪問兩個節點的34594端口也能夠
Kubernetes1.12版本三節點詳細搭建
Kubernetes1.12版本三節點詳細搭建

解決沒有權限查看日誌
查看其中的一個日誌報錯
Kubernetes1.12版本三節點詳細搭建
這個是沒有權限
建立一個匿名用戶訪問日誌
在node1和node2節點的kubelet.config文件裏啓用匿名用戶
[root@node1 ~]# vim /opt/kubernetes/cfg/kubelet.config
Kubernetes1.12版本三節點詳細搭建
[root@node1 ~]# systemctl restart kubelet

node2和node1修改地方同樣
Kubernetes1.12版本三節點詳細搭建

再次訪問仍是報錯,要付於匿名用戶管理員權限
[root@master ~]# kubectl create clusterrolebinding cluster-system-anonymous --clusterrole=cluster-admin --user=system:anonymous
再次測試正常
Kubernetes1.12版本三節點詳細搭建

-

相關文章
相關標籤/搜索