轉:https://www.cnblogs.com/wdliu/archive/2018/06/06/9147346.htmlhtml
1、簡介 |
Kubernetes是Google在2014年6月開源的一個容器集羣管理系統,使用Go語言開發,Kubernetes也叫K8S。 K8S是Google內部一個叫Borg的容器集羣管理系統衍生出來的,Borg已經在Google大規模生產運行十年之久。 K8S主要用於自動化部署、擴展和管理容器應用,提供了資源調度、部署管理、服務發現、擴容縮容、監控等一整套功能。 2015年7月,Kubernetes v1.0正式發佈,截止到2018年6月,最新穩定版本是v1.10。 Kubernetes目標是讓部署容器化應用簡單高效。node
官方網站:https://kubernetes.io/linux
2、環境規劃 |
軟件 | 版本 |
操做系統 | CentOS Linux release 7.4.1708 (Core) |
Kubernetes git |
1.9 |
Docker github |
17.12-ce docker |
Etcd json |
3.2 |
Flanneld
|
0.9.1 |
角色centos |
IP | 組件 |
master | 10.1.210.33 |
kube-apiserver |
node1 | 10.1.210.32 |
kubelet |
node2 | 10.1.210.34 | kubelet |
3、docker部署 |
1.關閉seliux和firewalld
#關閉selinux vi /etc/selinux/config 將SELINUX=enforcing修改成SELINUX=disabled 重啓系統 reboot #關閉firewalld systemctl stop firewalld
2.若是主機上部署了比較老版本的docker(老版本也能夠),先進行卸載,可跳過此步驟
#查看是否已經安裝的Docker軟件包 yum list installed | grep docker #移除已經安裝的docker包 yum remove docker-ce #刪除殘留的鏡像文件信息 rm -rf /var/lib/docker #找到原有docker殘留文件,並刪除 find / -name "docker*" rm -rf 殘留文件
3.安裝docker-ce
方式一:
官方安裝方法:
# 安裝yum工具包 yum install -y yum-utils device-mapper-persistent-data lvm2 #添加docker-ce stable版本的倉庫 sudo yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo #安裝docker yum install docker-ce #設置開機自啓動 systemctl enable docker.service #啓動 systemctl start docker #查看是否安裝成功 docker info
方式二:
使用阿里雲鏡像,速度更快,安裝步驟:
# 安裝yum工具包 yum install -y yum-utils device-mapper-persistent-data lvm2 # 添加倉庫 yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # 更新yum源緩存 yum makecache fast # 查找Docker-CE的版本: yum list docker-ce.x86_64 --showduplicates | sort -r #安裝指定版本docker #yum -y install docker-ce-[VERSION] yum install docker-ce-17.12.0.ce-1.el7.centos
4.可能出現的錯誤:
啓動報錯:docker Failed to start docker.service: Unit not found,這個問題苦惱了半天,重裝了幾遍仍是同樣,網上不少方法都沒有解決,後來把之前裝的docker文件刪除乾淨,再重裝,啓動成功了。
緣由:因爲舊版docker的配置文件或者安裝文件沒有徹底刪除,這個問題苦惱了半天。
解決版本:卸載現有版本的docker,使用find / -name "docker*" 找到安裝的文件,所有刪除,再安裝新的docker
4、生成自籤TLS證書 |
1.安裝證書生成工具cfssl
這裏我選擇在master上進行證書的生成
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
2.生成模版json
# 創建存放生成的證書文件,方便管理 mkdir -p /opt/kubernetes/ssl cd /opt/kubernetes/ssl # 生成證書模版 cfssl print-defaults config > config.json ###生成的模版示列####
#cat config.json
{ "signing": { "default": { "expiry": "168h" }, "profiles": { "www": { "expiry": "8760h", "usages": [ "signing", "key encipherment", "server auth" ] }, "client": { "expiry": "8760h", "usages": [ "signing", "key encipherment", "client auth" ] } } } }
3.修改TLS模版證書爲咱們本身使用:
{ "signing": { "default": { "expiry": "87600h" }, "profiles": { "kubernetes": { "expiry": "87600h", "usages": [ "signing", "key encipherment", "server auth", "client auth" ] } } } }
4.生成ca模版以及修改模版
#生成csr模版 cfssl print-defaults csr > csr.json #結果 { "CN": "example.net", "hosts": [ "example.net", "www.example.net" ], "key": { "algo": "ecdsa", "size": 256 }, "names": [ { "C": "US", "L": "CA", "ST": "San Francisco" } ] } # 修改csr文件爲咱們使用 { "CN": "kubernetes", "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "L": "Beijing", "ST": "Beijing", "O": "k8s", "OU": "System" } ] }
5.生成證書、和key
cfssl gencert -initca csr.json | cfssljson -bare ca -
6.因爲須要生成的證書太多,而且容易出錯,一旦出錯整個下面的步驟都將進行不下去,因此編寫了腳本進行生成,注意,其中須要修改成本身集羣IP地址,下面是是腳本:
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", "10.1.210.32", "10.1.210.33", "10.1.210.34", "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
7.執行腳本完成後,會生成以下證書文件:
8.證書使用組件說明:
組件 | 使用的證書 |
etcd | ca.pem server.pem server-key.pem |
flannel | ca.pem server.pem server-key.pem |
kube-apiserver | ca.pem server.pem server-key.pem |
kubelet | ca.pem ca-key.pem |
kube-proxy | ca.pem kube-proxy.pem kube-proxy-key.pem |
kubectl | ca.pem admin.pem admin-key.pem |
5、部署etcd集羣 |
1.軟件包下載
下載地址:https://github.com/coreos/etcd/releases/download/v3.2.12/etcd-v3.2.12-linux-amd64.tar.gz
2.解壓,配置。注意,這裏爲了規範,將全部軟件部署在/opt/kubernetes目錄下
#建立部署目錄 mkdir /opt/kubernetes -p #建立其餘目錄,包括配置文件、可執行腳本、 mkdir /opt/kubernetes/{bin,conf,ssl} #解壓 tar zxvf etcd-v3.2.12-linux-amd64.tar.gz #拷貝可執行文件 cp etcd-v3.2.12-linux-amd64/etcd /opt/kubernetes/bin/ cp etcd-v3.2.12-linux-amd64/etcdctl /opt/kubernetes/bin/
3.建立配置文件與服務unit,使用配置文件時候請刪除註釋。
vi /opt/kubernetes/conf/etcd.conf #[Member] ETCD_NAME="etcd02" ETCD_DATA_DIR="/var/lib/etcd/default.etcd" ETCD_LISTEN_PEER_URLS="https://10.1.210.33:2380" #集羣通訊端口 ETCD_LISTEN_CLIENT_URLS="https://10.1.210.33:2379" #監聽的數據端口 #[Clustering] ETCD_INITIAL_ADVERTISE_PEER_URLS="https://10.1.210.33:2380" ETCD_ADVERTISE_CLIENT_URLS="https://10.1.210.33:2379" ETCD_INITIAL_CLUSTER="etcd01=https://10.1.210.32:2380,etcd02=https://10.1.210.33:2380,etcd03=https://10.1.210.34:2380" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster" #認證token ETCD_INITIAL_CLUSTER_STATE="new" #集羣創建狀態 vi /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/conf/etcd.conf 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
4.重載system服務,啓動etcd
systemctl daemon-reload systemctl start etcd
5.其餘兩個節點作相同的操做,注意配置文件稍微有些變化,這裏就不重複說明,因爲使用了證書,全部查看集羣操做都須要使用證書,以下:
/opt/kubernetes/bin/etcdctl \ > --ca-file=/opt/kubernetes/ssl/ca.pem \ > --cert-file=/opt/kubernetes/ssl/server.pem \ > --key-file=/opt/kubernetes/ssl/server-key.pem cluster-health
如圖下所示,咱們的etcd集羣部署完成
6、部署flannel網絡服務 |
1.分配集羣網絡存儲到etcd中,以供flannel使用
/opt/kubernetes/bin/etcdctl \ > --ca-file=/opt/kubernetes/ssl/ca.pem \ > --cert-file=/opt/kubernetes/ssl/server.pem \ > --key-file=/opt/kubernetes/ssl/server-key.pem set /coreos.com/network/config '{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}'
2.下載安裝包
下載地址:https://github.com/coreos/flannel/releases/download/v0.9.1/flannel-v0.9.1-linux-amd64.tar.gz
3.部署、配置flannel,這裏須要注意,flannel網絡是在node節點上進行的,全部咱們在10.1.210.3二、10.1.210.34上進行部署,以10.1.210.32爲列
#解壓 tar zxvf flannel-v0.9.1-linux-amd64.tar.gz #拷貝啓動腳本 cp flanneld /opt/kubernetes/bin/ cp mk-docker-opts.sh /opt/kubernetes/bin/ #配置文件配置: vi /opt/kubernetes/conf/flanneld.conf ETCD_ENDPOINTS="--etcd-endpoints=https://10.1.210.32:2379,https://10.1.210.33:2379,https://10.1.210.34:2379" FLANNEL_OPTIONS="--etcd-endpoints=${ETCD_ENDPOINTS} \ -etcd-cafile=/opt/kubernetes/ssl/ca.pem \ -etcd-certfile=/opt/kubernetes/ssl/server.pem \ -etcd-keyfile=/opt/kubernetes/ssl/server-key.pem" #啓動服務器配置 vi /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/conf/flanneld.conf 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
4.啓動
systemctl daemon-reload systemctl start flanneld
5.啓動成功後會生成flannel的網卡以下圖:
6.修改docker配置文件指定flannel網絡
vi /usr/lib/systemd/system/docker.service #修改成 EnvironmentFile=/run/flannel/subnet.env ExecStart=/usr/bin/dockerd $DOCKER_NETWORK_OPTIONS
7.重啓docker,檢查docker0網卡是否和flannel網卡網段一致
systemctl daemon-reload systemctl restart docker
8.在集羣的另外一個節點也按照一樣的方法安裝,檢查兩個節點flannel網絡是否通訊的辦法互ping flannel網關(網卡IP)