如果大家想一键部署K8S,也可以使用我编写的脚本:
https://github.com/jesssecat/K8s/blob/master/K8s-initial.sh
下载玩,运行即可,别忘了点赞start哦,(#^.^#)
Kubeadm初始化Kubernetes集群:
网络规划:
节点网络 :192.168.205.10
service网络 :10.96.0.0/12
pod网络 :10.244.0.0/16
集群规划:
master :192.168.205.10
node1 :192.168.205.20
node2 :192.168.205.30
node3 :192.168.205.40
kubadm
1,master,node:安装kubelet,kubeadm,docker
2,master:kubeadm init
3,nodes:kubeadm join
1.各个节点同步时间,务必一致
设置为北京时间
timedatectl set-timezone Asia/Shanghai
2.设置Kubernetes仓库
新建kubernetes.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
建立docker-ee仓库
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum repolist 查看是否生效
将这两个repo仓库分发到不同的节点
3.master安装:
yum install docker-ce kubelet kubeadm kubectl
安装过程中可能会有key的错误,这是上面我们阿里云的仓库导入的问题
我们下载下来,手动导入key
将阿里的kubernetes仓库gpgcheck=1改为0
[kubernetes]
name=Kubernetes Repo
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
enabled=1
手动下载
https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
rpm --import yum-key.gpg
然后重新执行:yum install docker-ce kubelet kubeadm kubectl
安装的列表:
Installing : kubectl-1.11.2-0.x86_64 1/6
Installing : cri-tools-1.11.0-0.x86_64 2/6
Installing : socat-1.7.3.2-2.el7.x86_64 3/6
Installing : kubernetes-cni-0.6.0-0.x86_64 4/6
Installing : kubelet-1.11.2-0.x86_64 5/6
Installing : kubeadm-1.11.2-0.x86_64 6/6
Verifying : socat-1.7.3.2-2.el7.x86_64 1/6
Verifying : kubelet-1.11.2-0.x86_64 2/6
Verifying : cri-tools-1.11.0-0.x86_64 3/6
Verifying : kubernetes-cni-0.6.0-0.x86_64 4/6
Verifying : kubeadm-1.11.2-0.x86_64 5/6
Verifying : kubectl-1.11.2-0.x86_64 6/6
Installed:
kubeadm.x86_64 0:1.11.2-0 kubectl.x86_64 0:1.11.2-0 kubelet.x86_64 0:1.11.2-0
Dependency Installed:
cri-tools.x86_64 0:1.11.0-0 kubernetes-cni.x86_64 0:0.6.0-0 socat.x86_64 0:1.7.3.2-2.el7
4.查看环境
可选:
vim /usr/lib/systemd/system/docker.service
[Service]类目下新加:
Environment="HTTPS_PROXY=http://www.ik8s.io:10080"
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
echo 1 > /proc/sys/net/ipv4/ip_forward
查看安装目录和配置
[root@docker-master yum.repos.d]# rpm -ql kubelet
/etc/kubernetes/manifests
/etc/sysconfig/kubelet
/etc/systemd/system/kubelet.service
/usr/bin/kubelet
[root@docker-master yum.repos.d]# cat /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS=
查看错误的日志
tail /var/log/messages
设置为开机自启
systemctl stop kubelet
systemctl stop docker
运行下面:
标准:
kubeadm init --kubernetes-version=v1.11.2 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12
报错添加:
kubeadm init --kubernetes-version=v1.11.2 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --ignore-preflight-errors=Swap
声明APIserver运行(我运行这个安装成功):
kubeadm init --apiserver-advertise-address=192.168.205.10 --kubernetes-version=v1.11.2 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --ignore-preflight-errors=Swap
kubeadm init --kubernetes-version=v1.11.2 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --ignore-preflight-errors=Swap
生成秘钥:
kubeadm join 192.168.205.10:6443 --token y6ob3k.g1nyvlenbmj6rzye --discovery-token-ca-cert-hash sha256:07f9cf031b69c808822abf24b8742e1434c880a57eb53b37c0096eda4984d10a --ignore-preflight-errors=Swap
如果报错:
[WARNING SystemVerification]: docker version is greater than the most recently validated version. Docker version: 18.06.1-ce. Max validated version: 17.03
[preflight] Some fatal errors occurred:
[ERROR Swap]: running with swap on is not supported. Please disable swap
[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`
需要修改配置:
vim /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS="--fail-swap-on=false"
#如果swap-on会出错的话,我们不让他们出错
继续执行:kubeadm init --kubernetes-version=v1.11.1 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --ignore-preflight-errors=Swap
5. node1和node2开始安装环境
yum install docker-ce kubelet kubeadm kubectl
6.master查看已经下载的镜像
docker images ls
k8s.gcr.io/kube-proxy-amd64
k8s.gcr.io/kube-apiserver-amd64
k8s.gcr.io/kube-scheduler-amd64
k8s.gcr.io/kube-controller-manager-amd64
k8s.gcr.io/coredns
k8s.gcr.io/etcd-amd64
k8s.gcr.io/pause
安装完成后的提示:
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube #创建一个用户目录
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config #非常重要的配置文件
sudo chown $(id -u):$(id -g) $HOME/.kube/config
You can now join any number of machines by running the following on each node
as root:
kubeadm join 10.0.2.15:6443 --token 8yn7ze.zfbpl707shtjy90h --discovery-token-ca-cert-hash sha256:a3a626410f496657a0f590f1339b51f72ff0414f7c074ca276127ac25bb21ab3
在master主机上查看:
ss -tnl 查看是否有:6443端口
查看机器节点是否健康
kubectl get cs
NAME STATUS MESSAGE ERROR
scheduler Healthy ok
controller-manager Healthy ok
etcd-0 Healthy {"health": "true"}
执行上面的提示:
mkdir -p $HOME/.kube #创建一个用户目录
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config #非常重要的配置文件
7.查看节点信息
[root@docker-master yum.repos.d]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
docker-master NotReady master 42m v1.11.2
可看到:主节点的status是没有启动的,因为他还缺少一个组件(flannel)
github.com/coreos/flannel
在github上查看到如下的信息:
Deploying flannel manually
Flannel can be added to any existing Kubernetes cluster though it's simplest to add flannel before any pods using the pod network have been started.
For Kubernetes v1.7+ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
手动部署的话,直接运行脚本即可:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
获取正在运行的Pod
kubectl get pods -n kube-system
提示No resources found.
其实运行完flannel 并不是直接就可以成功的
需要查看一下docker images ls 是否有flannel镜像
[root@docker-master]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
k8s.gcr.io/kube-proxy-amd64 v1.11.1 d5c25579d0ff 6 weeks ago 97.8MB
k8s.gcr.io/kube-controller-manager-amd64 v1.11.1 52096ee87d0e 6 weeks ago 155MB
k8s.gcr.io/kube-scheduler-amd64 v1.11.1 272b3a60cd68 6 weeks ago 56.8MB
k8s.gcr.io/kube-apiserver-amd64 v1.11.1 816332bd9d11 6 weeks ago 187MB
k8s.gcr.io/coredns 1.1.3 b3b94275d97c 3 months ago 45.6MB
k8s.gcr.io/etcd-amd64 3.2.18 b8df3b177be2 4 months ago 219MB
quay.io/coreos/flannel v0.10.0-amd64 f0fad859c909 7 months ago 44.6MB
k8s.gcr.io/pause 3.1 da86e6ba6ca1 8 months ago 742kB
如果有flannel,是安装成功的
这是查看一下状态是成功的
[root@docker-master yum.repos.d]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
docker-master Ready master 53m v1.11.2
[root@docker-master yum.repos.d]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-78fcdf6894-dljx2 1/1 Running 0 54m
coredns-78fcdf6894-f97qd 1/1 Running 0 54m
etcd-docker-master 1/1 Running 0 6m
kube-apiserver-docker-master 1/1 Running 0 6m
kube-controller-manager-docker-master 1/1 Running 0 6m
kube-flannel-ds-amd64-6r56g 1/1 Running 0 6m #正常运行
kube-proxy-ht2dq 1/1 Running 0 54m
kube-scheduler-docker-master 1/1 Running 0 6m
kubectl get ns #获取命名空间
[root@docker-master ]# kubectl get ns
NAME STATUS AGE
default Active 56m
kube-public Active 56m
kube-system Active 56m #系统级的pod都在这里
8. 在node1和node2上开始安装
scp /usr/lib/systemd/system/docker.service node1:/usr/lib/systemd/system/docker.service
scp /usr/lib/systemd/system/docker.service node1:/usr/lib/systemd/system/docker.service
scp /etc/sysconfig/kubelet node1:/etc/sysconfig/
scp /etc/sysconfig/kubelet node2:/etc/sysconfig/
将配置文件分发
node1和node2启动docker
systemctl start docker
docker info 查看是否生效
加入开机启动:
systemctl enable docker kubelet
将master生成的秘钥:
kubeadm join 10.0.2.15:6443 --token 8yn7ze.zfbpl707shtjy90h --discovery-token-ca-cert-hash sha256:a3a626410f496657a0f590f1339b51f72ff0414f7c074ca276127ac25bb21ab3
后面加入:
kubeadm join 10.0.2.15:6443 --token 8yn7ze.zfbpl707shtjy90h --discovery-token-ca-cert-hash sha256:a3a626410f496657a0f590f1339b51f72ff0414f7c074ca276127ac25bb21ab3 --ignore-preflight-errors=Swap
查看扩展节点信息:
kubectl get pods -n kube-system -o wide
查看节点信息:
kubectl get pods -n kube-system -o wide