您好,登錄后才能下訂單哦!
1、Master組件
●kube-apiserver
Kubernetes API,集群的統一入口,各組件協調者,以RESTful API提供接口服務,所有對象資源的增刪改查和監聽操作都交給APIServer處理后再提交給Etcd存儲。
●kube-controller-manager
處理集群中常規后臺任務,一個 資源對應一個控制 器,而ControllerManager就是負責管理這些控制器的。
●kube-scheduler
根據調度算法為新創建的Pod選擇一個Node節點,可以任意部署,可以部署在同一個節點上,也可以部署在不同的節點上。
●etcd
分布式鍵值存儲系統。用于保存集群狀態數據,比如Pod、Service 等對象信息。
2、Node組件
●kubelet
kubelet是Master在Node節點上的Agent,管理本機運行容器的生命周期,比如創建容器、Pod掛載數據卷、下 載secret、獲取容器和節點狀態等工作。kubelet將 每個Pod轉換成一組容器。
●kube-proxy
在Node節點上實現Pod網絡代理,維護網絡規則和四層負載均衡工作。
●docker或rocket
容器引擎,運行容器。
工作原理:
1、準備包含應用程序的Deployment的yml文件,然后通過kubectl客戶端工具發送給ApiServer。
2、ApiServer接收到客戶端的請求并將資源內容存儲到數據庫(etcd)中。
3、Controller組件(包括scheduler、replication、endpoint)監控資源變化并作出反應。
4、ReplicaSet檢查數據庫變化,創建期望數量的pod實例。
5、Scheduler再次檢查數據庫變化,發現尚未被分配到具體執行節點(node)的Pod,然后根據一組相關規則將pod分配到可以運行它們的節點上,并更新數據庫,記錄pod分配情況。
6、Kubelete監控數據庫變化,管理后續pod的生命周期,發現被分配到它所在的節點上運行的那些pod。如果找到新pod,則會在該節點上運行這個新pod。
附:kuberproxy運行在集群各個主機上,管理網絡通信,如服務發現、負載均衡。當有數據發送到主機時,將其路由到正確的pod或容器。對于從主機上發出的數據,它可以基于請求地址發現遠程服務器,并將數據正確路由,在某些情況下會使用輪循調度算法(Round-robin)將請求發送到集群中的多個實例。
Kubernetes核心概念
1、Pod
●最小部署單元
●一組容器的集合
●一個Pod中的容器共享網絡命名空間
●Pod是短暫的
2、Controllers
●ReplicaSet :確保 預期的Pod副本數量
●Deployment:無狀態應用 部署
●StatefulSet :有狀態應用部署
●DaemonSet:確保所有Node運行同一個Pod
●Job:一次性任務
●Cronjob:定時任務
更高級層次對象,部署和管理Pod
3、Service
●防止Pod失聯
●定義一組Pod的訪問策略
●Label: 標簽,附加到某個資源上,用于關聯對象、查詢和篩選
●Namespaces :命 名空間,將對象邏輯上隔離
●Annotations :注釋
Kubernetes集群部署
1.官方提供的三種部署方式
●minikube
Minikube是一個工具,可以在本地快速運行一個單點的Kubernetes,僅用于嘗試Kubernetes或日常開發的用戶使用。
部署地址: https://kubernetes.io/docs/setup/minikube/
●kubeadm
Kubeadm也是一個工具,提供kubeadm init和kubeadm join,用于快速部署Kubernetes集群。
部署地址:https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm/
●二進制包
推薦,從官方下載發行版的二進制包,手動部署每個組件,組成Kubernetes集群。
下載地址:https://github.com/kubernetes/kubernetes/releases
3.自簽SSL證書
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Kubernetes二進制部署
k8s軟件包:
鏈接:https://pan.baidu.com/s/1oN2wkGZ_7parS8sMaaogGw?
提取碼:lbjx
k8s部署規劃:
master:192.168.35.128 ? ? ?kube-apiserver kube-controller-manager kube-scheduler etcd
node1:192.168.35.195 ? ? ? kubelet kube-proxy docker flannel etcd
node2:192.168.35.138 ? ? ? kubelet kube-proxy docker flannel etcd
master操作:
[root@localhost ~]# mkdir k8s
[root@localhost ~]# cd k8s/
[root@localhost k8s]# ls ? ?//從宿主機拖進來
etcd-cert.sh ?etcd.sh
[root@localhost k8s]# mkdir etcd-cert
[root@localhost k8s]# mv etcd-cert.sh etcd-cert
上面?etcd-cert.sh etcd.sh腳本
vim 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
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"
? ? ? ? ]
? ? ? }
? ? }
? }
}
EOF
cat > ca-csr.json <<EOF
{
? ? "CN": "etcd CA",
? ? "key": {
? ? ? ? "algo": "rsa",
? ? ? ? "size": 2048
? ? },
? ? "names": [
? ? ? ? {
? ? ? ? ? ? "C": "CN",
? ? ? ? ? ? "L": "Beijing",
? ? ? ? ? ? "ST": "Beijing"
? ? ? ? }
? ? ]
}
EOF
cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
#-----------------------
cat > server-csr.json <<EOF
{
? ? "CN": "etcd",
? ? "hosts": [
? ? "10.206.240.188",
? ? "10.206.240.189",
? ? "10.206.240.111"
? ? ],
? ? "key": {
? ? ? ? "algo": "rsa",
? ? ? ? "size": 2048
? ? },
? ? "names": [
? ? ? ? {
? ? ? ? ? ? "C": "CN",
? ? ? ? ? ? "L": "BeiJing",
? ? ? ? ? ? "ST": "BeiJing"
? ? ? ? }
? ? ]
}
EOF
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | cfssljson -bare server
下載官方包?
[root@localhost k8s]# 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
[root@localhost k8s]# bash cfssl.sh ?//下載cfssl官方包
? % Total ? ?% Received % Xferd ?Average Speed ? Time ? ?Time ? ? Time ?Current
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Dload ?Upload ? Total ? Spent ? ?Left ?Speed
100 ?9.8M ?100 ?9.8M ? ?0 ? ? 0 ?77052 ? ? ?0 ?0:02:14 ?0:02:14 --:--:-- 94447
? % Total ? ?% Received % Xferd ?Average Speed ? Time ? ?Time ? ? Time ?Current
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Dload ?Upload ? Total ? Spent ? ?Left ?Speed
100 2224k ?100 2224k ? ?0 ? ? 0 ?66701 ? ? ?0 ?0:00:34 ?0:00:34 --:--:-- 71949
? % Total ? ?% Received % Xferd ?Average Speed ? Time ? ?Time ? ? Time ?Current
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Dload ?Upload ? Total ? Spent ? ?Left ?Speed
100 6440k ?100 6440k ? ?0 ? ? 0 ?74368 ? ? ?0 ?0:01:28 ?0:01:28 --:--:-- 93942
[root@localhost k8s]# ls /usr/local/bin/
cfssl ?cfssl-certinfo ?cfssljson
//cfssl 生成證書工具、cfssljson通過傳入json文件生成證書、cfssl-certinfo查看證書信息
定義證書
[root@localhost k8s]# cd etcd-cert/
//定義ca證書
cat > ca-config.json <<EOF
{
? "signing": {
? ? "default": {
? ? ? "expiry": "87600h"
? ? },
? ? "profiles": {
? ? ? "www": {
? ? ? ? ?"expiry": "87600h",
? ? ? ? ?"usages": [
? ? ? ? ? ? "signing",
? ? ? ? ? ? "key encipherment",
? ? ? ? ? ? "server auth",
? ? ? ? ? ? "client auth" ? ??
? ? ? ? ] ?
? ? ? }?
? ? } ? ? ? ??
? }
}
EOF?
實現證書簽名
//實現證書簽名
cat > ca-csr.json <<EOF?
{ ??
? ? "CN": "etcd CA",
? ? "key": {
? ? ? ? "algo": "rsa",
? ? ? ? "size": 2048
? ? },
? ? "names": [
? ? ? ? {
? ? ? ? ? ? "C": "CN",
? ? ? ? ? ? "L": "Beijing",
? ? ? ? ? ? "ST": "Beijing"
? ? ? ? }
? ? ]
}
EOF
生產證書,生成ca-key.pem ?ca.pem
//生產證書,生成ca-key.pem ?ca.pem
cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
2020/01/15 18:15:15 [INFO] generating a new CA key and certificate from CSR
2020/01/15 18:15:15 [INFO] generate received request
2020/01/15 18:15:15 [INFO] received CSR
2020/01/15 18:15:15 [INFO] generating key: rsa-2048
2020/01/15 18:15:15 [INFO] encoded CSR
2020/01/15 18:15:15 [INFO] signed certificate with serial number 661808851940283859099066838380794010566731982441
指定etcd三個節點之間的通信驗證
//指定etcd三個節點之間的通信驗證
cat > server-csr.json <<EOF
{
? ? "CN": "etcd",
? ? "hosts": [
? ? "192.168.35.128",
? ? "192.168.35.195",
? ? "192.168.35.138"
? ? ],
? ? "key": {
? ? ? ? "algo": "rsa",
? ? ? ? "size": 2048
? ? },
? ? "names": [
? ? ? ? {
? ? ? ? ? ? "C": "CN",
? ? ? ? ? ? "L": "BeiJing",
? ? ? ? ? ? "ST": "BeiJing"
? ? ? ? }
? ? ]
}
EOF
生成ETCD證書 server-key.pem ? server.pem
//生成ETCD證書 server-key.pem ? server.pem
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | cfssljson -bare server
www server-csr.json | cfssljson -bare server
2020/01/15 18:24:09 [INFO] generate received request
2020/01/15 18:24:09 [INFO] received CSR
2020/01/15 18:24:09 [INFO] generating key: rsa-2048
2020/01/15 18:24:09 [INFO] encoded CSR
2020/01/15 18:24:09 [INFO] signed certificate with serial number 613252568370198035643630635602034323043189506463
2020/01/15 18:24:09 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable for
websites. For more information see the Baseline Requirements for the Issuance and Management
of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (https://cabforum.org);
specifically, section 10.2.3 ("Information Requirements").
復制軟件包到centos7中
[root@localhost etcd-cert]# cd /root/k8s/
[root@localhost k8s]# ls ? ? ? ? ? ? ?//直接拉取到目錄下
cfssl.sh ? etcd.sh ? ? ? ? ? ? ? ? ? ? ? ? ?flannel-v0.10.0-linux-amd64.tar.gz
etcd-cert ?etcd-v3.3.10-linux-amd64.tar.gz ?kubernetes-server-linux-amd64.tar.gz
解壓
[root@localhost k8s]# tar zxvf etcd-v3.3.10-linux-amd64.tar.gz
[root@localhost k8s]# ls etcd-v3.3.10-linux-amd64
Documentation ?etcd ?etcdctl ?README-etcdctl.md ?README.md ?READMEv2-etcdctl.md
[root@localhost k8s]# mkdir /opt/etcd/{cfg,bin,ssl} -p ? ?//配置文件,命令文件,證書
[root@localhost k8s]# mv etcd-v3.3.10-linux-amd64/etcd etcd-v3.3.10-linux-amd64/etcdctl /opt/etcd/bin/
證書拷貝
//證書拷貝
[root@localhost k8s]# cp etcd-cert/*.pem /opt/etcd/ssl/
//進入卡住狀態等待其他節點加入
[root@localhost k8s]# bash etcd.sh etcd01 192.168.35.128 etcd02=https://192.168.35.195:2380,etcd03=https://192.168.35.138:2380
使用另外一個會話打開,會發現etcd進程已經開啟
[root@localhost ~]# ps aux | grep etcd
root ? ? ? 4653 ?0.3 ?0.6 10523616 12140 ? ? ? ?Ssl ?19:49 ? 0:00 /opt/etcd/bin/etcd --name=etcd01 --data-dir=/var/lib/etcd/default.etcd --listen-peer-urls=https://192.168.35.128:2380 --listen-client-urls=https://192.168.35.128:2379,http://127.0.0.1:2379 --advertise-client-urls=https://192.168.35.128:2379 --initial-advertise-peer-urls=https://192.168.35.128:2380 --initial-cluster=etcd01=https://192.168.35.128:2380,etcd02=https://192.168.35.195:2380,etcd03=https://192.168.35.138:2380 --initial-cluster-token=etcd-cluster --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
root ? ? ? 4719 ?0.0 ?0.0 112676 ? 984 pts/2 ? ?R+ ? 19:50 ? 0:00 grep --color=auto etcd
關閉防火墻
systemctl stop firewalld.service?
setenforce 0
拷貝證書去其他節點
[root@localhost k8s]# scp -r /opt/etcd/ root@192.168.35.195:/opt/
[root@localhost k8s]# scp -r /opt/etcd/ root@192.168.35.138:/opt
啟動腳本拷貝其他節點
[root@localhost k8s]# scp /usr/lib/systemd/system/etcd.service root@192.168.35.195:/usr/lib/systemd/system/
[root@localhost k8s]# scp /usr/lib/systemd/system/etcd.service root@192.168.35.138:/usr/lib/systemd/system/
在node01節點修改
[root@localhost ~]# vim /opt/etcd/cfg/etcd
#[Member]
ETCD_NAME="etcd02"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.35.195:2380"
ETCD_LISTEN_CLIENT_URLS="https://192.168.35.195:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.35.195:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.35.195:2379"
ETCD_INITIAL_CLUSTER="etcd01=https://192.168.35.128:2380,etcd02=https://192.168.35.195:2380,etcd03=https://192.168.35.138:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
在node02節點修改
[root@localhost ~]# vim /opt/etcd/cfg/etcd
#[Member]
ETCD_NAME="etcd03"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.35.138:2380"
ETCD_LISTEN_CLIENT_URLS="https://192.168.35.138:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.35.138:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.35.138:2379"
ETCD_INITIAL_CLUSTER="etcd01=https://192.168.35.128:2380,etcd02=https://192.168.35.195:2380,etcd03=https://192.168.35.138:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
先在master上開啟服務
[root@localhost system]# cd /root/k8s/
[root@localhost k8s]# bash etcd.sh etcd01 192.168.35.128 etcd02=https://192.168.35.195:2380,etcd03=https://192.168.35.138:2380
在node1和node2上開啟服務
[root@localhost ~]# systemctl start etcd
[root@localhost ~]# systemctl status etcd
再去master查看,會發現同步成功
[root@localhost k8s]# bash etcd.sh etcd01 192.168.35.128 etcd02=https://192.168.35.195:2380,etcd03=https://192.168.35.138:2380
檢查群集狀態
[root@localhost k8s]# cd etcd-cert/
[root@localhost etcd-cert]# /opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.35.128:2379,https://192.168.35.195:2379,https://192.168.35.138:2379" cluster-health
member 12a96220ac829a49 is healthy: got healthy result from https://192.168.35.195:2379
member 76797989afd0ecba is healthy: got healthy result from https://192.168.35.128:2379
member ff469df2baaba1da is healthy: got healthy result from https://192.168.35.138:2379
cluster is healthy
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。