K8s 管理平台使用者越来越多,要搞清楚其特性,最好来实战安装一下
一.准备工作
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y kubelet kubeadm kubectl
apt-get update && apt-get install -y apt-transport-https
CentOS / RHEL / Fedora
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetesbaseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
setenforce 0
yum install -y kubelet kubeadm kubectl
systemctl enable kubelet && systemctl start kubelet
二.配置master节点
网络上有很多都是手工安装各个组件 etcd flannel
我们按照最简单的方式,使用kubeadm来执行安装,即按照官方文档的方法来操作
https://kubernetes.io/docs/setup/independent/create-cluster-kubeadm/
所有管理组件采用DaemonSet Pod来安装
vi ~/kubeadm.conf
apiVersion: kubeadm.k8s.io/v1alpha1
kind: MasterConfiguration
api:
advertiseAddress: 0.0.0.0
networking:
podSubnet: 10.244.0.0/16
etcd:
image: registry.cn-hangzhou.aliyuncs.com/google_containers/etcd-amd64:3.1.11
kubernetesVersion: v1.10.3
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
执行操作命令
Kubeadm init —config /root/kubeadm.conf
2.安装kube-flannel
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.10.0/Documentation/kube-flannel.yml
三.配置worker节点
1.将worker节点加入master
kubeadm join 192.168.0.98:6443 --token catsr2.vxxxx --discovery-token-ca-cert-hash sha256:xxxxxx
这些信息来自master节点执行kubeadm init 完成后输出的结果
如果没有记录,可以在master节点中执行如下命令获取
kubeadm token create --print-join-command
2.安装kube-flannel
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.10.0/Documentation/kube-flannel.yml
三.配置worker节点
1.将worker节点加入master
kubeadm join 192.168.0.98:6443 --token catsr2.vxxxx --discovery-token-ca-cert-hash sha256:xxxxxx
这些信息来自master节点执行kubeadm init 完成后输出的结果
如果没有记录,可以在master节点中执行如下命令获取
kubeadm token create --print-join-command
api:
advertiseAddress: 0.0.0.0
networking:
podSubnet: 10.244.0.0/16
etcd:
image: registry.cn-hangzhou.aliyuncs.com/google_containers/etcd-amd64:3.1.11
kubernetesVersion: v1.10.3
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
kind: MasterConfiguration
api:
advertiseAddress: 0.0.0.0
networking:
podSubnet: 10.244.0.0/16
etcd:
image: registry.cn-hangzhou.aliyuncs.com/google_containers/etcd-amd64:3.1.11
kubernetesVersion: v1.10.3
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
执行操作命令
Kubeadm init —config /root/kubeadm.conf
2.安装kube-flannel
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.10.0/Documentation/kube-flannel.yml
三.配置worker节点
1.将worker节点加入master
kubeadm join 192.168.0.98:6443 --token catsr2.vxxxx --discovery-token-ca-cert-hash sha256:xxxxxx
这些信息来自master节点执行kubeadm init 完成后输出的结果
如果没有记录,可以在master节点中执行如下命令获取
kubeadm token create --print-join-command
2.master向worker 分发kube-proxy和flannel pod 直接去拉取 k8s.gcr.io上的镜像是不成功的
所以需要在worker节点上 先下载阿里云的镜像源中pause-amd,然后再做个tag
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64:3.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64:3.1 k8s.gcr.io/google_containers/pause-amd64:3.1
最终完成结果如图所示
四.Troubleshooting
1.无法执行kubectl操作,可以按照执行如下命令之一
export KUBECONFIG=/etc/kubernetes/kubelet.conf
alias kubectl='kubectl --kubeconfig=/etc/kubernetes/kubelet.conf
阿里云容器服务可以直接通过web方式一键安装,更省心省力
https://yq.aliyun.com/articles/73922
https://www.aliyun.com/product/kubernetes