记录一下从零安装docker,并使用kubeadm初始化kubernets集群。
准备工作
由于完全是新创建的ECS实例,所以有些很基础的准备工作:
- 创建新用户
useradd -m -G wheel docker
passwd docker
- 添加ssh密钥登陆
ssh docker@aliyun2
- 禁用ssh密码登陆
sudo vi /etc/ssh/sshd_config
# /etc/ssh/sshd_config
...
PermitRootLogin no
PasswordAuthentication no
sudo service sshd restart
安装Docker
参照Docker CE 镜像源站安装docker
# step 1: 安装必要的一些系统工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3: 更新并安装 Docker-CE
sudo yum makecache fast
sudo yum -y install docker-ce
# Step 4: 开启Docker服务
systemctl start docker && systemctl enable docker
阿里云镜像加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["您的专属加速器地址"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
安装kubeadm、kubelet、kubectl
sudo bash -c 'cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF'
sudo setenforce 0
sudo yum install -y kubelet kubeadm kubectl
systemctl enable kubelet && systemctl start kubelet
检查一下cgroup-driver
docker info | grep -i cgroup
cat /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
我所看到的结果是docker使用cgroupfs,而kubelet使用的是systemd。将kubelet的cgroup-driver设为cgroupfs
sudo sed -i "s/cgroup-driver=systemd/cgroup-driver=cgroupfs/g" /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
systemctl daemon-reload
systemctl restart kubelet
初始化master
准备了3台主机来加入k8s集群。为了方便,修改了host文件来映射hostname。3台主机分别为master, node1, node2
,其中node2是刚刚准备好的ECS。
切换到master
sudo kubeadm init --kubernetes-version=1.10.1 --apiserver-advertise-address=<ip> --apiserver-cert-extra-sans=<extra-ip> --pod-network-cidr=192.168.0.0/16
- 因为有的节点不是阿里云的,所以apiserver-advertise-address直接使用公网IP
- 不指定kubernetes-version的话, 会请求国外地址
- 这一步会拉取k8s.gcr.io/etcd-amd64等镜像,国内无法直接拉取。我选择用docker tag本地镜像来解决
- apiserver-cert-extra-sans
- pod-network-cidr
配置Calico网络
kubectl apply -f https://docs.projectcalico.org/v3.1/getting-started/kubernetes/installation/hosted/kubeadm/1.7/calico.yaml
# 取消master隔离限制
kubectl taint nodes --all node-role.kubernetes.io/master-
在后续使用中,若遇到Unable to connect to the server: x509: certificate signed by unknown authority (possibly because of "crypto/rsa: verification error" while trying to verify candidate authority certificate "kubernetes")
,在master节点执行以下命令:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
加入节点
切换到node01, node02
kubeadm join <ip>:6443 --token <token> --discovery-token-ca-cert-hash <hash>
master节点上
kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master NotReady master 8m v1.10.1
k8s-node01 Ready <none> 5m v1.10.1
k8s-node02 Ready <none> 6m v1.10.1
在集群外的电脑上使用kubectl
scp root@<master ip>:/etc/kubernetes/admin.conf .
kubectl --kubeconfig ./admin.conf get nodes
网友评论