一.安装要求
1.2台以上同属于一个内网的机器,操作系统需要高于centos7以上
2.硬件配置内存需要大于2G
3.可以访问外网,需要拉取镜像
4.禁止swap分区
二.学习目标
1.在所有节点安装Docker和kubeadm
2.部署kubernetes Master
3.部署容器网络插件fannel
4.部署kubernetes Node,将节点加入到Kubernetes集群中
5.部署Dashboard web页面,可视化查看kubernetes资源
三.环境准备
关闭防火墙:
$ systemctl stop firewalld
$ systemctl disable firewalld

关闭selinux:
$ sed -i 's/enforcing/disabled/' /etc/selinux/config
$ setenforce 0

关闭swap:
$ swapoff -a $ 临时
$ vim /etc/fstab $ 永久

添加主机名与IP对应关系(记得设置主机名):
设置主机名:
vi /etc/sysconfig/network
HOSTNAME=k8s-master
$ cat /etc/hosts
172.16.100.234 k8s-master
172.16.100.120 k8s-node1

将桥接的IPv4流量传递到iptables的链:
modprobe br_netfilter
$ cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
$ sysctl --system
四.所有节点安装Docker/kubeadm/kubelet
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
$ systemctl enable docker && systemctl start docker
$ docker --version
Docker version 19.03.12, build 48a66213fe
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

3.安装kubeadm,kubelet kubectl
$ yum install -y kubelet kubeadm kubectl
$ systemctl enable kubelet
#安装kubelet 后会在/etc下生成文件目录/etc/kubernetes/manifests/
5.部署kubernetes Master
kubeadm init --kubernetes-version=v1.19.0 --image-repository=registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/16 --apiserver-advertise-address=172.16.100.234
使用kubectl工具:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
$ kubectl get nodes 查看节点是否ready
6.安装Pod网络插件(只需要在master上执行)
$ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
7.加入Kubernete Node
需要在Node端执行
kubeadm join 172.16.100.234:6443 --token 0ieaj1.qtkmnn7d1i35gg0z \
--discovery-token-ca-cert-hash sha256:ae06a272dda677e5966db22882d409be709178be14c062926de356c5d6b48133
8.测试集群
$ kubectl create deployment nginx --image=nginx
$ kubectl expose deployment nginx --port=80 --type=NodePort
$ kubectl get pod,svc
9.部署Dashboard
9.1.wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta5/aio/deploy/recommended.yaml
9.2修改yml文件(默认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
    9.3
    kubectl apply -f recommended.yaml
    kubectl get pods,svc -n kube-system (查看安装情况)
    9.4.创建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,使用令牌登录
    登录地址https://172.16.100.234:30001/#/login,复制令牌即可登录

k8s新版本集群搭建从0到1

常见问题:
1.新增node,记得新生成token
kubeadm token create产生临时token
kubeadm token list获取token
然后使用命令将节点接入进去.
2./etc/hosts 的修改