前言
Kubernetes Cluster API 提供了一種聲明式的集羣創建、配置、管理模式,能夠對集羣的全生命週期進行有效管理(https://cluster-api.sigs.k8s.io/introduction.html)本文提供簡單試用記錄。
準備 Kubernetes 集羣(管理集羣)
節點 | 操作系統 | IP | Docker 版本 | kubernetes 版本 | 網絡 |
---|---|---|---|---|---|
node-01 | Ubuntu 18.04.1 LTS | 192.168.137.131 | 19.03.1 | v1.17.1 | Calico v3.7.3 |
node-02 | Ubuntu 18.04.1 LTS | 192.168.137.99 | 19.03.1 | v1.17.1 | Calico v3.7.3 |
管理集羣安裝命令筆記:
(node-01) # kubeadm init --pod-network-cidr=192.200.0.0/16
(node-01) # mkdir -p $HOME/.kube
(node-01) # sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
(node-01) # wget https://docs.projectcalico.org/v3.11/manifests/calico.yaml
(node-01) # sed -i "s/192.168/192.200/g" calico.yaml
(node-01) # kubectl apply -f calico.yaml
(node-02) # kubeadm join 192.168.137.131:6443 --token xxx --discovery-token-ca-cert-hash sha256:xxx
部署 Cluster API Controller
(node-01) # kubectl create -f https://github.com/kubernetes-sigs/cluster-api/releases/download/v0.2.9/cluster-api-components.yaml
部署 Bootstrap Provider (cluster-api-bootstrap-provider-kubeadm
)
通過 watch
和 process
KubeadmConfig
Cluster
Machine
對象,實現對集羣機器的配置
(node-01) # kubectl create -f https://github.com/kubernetes-sigs/cluster-api-bootstrap-provider-kubeadm/releases/download/v0.1.5/bootstrap-components.yaml
部署 Infrastructure Provider (cluster-api-provider-docker
)
通過 watch
和 process
DockerCluster
DockerMachine
Cluster
Machine
來供應基礎設施
(node-01) # kubectl create -f https://github.com/kubernetes-sigs/cluster-api-provider-docker/releases/download/v0.2.1/provider-components.yaml
創建一個單節點 Kubernetes 集羣
-
創建 Cluster 對象,指定後端使用 DockerCluster
準備部署文件 cluster.yaml
apiVersion: cluster.x-k8s.io/v1alpha2
kind: Cluster
metadata:
name: capi-quickstart
spec:
clusterNetwork:
pods:
cidrBlocks: ["192.200.0.0/16"]
infrastructureRef:
apiVersion: infrastructure.cluster.x-k8s.io/v1alpha2
kind: DockerCluster
name: capi-quickstart
---
apiVersion: infrastructure.cluster.x-k8s.io/v1alpha2
kind: DockerCluster
metadata:
name: capi-quickstart
(node-01) # kubectl apply -f cluster.yaml
-
創建 Machine 對象,指定使用 DockerMachine 供應機器,使用 KubeadmConfig 進行節點配置
準備部署文件 machine.yaml
apiVersion: cluster.x-k8s.io/v1alpha2
kind: Machine
metadata:
name: capi-quickstart-controlplane-0
labels:
cluster.x-k8s.io/control-plane: "true"
cluster.x-k8s.io/cluster-name: "capi-quickstart"
spec:
version: v1.15.3
bootstrap:
configRef:
apiVersion: bootstrap.cluster.x-k8s.io/v1alpha2
kind: KubeadmConfig
name: capi-quickstart-controlplane-0
infrastructureRef:
kind: DockerMachine
apiVersion: infrastructure.cluster.x-k8s.io/v1alpha2
name: capi-quickstart-controlplane-0
---
apiVersion: infrastructure.cluster.x-k8s.io/v1alpha2
kind: DockerMachine
metadata:
name: capi-quickstart-controlplane-0
---
apiVersion: bootstrap.cluster.x-k8s.io/v1alpha2
kind: KubeadmConfig
metadata:
name: capi-quickstart-controlplane-0
spec:
initConfiguration:
nodeRegistration:
kubeletExtraArgs:
# Default thresholds are higher to provide a buffer before resources
# are completely depleted, at the cost of requiring more total
# resources. These low thresholds allow running with fewer resources.
# Appropriate for testing or development only.
eviction-hard: nodefs.available<0%,nodefs.inodesFree<0%,imagefs.available<0%
clusterConfiguration:
networking:
serviceSubnet: "10.96.0.0/12"
podSubnet: "192.200.0.0/16"
dnsDomain: "cluster.local"
controllerManager:
extraArgs:
# Enables dynamic storage provisioning without a cloud provider.
# Appropriate for testing or development only.
enable-hostpath-provisioner: "true"
(node-01) # kubectl apply -f machine.yaml
- 查看集羣狀態
(node-01) # kubectl get cluster
NAME PHASE
capi-quickstart provisioned
(node-01) # kubectl get machine
NAME PROVIDERID PHASE
capi-quickstart-controlplane-0 docker:////capi-quickstart-capi-quickstart-controlplane-0 running
訪問新創建的集羣
獲取新集羣的 kubeconfig
文件
(node-01) # kubectl --namespace=default get secret/capi-quickstart-kubeconfig -o json \
| jq -r .data.value \
| base64 --decode \
> ./capi-quickstart.kubeconfig
注意:執行下面步驟時,需要到 capi-quickstart-controlplane-0
所在宿主機執行,實驗中是 node-02,因此需要提前將 capi-quickstart.kubeconfig 拷貝到對應主機:
(node-02) # kubectl --kubeconfig=./capi-quickstart.kubeconfig apply -f https://docs.projectcalico.org/v3.8/manifests/calico.yaml
(node-02) # kubectl --kubeconfig=./capi-quickstart.kubeconfig get node
NAME STATUS ROLES AGE VERSION
capi-quickstart-capi-quickstart-controlplane-0 Ready master 32m v1.15.3
新集羣的節點狀態已經 Ready
!
到這裏,我們已經能夠對新建集羣使用 kubectl 進行常規操作,後續再介紹集羣擴容等高級特性。