最近,有部分用戶飄了……node
以爲Rainbond提供的既簡潔、又易用、並且生產就緒的Kubernets體驗不過癮……linux
想要挑戰一下Kubernetes全手動部署……nginx
並在凌晨一點撥通了客服小哥的電話……git
所以本着不重複造輪子而且關愛客服小哥身心健康的主張,咱們搬來了Kairen的精彩教程——github
Kubernetes官方提供了多種安裝方式Picking the right solution,本文將以全手動安裝方式
來部署Kubernetes v1.8.x版本,學習和了解Kubernetes的構建流程。web
版本明細:docker
系統:ubuntu 16.x
或 centos 7.x
節點:json
master爲主要控制節點和部署節點,node爲應用運行節點 全部操做均爲
root
bootstrap
安裝前需確認如下事項:ubuntu
$ systemctl stop firewalld && systemctl disable firewalld
$ setenforce 0
$ vim /etc/selinux/config
SELINUX=disabled
複製代碼
/etc/host
解析到全部主機...
172.16.35.10 node1
172.16.35.11 node2
172.16.35.12 master1
複製代碼
全部節點都須要安裝docker
$ curl -fsSL "https://get.docker.com/" | sh
複製代碼
注意:centos安裝docker完成後須要執行:
$ systemctl enable docker && systemctl start docker
複製代碼
編輯/lib/systemd/system/docker.service
,在ExecStart=..
加入:
ExecStartPost=/sbin/iptables -A FORWARD -s 0.0.0.0/0 -j ACCEPT
複製代碼
完成後重啓docker服務:
$ systemctl daemon-reload && systemctl restart docker
複製代碼
全部節點都須要設定/etc/sysctl.d/k8s.conf
系統參數
$ cat <<EOF > /etc/sysctl.d/k8s.conf
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
$ sysctl -p /etc/sysctl.d/k8s.conf
複製代碼
在master1安裝CFSSL
工具,用來創建TLS certificates
$ export CFSSL_URL="https://pkg.cfssl.org/R1.2"
$ wget "${CFSSL_URL}/cfssl_linux-amd64" -O /usr/local/bin/cfssl
$ wget "${CFSSL_URL}/cfssljson_linux-amd64" -O /usr/local/bin/cfssljson
$ chmod +x /usr/local/bin/cfssl /usr/local/bin/cfssljson
複製代碼
安裝Kubernetes以前,咱們須要完成一些必要的系統配置,高可用共享配置和服務發現存儲Etcd即是其中的重要一環,節點會從Etcd中獲取所需數據。
這裏須要生成client和server各組件certificate,代替kubernetes admin user生成client證書。
首先在master1
創建/etc/etcd/ssl
目錄,然後進入目錄進行如下操做:
$ mkdir -p /etc/etcd/ssl && cd /etc/etcd/ssl
$ export PKI_URL="https://kairen.github.io/files/manual-v1.8/pki"
複製代碼
下載ca-config.json
和etcd-ca-csr.json
:
$ wget "${PKI_URL}/ca-config.json" "${PKI_URL}/etcd-ca-csr.json"
$ cfssl gencert -initca etcd-ca-csr.json | cfssljson -bare etcd-ca
$ ls etcd-ca*.pem
etcd-ca-key.pem etcd-ca.pem
複製代碼
下載etcd-csr.json
並生成Etcd certificate證書:
$ wget "${PKI_URL}/etcd-csr.json"
$ cfssl gencert \
-ca=etcd-ca.pem \
-ca-key=etcd-ca-key.pem \
-config=ca-config.json \
-profile=kubernetes \
etcd-csr.json | cfssljson -bare etcd
$ ls etcd*.pem
etcd-ca-key.pem etcd-ca.pem etcd-key.pem etcd.pem
複製代碼
若是節點IP不一樣,須要修改
etcd-csr.json
的hosts
完成後刪除沒必要要的文件:
$ rm -rf *.json
複製代碼
並確認/etc/etcd/ssl
包含:
$ ls /etc/etcd/ssl
etcd-ca.csr etcd-ca-key.pem etcd-ca.pem etcd.csr etcd-key.pem etcd.pem
複製代碼
首先在master1
節點下載Etcd,解壓到/opt
安裝:
$ export ETCD_URL="https://github.com/coreos/etcd/releases/download"
$ cd && wget -qO- --show-progress "${ETCD_URL}/v3.2.9/etcd-v3.2.9-linux-amd64.tar.gz" | tar -zx
$ mv etcd-v3.2.9-linux-amd64/etcd* /usr/local/bin/ && rm -rf etcd-v3.2.9-linux-amd64
複製代碼
完成後新建Etcd Group和User,並設定Etcd目錄:
$ groupadd etcd && useradd -c "Etcd user" -g etcd -s /sbin/nologin -r etcd
複製代碼
下載etcd相關配置,咱們未來管理Etcd:
$ export ETCD_CONF_URL="https://kairen.github.io/files/manual-v1.8/master"
$ wget "${ETCD_CONF_URL}/etcd.conf" -O /etc/etcd/etcd.conf
$ wget "${ETCD_CONF_URL}/etcd.service" -O /lib/systemd/system/etcd.service
複製代碼
若是沒用本文準備部分的IP,請用本身的IP代替
172.16.35.12
創建var 存放數據,而後啓動Etcd服務:
$ mkdir -p /var/lib/etcd && chown etcd:etcd -R /var/lib/etcd /etc/etcd
$ systemctl enable etcd.service && systemctl start etcd.service
複製代碼
經過如下命令驗證:
$ export CA="/etc/etcd/ssl"
$ ETCDCTL_API=3 etcdctl \
--cacert=${CA}/etcd-ca.pem \
--cert=${CA}/etcd.pem \
--key=${CA}/etcd-key.pem \
--endpoints="https://172.16.35.12:2379" \
endpoint health
# output
https://172.16.35.12:2379 is healthy: successfully committed proposal: took = 641.36µs
複製代碼
Master是Kubernetes的大總管,經過apiserver
、Controller manager
以及Scheduler
管理全部節點。
本部分將下載Kubernetes並安裝到master1節點上,而後生成相關TLS certificates和CA,供集羣組件使用。
# Download Kubernetes
$ export KUBE_URL="https://storage.googleapis.com/kubernetes-release/release/v1.8.6/bin/linux/amd64"
$ wget "${KUBE_URL}/kubelet" -O /usr/local/bin/kubelet
$ wget "${KUBE_URL}/kubectl" -O /usr/local/bin/kubectl
$ chmod +x /usr/local/bin/kubelet /usr/local/bin/kubectl
# Download CNI
$ mkdir -p /opt/cni/bin && cd /opt/cni/bin
$ export CNI_URL="https://github.com/containernetworking/plugins/releases/download"
$ wget -qO- --show-progress "${CNI_URL}/v0.6.0/cni-plugins-amd64-v0.6.0.tgz" | tar -zx
複製代碼
與Etcd部分原理同樣,操做也截然不同,首先在master1
創建pki
目錄,並進入目錄執行:
$ mkdir -p /etc/kubernetes/pki && cd /etc/kubernetes/pki
$ export PKI_URL="https://kairen.github.io/files/manual-v1.8/pki"
$ export KUBE_APISERVER="https://172.16.35.12:6443"
複製代碼
下載ca-config.json
和etcd-ca-csr.json
:
$ wget "${PKI_URL}/ca-config.json" "${PKI_URL}/ca-csr.json"
$ cfssl gencert -initca ca-csr.json | cfssljson -bare ca
$ ls ca*.pem
ca-key.pem ca.pem
複製代碼
下載apiserver-csr.json
,生成kube-apiserver certificate
證書:
$ wget "${PKI_URL}/apiserver-csr.json"
$ cfssl gencert \
-ca=ca.pem \
-ca-key=ca-key.pem \
-config=ca-config.json \
-hostname=10.96.0.1,172.16.35.12,127.0.0.1,kubernetes.default \
-profile=kubernetes \
apiserver-csr.json | cfssljson -bare apiserver
$ ls apiserver*.pem
apiserver-key.pem apiserver.pem
複製代碼
若是節點IP不一樣,須要修改
-hostname
下載front-proxy-ca-csr.json
,生成Front proxy CA,Front proxy主要用在API aggregator上:
$ wget "${PKI_URL}/front-proxy-ca-csr.json"
$ cfssl gencert \
-initca front-proxy-ca-csr.json | cfssljson -bare front-proxy-ca
$ ls front-proxy-ca*.pem
front-proxy-ca-key.pem front-proxy-ca.pem
複製代碼
下載front-proxy-client-csr.json
,生成front-proxy-client證書:
$ wget "${PKI_URL}/front-proxy-client-csr.json"
$ cfssl gencert \
-ca=front-proxy-ca.pem \
-ca-key=front-proxy-ca-key.pem \
-config=ca-config.json \
-profile=kubernetes \
front-proxy-client-csr.json | cfssljson -bare front-proxy-client
$ ls front-proxy-client*.pem
front-proxy-client-key.pem front-proxy-client.pem
複製代碼
手工方式生成CA很是麻煩,只適合少許機器,每次簽證時都須要綁定Node IP,隨着機器增長會帶來不少的不便,所以這裏使用TLS Bootstrapping的方式來進行受權,由apiserver自動爲符合條件的Node發送證書受權加入集羣。
作法是在kubelet啓動時,向kuber-apiserver傳送TLS Bootstrapping請求,而kube-apiserver驗證kubelet請求的token是否與設定的同樣,若是同樣則自動生成Kuberlet證書和密鑰。具體做法能夠參考TLS bootstrapping。
首先生成BOOTSTRAP_TOKEN
,並創建bootstrap.conf
的kubeconfig:
$ export BOOTSTRAP_TOKEN=$(head -c 16 /dev/urandom | od -An -t x | tr -d ' ')
$ cat <<EOF > /etc/kubernetes/token.csv
${BOOTSTRAP_TOKEN},kubelet-bootstrap,10001,"system:kubelet-bootstrap"
EOF
# bootstrap set-cluster
$ kubectl config set-cluster kubernetes \
--certificate-authority=ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=../bootstrap.conf
# bootstrap set-credentials
$ kubectl config set-credentials kubelet-bootstrap \
--token=${BOOTSTRAP_TOKEN} \
--kubeconfig=../bootstrap.conf
# bootstrap set-context
$ kubectl config set-context default \
--cluster=kubernetes \
--user=kubelet-bootstrap \
--kubeconfig=../bootstrap.conf
# bootstrap set default context
$ kubectl config use-context default --kubeconfig=../bootstrap.conf
複製代碼
若是想用CA的方式來認證,能夠參考Kubelet certificate
下載admin-csr.json
,並生成admin certificate
證書:
$ wget "${PKI_URL}/admin-csr.json"
$ cfssl gencert \
-ca=ca.pem \
-ca-key=ca-key.pem \
-config=ca-config.json \
-profile=kubernetes \
admin-csr.json | cfssljson -bare admin
$ ls admin*.pem
admin-key.pem admin.pem
複製代碼
而後執行一下命令生成名爲admin.conf
的kubeconfig:
# admin set-cluster
$ kubectl config set-cluster kubernetes \
--certificate-authority=ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=../admin.conf
# admin set-credentials
$ kubectl config set-credentials kubernetes-admin \
--client-certificate=admin.pem \
--client-key=admin-key.pem \
--embed-certs=true \
--kubeconfig=../admin.conf
# admin set-context
$ kubectl config set-context kubernetes-admin@kubernetes \
--cluster=kubernetes \
--user=kubernetes-admin \
--kubeconfig=../admin.conf
# admin set default context
$ kubectl config use-context kubernetes-admin@kubernetes \
--kubeconfig=../admin.conf
複製代碼
下載manager-csr.json
,並生成kube-controller-manager certificate
證書:
$ wget "${PKI_URL}/manager-csr.json"
$ cfssl gencert \
-ca=ca.pem \
-ca-key=ca-key.pem \
-config=ca-config.json \
-profile=kubernetes \
manager-csr.json | cfssljson -bare controller-manager
$ ls controller-manager*.pem
複製代碼
若是節點IP不一樣,須要修改
manager-csr.json
的hosts
而後執行命令生成名爲controller-manager.conf
的kubeconfig:
# controller-manager set-cluster
$ kubectl config set-cluster kubernetes \
--certificate-authority=ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=../controller-manager.conf
# controller-manager set-credentials
$ kubectl config set-credentials system:kube-controller-manager \
--client-certificate=controller-manager.pem \
--client-key=controller-manager-key.pem \
--embed-certs=true \
--kubeconfig=../controller-manager.conf
# controller-manager set-context
$ kubectl config set-context system:kube-controller-manager@kubernetes \
--cluster=kubernetes \
--user=system:kube-controller-manager \
--kubeconfig=../controller-manager.conf
# controller-manager set default context
$ kubectl config use-context system:kube-controller-manager@kubernetes \
--kubeconfig=../controller-manager.conf
複製代碼
下載scheduler-csr.json
,生成kube-scheduler certificate證書:
$ wget "${PKI_URL}/scheduler-csr.json"
$ cfssl gencert \
-ca=ca.pem \
-ca-key=ca-key.pem \
-config=ca-config.json \
-profile=kubernetes \
scheduler-csr.json | cfssljson -bare scheduler
$ ls scheduler*.pem
scheduler-key.pem scheduler.pem
複製代碼
若是節點IP不一樣,須要修改
scheduler-csr.json
的hosts
而後執行一下命令生成名爲scheduler.conf
的kubeconfig:
# scheduler set-cluster
$ kubectl config set-cluster kubernetes \
--certificate-authority=ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=../scheduler.conf
# scheduler set-credentials
$ kubectl config set-credentials system:kube-scheduler \
--client-certificate=scheduler.pem \
--client-key=scheduler-key.pem \
--embed-certs=true \
--kubeconfig=../scheduler.conf
# scheduler set-context
$ kubectl config set-context system:kube-scheduler@kubernetes \
--cluster=kubernetes \
--user=system:kube-scheduler \
--kubeconfig=../scheduler.conf
# scheduler set default context
$ kubectl config use-context system:kube-scheduler@kubernetes \
--kubeconfig=../scheduler.conf
複製代碼
下載kubelet-csr.json
,並生成master node certificate證書:
$ wget "${PKI_URL}/kubelet-csr.json"
$ sed -i 's/$NODE/master1/g' kubelet-csr.json
$ cfssl gencert \
-ca=ca.pem \
-ca-key=ca-key.pem \
-config=ca-config.json \
-hostname=master1,172.16.35.12 \
-profile=kubernetes \
kubelet-csr.json | cfssljson -bare kubelet
$ ls kubelet*.pem
kubelet-key.pem kubelet.pem
複製代碼
$NODE
須要隨節點名稱不一樣而改變
而後執行一下命令生成名爲kubelet.conf
的kubeconfig:
# kubelet set-cluster
$ kubectl config set-cluster kubernetes \
--certificate-authority=ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=../kubelet.conf
# kubelet set-credentials
$ kubectl config set-credentials system:node:master1 \
--client-certificate=kubelet.pem \
--client-key=kubelet-key.pem \
--embed-certs=true \
--kubeconfig=../kubelet.conf
# kubelet set-context
$ kubectl config set-context system:node:master1@kubernetes \
--cluster=kubernetes \
--user=system:node:master1 \
--kubeconfig=../kubelet.conf
# kubelet set default context
$ kubectl config use-context system:node:master1@kubernetes \
--kubeconfig=../kubelet.conf
複製代碼
Service account不須要CA認證,也就不須要CA來作Service account key的檢查,這裏咱們創建一組private和public的密鑰供Service account key使用:
$ openssl genrsa -out sa.key 2048 $ openssl rsa -in sa.key -pubout -out sa.pub $ ls sa.* sa.key sa.pub
複製代碼
完成後刪除沒必要要文件:
$ rm -rf *.json *.csr
複製代碼
確認/etc/kubernetes
和/etc/kubernetes/pki
包含如下文件:
$ ls /etc/kubernetes/
admin.conf bootstrap.conf controller-manager.conf kubelet.conf pki scheduler.conf token.csv
$ ls /etc/kubernetes/pki
admin-key.pem apiserver-key.pem ca-key.pem controller-manager-key.pem front-proxy-ca-key.pem front-proxy-client-key.pem kubelet-key.pem sa.key scheduler-key.pem
admin.pem apiserver.pem ca.pem controller-manager.pem front-proxy-ca.pem front-proxy-client.pem kubelet.pem sa.pub scheduler.pem
複製代碼
下載Kubernetes核心組件Yaml文件,這裏咱們利用Kubernetes Statics Pod來創建Master核心組件,所以下載全部Static Pod文件到etc/kubernetes/manifests
目錄「
$ export CORE_URL="https://kairen.github.io/files/manual-v1.8/master"
$ mkdir -p /etc/kubernetes/manifests && cd /etc/kubernetes/manifests
$ for FILE in apiserver manager scheduler; do
wget "${CORE_URL}/${FILE}.yml.conf" -O ${FILE}.yml
done
複製代碼
一樣的,若是IP與本文IP準備不一樣的話,須要修改
apiserver.yml
、manager.yml
、scheduler.yml
apiserver中的
NodeRestriction
請參考Using Node Authorization
生成一個用來加密Etcd的key
$ head -c 32 /dev/urandom | base64
SUpbL4juUYyvxj3/gonV5xVEx8j769/99TSAf8YT/sQ=
複製代碼
在/etc/kubernetes/
目錄創建encryption.yml
的加密YAML文件:
$ cat <<EOF > /etc/kubernetes/encryption.yml
kind: EncryptionConfig
apiVersion: v1
resources:
- resources:
- secrets
providers:
- aescbc:
keys:
- name: key1
secret: SUpbL4juUYyvxj3/gonV5xVEx8j769/99TSAf8YT/sQ=
- identity: {}
EOF
複製代碼
Etcd加密可參考Encrypting data at rest
在/etc/kubernetes/
目錄創建audit-policy.yml
的auditing policay YAML文件:
$ cat <<EOF > /etc/kubernetes/audit-policy.yml
apiVersion: audit.k8s.io/v1beta1
kind: Policy
rules:
- level: Metadata
EOF
複製代碼
audit policy請參考Audit
下載kubelet.service
相關文件來管理kubelet:
$ export KUBELET_URL="https://kairen.github.io/files/manual-v1.8/master"
$ mkdir -p /etc/systemd/system/kubelet.service.d
$ wget "${KUBELET_URL}/kubelet.service" -O /lib/systemd/system/kubelet.service
$ wget "${KUBELET_URL}/10-kubelet.conf" -O /etc/systemd/system/kubelet.service.d/10-kubelet.conf
複製代碼
若
cluster-dns
或cluster-domain
有變更,須要修改10-kubelet.conf
最後創建var並啓動kubelet服務:
$ mkdir -p /var/lib/kubelet /var/log/kubernetes
$ systemctl enable kubelet.service && systemctl start kubelet.service
複製代碼
完成後須要一段時間來下載鏡像文件並啓動組件:
$ watch netstat -ntlp
tcp 0 0 127.0.0.1:10248 0.0.0.0:* LISTEN 23012/kubelet
tcp 0 0 127.0.0.1:10251 0.0.0.0:* LISTEN 22305/kube-schedule
tcp 0 0 127.0.0.1:10252 0.0.0.0:* LISTEN 22529/kube-controll
tcp6 0 0 :::6443 :::* LISTEN 22956/kube-apiserve
複製代碼
看到上述信息即代表服務啓動正常,若是出現問題可經過docker cli查看
完成後,複製admin kubeconfig並經過如下命令驗證:
$ cp /etc/kubernetes/admin.conf ~/.kube/config
$ kubectl get cs
NAME STATUS MESSAGE ERROR
etcd-0 Healthy {"health": "true"}
scheduler Healthy ok
controller-manager Healthy ok
$ kubectl get node
NAME STATUS ROLES AGE VERSION
master1 NotReady master 1m v1.8.6
$ kubectl -n kube-system get po
NAME READY STATUS RESTARTS AGE
kube-apiserver-master1 1/1 Running 0 4m
kube-controller-manager-master1 1/1 Running 0 4m
kube-scheduler-master1 1/1 Running 0 4m
複製代碼
確認服務可以執行logs等命令:
$ kubectl -n kube-system logs -f kube-scheduler-master1
Error from server (Forbidden): Forbidden (user=kube-apiserver, verb=get, resource=nodes, subresource=proxy) ( pods/log kube-apiserver-master1)
複製代碼
出現403 Forbidden問題代表
kube-apiserver user
並無nodes的權限
因爲上述權限問題,咱們須要創建一個apiserver-to-kubelet-rbac.yml
來定義權限,以供咱們執行logs、exec等命令:
$ cd /etc/kubernetes/
$ export URL="https://kairen.github.io/files/manual-v1.8/master"
$ wget "${URL}/apiserver-to-kubelet-rbac.yml.conf" -O apiserver-to-kubelet-rbac.yml
$ kubectl apply -f apiserver-to-kubelet-rbac.yml
# 測試 logs
$ kubectl -n kube-system logs -f kube-scheduler-master1
...
I1031 03:22:42.527697 1 leaderelection.go:184] successfully acquired lease kube-system/kube-scheduler
複製代碼
Node運行容器實例的節點,即工做節點。本部分咱們會下載Kubernetes binary並創建node 的certificate來提供給節點註冊認證用。Kubernetes使用Node Authorizer來提供Authorization mode,這種受權模式會替Kubelet生成API request。
開始前,咱們先在master1
將須要的ca和cert複製到Node節點上:
$ for NODE in node1 node2; do
ssh ${NODE} "mkdir -p /etc/kubernetes/pki/"
ssh ${NODE} "mkdir -p /etc/etcd/ssl"
# Etcd ca and cert
for FILE in etcd-ca.pem etcd.pem etcd-key.pem; do
scp /etc/etcd/ssl/${FILE} ${NODE}:/etc/etcd/ssl/${FILE}
done
# Kubernetes ca and cert
for FILE in pki/ca.pem pki/ca-key.pem bootstrap.conf; do
scp /etc/kubernetes/${FILE} ${NODE}:/etc/kubernetes/${FILE}
done
done
複製代碼
首先獲取全部須要執行的文件:
# Download Kubernetes
$ export KUBE_URL="https://storage.googleapis.com/kubernetes-release/release/v1.8.6/bin/linux/amd64"
$ wget "${KUBE_URL}/kubelet" -O /usr/local/bin/kubelet
$ chmod +x /usr/local/bin/kubelet
# Download CNI
$ mkdir -p /opt/cni/bin && cd /opt/cni/bin
$ export CNI_URL="https://github.com/containernetworking/plugins/releases/download"
$ wget -qO- --show-progress "${CNI_URL}/v0.6.0/cni-plugins-amd64-v0.6.0.tgz" | tar -zx
複製代碼
下載Kubernetes相關文件,包括drop-in file、systemd service等:
$ export KUBELET_URL="https://kairen.github.io/files/manual-v1.8/node"
$ mkdir -p /etc/systemd/system/kubelet.service.d
$ wget "${KUBELET_URL}/kubelet.service" -O /lib/systemd/system/kubelet.service
$ wget "${KUBELET_URL}/10-kubelet.conf" -O /etc/systemd/system/kubelet.service.d/10-kubelet.conf
複製代碼
若是
cluster-dns
或cluster-domain
有改變的話,須要修改10-kubelet.conf
而後在全部node創建var,並啓動kubelet服務:
$ mkdir -p /var/lib/kubelet /var/log/kubernetes /etc/kubernetes/manifests
$ systemctl enable kubelet.service && systemctl start kubelet.service
複製代碼
重複完成全部節點後,在master1節點創建ClusterRoleBinding(由於咱們採用的是TLS Bootstrapping):
$ kubectl create clusterrolebinding kubelet-bootstrap \
--clusterrole=system:node-bootstrapper \
--user=kubelet-bootstrap
複製代碼
在master進行驗證,咱們能夠看到節點處於pending:
$ kubectl get csr
NAME AGE REQUESTOR CONDITION
node-csr-YWf97ZrLCTlr2hmXsNLfjVLwaLfZRsu52FRKOYjpcBE 2s kubelet-bootstrap Pending
node-csr-eq4q6ffOwT4yqYQNU6sT7mphPOQdFN6yulMVZeu6pkE 2s kubelet-bootstrap Pending
複製代碼
經過kubectl,容許節點加入集羣:
$ kubectl get csr | awk '/Pending/ {print $1}' | xargs kubectl certificate approve
certificatesigningrequest "node-csr-YWf97ZrLCTlr2hmXsNLfjVLwaLfZRsu52FRKOYjpcBE" approved
certificatesigningrequest "node-csr-eq4q6ffOwT4yqYQNU6sT7mphPOQdFN6yulMVZeu6pkE" approved
$ kubectl get csr
NAME AGE REQUESTOR CONDITION
node-csr-YWf97ZrLCTlr2hmXsNLfjVLwaLfZRsu52FRKOYjpcBE 30s kubelet-bootstrap Approved,Issued
node-csr-eq4q6ffOwT4yqYQNU6sT7mphPOQdFN6yulMVZeu6pkE 30s kubelet-bootstrap Approved,Issued
$ kubectl get no
NAME STATUS ROLES AGE VERSION
master1 NotReady master 21m v1.8.6
node1 NotReady node 8s v1.8.6
node2 NotReady node 8s v1.8.6
複製代碼
完成以上全部步驟,咱們還須要安裝一些插件,好比kube-dns、kube-proxy等等。
Kube-proxy是實現Service的關鍵組件,kube-proxy會在每一個節點上執行,而後監聽API Server的Service和Endpoint變化,並根據變化執行iptables實現網絡轉發。
這裏咱們須要DaemonSet來執行,並須要生成一些certificate。
首先在master1
下載kube-proxy-csr.json
,並生成kube-proxy certificate證書:
$ export PKI_URL="https://kairen.github.io/files/manual-v1.8/pki"
$ cd /etc/kubernetes/pki
$ wget "${PKI_URL}/kube-proxy-csr.json" "${PKI_URL}/ca-config.json"
$ cfssl gencert \
-ca=ca.pem \
-ca-key=ca-key.pem \
-config=ca-config.json \
-profile=kubernetes \
kube-proxy-csr.json | cfssljson -bare kube-proxy
$ ls kube-proxy*.pem
kube-proxy-key.pem kube-proxy.pem
複製代碼
而後經過如下命令生成名爲`kube-proxy.conf·的kubeconfig:
# kube-proxy set-cluster
$ kubectl config set-cluster kubernetes \
--certificate-authority=ca.pem \
--embed-certs=true \
--server="https://172.16.35.12:6443" \
--kubeconfig=../kube-proxy.conf
# kube-proxy set-credentials
$ kubectl config set-credentials system:kube-proxy \
--client-key=kube-proxy-key.pem \
--client-certificate=kube-proxy.pem \
--embed-certs=true \
--kubeconfig=../kube-proxy.conf
# kube-proxy set-context
$ kubectl config set-context system:kube-proxy@kubernetes \
--cluster=kubernetes \
--user=system:kube-proxy \
--kubeconfig=../kube-proxy.conf
# kube-proxy set default context
$ kubectl config use-context system:kube-proxy@kubernetes \
--kubeconfig=../kube-proxy.conf
複製代碼
刪除沒必要要的文件:
$ rm -rf *.json
複製代碼
確認/etc/kubernetes
有如下文件:
$ ls /etc/kubernetes/
admin.conf bootstrap.conf encryption.yml kube-proxy.conf pki token.csv
audit-policy.yml controller-manager.conf kubelet.conf manifests scheduler.conf
複製代碼
在master1
上將kube-proxy相關文件複製到Node節點上:
$ for NODE in node1 node2; do
echo "--- $NODE ---"
for FILE in pki/kube-proxy.pem pki/kube-proxy-key.pem kube-proxy.conf; do
scp /etc/kubernetes/${FILE} ${NODE}:/etc/kubernetes/${FILE}
done
done
複製代碼
完成後,在master1
經過kubectl創建kube-proxy daemon:
$ export ADDON_URL="https://kairen.github.io/files/manual-v1.8/addon"
$ mkdir -p /etc/kubernetes/addons && cd /etc/kubernetes/addons
$ wget "${ADDON_URL}/kube-proxy.yml.conf" -O kube-proxy.yml
$ kubectl apply -f kube-proxy.yml
$ kubectl -n kube-system get po -l k8s-app=kube-proxy
NAME READY STATUS RESTARTS AGE
kube-proxy-bpp7q 1/1 Running 0 47s
kube-proxy-cztvh 1/1 Running 0 47s
kube-proxy-q7mm4 1/1 Running 0 47s
複製代碼
Kube DNS是Kubernetes集羣內部Pod之間通訊的重要插件,容許Pod經過Domain Name連接Service,主要由Kube DNS與Sky DNS組合而成,經過Kube DNS監聽Service與Endpoint變化,來提供給Sky DNS信息以更新解析位址。
安裝只須要在master1
經過kubectl創建kube-dns deployment便可:
$ export ADDON_URL="https://kairen.github.io/files/manual-v1.8/addon"
$ wget "${ADDON_URL}/kube-dns.yml.conf" -O kube-dns.yml
$ kubectl apply -f kube-dns.yml
$ kubectl -n kube-system get po -l k8s-app=kube-dns
NAME READY STATUS RESTARTS AGE
kube-dns-6cb549f55f-h4zr5 0/3 Pending 0 40s
複製代碼
Calico是一款純3層協議(不須要Overlay 網路),已與各類雲原平生臺有良好的整合,在每一個節點節點利用Linux Kernel實現高效的vRouter來負責數據轉發,而當數據中心複雜度增長時,能夠用BGP route reflector來達成。
首先在master1
經過kubectl創建Calico policy controller:
$ export CALICO_CONF_URL="https://kairen.github.io/files/manual-v1.8/network"
$ wget "${CALICO_CONF_URL}/calico-controller.yml.conf" -O calico-controller.yml
$ kubectl apply -f calico-controller.yml
$ kubectl -n kube-system get po -l k8s-app=calico-policy
NAME READY STATUS RESTARTS AGE
calico-policy-controller-5ff8b4549d-tctmm 0/1 Pending 0 5s
複製代碼
若是節點IP不一樣,須要修改calico-controller.yml的ETCD_ENDPOINTS
在`master1·下載Calico CLI工具:
$ wget https://github.com/projectcalico/calicoctl/releases/download/v1.6.1/calicoctl
$ chmod +x calicoctl && mv calicoctl /usr/local/bin/
複製代碼
而後在全部節點下載Calico,並執行如下命令:
$ export CALICO_URL="https://github.com/projectcalico/cni-plugin/releases/download/v1.11.0"
$ wget -N -P /opt/cni/bin ${CALICO_URL}/calico
$ wget -N -P /opt/cni/bin ${CALICO_URL}/calico-ipam
$ chmod +x /opt/cni/bin/calico /opt/cni/bin/calico-ipam
複製代碼
接着在全部節點下載CNI plugins以及calico-node.service:
$ mkdir -p /etc/cni/net.d
$ export CALICO_CONF_URL="https://kairen.github.io/files/manual-v1.8/network"
$ wget "${CALICO_CONF_URL}/10-calico.conf" -O /etc/cni/net.d/10-calico.conf
$ wget "${CALICO_CONF_URL}/calico-node.service" -O /lib/systemd/system/calico-node.service
複製代碼
若是節點IP不一樣,須要修改10-calico.conf的etcd_endpoints
若是部署機器是虛擬機,須要修改calico-node.service,並在IP_AUTODETECTION_METHOD (包含IP6)部分指定綁定的網卡,以避免預設綁定到NAT網路上
以後在全部節點啓動Calico-node:
$ systemctl enable calico-node.service && systemctl start calico-node.service
複製代碼
在master1
查看Calico nodes:
$ cat <<EOF > ~/calico-rc
export ETCD_ENDPOINTS="https://172.16.35.12:2379"
export ETCD_CA_CERT_FILE="/etc/etcd/ssl/etcd-ca.pem"
export ETCD_CERT_FILE="/etc/etcd/ssl/etcd.pem"
export ETCD_KEY_FILE="/etc/etcd/ssl/etcd-key.pem"
EOF
$ . ~/calico-rc
$ calicoctl get node -o wide
NAME ASN IPV4 IPV6
master1 (64512) 172.16.35.12/24
node1 (64512) 172.16.35.10/24
node2 (64512) 172.16.35.11/24
複製代碼
查看pending的pod是否已執行:
$ kubectl -n kube-system get po
NAME READY STATUS RESTARTS AGE
calico-policy-controller-5ff8b4549d-tctmm 1/1 Running 0 4m
kube-apiserver-master1 1/1 Running 0 20m
kube-controller-manager-master1 1/1 Running 0 20m
kube-dns-6cb549f55f-h4zr5 3/3 Running 0 5m
kube-proxy-fnrkb 1/1 Running 0 6m
kube-proxy-l72bq 1/1 Running 0 6m
kube-proxy-m6rfw 1/1 Running 0 6m
kube-scheduler-master1 1/1 Running 0 20m
複製代碼
省事的作法是用Standard Hosted方式安裝。
本部分說明如何部署官方經常使用的addons,例如dashboard、heapster等。
Dashboard是Kubernetes官方開發的儀表板,讓咱們以能夠i 經過web-based方式管理kubernetes集羣。
在master1
經過kubectl創建kubernetes dashboard便可:
$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml
$ kubectl -n kube-system get po,svc -l k8s-app=kubernetes-dashboard
NAME READY STATUS RESTARTS AGE
po/kubernetes-dashboard-747c4f7cf-md5m8 1/1 Running 0 56s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc/kubernetes-dashboard ClusterIP 10.98.120.209 <none> 443/TCP 56s
複製代碼
這邊會額外創建一個名稱爲open-api
的Cluster Role Binding,放拜年測試使用,通常狀況下不開啓(開啓會存取全部API)。
$ cat <<EOF | kubectl create -f -
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: open-api
namespace: ""
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: User
name: system:anonymous
EOF
複製代碼
管理者能夠針對特定使用者來開放API存取權限,這裏咱們爲了方便直接綁在cluster-admin cluster role。
1.7版本後的Dashboard再也不提供全部權限,須要創建一個service account來綁定cluster-admin role:
$ kubectl -n kube-system create sa dashboard
$ kubectl create clusterrolebinding dashboard --clusterrole cluster-admin --serviceaccount=kube-system:dashboard
$ SECRET=$(kubectl -n kube-system get sa dashboard -o yaml | awk '/dashboard-token/ {print $3}')
$ kubectl -n kube-system describe secrets ${SECRET} | awk '/token:/{print $2}'
eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkYXNoYm9hcmQtdG9rZW4tdzVocmgiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGFzaGJvYXJkIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiYWJmMTFjYzMtZjRlYi0xMWU3LTgzYWUtMDgwMDI3NjdkOWI5Iiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmRhc2hib2FyZCJ9.Xuyq34ci7Mk8bI97o4IldDyKySOOqRXRsxVWIJkPNiVUxKT4wpQZtikNJe2mfUBBD-JvoXTzwqyeSSTsAy2CiKQhekW8QgPLYelkBPBibySjBhJpiCD38J1u7yru4P0Pww2ZQJDjIxY4vqT46ywBklReGVqY3ogtUQg-eXueBmz-o7lJYMjw8L14692OJuhBjzTRSaKW8U2MPluBVnD7M2SOekDff7KpSxgOwXHsLVQoMrVNbspUCvtIiEI1EiXkyCNRGwfnd2my3uzUABIHFhm0_RZSmGwExPbxflr8Fc6bxmuz-_jSdOtUidYkFIzvEWw2vRovPgs3MXTv59RwUw
複製代碼
複製token,而後貼到Kubernetes dashboard
Heapster是Kubernetes社區維護的容器集羣監控和分析工具。Heapster會從Kubernetes apiserver取得全部Node數據,而後再經過Node獲取kubelet上的數據,最後再將全部收集到數據送到Heapster後臺儲存InfluxDB,最後利用Grafana抓取InfluxDB數據源來進行展現。
在master1
經過kubectl來創建kubernetes monitor便可:
$ export ADDON_URL="https://kairen.github.io/files/manual-v1.8/addon"
$ wget ${ADDON_URL}/kube-monitor.yml.conf -O kube-monitor.yml
$ kubectl apply -f kube-monitor.yml
$ kubectl -n kube-system get po,svc
NAME READY STATUS RESTARTS AGE
...
po/heapster-74fb5c8cdc-62xzc 4/4 Running 0 7m
po/influxdb-grafana-55bd7df44-nw4nc 2/2 Running 0 7m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
...
svc/heapster ClusterIP 10.100.242.225 <none> 80/TCP 7m
svc/monitoring-grafana ClusterIP 10.101.106.180 <none> 80/TCP 7m
svc/monitoring-influxdb ClusterIP 10.109.245.142 <none> 8083/TCP,8086/TCP 7m
···
複製代碼
Kubernetes能夠選擇使用指令直接創建應用和服務,或者咱們能夠寫YAML、JSON文件來配置,以下所示:
$ kubectl run nginx --image=nginx --port=80
$ kubectl expose deploy nginx --port=80 --type=LoadBalancer --external-ip=172.16.35.12
$ kubectl get svc,po
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 1h
svc/nginx LoadBalancer 10.97.121.243 172.16.35.12 80:30344/TCP 22s
NAME READY STATUS RESTARTS AGE
po/nginx-7cbc4b4d9c-7796l 1/1 Running 0 28s 192.160.57.181 ,172.16.35.12 80:32054/TCP 21s
複製代碼
這裏type能夠選擇NodePort和LoadBalancer在本地裸機部署,二者差別在於NodePort只映射Host port到Container port,而LoadBalancer則繼承NodePort額外映射Host target port到Container port
最後,咱們能夠經過如下方式來擴展服務數量:
$ kubectl scale deploy nginx --replicas=2
$ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
nginx-158599303-0h9lr 1/1 Running 0 25s 10.244.100.5 node2
nginx-158599303-k7cbt 1/1 Running 0 1m 10.244.24.3 node1
複製代碼
相關閱讀
技術
Kubernetes Autoscaling是如何工做的? 2018/05/07
技術
用戶評測 | Docker管理面板系列——雲幫(Rainbond 出色的k8s管理面板) 2018/05/09
技術
如何把應用轉移到Kubernetes 2018/05/04
技術
Kubernetes伸縮到2500個節點中遇到的問題和解決方法 2018/04/24
技術
kubernetes容器網絡接口(CNI) midonet網絡插件的設計與實現 2017/05/04
技術
在生產環境使用Kuberntes一年後,咱們總結了這些經驗和教訓 2017/02/23
技術
關於K8s容器集羣日誌收集的總結 2016/12/15
技術
Kubernetes集羣中的高性能網絡策略 2016/12/08
行業
開了香檳的Kubernetes並不打算放慢成功的腳步 2018/05/09
行業
上手kubernetes以前,你應該知道這6件事 2018/05/03
行業
爲何說Kubernetes是雲服務的將來? 2016/10/21