kubeadm是官方社区推出的一个用于快速部署kubernetes集群的工具。
这个工具能通过两条指令完成一个kubernetes集群的部署:
$ kubeadm init
$ kubeadm join <Master节点的IP和端口 >
角色 | IP |
---|---|
k8s-master | 192.168.153.151 |
k8s-node1 | 192.168.153.152 |
k8s-node2 | 192.168.153.153 |
systemctl stop firewalld
systemctl disable firewalld
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
swapoff -a
sed -i 's/.*swap.*/#&/' /etc/fstab
hostnamectl set-hostname k8s-master
hostnamectl set-hostname k8s-node1
hostnamectl set-hostname k8s-node2
cat >> /etc/hosts << EOF
192.168.153.151 k8s-master
192.168.153.152 k8s-node1
192.168.153.153 k8s-node2
EOF
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
生效命令
sysctl --system
重启服务器
reboot
安装命令
yum install ntpdate -y
同步时间
ntpdate ntp.aliyun.com
Kubernetes默认CRI(容器运行时)为Docker,因此先安装Docker。
安装wget命令
yum -y install wget
获取阿里云的源
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum -y install docker-ce-18.06.1.ce-3.el7
systemctl enable docker && systemctl start docker
docker --version
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF
systemctl restart docker
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=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
由于版本更新频繁,这里指定版本号部署:
yum install -y kubelet-1.17.0 kubeadm-1.17.0 kubectl-1.17.0
将kubelet设为开机自启,先不启动
systemctl enable kubelet
在192.168.153.151 (Master)执行
kubeadm init \
--apiserver-advertise-address=192.168.153.151 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.17.0 \
--service-cidr=10.96.0.0/12 \
--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
安装官网的插件
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
安装失败时可以下载我上传的kube-flannel.yml 文件,其中镜像地址已经修改
下载链接>> https://download.csdn.net/download/m0_46674735/14930704
执行命令:kubectl apply -f kube-flannel.yaml
安装即可
[root@host-151 ~]# kubectl apply -f kube-flannel.yaml
podsecuritypolicy.policy/psp.flannel.unprivileged created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.apps/kube-flannel-ds-amd64 created
daemonset.apps/kube-flannel-ds-arm64 created
daemonset.apps/kube-flannel-ds-arm created
daemonset.apps/kube-flannel-ds-ppc64le created
daemonset.apps/kube-flannel-ds-s390x created
查看pods状态已经全部running
[root@host-151 ~]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-9d85f5447-gc8rh 1/1 Running 0 4m39s
coredns-9d85f5447-lr8d9 1/1 Running 0 4m39s
etcd-k8s-master 1/1 Running 0 4m53s
kube-apiserver-k8s-master 1/1 Running 0 4m53s
kube-controller-manager-k8s-master 1/1 Running 0 4m53s
kube-flannel-ds-amd64-sjrbq 1/1 Running 0 104s
kube-proxy-rf842 1/1 Running 0 4m39s
kube-scheduler-k8s-master 1/1 Running 0 4m53s
在192.168.153.152/153 上执行
命令由初始化master之后生成,直接复制即可
kubeadm join 192.168.153.151:6443 --token 4ewer2.chl3n0wtfi36xc6c \
--discovery-token-ca-cert-hash sha256:78512851a2e78e14ea454f0a65e9853f9085ec8348a2e0f781347fa5c882f53a
[root@host-151 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master Ready master 5m9s v1.17.0
k8s-node1 Ready <none> 2m58s v1.17.0
k8s-node2 Ready <none> 2m54s v1.17.0
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta8/aio/deploy/recommended.yaml
执行失败时可以用上传的文件执行下面这条命令:
下载链接>> https://download.csdn.net/download/m0_46674735/14930771
kubectl apply -f kubernetes-dashboard.yaml
默认Dashboard只能集群内部访问,修改Service为NodePort类型,暴露到外部,文档中已经修改,直接运行即可
创建service account并绑定默认cluster-admin管理员集群角色:
kubectl create serviceaccount dashboard-admin -n kube-system
kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')
https://nodeip:30001
(注意:用的是https
)以火狐浏览器为例:
点击高级选项
点击接受风险并继续
点击token,将上面生成的token粘贴进去
成功访问管理页面
在Kubernetes集群中创建一个pod,验证是否正常运行:
kubectl create deployment nginx --image=nginx
查看pod运行成功
[root@host-151 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-86c57db685-vvmkp 1/1 Running 0 27s