kubeadm安装k8s集群kubeadm安装Kubernetes V1.16.2集群详细文档kubeadm安装
安装环境(vm15.0.4下虚拟机3台,centos 7.6):
Master:192.168.10.100
Node1:192.168.10.101
Node2:192.168.10.102
1:安装虚拟机,配置网络接口,使虚拟机访问访问正常
2:修改主机名
hostnamectl set-hostname kube-master
hostnamectl set-hostname kube-node1
hostnamectl set-hostname kube-node2
3:配置主机映射
[root@kube-master ~]# more /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.10.100 kube-master
192.168.10.101 kube-node1
192.168.10.102 kube-node2
4:kube-master上配置免密钥登陆
ssh-keygen #一路回车即可
ssh-copy-id kube-master
ssh-copy-id kube-node1
ssh-copy-id kube-node2
5:三台主机关闭防火墙,selinux,配置系统内核
#关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
#关闭selinux
sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux
sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
sed -i "s/^SELINUX=permissive/SELINUX=disabled/g" /etc/sysconfig/selinux
sed -i "s/^SELINUX=permissive/SELINUX=disabled/g" /etc/selinux/config
#配置系统内核
echo 1 >/proc/sys/net/bridge/bridge-nf-call-iptables
echo 1 >/proc/sys/net/bridge/bridge-nf-call-ip6tables
6:配置yum源环境(使用阿里云镜像)三台主机配置
#docker-ce yum源配置
cd /etc/yum.repos.d/
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# kubernetes yum源配置
cd /etc/yum.repos.d
[root@k8s-master yum.repos.d]# cat kubenetes.repo
[kubernetes]
name=Kubernetes Repo
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
enabled=1
yum repolist 检测yum源列表
7:gpg校验配置,三台主机配置
下载校验文件
wget https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
wget https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
导入校验文件
rpm --import rpm-package-key.gpg
rpm --import yum-key.gpg
8:安装docker-ce,kubeadm组件
master节点安装
yum install docker-ce kubelet kubeadm kubectl
node节点安装
yum install docker-ce kubelet kubeadm
9:启动docker,kubelet
编辑kubelet的配置文件/etc/sysconfig/kubelet,设置其忽略Swap启用的状态错误,内容如下: KUBELET_EXTRA_ARGS="--fail-swap-on=false" master以及node节点均需设置;
[root@k8s-master yum.repos.d]# cat /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS="--fail-swap-on=false"
google的镜像仓库k8s.grc.io国内无法访问,如有代理服务器,则编辑docker.service文件,添加代理服务器
vim /usr/lib/systemd/system/docker.service
添加如下内容
Environment="HTTPS_PROXY=https://[proxy-addr]:[proxy-port]/"
保存退出,从新加载配置文件,重启docker
systemctl daemon-reload
systemctl restart docker
设置docker,kubelet开机自启动,开启docker
systemctl enable docker kubelet
systemctl start docker
无代理服务,可以把k8s的软件包下载到本地安装,通过(docker load)命令加载tar包;
10:初始化master节点,node节点
master节点需要pull完成的相关镜像
k8s.gcr.io/kube-proxy v1.16.2 8454cbe08dc9 2 days ago 86.1MB
k8s.gcr.io/kube-scheduler v1.16.2 ebac1ae204a2 2 days ago 87.3MB
k8s.gcr.io/kube-controller-manager v1.16.2 6e4bffa46d70 2 days ago 163MB
k8s.gcr.io/kube-apiserver v1.16.2 c2c9a0406787 2 days ago 217MB
k8s.gcr.io/etcd 3.3.15-0 b2756210eeab 6 weeks ago 247MB
k8s.gcr.io/coredns 1.6.2 bf261d157914 2 months ago 44.1MB
quay.io/coreos/flannel v0.11.0-amd64 ff281650a721 8 months ago 52.6MB
k8s.gcr.io/pause 3.1 da86e6ba6ca1 22 months ago 742kB
node节点需要pull完成的相关镜像
k8s.gcr.io/kube-proxy v1.16.2 8454cbe08dc9 2 days ago 86.1MB
quay.io/coreos/flannel v0.11.0-amd64 ff281650a721 8 months ago 52.6MB
k8s.gcr.io/pause 3.1 da86e6ba6ca1 22 months ago 742kB
目前默认版本为1.16.2
kubeadm init --help 查看启动参数 默认监听端口6443
kubeadm init --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --ignore-preflight-errors=Swap
启动完成后会显示join命令,记得保存
根据提示完成非root用户配置
mkdir ~/.kube
cp /etc/kubernetes/admin.conf ~/.kube/
chown $(id -u):$(id -g) $HOME/.kube/config
检查启动情况(nodes此时还只有master节点,未安装flannel时候状态不是ready)
[root@kube-master yum.repos.d]# kubectl get componentstatus
NAME STATUS MESSAGE ERROR
scheduler Healthy ok
controller-manager Healthy ok
etcd-0 Healthy {"health": "true"}
[root@kube-master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
kube-master Ready master 24h v1.16.2
kube-node1 Ready <none> 23h v1.16.2
kube-node2 Ready <none> 23h v1.16.2
11:安装flannel组件
coredns依赖flannel组件,未安装flannel时候查看coredns的状态为prending
[root@kube-master ~]# kubectl get pods -n kube-system -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
coredns-5644d7b6d9-lt5kp 1/1 Running 2 24h 10.244.0.7 kube-master <none> <none>
coredns-5644d7b6d9-nblnl 1/1 Running 2 24h 10.244.0.6 kube-master <none> <none>
etcd-kube-master 1/1 Running 2 24h 192.168.10.100 kube-master <none> <none>
kube-apiserver-kube-master 1/1 Running 2 24h 192.168.10.100 kube-master <none> <none>
kube-controller-manager-kube-master 1/1 Running 2 24h 192.168.10.100 kube-master <none> <none>
kube-proxy-5c5km 1/1 Running 2 23h 192.168.10.101 kube-node1 <none> <none>
kube-proxy-j56kp 1/1 Running 2 23h 192.168.10.102 kube-node2 <none> <none>
kube-proxy-pvlzt 1/1 Running 2 24h 192.168.10.100 kube-master <none> <none>
kube-scheduler-kube-master 1/1 Running 2 24h 192.168.10.100 kube-master <none> <none>
安装flannel
master节点
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
正常会自动pull flannel镜像,如网络不稳定,可以手动pull
docker pull quay.io/coreos/flannel:v0.11.0-amd64
node节点在加入k8s之后会自动pull flannel镜像,无需额外配置;
12:node节点加入k8s
kubeadm join 192.168.10.100:6443 --token 5zx03y.ixb80qum7ktmu1mk --discovery-token-ca-cert-hash sha256:7b2a69f18281cb245a417f53dbd18076583556adbfcba29a70858e780300c3c7 --ignore-preflight-errors=Swap
查看状态
[root@kube-master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
kube-master Ready master 24h v1.16.2
kube-node1 Ready <none> 23h v1.16.2
kube-node2 Ready <none> 23h v1.16.2
13:安装kubectl补全命令
yum install bash-completion
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)
14:可能碰到的问题,如果受用kubectl命令报连接不上8080端口,使用下面配置
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
source ~/.bash_profile