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

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

关闭selinux:
$ sed -i 's/enforcing/disabled/' /| K Netc/selinux/+ [ q J l 1 Aconfig
$ setenforce 0

关闭swap:
$ swapoff -a $ 临时
$ vid N A Bm /etc/fstab $ 永久

添加主机q v s $ u w G b l名与IP对应关系(记得设置主机名):
设置主机名:
vi /etc/sysconfig/network
HOSTNAME{ 1 G 4 4=k8s-master
$ cat /etc/hosts
172.16.100.234 k8s-master
172.16.100.120 k8s-nod3 } & / te1

将桥接的IPv4流量传递到iptables的链:
modprobe br_netfilter
$ cat > /etc/sysctl.d/ku = @ * M K m8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
E; Q 1 : S e : h ZOF
$ sysctl --system
四.所有节点安装Docker/kubeadm/kubelet
1.v N ( H安装Docker
$ wget https://mirrors.E f A S g t |alir { l f ~ 0 Pyun.com/docker-ce/li7 x L m i qnux/cen: , A # _ mtos/docker-ce.repo -O /etc/yum.reposB 0 c G } & L.d/doc` r ; I , . 0ker-ce.repo
$ yum -y install docker-ce
$ systV % K Z X | . ^ =emctl enable do^ v I J ] 9cker &&a/ e t K P h ;mp; systemctl start docker
$ docker --version
Docker v9 j h p kersion 19.03.12, build 48a66213fe
2u = ^ / F ) D 4.添加阿里云的yum源码
$ cat > /etc/yum.repos.d/kubernetes.& ) q H K +repo << EOF
[kubernetes]
naR , X } 0 6 $ ;me=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetK ( y Y ? xes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpgd n % U u @ ) https://mirU V M w irors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

3.安装kubeadm,kubelet kubectl
$ yum install -y kubelet kubeadm kubectl
$ system] : + c =ctl enable kubeletc Y 5 3 z L Y k
#安装kubelet 后会在= b b v ~ )/etc下生成文件目录/etc/kubernetes/manifef 1 bsts/
5.部署kubernetes Master
kubeadm init --kubernetesV 9 } o-version=v1.19.0 --image-repoU } ] C Ssitory=registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/16 --apiserver-advertise-ad8 [ ] m -dress=172.16.100.2K M 9 b r [ q r 534
使用kubectl工具:
mkdir -p $HOME/.x 2 X : =kube
sudo cp -i /etc/kubernetes6 g */admin.conf $HOME/Z c y U ] . n w s.kube/config
su| h & vdo ch | Go V # e y kwn $(id -u):$(id -g) $HOMEL { w u t 0 m ~/.kube/config
$ kubectl get nodes 查看节点是否rk # D neady
6.安装Pod网络插件
$ k! $ + % ( Aubectl apply -f https://raw.githubusercontE ? 1 ) ?ent.com/coreos/flannel/master/Documentation/kube-flannel.yml
7.加入Kubernete Node
需要在Node端执行
kubeadm join 172V ? ; 0 F.16.100.234:6443 --token 0ieaj1.qtkmnn7d1i35gg0z \
--discovery-token-ca-cert-hw C z a ~ 3 c I *ash sha256:ae06a272dda677e59: u ^ 9 _ p66b D e j | ( } 3 Ydb22882d409be709178be14c062926de356c5d6b q = I 0 x48133
8.测试集
$ kubectl create deplo: f $yment nginx --image=nginx
$ kubectl expose deployment nginx --C y ? R p X X mport=80 --type=NodePort
$ kubectl get pod,svc
9.部署Dashboard
9.1.wget https://raw.githubusercontent._ s * ]com/kuberD 4 _netes/dashboard/v2.0.0-beta5/aio/deploy/recommended.yaml
9.2修改yml文件(默T B y :认Dashboard只能集群内部访问,修改Service为NodePort类型,暴露到外部:不修改的话8 ? 3 # 9 ] ~外部不能访问。)
kind: Service
apiV. k D _ 1 | S Fersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
n^ 0 , g J u r Pamespace: kube-system
spec:
type: NodePort(需要增加)
ports:

  • port: 443
    targetPort: 8443
    nodN s @ ,ePort: 30001(需要增加)
    selector:
    k8s-app: kubernetes-dashboard
    9.3
    kub& 2 2 | H 1 uectl apply -f recommended.yaml
    kubectl get pods,svc -n kube-system (查看安装情况)
    9.4.创建service account并绑定默认cluster-admin管理员集群n W x Y l ~ r A色:
    $ kubecL & Y : $ k l & Etl# I 4 , s O create serv] % F 3 wiceaccou1 E J ) 3nt dashboard-admin -n kube-system
    $ kubectl crR 3 b f * R % [ Qeate clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-sH = ? g lystem:dashboard-admin
    $ kubJ ? }ectl describe secrets -n kube-} 1 a 6 v N F v Nsystem $(kubectl -n kube3 | q-system get secret | awk '/dashboard-admin/{print $1}')
    记录保存token,使用令牌登录
    登录地址https://172.16.100.234:30001/#/login,复制令牌即可登录

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