部署Kubernetes Cluster

环境设置

设置三台机器的主机名:  
Master上执行:

hostnamectl --static set-hostname  k8s-master

Node1上执行:

hostnamectl --static set-hostname  k8s-node1

Node2上执行:

hostnamectl --static set-hostname  k8s-node2

在三台机器上设置hosts,均执行如下命令:

echo '10.0.251.148    k8s-master
10.0.251.148   etcd
10.0.251.148   registry
10.0.251.153   k8s-node-1
10.0.251.155    k8s-node-2' >> /etc/hosts

还可以设置免密

安装docker

所有节点都需要安装Docker

apt-get update && apt-get install docker.io

安装kubelet、kubeadm和kubectl

在所有节点上安装

  • kubelet:运行在所有节点上,负责启动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 update
apt-get install -y kubelet kubeadm kubectl

用 kubeadm创建Cluster

初始化Master

kubeadm init --apiserver-advertise-address 192.168.24.136
--pod-network-cidr=10.244.0.0/16
  • –apiserver-advertise-address指明用Master的哪个interface与Cluster的其他节点通信。
  • –pod-network-cidr指定Pod网络的范围。

执行的时候可能会报错:

root@k8s-master:~# kubeadm init --apiserver-advertise-address 192.168.24.136 --pod-network-cidr=10.244.0.0/16
[init] Using Kubernetes version: v1.14.3
[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/
error execution phase preflight: [preflight] Some fatal errors occurred:
	[ERROR Swap]: running with swap on is not supported. Please disable swap
[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`

这个时候需要关闭swap分区,执行 swapoff -a 就行。

初始化过程:

root@k8s-master:~# kubeadm init --apiserver-advertise-address 192.168.24.136 --pod-network-cidr=10.244.0.0/16
[init] Using Kubernetes version: v1.14.3
[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 "etcd/ca" certificate and key
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.24.136 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.24.136 127.0.0.1 ::1]
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[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.24.136]
[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 21.003409 seconds
[upload-config] storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.14" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --experimental-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: zsvosn.h7f9ewmxwytbvygx
[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.24.136:6443 --token zsvosn.h7f9ewmxwytbvygx \
    --discovery-token-ca-cert-hash sha256:f05e0657e534eae317daa5337153e70f390083ec382c059a82d542e8c05d5bc1 
root@k8s-master:~# 

(1)kubeadm执行初始化前的检查。
(2)生成token和证书。
(3)生成KubeConfig文件,kubelet需要用这个文件与Master通信。
(4)安装Master组件,会从Google的Registry下载组件的Docker镜像。这一步可能会花一些时间,主要取决于网络质量。
(5)安装附加组件kube-proxy和kube-dns。
(6)Kubernetes Master初始化成功。
(7)提示如何配置kubectl,后面会实践。
(8)提示如何安装Pod网络,后面会实践。
(9)提示如何注册其他节点到Cluster,后面会实践。

配置kubectl

在master上执行
kubectl是kubernetes cluster的命令行工具,前面已经在所有节点安装了kubectl,master初始化之后还需要做一些配置工作才能使用kubectl。
我这里使用的是root账号:

mkdir -p ~/.kube
cp -i /etc/kubernetes/admin.conf ~/.kube/config
chown $(id -u):$(id -g) ~/.kube/config

为了使用更便捷,启用kubectl命令的自动补全功能:

echo "source <(kubectl completion bash)" >> ~/.bashrc

安装Pod网络

在master上执行
要让kubernetes cluster能够工作,必须要安装Pod网络,否则Pod间无法通信,这里的方案是使用flannel。

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

添加k8s-node1和k8s-node2

在node节点上分别执行如下命令,将其注册到Cluster中:

kubeadm join 192.168.24.136:6443 --token zsvosn.h7f9ewmxwytbvygx \
 --discovery-token-ca-cert-hash sha256:f05e0657e534eae317daa5337153e70f390083ec382c059a82d542e8c05d5bc1 

如果token当时没有记录下来,可以通过 kubeadm token list 查看。

最后我们可以通过 kubectl get nodes 查看节点的状态。

root@k8s-master:~# kubectl get nodes
NAME         STATUS   ROLES    AGE     VERSION
k8s-master   Ready    master   23m     v1.14.3
k8s-node1    Ready    <none>   2m56s   v1.14.3
root@k8s-master:~# 

所有节点都是ready状态,kubernetes集群搭建完成。

猜你喜欢

转载自blog.csdn.net/cbmljs/article/details/91363545