kubeadm方式部署Kubernetes
kubeadm是官方社区推出的一个用于快速部署kubernetes集群的工具。
这个工具能通过两条指令完成一个kubernetes集群的部署:
# 创建一个 Master 节点
$ kubeadm init
# 将一个 Node 节点加入到当前集群中
$ kubeadm join <Master节点的IP和端口 >
1. 安装要求
在开始之前,部署Kubernetes集群机器需要满足以下几个条件:
一台或多台机器,操作系统 CentOS7.x-86_x64
硬件配置:2GB或更多RAM,2个CPU或更多CPU,硬盘30GB或更多
集群中所有机器之间网络互通
可以访问外网,需要拉取镜像
禁止swap分区
2.学习目标
在所有节点上安装Docker和kubeadm
部署Kubernetes Master
部署容器网络插件
部署 Kubernetes Node,将节点加入Kubernetes集群中
部署Dashboard Web页面,可视化查看Kubernetes资源
3. 准备环境
关闭防火墙:
$ systemctl stop firewalld
$ systemctl disable firewalld
关闭selinux:
$ sed -i 's/enforcing/disabled/' /etc/selinux/config
$ setenforce 0
关闭swap:
$ swapoff -a $ 临时
$ vim /etc/fstab $ 永久
或者#echo "vm.swappiness = 0">> /etc/sysctl.conf
添加主机名与IP对应关系(记得设置主机名):
$ cat /etc/hosts
192.168.31.63 k8s-master
192.168.31.65 k8s-node1
192.168.31.66 k8s-node2
hostnamectl set-hostname hostname
将桥接的IPv4流量传递到iptables的链:
$ cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
$ sysctl --system
4. 所有节点安装Docker/kubeadm/kubelet
Kubernetes默认CRI(容器运行时)为Docker,因此先安装Docker。
4.1 安装Docker
$ 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
swapoff -a
Docker version 18.06.1-ce, build e68fc7a
4.2 添加阿里云YUM软件源
$ 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=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
4.3 安装kubeadm,kubelet和kubectl
由于版本更新频繁,这里指定版本号部署:
$ yum install -y kubelet-1.15.6 kubeadm-1.15.6 kubectl-1.15.6
$ systemctl enable kubelet
5. 部署Kubernetes Master
在192.168.31.63(Master)执行。
$ kubeadm init \
--apiserver-advertise-address=192.168.224.40 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.15.6 \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.244.0.0/16
注释:apiserver-advertise-address的master ip,另外内容不够1700M cpu没有2核以上执行命令报错
由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里指定阿里云镜像仓库地址。
报错集锦:
```
ERROR ImagePull]: failed to pull image k8s.gcr.io/coredns:1.3.1: output: Error response from daemon: Get https://k8s.gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
, error: exit status 1
```
上述报错:因为要下载k8s.gcr.io的docker镜像,但是国内连不上https://k8s.gcr.io/v2/。
可以执行以下语句,kubernetes-version版本号修改和文章开头kubelet一致版本:
kubeadm init --image-repository=registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16 --kubernetes-version=v1.15.6
···
[WARNING Hostname]: hostname "master1": lookup master1 on 192.168.211.2:53: no such host
error execution phase preflight: [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=...`
···
执行swapoff -a 临时关闭,再执行kubeadm init --image-repository=registry.aliyuncs.com/google_containers
--pod-network-cidr=10.244.0.0/16 --kubernetes-version=v1.15.0
即可
报错:
[root@localhost ~]# kubeadm init --apiserver-advertise-address=192.168.224.40 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.15.6 --service-cidr=10.1.0.0/16 --pod-network-cidr=10.244.0.0/16
[init] Using Kubernetes version: v1.15.6
[preflight] Running pre-flight checks
[WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
error execution phase preflight: [preflight] Some fatal errors occurred:
[ERROR Port-10251]: Port 10251 is in use
[ERROR Port-10252]: Port 10252 is in use
[ERROR FileAvailable--etc-kubernetes-manifests-kube-apiserver.yaml]: /etc/kubernetes/manifests/kube-apiserver.yaml already exists
[ERROR FileAvailable--etc-kubernetes-manifests-kube-controller-manager.yaml]: /etc/kubernetes/manifests/kube-controller-manager.yaml already exists
[ERROR FileAvailable--etc-kubernetes-manifests-kube-scheduler.yaml]: /etc/kubernetes/manifests/kube-scheduler.yaml already exists
[ERROR FileAvailable--etc-kubernetes-manifests-etcd.yaml]: /etc/kubernetes/manifests/etcd.yaml already exists
[ERROR Port-10250]: Port 10250 is in use
[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`
执行:kubeadm reset
再根据提示执行:rm -f $HOME/.kube/config
最后在执行初始化命令即可: kubeadm init --image-repository=registry.aliyuncs.com/google_containers
报错:
error execution phase preflight: [preflight] Some fatal errors occurred:
[ERROR ImagePull]: failed to pull image registry.aliyuncs.com/google_containers/coredns/coredns:v1.8.0: output: Error response from daemon: pull access denied for registry.aliyuncs.com/google_containers/coredns/coredns, repository does not exist or may require 'docker login': denied: requested access to the resource is denied
, error: exit status 1
[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`
To see the stack trace of this error execute with --v=5 or higher
解决方案:
(1)docker pull coredns/coredns:1.8.0
(2)docker tag coredns/coredns:1.8.0 registry.aliyuncs.com/google_containers/coredns/coredns:v1.8.0
(3)docker rmi coredns/coredns:1.8.0
(4)kubeadm reset
(5)最后再执行初始化命令
使用kubectl工具:
mkdir-p$HOME/.kube
sudocp-i/etc/kubernetes/admin.conf$HOME/.kube/config
sudochown$(id -u):$(id -g)$HOME/.kube/config
$ kubectlgetnodes
6. 安装Pod网络插件(CNI)
$ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml
报错无法访问,需要:在/etc/hosts
199.232.96.133 raw.githubusercontent.com
再次访问即可
确保能够访问到quay.io这个registery。 master执行
# kubectl get pods -n kube-system
假设在安装pod网络插件无法访问和访问quay.io都无法通过的情况下,
可采取:
#grep image kube-flannel.yaml 过滤包
下载
#docker pull quay.io/coreos/flannel:v0.11.0-amd64
#docker pull quay.io/coreos/flannel:v0.11.0-arm
#docker pull quay.io/coreos/flannel:v0.11.0-ppc64le
#quay.io/coreos/flannel:v0.11.0-s390x
#kubectl apply -f kube-flannel.yml
7. 加入Kubernetes Node
在192.168.31.65/66(Node)执行。
向集群添加新节点,执行在kubeadm init输出的kubeadm join命令:
$ kubeadm join 192.168.31.63:6443 --token l79g5t.6ov4jkddwqki1dxe --discovery-token-ca-cert-hash sha256:4f07f9068c543130461c9db368d62b4aabc22105451057f887defa35f47fa076
8. 测试kubernetes集群
在Kubernetes集群中创建一个pod,验证是否正常运行:
$ kubectl create deployment nginx --image=nginx
$ kubectl expose deployment nginx --port=80 --type=NodePort
$ kubectl get pod,svc
$ kubectl get pod,svc -o wide
访问地址:http://NodeIP:Port
9. 部署 Dashboard
$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml
这里一般报错找不到这个域名,应在、/etc/hosts目录文件下添加199.232.96.133 raw.githubusercontent.com 报错解决。
镜像下载因为网络的原因:
镜像难以下载,需要修改以下两个地方
image: tigerfive/kubernetes-dashboard-amd64:v1.10.1
spec:
type: NodePort
ports:
- port: 443
targetPort: 8443
nodePort: 30001
默认Dashboard只能集群内部访问,修改Service为NodePort类型,暴露到外部:
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kube-system
spec:
type: NodePort
ports:
- port: 443
targetPort: 8443
nodePort: 30001
selector:
k8s-app: kubernetes-dashboard
$ kubectl apply -f kubernetes-dashboard.yaml
访问地址:https://NodeIP:30001
创建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}')
使用输出的token登录Dashboard。