[关闭]
@zhangyy 2020-11-30T18:11:47.000000Z 字数 7839 阅读 375

使用kubeadmin 配置 k8s 集群

kubernetes系列


  • 一:系统初始化

  • 二:kubernetes 安装


一:系统初始化

1.1 系统主机名

  1. 192.168.100.11 node01.flyfish
  2. 192.168.100.12 node02.flyfish
  3. 192.168.100.13 node03.flyfish
  4. 192.168.100.14 node04.flyfish
  5. 192.168.100.15 node05.flyfish
  6. 192.168.100.16 node06.flyfish
  7. 192.168.100.17 node07.flyfish

image_1e1im5e05tf118u5n8k18t61jnf37.png-57kB


1.2 关闭firewalld 清空iptables 与 selinux 规则

  1. 系统节点全部执行:
  2. systemctl stop firewalld && systemctl disable firewalld && yum -y install iptables-services && systemctl start iptables && systemctl enable iptables && iptables -F && service iptables save
  1. 关闭 SELINUXswap 内存
  2. swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
  3. setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

image_1dt2ssl4j1m715b2du7pr31l2ff.png-230.7kB

image_1dt2t3bdg1rk41r0s190kecdtdq23.png-66.3kB


1.3 安装 依赖包

  1. 全部节点安装
  2. yum install -y conntrack ntpdate ntp ipvsadm ipset jq iptables curl sysstat libseccomp wget vim net-tools git

image_1dt2svjhtrii27cuu2eug1dbc19.png-279.7kB

image_1dt2t6djs1ia0t701tgc8afbd22g.png-384.2kB

1.4 升级调整内核参数,对于 K8S

  1. 所有节点都执行
  2. cat > kubernetes.conf <<EOF
  3. net.bridge.bridge-nf-call-iptables=1
  4. net.bridge.bridge-nf-call-ip6tables=1
  5. net.ipv4.ip_forward=1
  6. net.ipv4.tcp_tw_recycle=0
  7. vm.swappiness=0 # 禁止使用 swap 空间,只有当系统 OOM 时才允许使用它 vm.overcommit_memory=1 # 不检查物理内存是否够用
  8. vm.panic_on_oom=0 # 开启 OOM
  9. fs.inotify.max_user_instances=8192
  10. fs.inotify.max_user_watches=1048576
  11. fs.file-max=52706963
  12. fs.nr_open=52706963
  13. net.ipv6.conf.all.disable_ipv6=1
  14. net.netfilter.nf_conntrack_max=2310720
  15. EOF
  16. cp kubernetes.conf /etc/sysctl.d/kubernetes.conf
  17. sysctl -p /etc/sysctl.d/kubernetes.conf

image_1dt2tjj4g9eduel18fbf7c1ofj3j.png-148.7kB

image_1dt2tk53ej2mtt010hd195dd1640.png-186.3kB


1.5 调整系统时区

  1. # 设置系统时区为 中国/上海 timedatectl set-timezone Asia/Shanghai
  2. # 将当前的 UTC 时间写入硬件时钟 timedatectl set-local-rtc 0
  3. # 重启依赖于系统时间的服务
  4. systemctl restart rsyslog && systemctl restart crond

1.6 关闭系统不需要服务

  1. systemctl stop postfix && systemctl disable postfix

image_1dt2tsmq91g6n1s9klui1c851j5s4d.png-72.3kB

1.7 设置 rsyslogd 和 systemd journald

  1. mkdir /var/log/journal # 持久化保存日志的目录
  2. mkdir /etc/systemd/journald.conf.d
  3. cat > /etc/systemd/journald.conf.d/99-prophet.conf <<EOF
  4. [Journal]
  5. # 持久化保存到磁盘
  6. Storage=persistent
  7. # 压缩历史日志
  8. Compress=yes
  9. SyncIntervalSec=5m
  10. RateLimitInterval=30s
  11. RateLimitBurst=1000
  12. # 最大占用空间 10G
  13. SystemMaxUse=10G
  14. # 单日志文件最大 200M
  15. SystemMaxFileSize=200M
  16. # 日志保存时间 2 周
  17. MaxRetentionSec=2week
  18. # 不将日志转发到 syslog
  19. ForwardToSyslog=no
  20. EOF
  21. systemctl restart systemd-journald

image_1dt2ugl8apjk1uf4921am2s0i1g.png-150kB

1.7 升级系统内核为 4.44

  1. CentOS 7.x 系统自带的 3.10.x 内核存在一些 Bugs,导致运行的 DockerKubernetes 不稳定,例如: rpm -Uvh
  2. http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
  3. rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
  4. # 安装完成后检查 /boot/grub2/grub.cfg 中对应内核 menuentry 中是否包含 initrd16 配置,如果没有,再安装 一次!
  5. yum --enablerepo=elrepo-kernel install -y kernel-lt
  6. # 设置开机从新内核启动
  7. grub2-set-default "CentOS Linux (4.4.182-1.el7.elrepo.x86_64) 7 (Core)"
  8. reboot
  9. # 重启后安装内核源文件
  10. yum --enablerepo=elrepo-kernel install kernel-lt-devel-$(uname -r) kernel-lt-headers-$(uname -r)
  11. -----------
  12. 或者:
  13. rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org && \
  14. rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm && \
  15. yum --disablerepo=\* --enablerepo=elrepo-kernel repolist && \
  16. yum --disablerepo=\* --enablerepo=elrepo-kernel install -y kernel-ml.x86_64 && \
  17. yum remove -y kernel-tools-libs.x86_64 kernel-tools.x86_64 && \
  18. yum --disablerepo=\* --enablerepo=elrepo-kernel install -y kernel-ml-tools.x86_64 && \
  19. grub2-set-default 0

image_1dt2uvsadhr1teqkqf11iu11qe2d.png-129.2kB

image_1dt2v0mbv1h2q11cq4v1ooj2m32q.png-215.2kB

image_1dt2velm4c9i1v4rg4goqv1fkl37.png-92.5kB

image_1dt2vgk5hhk3tf9ss31mp7enn3k.png-39.1kB

1.8 kube-proxy开启ipvs的前置条件

  1. modprobe br_netfilter
  2. cat > /etc/sysconfig/modules/ipvs.modules <<EOF
  3. #!/bin/bash
  4. modprobe -- ip_vs
  5. modprobe -- ip_vs_rr
  6. modprobe -- ip_vs_wrr
  7. modprobe -- ip_vs_sh
  8. modprobe -- nf_conntrack_ipv4
  9. EOF
  10. chmod 755 /etc/sysconfig/modules/ipvs.modules
  11. bash /etc/sysconfig/modules/ipvs.modules
  12. lsmod | grep -e ip_vs -e nf_conntrack_ipv4

image_1dt2vpt1t1g7bulhkp11nve1acf44.png-188kB

二: 开始安装k8s 集群

2.1 安装docker

  1. 机器节点都执行:
  2. yum install -y yum-utils device-mapper-persistent-data lvm2
  3. yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  4. yum update -y && yum install docker-ce-18.09.9 docker-ce-cli-18.09.9 containerd.io -y
  5. 重启机器: reboot
  6. 查看内核版本: uname -r
  7. 在加载: grub2-set-default "CentOS Linux (4.4.182-1.el7.elrepo.x86_64) 7 (Core)" && reboot
  8. 如果还不行
  9. 就改 文件 vim /etc/grub2.cfg 注释掉 3.10 内核
  10. 保证 内核的版本 4.4
  11. service docker start
  12. chkconfig docker on
  13. ## 创建 /etc/docker 目录
  14. mkdir /etc/docker
  15. # 配置 daemon.
  16. cat > /etc/docker/daemon.json <<EOF
  17. { "exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": { "max-size": "100m" } }
  18. EOF
  19. mkdir -p /etc/systemd/system/docker.service.d
  20. # 重启docker服务
  21. systemctl daemon-reload && systemctl restart docker && systemctl enable docker

image_1dt308d8q15hhr3452a4cu1irp4k.png-201.8kB

image_1dt308sh919rj12edsh71n5a1cn051.png-61.7kB

image_1dt3267rr1ldcil8fnpec81ue25u.png-231.4kB

image_1dt326vloij4lpqqr3jk8kj96b.png-67kB

image_1dt328r2f8o72eb1g6tjt766h78.png-55.2kB

image_1dt32jaf7jso1qgvrt4lqff59a8.png-217.7kB


2.2 安装 Kubeadm (主从配置)

  1. cat >> /etc/yum.repos.d/kubernetes.repo << EOF
  2. [kubernetes]
  3. name=Kubernetes
  4. baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
  5. enabled=1
  6. gpgcheck=0
  7. repo_gpgcheck=0
  8. gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
  9. http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
  10. EOF

image_1dt3300p81kureepqs6bfco7tbf.png-74.8kB

image_1dt32tlcpahnqoqb69kelsunb2.png-72.3kB

  1. yum -y install kubeadm-1.15.1 kubectl-1.15.1 kubelet-1.15.1
  2. systemctl enable kubelet.service

image_1dt332hhd1urc303g3m1ug12knbs.png-245.6kB

image_1dt334tevh8p1lf4knqfi4s0cc9.png-59.7kB

  1. 节点全部配置:
  2. 上传导入文件
  3. kubeadm-basic.images.tar.gz
  4. tar -zxvf kubeadm-basic.images.tar.gz
  5. ### 写一个导入脚本
  6. vim load-image.sh
  7. ---
  8. #!/bin/bash
  9. ls /root/kubeadm-basic.images > /tmp/image-list.txt
  10. cd /root/kubeadm-basic.images
  11. for i in $(cat /tmp/image-list.txt)
  12. do
  13. docker load -i $i
  14. done
  15. ---
  16. chmod +x load-image.sh
  17. ./load-image.sh

image_1dt33rtid1ta61k8bnl2bov1kqrcm.png-68.5kB

image_1dt33tclbfl2lp9a73jtq0gdg.png-58.3kB

image_1dt33snpq16n1pq71t8t18qdv45d3.png-259.8kB

image_1dt33ut6q1b37lbs1kllpdofo8dt.png-79.8kB

  1. scp kubeadm-basic.images.tar.gz load-image.sh root@172.17.100.2:/root
  2. scp kubeadm-basic.images.tar.gz load-image.sh root@172.17.100.3:/root

image_1dt341lmuhio12rv1ffbie8161fea.png-84.2kB

  1. login : 172.17.100.2
  2. tar -zxvf kubeadm-basic.images.tar.gz
  3. ./load-image.sh
  4. login: 172.17.100.3
  5. tar -zxvf kubeadm-basic.images.tar.gz
  6. ./load-image.sh

image_1dt345ukq1gm648b12ifkofdnden.png-179.7kB

image_1dt346elu1h1is64125014u61i59f4.png-186.6kB

2.3 初始化主节点

  1. kubeadm config print init-defaults > kubeadm-config.yaml
  2. vim kubeadm-config.yaml
  3. ---
  4. advertiseAddress: 192.168.100.11 (改为主节点IP地址)
  5. 增加
  6. podSubnet: "10.244.0.0/16"
  7. ---
  8. ---
  9. apiVersion: kubeproxy.config.k8s.io/v1alpha1
  10. kind: KubeProxyConfiguration
  11. featureGates:
  12. SupportIPVSProxyMode: true
  13. mode: "ipvs"
  14. ---

image_1e1gqthmb1j0k124dslq14vb1s49p.png-131.3kB

1.png-47.5kB

  1. kubeadm init --config=kubeadm-config.yaml --experimental-upload-certs | tee kubeadm-init.log

image_1dt35eaegkga9ep1tht1j8c1raj15.png-302.4kB

image_1e1gqvm0qeskba0f4d3b1i3u16.png-166.6kB

  1. mkdir -p $HOME/.kube
  2. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  3. sudo chown $(id -u):$(id -g) $HOME/.kube/config

image_1dt35ppm41bgs14unub1b3u1snb2s.png-82.5kB

  1. kubectl get node

image_1dt35r9nd5nl1q9o1kmea5dil39.png-37kB

2.4 准备flannel 的 网络

  1. cd /etc/kubernetes/manifests/
  2. vim kube-controller-manager.yaml
  3. ---
  4. 增加:
  5. - --allocate-node-cidrs=true
  6. - --cluster-cidr=10.244.0.0/16
  7. ---

image_1dtinj6a64va1df9b061g50146r9.png-159.7kB

  1. mkdir -p k8s-install/{core,plugin}
  2. mv kubeadm-init.log k8s-install/core
  3. mv kubeadm-config.yaml k8s-install/core
  4. cd k8s-install/plugin
  5. wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
  6. vim kube-flannel.yml
  7. -----
  8. 因为quay.io/coreos/flannel:v0.11.0-amd64 这个 quay.io 这个 在国外需要翻墙,才能下载到flannel:v0.11.0-amd64 资源 所以要改掉 改成:quay-mirror.qiniu.com
  9. -----

image_1dtio16j311al1g1lvv21svs9v3m.png-159kB

  1. kubectl create -f kube-flannel.yml
  2. kubectl get pod -n kube-system

image_1dt36gvmgr4nt1clpln9a170a3m.png-198.3kB

image_1dt36hi5d1nmm1db717vtp8c1npb43.png-126.6kB

image_1dt36i0v6dim1vej60alh13mb4g.png-100kB

image_1dt36k59cav21sei19lrjqe1mug4t.png-109.5kB

image_1dt36lnpj130o1v4a1b231olhgv05a.png-42.1kB

image_1dtiodj7i1ub9kdfdug1seok4n1t.png-331.3kB

2.5 将其它节点加入到这个集群里面

  1. node01.flyfish
  2. cd /root/k8s-install/
  3. cat kubeadm-init.log
  4. ---
  5. 到最后一行有加入命令
  6. kubeadm join 192.168.100.11:6443 --token abcdef.0123456789abcdef \
  7. --discovery-token-ca-cert-hash sha256:1e93d61321b0fee1993134f1379ccb73cb9bd79f5cb4af965be1abb77dae295b
  8. ---
  9. node02.flyfish:
  10. kubeadm join 192.168.100.11:6443 --token abcdef.0123456789abcdef \
  11. --discovery-token-ca-cert-hash sha256:1e93d61321b0fee1993134f1379ccb73cb9bd79f5cb4af965be1abb77dae295b
  12. node03.flyfish:
  13. kubeadm join 192.168.100.11:6443 --token abcdef.0123456789abcdef \
  14. --discovery-token-ca-cert-hash sha256:1e93d61321b0fee1993134f1379ccb73cb9bd79f5cb4af965be1abb77dae295b

image_1dtio885bqua1p312u317ku19cd13.png-112kB

image_1dtio9uqn13fl1ge158h1ur54mm1g.png-196.2kB

  1. node01.flyfish:
  2. kubectl get node
  3. kubectl get pods -n kube-system
  4. 至此k8s 三节点集群完成安装:

image_1dtiogg2f198mshs1api18amj3r2a.png-193.3kB


2.6 部署dashboard 界面

  1. 注:在node01.flyfish节点上进行如下操作
  2. 1.创建Dashboardyaml文件
  3. wget https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml
  4. ----
  5. 使用如下命令或直接手动编辑kubernetes-dashboard.yaml文件
  6. sed -i 's/k8s.gcr.io/registry.cn-hangzhou.aliyuncs.com/kuberneters/g' kubernetes-dashboard.yaml
  7. sed -i '/targetPort:/a\ \ \ \ \ \ nodePort: 30001\n\ \ type: NodePort' kubernetes-dashboard.yaml
  8. ---
  9. 手动编辑kubernetes-dashboard.yaml文件时,需要修改两处内容,首先在Dashboard Deployment部分修改Dashboard镜像下载链接,由于默认从官方社区下载,而不“科学上网”是无法下载的,因此修改为:image: registry.cn-hangzhou.aliyuncs.com/kuberneters/kubernetes-dashboard-amd64:v1.10.1 修改后内容如图:

image_1e1gvsfur13k41o4f18lamd1ef99.png-76.9kB

  1. 此外,需要在Dashboard Service内容加入nodePort 30001type: NodePort两项内容,将Dashboard访问端口映射为节点端口,以供外部访问,编辑完成后,状态如图

image_1e1gvuhuj2716fde2nadr7uqm.png-132.7kB

  1. 部署Dashboard
  2. kubectl create -f kubernetes-dashboard.yaml
  3. 创建完成后,检查相关服务运行状态
  4. kubectl get deployment kubernetes-dashboard -n kube-system
  5. kubectl get pods -n kube-system -o wide
  6. kubectl get services -n kube-system
  7. netstat -ntlp|grep 30001

image_1e1h022bg1rcf1b18s7o1k2q121e13.png-267kB

  1. Firefox浏览器输入Dashboard访问地址:
  2. https://192.168.100.12:30001

  1. 授权令牌
  2. kubectl create serviceaccount dashboard-admin -n kube-system
  3. kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
  4. kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')

image_1e1h086pt1mvmem71q05plv17v61g.png-227.2kB

  1. 使用输出的token登录Dashboard

image_1e1h09pb78lgiabk1jv0j1nl72d.png-39kB

  1. 认证通过后,登录Dashboard首页如图

image_1e1h0aurs1l4u1qq05jcqp152p2q.png-303.5kB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注