Kubernetes 安装
安装docker
sudo apt install vim -y
sudo apt install curl -y
curl -fsSL get.docker.com -o get-docker.sh
sudo sh get-docker.sh --mirror Aliyun
sudo systemctl enable docker
sudo systemctl start docker
sudo groupadd docker
sudo usermod -aG docker $USER
sudo usermod -aG docker <你的用户名>
docker使用镜像
vim /etc/docker/daemon.json
修改成
{
"registry-mirrors": [
"https://registry.docker-cn.com"
]
}
sudo systemctl daemon-reload
sudo systemctl restart docker
关闭交换
sudo swapoff -a
永久关闭交换
vim /etc/fstab
UUID=42cf9d46-ae0d-488d-9e3a-b6f78708bea1 / ext4 errors=remount-ro 0 1
#/swapfile none swap sw 0 0
修改后重启
安装kubelet kubeadm kubectl
apt-get update && apt-get install -y apt-transport-https curl
cat <<EOF > /etc/apt/sources.list.d/kubernetes.list
deb http://mirrors.ustc.edu.cn/kubernetes/apt kubernetes-xenial main
EOF
apt-get update
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 6A030B21BA07F4FB
apt-get update
apt-get install -y kubelet kubeadm kubectl
apt-mark hold kubelet kubeadm kubectl
这里可能会出一点问题,最开始中科大的源是可以用的,不过这几天用不了了,所以换成阿里云的源可以试试
rm /etc/apt/sources.list.d/kubernetes.list
apt-get update && apt-get install -y apt-transport-https curl
cat <<EOF > /etc/apt/sources.list.d/kubernetes.list
deb http://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial main
EOF
apt-get update
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 6A030B21BA07F4FB
apt-get update
apt-get install -y kubelet kubeadm kubectl
apt-mark hold kubelet kubeadm kubectl
sudo vim /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
在末尾添加
Environment="KUBELET_CGROUP_ARGS=--cgroup-driver=cgroupfs"
systemctl daemon-reload
systemctl restart kubelet
关闭防火墙
vim /etc/sysctl.conf
在末尾添加
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
下载需要的镜像并修改标签
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.15.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.15.1 k8s.gcr.io/kube-apiserver:v1.15.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.15.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.15.1 k8s.gcr.io/kube-controller-manager:v1.15.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.15.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.15.1 k8s.gcr.io/kube-scheduler:v1.15.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.15.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.15.1 k8s.gcr.io/kube-proxy:v1.15.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1 k8s.gcr.io/pause:3.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.3.10
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.3.10 k8s.gcr.io/etcd:3.3.10
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.3.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.3.1 k8s.gcr.io/coredns:1.3.1
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/pause-amd64:3.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/ k8s.gcr.io/
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kubernetes-dashboard-amd64:v1.10.0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kubernetes-dashboard-amd64:v1.10.0 k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.0
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/heapster-amd64:v1.5.4
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/heapster-amd64:v1.5.4 k8s.gcr.io/heapster-amd64:v1.5.4
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/heapster-grafana-amd64:v5.0.4
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/heapster-grafana-amd64:v5.0.4 k8s.gcr.io/heapster-grafana-amd64:v5.0.4
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/heapster-influxdb-amd64:v1.5.2
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/heapster-influxdb-amd64:v1.5.2 k8s.gcr.io/heapster-influxdb-amd64:v1.5.2
以上所有步骤在所有master和slave机器上完成。
master节点步骤
完成上面步骤后
sudo kubeadm init --kubernetes-version=v1.15.1 --pod-network-cidr=192.168.0.0/16
启动
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
初始化master节点
拉取网络插件calico镜像
docker pull quay.io/calico/node:v3.1.3
docker pull quay.io/calico/cni:v3.1.3
docker pull quay.io/calico/typha:v0.7.4
配置网络插件
kubectl apply -f https://docs.projectcalico.org/v3.1/getting-started/kubernetes/installation/hosted/rbac-kdd.yaml
kubectl apply -f https://docs.projectcalico.org/v3.1/getting-started/kubernetes/installation/hosted/kubernetes-datastore/calico-networking/1.7/calico.yaml
配置完成后可能ssh就断开了,因为calico的网段也是192.168.0.0。暂时没有什么很好的解决方案,但是通过连在同一个交换机上的电脑仍然可以ssh连接或者直接操作master节点。所以我用笔记本的有线网络连接集群,无线网络连接控制电脑来控制集群
打印加入集群的命令
kubeadm token create --print-join-command
kubeadm join 192.168.3.51:6443 --token loi96z.64to8ymreeus3bi1 --discovery-token-ca-cert-hash sha256:fceb3233a84491d8ed9b5937869e92d236153fcab3f08d01356da01aea9ce916
kubeadm join 192.168.3.51:6443 --token u2detf.qlouwrt8ee9ch2q5 --discovery-token-ca-cert-hash sha256:b035c8325964ae4ffcf25140a5f2ac89042b844b4c1c5084748c5dff5f3e2d07
slave节点
完成初始化步骤后,通过master节点打印出的加入集群命令加入集群
例如
kubeadm join 192.168.3.51:6443 --token yrfk5v.o7jmxj03lopu5042 --discovery-token-ca-cert-hash sha256:b035c8325964ae4ffcf25140a5f2ac89042b844b4c1c5084748c5dff5f3e2d07