kubernetes 安裝手冊(成功版)


管理組件采用staticPod或者daemonSet形式跑的,宿主機os能跑docker應該本篇教程能大多適用
安裝完成僅供學習和實驗

本次安裝的版本:

  • Kubernetes v1.10.0 (1.10.0和1.10.3親測成功)
  • CNI v0.6.0
  • Etcd v3.1.13
  • Calico v3.0.4
  • Docker CE latest version(18.03)

節點信息
本教學將以下列節點數與規格來進行部署Kubernetes集群,系統可采用Ubuntu 16.xCentOS 7.x

IP Hostname CPU Memory
192.16.35.11 K8S-M1 1 4G
192.16.35.12 K8S-M2 1 4G
192.16.35.13 K8S-M3 1 4G
192.16.35.14 K8S-N1 1 4G
192.16.35.15 K8S-N2 1 4G
192.16.35.16 K8S-N3 1 4G

另外由所有master節點提供一組VIP 192.16.35.10

  • 所有操作全部用root使用者進行(方便用),以SRE來說不推薦。
  • 可以下載Vagrantfile來建立Virtualbox虛擬機集群。不過需要注意機器資源是否足夠

事前准備

  • 所有機器彼此網路互通,並且k8s-m1SSH登入其他節點為passwdless。
  • 所有防火牆與SELinux 已關閉。如CentOS:

    1
    2
    3
    4
    $ systemctl stop firewalld && systemctl disable firewalld
    $ setenforce 0
    $ vim /etc/selinux/config
    SELINUX=disabled
  • 所有機器需要設定/etc/hosts解析到所有集群主機。

    1
    2
    3
    4
    5
    6
    7
    ...
    192.16.35.11 k8s-m1
    192.16.35.12 k8s-m2
    192.16.35.13 k8s-m3
    192.16.35.14 k8s-n1
    192.16.35.15 k8s-n2
    192.16.35.16 k8s-n3
  • 所有機器需要安裝Docker CE 版本的容器引擎:

    1
    $ curl -fsSL "https://get.docker.com/" | sh
  • 不管是在Ubuntu或CentOS都只需要執行該指令就會自動安裝最新版Docker。
  • CentOS安裝完成后,需要再執行以下指令:
1
$ systemctl enable docker && systemctl start docker
  • 所有機器需要設定/etc/sysctl.d/k8s.conf的系統參數。

    1
    2
    3
    4
    5
    6
    7
    $ 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
  • Kubernetes v1.8+要求關閉系統Swap,若不關閉則需要修改kubelet設定參數,在所有機器使用以下指令關閉swap並注釋掉/etc/fstab中swap的行:

    1
    2
    $ swapoff -a && sysctl -w vm.swappiness=0
    $ sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab
  • 確保getenforce的值是Disabled,如果不是請重啟

  • 所有機器提前拉取以下鏡像

1
2
3
4
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
quay.io/calico/node v3.0.4 5361c5a52912 8 weeks ago 278MB
quay.io/calico/cni v2.0.3 cef0252b1749 2 months ago 69.1MB
k8s.gcr.io/pause-amd64 3.1 da86e6ba6ca1 5 months ago 742kB

這三個因為牆的原因會拉取不到,我已經save成文件了(有工具的可以直接pull上面鏡像)
文件地址是https://pan.baidu.com/s/1v7uN4ht-7qvA1uk9ZMmuMA
上面是百度雲,下載不了或者限速的可以用下面七牛雲地址下載並導入鏡像

1
2
$ wget http://ols7lqkih.bkt.clouddn.com/images.tar.gz
$ docker load -i images.tar.gz

 

  • 所有Node提前拉取以下鏡像
1
quay.io/calico/kube-controllers                      v2.0.2 0754e1c707e7 2 months ago 55.1MB

同樣被牆了,拉取不到用我的七牛雲地址導入

1
2
$ wget http://ols7lqkih.bkt.clouddn.com/calico-kube-proxy-adm64.tar.gz
$ docker load -i calico-kube-proxy-adm64.tar.gz

 

  • 所有機器下載Kubernetes二進制執行檔:

無越牆工具的,我已把kubectl和kubelet上傳到我的七牛雲了,使用下面下載

1
2
3
4
5
6
7
8
9
10
11
12
$ wget http://ols7lqkih.bkt.clouddn.com/kubelet -O /usr/local/bin/kubelet
$ chmod +x /usr/local/bin/kubelet
# node 請忽略下載 kubectl
$ wget http://ols7lqkih.bkt.clouddn.com/kubectl -O /usr/local/bin/kubectl
$ chmod +x /usr/local/bin/kubectl

# md5值為以下,自行對比下看看文件是否損壞了

[root@k8s-m1 ~]# md5sum /usr/local/bin/kubelet
a3ced404a71f94d2fa9230635ed4e407 kubelet
[root@k8s-m1 ~]# md5sum /usr/local/bin/kubectl
e1f801301614463e1f13cf28b4443608 kubectl

 

有工具的使用下面的原地址

1
2
3
4
5
6
7
$ export KUBE_URL="https://storage.googleapis.com/kubernetes-release/release/v1.10.0/bin/linux/amd64"
$ wget "${KUBE_URL}/kubelet" -O /usr/local/bin/kubelet
$ chmod +x /usr/local/bin/kubelet

# node 請忽略下載 kubectl
$ wget "${KUBE_URL}/kubectl" -O /usr/local/bin/kubectl
$ chmod +x /usr/local/bin/kubectl
  • 所有機器下載Kubernetes CNI 二進制執行檔:(centos命令報錯的話建議直接下載后解壓到目錄里)

    1
    2
    3
    mkdir -p /opt/cni/bin && cd /opt/cni/bin
    export CNI_URL="https://github.com/containernetworking/plugins/releases/download"
    wget -qO- "${CNI_URL}/v0.6.0/cni-plugins-amd64-v0.6.0.tgz" | tar -zx
  • k8s-m1需要安裝CFSSL工具,這將會用來建立 TLS Certificates。

    1
    2
    3
    4
    $ 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

建立集群CA keys 與Certificates
在這個部分,將需要產生多個元件的Certificates,這包含Etcd、Kubernetes 元件等,並且每個集群都會有一個根數位憑證認證機構(Root Certificate Authority)被用在認證API Server 與Kubelet 端的憑證。

  • PS這邊要注意CA JSON檔的CN(Common Name)O(Organization)等內容是會影響Kubernetes元件認證的。

Etcd
首先在k8s-m1建立/etc/etcd/ssl文件夾,然后進入目錄完成以下操作。

1
2
$ mkdir -p /etc/etcd/ssl && cd /etc/etcd/ssl
$ export PKI_URL="https://kairen.github.io/files/manual-v1.10/pki"

 

下載ca-config.jsonetcd-ca-csr.json文件,並從CSR json產生CA keys與Certificate:

1
2
$ wget "${PKI_URL}/ca-config.json" "${PKI_URL}/etcd-ca-csr.json"
$ cfssl gencert -initca etcd-ca-csr.json | cfssljson -bare etcd-ca

 

下載etcd-csr.json文件,並產生Etcd證書:

1
2
3
4
5
6
7
8
$ wget "${PKI_URL}/etcd-csr.json"
$ cfssl gencert \
-ca=etcd-ca.pem \
-ca-key=etcd-ca-key.pem \
-config=ca-config.json \
-hostname=127.0.0.1,192.16.35.11,192.16.35.12,192.16.35.13 \
-profile=kubernetes \
etcd-csr.json | cfssljson -bare etcd

 

  • -hostname需修改成所有masters 節點。

完成后刪除不必要文件:

1
$ rm -rf *.json *.csr

 

確認/etc/etcd/ssl有以下文件:

1
2
$ ls /etc/etcd/ssl
etcd-ca-key.pem etcd-ca.pem etcd-key.pem etcd.pem

 

復制相關文件至其他Etcd節點,這邊為所有master節點:

1
2
3
4
5
6
7
$ for NODE in k8s-m2 k8s-m3; do
echo "--- $NODE ---"
ssh ${NODE} "mkdir -p /etc/etcd/ssl"
for FILE in etcd-ca-key.pem etcd-ca.pem etcd-key.pem etcd.pem; do
scp /etc/etcd/ssl/${FILE} ${NODE}:/etc/etcd/ssl/${FILE}
done
done

 

Kubernetes
k8s-m1建立pki文件夾,然后進入目錄完成以下章節操作。

1
2
3
$ mkdir -p /etc/kubernetes/pki && cd /etc/kubernetes/pki
$ export PKI_URL="https://kairen.github.io/files/manual-v1.10/pki"
$ export KUBE_APISERVER="https://192.16.35.10:6443"

 

下載ca-config.jsonca-csr.json文件,並產生CA憑證:

1
2
3
4
$ 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

 

API Server Certificate
下載apiserver-csr.json文件,並產生kube-apiserver憑證:

1
2
3
4
5
6
7
8
9
10
11
$ 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,192.16.35.10,127.0.0.1,kubernetes.default \
-profile=kubernetes \
apiserver-csr.json | cfssljson -bare apiserver

$ ls apiserver*.pem
apiserver-key.pem apiserver.pem

 

  • 這邊-hostname10.96.0.1是Cluster IP的Kubernetes端點;
  • 192.16.35.10為虛擬IP 位址(VIP);
  • kubernetes.default為Kubernets DN。

Front Proxy Certificate
下載front-proxy-ca-csr.json文件,並產生Front Proxy CA金鑰,Front Proxy主要是用在API aggregator上:

1
2
3
4
5
6
$ 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證書:

1
2
3
4
5
6
7
8
9
10
$ 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

 

Admin Certificate
下載admin-csr.json文件,並產生admin certificate憑證:

1
2
3
4
5
6
7
8
9
10
$ 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文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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

 

Controller Manager Certificate
下載manager-csr.json檔案,並產生kube-controller-manager certificate憑證:

1
2
3
4
5
6
7
8
9
10
$ 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
controller-manager-key.pem controller-manager.pem

 

接着通過以下指令產生名稱為controller-manager.conf的kubeconfig文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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 Certificate
下載scheduler-csr.json文件,並產生kube-scheduler certificate憑證:

1
2
3
4
5
6
7
8
9
10
$ 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

 

接着通過以下指令產生名稱為scheduler.conf的kubeconfig文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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 use default context
$ kubectl config use-context system:kube-scheduler@kubernetes \
--kubeconfig=../scheduler.conf

 

Master Kubelet Certificate
接着在k8s-m1下載kubelet-csr.json檔案,並產生憑證:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ wget "${PKI_URL}/kubelet-csr.json"
$ for NODE in k8s-m1 k8s-m2 k8s-m3; do
echo "--- $NODE ---"
cp kubelet-csr.json kubelet-$NODE-csr.json;
sed -i "s/\$NODE/$NODE/g" kubelet-$NODE-csr.json;
cfssl gencert \
-ca=ca.pem \
-ca-key=ca-key.pem \
-config=ca-config.json \
-hostname=$NODE \
-profile=kubernetes \
kubelet-$NODE-csr.json | cfssljson -bare kubelet-$NODE
done

$ ls kubelet*.pem
kubelet-k8s-m1-key.pem kubelet-k8s-m1.pem kubelet-k8s-m2-key.pem kubelet-k8s-m2.pem kubelet-k8s-m3-key.pem kubelet-k8s-m3.pem
  • 這邊需要依據節點修改-hostname$NODE

完成后復制kubelet憑證至其他master節點:

1
2
3
4
5
6
7
$ for NODE in k8s-m2 k8s-m3; do
echo "--- $NODE ---"
ssh ${NODE} "mkdir -p /etc/kubernetes/pki"
for FILE in kubelet-$NODE-key.pem kubelet-$NODE.pem ca.pem; do
scp /etc/kubernetes/pki/${FILE} ${NODE}:/etc/kubernetes/pki/${FILE}
done
done

 

接着在k8s-m1執行以下指令產生名稱為kubelet.conf的kubeconfig文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ for NODE in k8s-m1 k8s-m2 k8s-m3; do
echo "--- $NODE ---"
ssh ${NODE} "cd /etc/kubernetes/pki && \
kubectl config set-cluster kubernetes \
--certificate-authority=ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=../kubelet.conf && \
kubectl config set-credentials system:node:${NODE} \
--client-certificate=kubelet-${NODE}.pem \
--client-key=kubelet-${NODE}-key.pem \
--embed-certs=true \
--kubeconfig=../kubelet.conf && \
kubectl config set-context system:node:${NODE}@kubernetes \
--cluster=kubernetes \
--user=system:node:${NODE} \
--kubeconfig=../kubelet.conf && \
kubectl config use-context system:node:${NODE}@kubernetes \
--kubeconfig=../kubelet.conf && \
rm kubelet-${NODE}.pem kubelet-${NODE}-key.pem"
done

Service Account Key
Service account 不是通過CA 進行認證,因此不要通過CA 來做Service account key 的檢查,這邊建立一組Private 與Public 密鑰提供給Service account key 使用:
k8s-m1執行以下指令

1
2
3
4
$ openssl genrsa -out sa.key 2048
$ openssl rsa -in sa.key -pubout -out sa.pub
$ ls sa.*
sa.key sa.pub

 

刪除不必要文件
所有資訊准備完成后,就可以將一些不必要文件刪除:

1
$ rm -rf *.json *.csr scheduler*.pem controller-manager*.pem admin*.pem kubelet*.pem

 

復制文件至其他節點
復制憑證文件至其他master節點:

1
2
3
4
5
6
$ for NODE in k8s-m2 k8s-m3; do
echo "--- $NODE ---"
for FILE in $(ls /etc/kubernetes/pki/); do
scp /etc/kubernetes/pki/${FILE} ${NODE}:/etc/kubernetes/pki/${FILE}
done
done

 

復制Kubernetes config文件至其他master節點:

1
2
3
4
5
6
$ for NODE in k8s-m2 k8s-m3; do
echo "--- $NODE ---"
for FILE in admin.conf controller-manager.conf scheduler.conf; do
scp /etc/kubernetes/${FILE} ${NODE}:/etc/kubernetes/${FILE}
done
done

 

Kubernetes Masters
本部分將說明如何建立與設定Kubernetes Master 角色,過程中會部署以下元件:

  • kube-apiserver:提供REST APIs,包含授權、認證與狀態儲存等。
  • kube-controller-manager:負責維護集群的狀態,如自動擴展,滾動更新等。
  • kube-scheduler:負責資源排程,依據預定的排程策略將Pod分配到對應節點上。
  • Etcd:儲存集群所有狀態的Key/Value儲存系統。
  • HAProxy:提供負載平衡器。
  • Keepalived:提供虛擬網路位址(VIP)。

部署與設定
首先在所有master節點下載部署元件的YAML文件,這邊不采用二進制執行檔與Systemd來管理這些元件,全部采用Static Pod來達成。這邊將檔案下載至/etc/kubernetes/manifests目錄:

(友情提醒鏡像需要工具才能pull
沒有工具請把鏡像的gcr.io/google_containers和k8s.gcr.io部分換成mirrorgooglecontainers
例如
gcr.io/google_containers/kube-apiserver-amd64 改成
mirrorgooglecontainers/kube-scheduler-amd64
keepalived里的interface網卡名改為各自宿主機的網卡名
后續的所有文件里的鏡像名同理(沒有越牆工具就這樣做)
)

1
2
3
4
5
6
7
8
9
10
11
12
13
$ export CORE_URL="https://kairen.github.io/files/manual-v1.10/master"
$ mkdir -p /etc/kubernetes/manifests && cd /etc/kubernetes/manifests
$ for FILE in kube-apiserver kube-controller-manager kube-scheduler haproxy keepalived etcd etcd.config; do
wget "${CORE_URL}/${FILE}.yml.conf" -O ${FILE}.yml
if [ ${FILE} == "etcd.config" ]; then
mv etcd.config.yml /etc/etcd/etcd.config.yml
sed -i "s/\${HOSTNAME}/${HOSTNAME}/g" /etc/etcd/etcd.config.yml
sed -i "s/\${PUBLIC_IP}/$(hostname -i)/g" /etc/etcd/etcd.config.yml
fi
done

$ ls /etc/kubernetes/manifests
etcd.yml haproxy.yml keepalived.yml kube-apiserver.yml kube-controller-manager.yml kube-scheduler.yml
  • 若IP與教學設定不同的話,請記得修改YAML文件,keepalived.yml里記得把interface改成宿主機的網卡名。
  • kube-apiserver中的·NodeRestriction·請參考Using Node Authorization

產生一個用來加密Etcd 的Key:

1
2
$ head -c 32 /dev/urandom | base64
SUpbL4juUYyvxj3/gonV5xVEx8j769/99TSAf8YT/sQ=

 

  • 注意每台master節點需要用一樣的Key。

然后在每台master機器的/etc/kubernetes/目錄下,使用上面的key配合下面命令來建立encryption.yml的加密YAML文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ 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

 

然后在每台master機器/etc/kubernetes/目錄下,建立audit-policy.yml的進階稽核策略YAML文件:

1
2
3
4
5
6
$ cat <<EOF > /etc/kubernetes/audit-policy.yml
apiVersion: audit.k8s.io/v1beta1
kind: Policy
rules:
- level: Metadata
EOF

 

  • Audit Policy請參考這篇Auditing

每台master機器下載haproxy.cfg檔案來提供給HAProxy容器使用:

1
2
$ mkdir -p /etc/haproxy/
$ wget "${CORE_URL}/haproxy.cfg" -O /etc/haproxy/haproxy.cfg

 

  • 若與本教學IP 不同的話,請記得修改設定檔。

每台master機器下載kubelet.service相關文件來管理kubelet:

1
2
3
$ mkdir -p /etc/systemd/system/kubelet.service.d
$ wget "${CORE_URL}/kubelet.service" -O /lib/systemd/system/kubelet.service
$ wget "${CORE_URL}/10-kubelet.conf" -O /etc/systemd/system/kubelet.service.d/10-kubelet.conf

 

  • 若cluster dns或domain有改變的話,需要修改10-kubelet.conf。

最后每台master機器建立var 存放資訊,然后啟動kubelet 服務:

1
2
$ mkdir -p /var/lib/kubelet /var/log/kubernetes /var/lib/etcd
$ systemctl enable kubelet.service && systemctl start kubelet.service

 

完成后會需要一段時間來下載映像檔與啟動元件,可以利用該指令來監看:

1
2
3
4
5
6
7
8
9
10
11
12
$ watch netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:10248 0.0.0.0:* LISTEN 10344/kubelet
tcp 0 0 127.0.0.1:10251 0.0.0.0:* LISTEN 11324/kube-schedule
tcp 0 0 0.0.0.0:6443 0.0.0.0:* LISTEN 11416/haproxy
tcp 0 0 127.0.0.1:10252 0.0.0.0:* LISTEN 11235/kube-controll
tcp 0 0 0.0.0.0:9090 0.0.0.0:* LISTEN 11416/haproxy
tcp6 0 0 :::2379 :::* LISTEN 10479/etcd
tcp6 0 0 :::2380 :::* LISTEN 10479/etcd
tcp6 0 0 :::10255 :::* LISTEN 10344/kubelet
tcp6 0 0 :::5443 :::* LISTEN 11295/kube-apiserve

 

  • 此處需要等待時間來拉取鏡像,需要耐心等待
  • 若看到以上資訊表示服務正常啟動,若發生問題可以用docker指令來查看。
  • 若看到關鍵的幾個管理組件容器退出的話就說明操作錯誤

上面會去拉取鏡像,需要一段時間,具體好沒好可以下面的操作來看狀態對不對

驗證集群
完成后,在任意一台master節點復制admin kubeconfig文件,並通過簡單指令驗證:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
$ cp /etc/kubernetes/admin.conf ~/.kube/config
$ kubectl get cs
NAME STATUS MESSAGE ERROR
controller-manager Healthy ok
scheduler Healthy ok
etcd-2 Healthy {"health": "true"}
etcd-1 Healthy {"health": "true"}
etcd-0 Healthy {"health": "true"}

$ kubectl get node
NAME STATUS ROLES AGE VERSION
k8s-m1 NotReady master 52s v1.10.0
k8s-m2 NotReady master 51s v1.10.0
k8s-m3 NotReady master 50s v1.10.0

$ kubectl -n kube-system get po
NAME READY STATUS RESTARTS AGE
etcd-k8s-m1 1/1 Running 0 7m
etcd-k8s-m2 1/1 Running 0 8m
etcd-k8s-m3 1/1 Running 0 7m
haproxy-k8s-m1 1/1 Running 0 7m
haproxy-k8s-m2 1/1 Running 0 8m
haproxy-k8s-m3 1/1 Running 0 8m
keepalived-k8s-m1 1/1 Running 0 8m
keepalived-k8s-m2 1/1 Running 0 7m
keepalived-k8s-m3 1/1 Running 0 7m
kube-apiserver-k8s-m1 1/1 Running 0 7m
kube-apiserver-k8s-m2 1/1 Running 0 6m
kube-apiserver-k8s-m3 1/1 Running 0 7m
kube-controller-manager-k8s-m1 1/1 Running 0 8m
kube-controller-manager-k8s-m2 1/1 Running 0 8m
kube-controller-manager-k8s-m3 1/1 Running 0 8m
kube-scheduler-k8s-m1 1/1 Running 0 8m
kube-scheduler-k8s-m2 1/1 Running 0 8m
kube-scheduler-k8s-m3 1/1 Running 0 8m

 

接着確認服務能夠執行logs 等指令:

1
2
$ kubectl -n kube-system logs -f kube-scheduler-k8s-m2
Error from server (Forbidden): Forbidden (user=kube-apiserver, verb=get, resource=nodes, subresource=proxy) ( pods/log kube-scheduler-k8s-m2)

 

  • 這邊會發現出現403 Forbidden問題,這是因為kube-apiserveruser並沒有nodes的資源存取權限,屬於正常。

后面kubectl的命令不需要每個master都執行了,任意一台master執行就行了
kubectl可以從url讀取內容來創建內容里的資源對象,也可以本地文件讀取
后面kubectl命令結尾的yaml文件記得先下載下來改下里面的鏡像倉庫部分gcr.io/google_containers和k8s.gcr.io部分換成mirrorgooglecontainers,還有里面的apiserver ip啥的
然后-f后面指定文件路徑即可
上面建議后面kubectl命令部分同理,不在多說廢話

1
2
3
4
5
6
7
8
9
$ kubectl apply -f "${CORE_URL}/apiserver-to-kubelet-rbac.yml.conf"
clusterrole.rbac.authorization.k8s.io "system:kube-apiserver-to-kubelet" configured
clusterrolebinding.rbac.authorization.k8s.io "system:kube-apiserver" configured

# 測試 logs
$ kubectl -n kube-system logs -f kube-scheduler-k8s-m2
...
I0403 02:30:36.375935 1 server.go:555] Version: v1.10.0
I0403 02:30:36.378208 1 server.go:574] starting healthz server on 127.0.0.1:10251

設定master節點允許Taint:

1
2
3
4
$ kubectl taint nodes node-role.kubernetes.io/master="":NoSchedule --all
node "k8s-m1" tainted
node "k8s-m2" tainted
node "k8s-m3" tainted

 

建立TLS Bootstrapping RBAC 與Secret
由於本次安裝啟用了TLS認證,因此每個節點的kubelet都必須使用kube-apiserver的CA的憑證后,才能與kube-apiserver進行溝通,而該過程需要手動針對每台節點單獨簽署憑證是一件繁瑣的事情,且一旦節點增加會延伸出管理不易問題;而TLS bootstrapping目標就是解決該問題,通過讓kubelet先使用一個預定低權限使用者連接到kube-apiserver,然后在對kube-apiserver申請憑證簽署,當授權Token一致時,Node節點的kubelet憑證將由kube-apiserver動態簽署提供。具體作法可以參考TLS BootstrappingAuthenticating with Bootstrap Tokens

首先在k8s-m1建立一個變數來產生BOOTSTRAP_TOKEN,並建立bootstrap-kubelet.conf的Kubernetes config文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$ cd /etc/kubernetes/pki
$ export TOKEN_ID=$(openssl rand 3 -hex)
$ export TOKEN_SECRET=$(openssl rand 8 -hex)
$ export BOOTSTRAP_TOKEN=${TOKEN_ID}.${TOKEN_SECRET}
$ export KUBE_APISERVER="https://192.16.35.10:6443"

# bootstrap set cluster
$ kubectl config set-cluster kubernetes \
--certificate-authority=ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=../bootstrap-kubelet.conf

# bootstrap set credentials
$ kubectl config set-credentials tls-bootstrap-token-user \
--token=${BOOTSTRAP_TOKEN} \
--kubeconfig=../bootstrap-kubelet.conf

# bootstrap set context
$ kubectl config set-context tls-bootstrap-token-user@kubernetes \
--cluster=kubernetes \
--user=tls-bootstrap-token-user \
--kubeconfig=../bootstrap-kubelet.conf

# bootstrap use default context
$ kubectl config use-context tls-bootstrap-token-user@kubernetes \
--kubeconfig=../bootstrap-kubelet.conf

 

  • 若想要用手動簽署憑證來進行授權的話,可以參考Certificate

接着在k8s-m1建立TLS bootstrap secret來提供自動簽證使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ cat <<EOF | kubectl create -f -
apiVersion: v1
kind: Secret
metadata:
name: bootstrap-token-${TOKEN_ID}
namespace: kube-system
type: bootstrap.kubernetes.io/token
stringData:
token-id: ${TOKEN_ID}
token-secret: ${TOKEN_SECRET}
usage-bootstrap-authentication: "true"
usage-bootstrap-signing: "true"
auth-extra-groups: system:bootstrappers:default-node-token
EOF

secret "bootstrap-token-65a3a9" created

 

k8s-m1建立 TLS Bootstrap Autoapprove RBAC:

1
2
3
4
$ kubectl apply -f "${CORE_URL}/kubelet-bootstrap-rbac.yml.conf"
clusterrolebinding.rbac.authorization.k8s.io "kubelet-bootstrap" created
clusterrolebinding.rbac.authorization.k8s.io "node-autoapprove-bootstrap" created
clusterrolebinding.rbac.authorization.k8s.io "node-autoapprove-certificate-rotation" created

 

Kubernetes Nodes
本部分將說明如何建立與設定Kubernetes Node 角色,Node 是主要執行容器實例(Pod)的工作節點。
在開始部署前,先在k8-m1將需要用到的文件復制到所有node節點上:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ cd /etc/kubernetes/pki
$ for NODE in k8s-n1 k8s-n2 k8s-n3; do
echo "--- $NODE ---"
ssh ${NODE} "mkdir -p /etc/kubernetes/pki/"
ssh ${NODE} "mkdir -p /etc/etcd/ssl"
# Etcd
for FILE in etcd-ca.pem etcd.pem etcd-key.pem; do
scp /etc/etcd/ssl/${FILE} ${NODE}:/etc/etcd/ssl/${FILE}
done
# Kubernetes
for FILE in pki/ca.pem pki/ca-key.pem bootstrap-kubelet.conf; do
scp /etc/kubernetes/${FILE} ${NODE}:/etc/kubernetes/${FILE}
done
done

 

部署與設定
在每台node節點下載kubelet.service相關文件來管理kubelet:

1
2
3
4
$ export CORE_URL="https://kairen.github.io/files/manual-v1.10/node"
$ mkdir -p /etc/systemd/system/kubelet.service.d
$ wget "${CORE_URL}/kubelet.service" -O /lib/systemd/system/kubelet.service
$ wget "${CORE_URL}/10-kubelet.conf" -O /etc/systemd/system/kubelet.service.d/10-kubelet.conf

 

  • cluster dnsdomain有改變的話,需要修改10-kubelet.conf

最后每台node節點建立var 存放資訊,然后啟動kubelet 服務:

1
2
$ mkdir -p /var/lib/kubelet /var/log/kubernetes
$ systemctl enable kubelet.service && systemctl start kubelet.service

 

驗證集群
完成后,在任意一台master節點並通過簡單指令驗證:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ kubectl get csr
NAME AGE REQUESTOR CONDITION
csr-bvz9l 11m system:node:k8s-m1 Approved,Issued
csr-jwr8k 11m system:node:k8s-m2 Approved,Issued
csr-q867w 11m system:node:k8s-m3 Approved,Issued
node-csr-Y-FGvxZWJqI-8RIK_IrpgdsvjGQVGW0E4UJOuaU8ogk 17s system:bootstrap:dca3e1 Approved,Issued
node-csr-cnX9T1xp1LdxVDc9QW43W0pYkhEigjwgceRshKuI82c 19s system:bootstrap:dca3e1 Approved,Issued
node-csr-m7SBA9RAGCnsgYWJB-u2HoB2qLSfiQZeAxWFI2WYN7Y 18s system:bootstrap:dca3e1 Approved,Issued

$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-m1 NotReady master 12m v1.10.0
k8s-m2 NotReady master 11m v1.10.0
k8s-m3 NotReady master 11m v1.10.0
k8s-n1 NotReady node 32s v1.10.0
k8s-n2 NotReady node 31s v1.10.0
k8s-n3 NotReady node 29s v1.10.0

 

Kubernetes Core Addons部署
當完成上面所有步驟后,接着需要部署一些插件,其中如Kubernetes DNSKubernetes Proxy等這種Addons是非常重要的。
Kubernetes Proxy
Kube-proxy是實現Service的關鍵插件,kube-proxy會在每台節點上執行,然后監聽API Server的Service與Endpoint資源物件的改變,然后來依據變化執行iptables來實現網路的轉發。這邊我們會需要建議一個DaemonSet來執行,並且建立一些需要的Certificates。

k8s-m1下載kube-proxy.yml來建立Kubernetes Proxy Addon:

1
2
3
4
5
6
7
8
9
10
11
12
$ kubectl apply -f "https://kairen.github.io/files/manual-v1.10/addon/kube-proxy.yml.conf"
serviceaccount "kube-proxy" created
clusterrolebinding.rbac.authorization.k8s.io "system:kube-proxy" created
configmap "kube-proxy" created
daemonset.apps "kube-proxy" created

$ kubectl -n kube-system get po -o wide -l k8s-app=kube-proxy
NAME READY STATUS RESTARTS AGE IP NODE
kube-proxy-8j5w8 1/1 Running 0 29s 192.16.35.16 k8s-n3
kube-proxy-c4zvt 1/1 Running 0 29s 192.16.35.11 k8s-m1
kube-proxy-clpl6 1/1 Running 0 29s 192.16.35.12 k8s-m2
...

 

Kubernetes DNS
Kube DNS是Kubernetes集群內部Pod之間互相溝通的重要Addon,它允許Pod可以通過Domain Name方式來連接Service,其主要由Kube DNS與Sky DNS組合而成,通過Kube DNS監聽Service與Endpoint變化,來提供給Sky DNS資訊,已更新解析位址。

k8s-m1下載kube-dns.yml來建立Kubernetes Proxy Addon:

1
2
3
4
5
6
7
8
$ kubectl apply -f "https://kairen.github.io/files/manual-v1.10/addon/kube-dns.yml.conf"
serviceaccount "kube-dns" created
service "kube-dns" created
deployment.extensions "kube-dns" created

$ kubectl -n kube-system get po -l k8s-app=kube-dns
NAME READY STATUS RESTARTS AGE
kube-dns-654684d656-zq5t8 0/3 Pending 0 1m

 

這邊會發現處於Pending狀態,是由於Kubernetes Pod Network還未建立完成,因此所有節點會處於NotReady狀態,而造成Pod無法被排程分配到指定節點上啟動,由於為了解決該問題,下節將說明如何建立Pod Network。

Calico Network 安裝與設定
Calico 是一款純3層的資料中心網路方案(不需要Overlay 網路),Calico 好處是它整合了各種雲原生平台,且Calico 在每一個節點利用Linux Kernel 實現高效的vRouter 來負責資料的轉發,而當資料中心復雜度增加時,可以用BGP route reflector 來達成。

  • 本次不采用手動方式來建立Calico網路,若想了解可以參考Integration Guide

k8s-m1下載calico.yaml來建立Calico Network:(yaml里的interface網卡名記得改成和宿主機網卡名一致)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ kubectl apply -f "https://kairen.github.io/files/manual-v1.10/network/calico.yml.conf"
configmap "calico-config" created
daemonset "calico-node" created
deployment "calico-kube-controllers" created
clusterrolebinding "calico-cni-plugin" created
clusterrole "calico-cni-plugin" created
serviceaccount "calico-cni-plugin" created
clusterrolebinding "calico-kube-controllers" created
clusterrole "calico-kube-controllers" created
serviceaccount "calico-kube-controllers" created

$ kubectl -n kube-system get po -l k8s-app=calico-node -o wide
NAME READY STATUS RESTARTS AGE IP NODE
calico-node-22mbb 2/2 Running 0 1m 192.16.35.12 k8s-m2
calico-node-2qwf5 2/2 Running 0 1m 192.16.35.11 k8s-m1
calico-node-g2sp8 2/2 Running 0 1m 192.16.35.13 k8s-m3
calico-node-hghp4 2/2 Running 0 1m 192.16.35.14 k8s-n1
calico-node-qp6gf 2/2 Running 0 1m 192.16.35.15 k8s-n2
calico-node-zfx4n 2/2 Running 0 1m 192.16.35.16 k8s-n3

 

  • 這邊若節點IP與網卡不同的話,請修改calico.yml文件。

k8s-m1下載Calico CLI來查看Calico nodes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ wget https://github.com/projectcalico/calicoctl/releases/download/v3.1.0/calicoctl -O /usr/local/bin/calicoctl
$ chmod u+x /usr/local/bin/calicoctl
$ cat <<EOF > ~/calico-rc
export ETCD_ENDPOINTS="https://192.16.35.11:2379,https://192.16.35.12:2379,https://192.16.35.13: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 node status
Calico process is running.

IPv4 BGP status
+--------------+-------------------+-------+----------+-------------+
| PEER ADDRESS | PEER TYPE | STATE | SINCE | INFO |
+--------------+-------------------+-------+----------+-------------+
| 192.16.35.12 | node-to-node mesh | up | 04:42:37 | Established |
| 192.16.35.13 | node-to-node mesh | up | 04:42:42 | Established |
| 192.16.35.14 | node-to-node mesh | up | 04:42:37 | Established |
| 192.16.35.15 | node-to-node mesh | up | 04:42:41 | Established |
| 192.16.35.16 | node-to-node mesh | up | 04:42:36 | Established |
+--------------+-------------------+-------+----------+-------------+
...

 

查看pending 的pod 是否已執行:

1
2
3
4
$ kubectl -n kube-system get po -l k8s-app=kube-dns
kubectl -n kube-system get po -l k8s-app=kube-dns
NAME READY STATUS RESTARTS AGE
kube-dns-654684d656-j8xzx 3/3 Running 0 10m

 

Gubernets Extra Addons部署
本節說明如何部署一些官方常用的Addons,如Dashboard、Heapster 等。

Dashboard
Dashboard是Kubernetes社區官方開發的儀表板,有了儀表板后管理者就能夠通過Web-based方式來管理Kubernetes集群,除了提升管理方便,也讓資源視覺化,讓人更直覺看見系統資訊的呈現結果。

k8s-m1通過kubectl來建立kubernetes dashboard即可:

1
2
3
4
5
6
7
$ 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
kubernetes-dashboard-7d5dcdb6d9-j492l 1/1 Running 0 12s

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes-dashboard ClusterIP 10.111.22.111 <none> 443/TCP 12s

 

這邊會額外建立一個名稱為open-api Cluster Role Binding,這僅作為方便測試時使用,在一般情況下不要開啟,不然就會直接被存取所有API:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ cat <<EOF | kubectl create -f -
apiVersion: rbac.authorization.k8s.io/v1
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。

完成后,就可以通過瀏覽器存取Dashboard https://192.16.35.10:6443/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/
在 1.7 版本以後的 Dashboard 將不再提供所有權限,因此需要建立一個 service account 來綁定 cluster-admin role:

1
2
3
4
$ kubectl -n kube-system create sa dashboard
$ kubectl create clusterrolebinding dashboard --clusterrole cluster-admin --serviceaccount=kube-system:dashboard
$ kubectl -n kube-system describe secrets | sed -rn '/\sdashboard-token-/,/^token/{/^token/s#\S+\s+##p}'
eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkYXNoYm9hcmQtdG9rZW4tdzVocmgiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGFzaGJvYXJkIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiYWJmMTFjYzMtZjRlYi0xMWU3LTgzYWUtMDgwMDI3NjdkOWI5Iiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmRhc2hib2FyZCJ9.Xuyq34ci7Mk8bI97o4IldDyKySOOqRXRsxVWIJkPNiVUxKT4wpQZtikNJe2mfUBBD-JvoXTzwqyeSSTsAy2CiKQhekW8QgPLYelkBPBibySjBhJpiCD38J1u7yru4P0Pww2ZQJDjIxY4vqT46ywBklReGVqY3ogtUQg-eXueBmz-o7lJYMjw8L14692OJuhBjzTRSaKW8U2MPluBVnD7M2SOekDff7KpSxgOwXHsLVQoMrVNbspUCvtIiEI1EiXkyCNRGwfnd2my3uzUABIHFhm0_RZSmGwExPbxflr8Fc6bxmuz-_jSdOtUidYkFIzvEWw2vRovPgs3MXTv59RwUw

 

  • 復制token,然后貼到Kubernetes dashboard。注意這邊一般來說要針對不同User開啟特定存取權限。

cmd-markdown-logo

Heapster
Heapster是Kubernetes社區維護的容器集群監控與效能分析工具。Heapster會從Kubernetes apiserver取得所有Node資訊,然后再通過這些Node來取得kubelet上的資料,最后再將所有收集到資料送到Heapster的后台儲存InfluxDB,最后利用Grafana來抓取InfluxDB的資料源來進行視覺化。

k8s-m1通過kubectl來建立kubernetes monitor即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ kubectl apply -f "https://kairen.github.io/files/manual-v1.10/addon/kube-monitor.yml.conf"
$ 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
···

 

完成后,就可以通過瀏覽器存取Grafana Dashboard https://192.16.35.10:6443/api/v1/namespaces/kube-system/services/monitoring-grafana/proxy/

cmd-markdown-logo

Ingress
Ingress是利用Nginx或HAProxy等負載平衡器來暴露集群內服務的元件,Ingress主要通過設定Ingress規格來定義Domain Name映射Kubernetes內部Service,這種方式可以避免掉使用過多的NodePort問題。

k8s-m1通過kubectl來建立Ingress Controller即可:

1
2
3
4
5
6
$ kubectl create ns ingress-nginx
$ kubectl apply -f "https://kairen.github.io/files/manual-v1.10/addon/ingress-controller.yml.conf"
$ kubectl -n ingress-nginx get po
NAME READY STATUS RESTARTS AGE
default-http-backend-5c6d95c48-rzxfb 1/1 Running 0 7m
nginx-ingress-controller-699cdf846-982n4 1/1 Running 0 7m

 

  • 這里也可以選擇Traefik 的Ingress Controller。

測試Ingress 功能
這邊先建立一個Nginx HTTP server Deployment 與Service:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ kubectl run nginx-dp --image nginx --port 80
$ kubectl expose deploy nginx-dp --port 80
$ kubectl get po,svc
$ cat <<EOF | kubectl create -f -
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: test-nginx-ingress
annotations:
ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: test.nginx.com
http:
paths:
- path: /
backend:
serviceName: nginx-dp
servicePort: 80
EOF

 

通過curl 來進行測試:

1
2
3
4
5
6
7
8
9
10
$ curl 192.16.35.10 -H 'Host: test.nginx.com'
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...

# 測試其他 domain name 是否會回傳 404
$ curl 192.16.35.10 -H 'Host: test.nginx.com1'
default backend - 404

 

Helm Tiller Server
Helm是Kubernetes Chart的管理工具,Kubernetes Chart是一套預先組態的Kubernetes資源套件。其中Tiller Server主要負責接收來至Client的指令,並通過kube-apiserver與Kubernetes集群做溝通,根據Chart定義的內容,來產生與管理各種對應API物件的Kubernetes部署文件(又稱為Release)。

首先在k8s-m1安裝Helm tool:

1
2
$ wget -qO- https://kubernetes-helm.storage.googleapis.com/helm-v2.8.1-linux-amd64.tar.gz | tar -zx
$ sudo mv linux-amd64/helm /usr/local/bin/

 

另外在所有node機器安裝 socat:

1
$ sudo apt-get install -y socat

 

接着初始化 Helm(這邊會安裝 Tiller Server):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ kubectl -n kube-system create sa tiller
$ kubectl create clusterrolebinding tiller --clusterrole cluster-admin --serviceaccount=kube-system:tiller
$ helm init --service-account tiller
...
Tiller (the Helm server-side component) has been installed into your Kubernetes Cluster.
Happy Helming!

$ kubectl -n kube-system get po -l app=helm
NAME READY STATUS RESTARTS AGE
tiller-deploy-5f789bd9f7-tzss6 1/1 Running 0 29s

$ helm version
Client: &version.Version{SemVer:"v2.8.1", GitCommit:"6af75a8fd72e2aa18a2b278cfe5c7a1c5feca7f2", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.8.1", GitCommit:"6af75a8fd72e2aa18a2b278cfe5c7a1c5feca7f2", GitTreeState:"clean"}

 

測試Helm 功能
這邊部署簡單Jenkins 來進行功能測試:

1
2
3
4
5
6
7
8
9
10
11
12
$ helm install --name demo --set Persistence.Enabled=false stable/jenkins
$ kubectl get po,svc -l app=demo-jenkins
NAME READY STATUS RESTARTS AGE
demo-jenkins-7bf4bfcff-q74nt 1/1 Running 0 2m

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
demo-jenkins LoadBalancer 10.103.15.129 <pending> 8080:31161/TCP 2m
demo-jenkins-agent ClusterIP 10.103.160.126 <none> 50000/TCP 2m

# 取得 admin 帳號的密碼
$ printf $(kubectl get secret --namespace default demo-jenkins -o jsonpath="{.data.jenkins-admin-password}" | base64 --decode);echo
r6y9FMuF2u

 

完成后,就可以通過瀏覽器存取Jenkins Web http://192.16.35.10:31161

cmd-markdown-logo
測試完成后,即可刪除:

1
2
3
4
5
6
$ helm ls
NAME REVISION UPDATED STATUS CHART NAMESPACE
demo 1 Tue Apr 10 07:29:51 2018 DEPLOYED jenkins-0.14.4 default

$ helm delete demo --purge
release "demo" deleted

 

更多Helm Apps可以到Kubeapps Hub尋找。
測試集群
SSH進入k8s-m1節點,然后關閉該節點:

1
$ sudo poweroff

 

接着進入到k8s-m2節點,通過kubectl來檢查集群是否能夠正常執行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 先檢查 etcd 狀態,可以發現 etcd-0 因為關機而中斷
$ 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 Unhealthy Get https://192.16.35.11:2379/health: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)

# 測試是否可以建立 Pod
$ kubectl run nginx --image nginx --restart=Never --port 80
$ kubectl get po
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 22s

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM