k8s搭建(使用kubeadm工具)
kubelet-v1.16.3版本
一、环境准备
3台虚拟机, centos7系统:
1、关闭iptables 、firewalld,关闭selinux
2、配置所有机器网络时间同步(NTP)
3、配置所有机器主机名及hosts,实现主机名能访问互通
各节点配置如下:
k8s-master1 192.168.11.21 master节点 2核CPU 2G内存
k8s-node1 192.168.11.22 data节点 1核CPU 1.5G内存
k8s-node2 192.168.11.23 data节点 1核CPU 1.5G内存
二、 master节点安装
1、安装docker
cd /etc/yum.repos.d/
#下载repo包 docer-ce
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O docker-ce.repo
yum install -y yum-utils device-mapper-persistent-data lvm2
yum install docker-ce
#设置开机自启动
systemctl enbale docker
systemctl start docker
#配置阿里云镜像加速
vi /etc/docker/daemon.json ,内容如下:
{
"registry-mirrors": ["https://4piak27r.mirror.aliyuncs.com"]
}
#重启docker服务
systemctl restart docker
#开iptables的网桥转发
vi /etc/sysctl.conf,添加以下内容
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
sysctl -p #ok
2、安装kubelet、kubeadm、kubectl
cd /etc/yum.repos.d/
vi kubernets.repo 内容如下:
[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
yum install kubelet kubeadm kubectl
#设置开启自启动
systemctl enable kubelet
systemctl start kubelet
tail -f /var/log/messages #查看系统日期消息,发现启动kubelet服务出错,没关系先不管 (因为master节点还没启动完成呢)
rpm -qa kubelet #查看安装的kubelet包版本
rpm -ql kubelet #查看安装的文件清单列表
3、初始化master节点
vi /etc/sysconfig/kubelet ,内容如下:
KUBELET_EXTRA_ARGS="--fail-swap-on=false"
kubeadm config images list #查看master初始化时需要拉取的docker镜像,发现有这么多
k8s.gcr.io/kube-apiserver:v1.16.3
k8s.gcr.io/kube-controller-manager:v1.16.3
k8s.gcr.io/kube-scheduler:v1.16.3
k8s.gcr.io/kube-proxy:v1.16.3
k8s.gcr.io/pause:3.1
k8s.gcr.io/etcd:3.3.15-0
k8s.gcr.io/coredns:1.6.2
直接从官方下载极大可能会失败(国内用户需要翻墙),这里采用一个变通的方案:先从阿里云镜像仓库中下载,在打tag为k8s.gcr.io对应的版本镜像。
(1) kube-apiserver镜像拉取
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.16.3
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.16.3 k8s.gcr.io/kube-apiserver:v1.16.3
(2) kube-controller-manager镜像拉取
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.16.3
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.16.3 k8s.gcr.io/kube-controller-manager:v1.16.3
(3) kube-scheduler镜像拉取
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.16.3
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.16.3 k8s.gcr.io/kube-scheduler:v1.16.3
(4) kube-proxy镜像拉取
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.16.3
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.16.3 k8s.gcr.io/kube-proxy:v1.16.3
(5) pause镜像拉取
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1 k8s.gcr.io/pause:3.1
(6) etcd镜像拉取
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.3.15-0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.3.15-0 k8s.gcr.io/etcd:3.3.15-0
(7) coredns镜像拉取
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.2
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.2 k8s.gcr.io/coredns:1.6.2
(8) flannel镜像拉取(本地导入方案好)
从网盘下载 flannel_v0.11.0-amd64.tar 包
链接:https://pan.baidu.com/s/1TXN2usM2URr5JnATHA90jQ
提取码:5ug1
docker load --input flannel_v0.11.0-amd64.tar
docker tag k8s.gcr.io/flannel:v0.11.0-amd64 quay.io/coreos/flannel:v0.11.0-amd64
kubeadm init --help
#初始化 master节点
kubeadm init --apiserver-advertise-address=0.0.0.0 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --ignore-preflight-errors=Swap
master初始化完成,会输出类似信息如下
(
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 should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.11.21:6443 --token 1tapwc.7onoovy0roj2fus1 \
--discovery-token-ca-cert-hash sha256:01fa8418a6fae25c254325810e5b58c43e69b75863ed4666c196eab647c23bb2
)
这些信息告诉我们:
(1)我们需要在master节点的用户home目录中创建一个.kube隐藏目录,然后将/etc/kubernetes/admin.conf 复制到用户的.kube目录中
(2)其他数据节点可以通过kubeadm join 命令加入集群, 并给出了加入集群的认证token信息
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml -O kube-flannel.yml
kubectl apply -f kube-flannel.yml #初始化master中的flannel组件 ok
(
kubectl常用命令:
kubectl get ns #查看namespace
kubectl get cs #查看componentstatus
kubectl get nodes #查看节点
kubectl get pods #查看pos
kubectl version # 版本信息
kubectl cluster-info # 查看集群信息
kubectl describe node k8s-master1
)
三、data节点的安装(k8s-node1和k8s-node2上)
1、安装docker
cd /etc/yum.repos.d/
#下载repo包 docer-ce
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O docker-ce.repo
yum install -y yum-utils device-mapper-persistent-data lvm2
yum install docker-ce
#设置开机自启动
systemctl enbale docker
systemctl start docker
#配置阿里云镜像加速
vi /etc/docker/daemon.json ,内容如下:
{
"registry-mirrors": ["https://4piak27r.mirror.aliyuncs.com"]
}
#重启docker服务
systemctl restart docker
#开iptables的网桥转发
vi /etc/sysctl.conf,添加以下内容
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
sysctl -p
2、安装kubelet、kubeadm、kubectl
cd /etc/yum.repos.d/
vi kubernets.repo 内容如下:
[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
yum install kubelet kubeadm kubectl
#设置开启自启动
systemctl enable kubelet
systemctl start kubelet
tail -f /var/log/messages #查看系统日期消息,发现启动kubelet服务出错,没关系先不管 (因为master节点还没启动完成呢)
rpm -qa kubelet #查看安装的kubelet包版本
rpm -ql kubelet #查看安装的文件清单列表
vi /etc/sysconfig/kubelet ,内容如下:
KUBELET_EXTRA_ARGS="--fail-swap-on=false"
systemctl enable kubelet
systemctl start kubelet
(4)、kube-proxy镜像拉取
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.16.3
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.16.3 k8s.gcr.io/kube-proxy:v1.16.3
(8)、从本地导入一个flannel的镜像包
scp k8s-master1:/root/flannel_v0.11.0-amd64.tar /root/
docker load --input flannel_v0.11.0-amd64.tar
docker tag k8s.gcr.io/flannel:v0.11.0-amd64 quay.io/coreos/flannel:v0.11.0-amd64
(5)、pause镜像
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1 k8s.gcr.io/pause:3.1
将master1节点上的/etc/cni 复制到data节点
scp -r k8s-master1:/etc/cni /etc/cni
systemctl restart kubelet
kubeadm join 192.168.11.21:6443 --token d2h00h.cykj3ykq6dvxpq64 \
--discovery-token-ca-cert-hash sha256:0ece17e3552cfbb33db3f3ac3fc367622e6d1d6d2db284cba99cac6af9e410bd \
--ignore-preflight-errors=Swap
vi /etc/sysconfig/kubelet ,新加内容
KUBELET_EXTRA_ARGS="--fail-swap-on=false"
KUBELET_OPTS="–image-pull-progress-deadline=60m" #配置
systemctl enable kubelet
systemctl start kubelet
其他附命令 :
kubectl create deployment nginx-deployment --image=nginx:1.16.1-alpine
kubectl expose deployment nginx-deployment --name=nginx --port=80 --target-port=80
kubectl run busybox --image=busybox --replicas=1 -it --restart=Never #直接进入一个buybox的pod内部
cat /var/run/flannel/subnet.env (master1节点)
FLANNEL_NETWORK=10.244.0.0/16
FLANNEL_SUBNET=10.244.0.1/24
FLANNEL_MTU=1450
FLANNEL_IPMASQ=true
cat /var/run/flannel/subnet.env (node1节点)
FLANNEL_NETWORK=10.244.0.0/16
FLANNEL_SUBNET=10.244.3.1/24
FLANNEL_MTU=1450
FLANNEL_IPMASQ=true
cat /var/run/flannel/subnet.env (node2节点)
FLANNEL_NETWORK=10.244.0.0/16
FLANNEL_SUBNET=10.244.4.1/24
FLANNEL_MTU=1450
FLANNEL_IPMASQ=true