前置学习Docker知识:https://www.jianshu.com/c/961f67f48e81
官方文档:https://kubernetes.io/docs/setup/independent/install-kubeadm/
使用kubeadmin安装
1.硬件配置:
#2核cpu及以上,8G内存及以上 详情参考:https://www.kubernetes.org.cn/doc-16
k8s-master1:192.168.199.130
k8s-node1: 192.168.199.131
k8s-node2: 192.168.199.132
####配置master与node免密
#k8s-node1,k8s-node2执行:
mkdir -p ~/.ssh
#k8s-master1执行:
ssh-keygen
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
scp ~/.ssh/authorized_keys k8s-node1:~/.ssh/
scp ~/.ssh/authorized_keys k8s-node2:~/.ssh/
2.所有机器需要执行的初始化操作
#所有机器执行命令关闭屏保,避免卡顿
setterm -blank 0
#关闭selinux及防火墙,重启机器生效
setenforce 0
vim /etc/selinux/config
修改为
SELINUX=disabled
systemctl stop firewalld
systemctl disable firewalld
#配置hosts
vim /etc/hosts
192.168.199.130 k8s-master1
192.168.199.131 k8s-node1
192.168.199.132 k8s-node2
systemctl restart network #重启网络生效
#永久关闭swap,
swapoff -a
vim /etc/fstab #注释掉swap相关行即可
#配置aliyun yum源和k8s源
yum install -y wget
cd /etc/yum.repos.d/
tar -cvf bak.tgz *
rm -rf CentOS-*
wget http://mirrors.aliyun.com/repo/Centos-7.repo
mv Centos-7.repo CentOS-Base.repo
wget https://mirrors.aliyun.com/repo/epel-7.repo
cat <<EOF > /etc/yum.repos.d/k8s.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
yum clean all
yum makecache
yum update
#设置相关属性
cat <<EOF>/etc/sysctl.d/k8s.conf
vm.swappiness=0
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
#执行命令生效
modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf
#配置命令补全
yum install -y bash-completion
echo 'source <(kubectl completion bash)' >> /etc/profile
source /etc/profile
#安装docker 并配置阿里云镜像加速器
yum install -y docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://i70c3eqq.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker
systemctl enable docker
#提前下载k8s相关镜像
docker pull k8s.gcr.io/kube-proxy-amd64:v1.11.1
docker pull k8s.gcr.io/kube-apiserver-amd64:v1.11.1
docker pull k8s.gcr.io/kube-controller-manager-amd64:v1.11.1
docker pull k8s.gcr.io/kube-scheduler-amd64:v1.11.1
docker pull k8s.gcr.io/coredns:1.1.3
docker pull quay.io/calico/node:v3.0.7
docker pull gcr.io/google_containers/heapster-amd64:v1.5.3
docker pull quay.io/calico/kube-controllers:v2.0.4
docker pull quay.io/calico/cni:v2.0.5
docker pull k8s.gcr.io/etcd-amd64:3.2.18
docker pull k8s.gcr.io/kubernetes-dashboard-amd64:v1.8.3
docker pull k8s.gcr.io/pause:3.1
docker pull gcr.io/google_containers/heapster-influxdb-amd64:v1.3.3
docker pull gcr.io/google_containers/heapster-grafana-amd64:v4.4.3
docker pull quay.io/coreos/etcd:v3.1.10
#安装k8s相关软件
yum install -y kubernetes-cni-0.6.0
yum install -y kubeadm-1.11.1 kubectl-1.11.1 kubelet-1.11.1
#修改kubelet启动参数
cat <<EOF> /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS="--fail-swap-on=false --runtime-cgroups=/lib/systemd/system/kubelet.service --kubelet-cgroups=/lib/systemd/system/kubelet.service"
EOF
#启动服务
systemctl restart docker
systemctl restart kubelet
systemctl enable kubelet
#查看服务是否启动
systemctl is-active kubelet
k8s-master1上执行
#kubeadm 初始化
kubeadm init --kubernetes-version=v1.11.1 --pod-network-cidr=10.244.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
#安装flannel网络 ,详细信息:https://v1-11.docs.kubernetes.io/docs/setup/
#注意 kube-flannel.yml中的网段信息与kubeadm 初始化时要相同,记得修改
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.10.0/Documentation/kube-flannel.yml
k8s-node1,k8s-node2上执行
#master 主机上查看taken
[root@k8s-master1 /]# kubeadm token create --print-join-command
kubeadm join 192.168.199.130:6443 --token 56dm7f.qcswziskyrs6h2b0 --discovery-token-ca-cert-hash sha256:38328747d17b4bff37ce5f9982c41ecdb54892f41465be46cdc10ab743ecf83d
#k8s-node1,k8s-node2 上执行一下命令,将其加入k8s-master1
kubeadm join 192.168.199.130:6443 --token p8ymwx.flsbhmoep3giidlb --discovery-token-ca-cert-hash sha256:38328747d17b4bff37ce5f9982c41ecdb54892f41465be46cdc10ab743ecf83d
执行完以上命令
#master上查看节点
[root@k8s-master1 /]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master1 Ready master 1h v1.11.1
k8s-node1 Ready <none> 1m v1.11.1
k8s-node2 Ready <none> 1m v1.11.1
#查看集群状态
[root@k8s-master1 /]# kubectl get cs
NAME STATUS MESSAGE ERROR
controller-manager Healthy ok
scheduler Healthy ok
etcd-0 Healthy {"health": "true"}
重新部署环境
#master 和 node节点都执行以下命令即可初始化环境
kubeadm reset
ifconfig cni0 down
ip link delete cni0
ifconfig flannel.1 down
ip link delete flannel.1
rm -rf /var/lib/cni/
网友评论