我們將部署三個節點的 Kubernetes Cluster。
k8s-master 是 Master,k8s-node1 和 k8s-node2 是 Node。
所有節點的操作系統均為 Ubuntu 16.04,當然其他 Linux 也是可以的。
官方安裝文檔可以參考 https://kubernetes.io/docs/setup/independent/install-kubeadm/
注意:Kubernetes 幾乎所有的安裝組件和 Docker 鏡像都放在 goolge 自己的網站上,這對國內的同學可能是個不小的障礙。建議是:網絡障礙都必須想辦法克服,不然連 Kubernetes 的門都進不了。
安裝 Docker
所有節點都需要安裝 Docker。
apt-get update && apt-get install docker.io
安裝 kubelet、kubeadm 和 kubectl
在所有節點上安裝 kubelet、kubeadm 和 kubectl。
kubelet 運行在 Cluster 所有節點上,負責啟動 Pod 和容器。
kubeadm 用於初始化 Cluster。
kubectl 是 Kubernetes 命令行工具。通過 kubectl 可以部署和管理應用,查看各種資源,創建、刪除和更新各種組件。
apt-get update && apt-get install -y apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF
apt-get updateapt-get install -y kubelet kubeadm kubectl
用 kubeadm 創建 Cluster
完整的官方文檔可以參考 https://kubernetes.io/docs/setup/independent/create-cluster-kubeadm/
初始化 Master
在 Master 上執行如下命令:
kubeadm init --apiserver-advertise-address 192.168.56.105 --pod-network-cidr=10.244.0.0/16
--apiserver-advertise-address
指明用 Master 的哪個 interface 與 Cluster 的其他節點通信。如果 Master 有多個 interface,建議明確指定,如果不指定,kubeadm 會自動選擇有默認網關的 interface。
--pod-network-cidr
指定 Pod 網絡的范圍。Kubernetes 支持多種網絡方案,而且不同網絡方案對 --pod-network-cidr
有自己的要求,這里設置為 10.244.0.0/16
是因為我們將使用 flannel 網絡方案,必須設置成這個 CIDR。在后面的實踐中我們會切換到其他網絡方案,比如 Canal。
初始化過程如下:
① kubeadm 執行初始化前的檢查。
② 生成 token 和證書。
③ 生成 KubeConfig 文件,kubelet 需要這個文件與 Master 通信。
④ 安裝 Master 組件,會從 goolge 的 Registry 下載組件的 Docker 鏡像,這一步可能會花一些時間,主要取決於網絡質量。
⑤ 安裝附加組件 kube-proxy 和 kube-dns。
⑥ Kubernetes Master 初始化成功。
⑦ 提示如何配置 kubectl,后面會實踐。
⑧ 提示如何安裝 Pod 網絡,后面會實踐。
⑨ 提示如何注冊其他節點到 Cluster,后面會實踐。
配置 kubectl
kubectl 是管理 Kubernetes Cluster 的命令行工具,前面我們已經在所有的節點安裝了 kubectl。Master 初始化完成后需要做一些配置工作,然后 kubectl 就能使用了。
依照 kubeadm init
輸出的第 ⑦ 步提示,推薦用 Linux 普通用戶執行 kubectl(root 會有一些問題)。
我們為 ubuntu 用戶配置 kubectl:
su - ubuntu
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
為了使用更便捷,啟用 kubectl 命令的自動補全功能。
echo "source <(kubectl completion bash)" >> ~/.bashrc
這樣 ubuntu 用戶就可以使用 kubectl 了。
下節我們將安裝 Pod 網絡並添加 k8s-node1 和 k8s-node2,完成集群部署。
CENTOS7.4部署k8s安裝
1:設置阿里源
cat <<EOF >
/
etc
/
yum.repos.d
/
kubernetes.repo
[kubernetes]
name
=
Kubernetes
baseurl
=
http:
/
/
mirrors.aliyun.com
/
kubernetes
/
yum
/
repos
/
kubernetes
-
el7
-
x86_64
enabled
=
1
gpgcheck
=
0
repo_gpgcheck
=
0
gpgkey
=
http:
/
/
mirrors.aliyun.com
/
kubernetes
/
yum
/
doc
/
yum
-
key.gpg
http:
/
/
mirrors.aliyun.com
/
kubernetes
/
yum
/
doc
/
rpm
-
package
-
key.gpg
EOF
2:設置selinux
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
3:安裝程序
yum install -y docker-ce kubelet kubeadm kubectl --disableexcludes=kubernetes
systemctl enable --now kubelet
systemctl enable docker.service
systemctl start docker.service
4:設置環境參數
vi /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
vm.swappiness=0
sysctl --system
5:加載組件
modprobe br_netfilter
6:關閉swap
vi /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS="--fail-swap-on=false"
7:kubeadm初始化
# kubeadm init --apiserver-advertise-address 192.168.169.31 --pod-network-cidr=10.244.0.0/16
W0624 18:24:52.059372 22813 version.go:98] could not fetch a Kubernetes version from the internet: unable to get URL "https://dl.k8s.io/release/stable-1.txt": Get https://dl.k8s.io/release/stable-1.txt: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
W0624 18:24:52.059515 22813 version.go:99] falling back to the local client version: v1.15.0
[init] Using Kubernetes version: v1.15.0
[preflight] Running pre-flight checks
[WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Activating the kubelet service
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [k8s-master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.169.31]
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.169.31 127.0.0.1 ::1]
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.169.31 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 16.003274 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.15" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node k8s-master as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node k8s-master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: zn5f6v.4wl5l93ar5an3v0d
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy
Your Kubernetes control-plane has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.169.31:6443 --token zn5f6v.4wl5l93ar5an3v0d \
--discovery-token-ca-cert-hash sha256:4b4ebc498106b4f96ba752a7773d1ea03f67da5af3b08302a7a55b76c3010ed3