博客列表 >Centos 7.x 线上安装 Kubernetes

Centos 7.x 线上安装 Kubernetes

哈
原创
2022年03月25日 10:30:46535浏览

镜像下载、域名解析、时间同步请点击 阿里云开源镜像站

安装依赖包

  1. yum install -y conntrack ntpdate ntp ipvsadm ipset jq iptables curl systat libseccomp wget vim net-tools git iptables-services

关闭防火墙,为iptables设置规则

  1. systemctl stop firewalld && systemctl disable firewalld && systemctl status firewalld
  2. systemctl start iptables && systemctl enable iptables && iptables -F && service iptables save

关闭SWAP 和 SELINUX

  1. swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
  2. setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

调整内核参数,对于k8s

  1. cat > /etc/sysctl.d/kubernetes.conf << EOF
  2. net.bridge.bridge-nf-call-iptables=1 #开启网桥模式
  3. net.bridge.bridge-nf-call-ip6tables=1 #开启网桥模式
  4. net.ipv4.ip_forward=1
  5. net.ipv4.tcp_tw_recycle=0
  6. vm.swappiness=0 #禁止使用 swap 空间, 只有当系统 OOM 时才允许使用它
  7. 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 #关闭IPV6协议
  14. net.netfilter.nf_conntrack_max=2310720
  15. EOF
  16. sysctl -p /etc/sysctl.d/kubernetes.conf

调整系统时区

  1. # 设置系统时区为 中国/上海
  2. timedatectl set-timezone Asia/Shanghai

关闭系统不需要服务,postfix是邮件服务

  1. systemctl stop postfix && systemctl disable postfix

设置rsyslogd 和 systemd journald

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

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 && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4

安装 Docker 软件

  1. # 配置daemon
  2. cat > /etc/docker/daemon.json << EOF
  3. {
  4. "exec-opts": ["native.cgroupdriver=systemd"],
  5. "log-driver": "json-file",
  6. "log-opts": {
  7. "max-size": "100m"
  8. }
  9. }
  10. EOF
  11. # 重启docker
  12. systemctl daemon-reload && systemctl restart docker

安装 Kubeadm (主从配置)

  1. # 配置yum源
  2. cat > /etc/yum.repos.d/kubernetes.repo << EOF
  3. [kubernetes]
  4. name=kubernetes
  5. baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
  6. enabled=1
  7. gpgcheck=0
  8. repo_gpgcheck=0
  9. gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
  10. EOF
  11. # 安装 kubeadm 初始化工具,kubectl 命令行管理工具,kubelet
  12. yum -y install kubeadm-1.15.1 kubectl-1.15.1 kubelet-1.15.1
  13. # 设置开机自启
  14. systemctl enable kubelet

初始化主节点

注意:
1.advertiseAddress需要更换为master服务器的ip地址

  1. # 打印默认的初始化文件,打印到kubeadm-init.yaml
  2. kubeadm config print init-defaults > kubeadm-init.yaml
  3. # 修改
  4. cat > kubeadm-init.yaml << EOF
  5. apiVersion: kubeadm.k8s.io/v1beta2
  6. bootstrapTokens:
  7. - groups:
  8. - system:bootstrappers:kubeadm:default-node-token
  9. token: abcdef.0123456789abcdef
  10. ttl: 24h0m0s
  11. usages:
  12. - signing
  13. - authentication
  14. kind: InitConfiguration
  15. localAPIEndpoint:
  16. advertiseAddress: xx.xx.xx.xx # master节点的IP地址
  17. bindPort: 6443
  18. nodeRegistration:
  19. criSocket: /var/run/dockershim.sock
  20. name: master
  21. taints:
  22. - effect: NoSchedule
  23. key: node-role.kubernetes.io/master
  24. ---
  25. apiServer:
  26. timeoutForControlPlane: 4m0s
  27. apiVersion: kubeadm.k8s.io/v1beta2
  28. certificatesDir: /etc/kubernetes/pki
  29. clusterName: kubernetes
  30. controllerManager: {}
  31. dns:
  32. type: CoreDNS
  33. etcd:
  34. local:
  35. dataDir: /var/lib/etcd
  36. imageRepository: k8s.gcr.io
  37. kind: ClusterConfiguration
  38. kubernetesVersion: v1.15.1
  39. networking:
  40. dnsDomain: cluster.local
  41. podSubnet: 10.244.0.0/16
  42. serviceSubnet: 10.96.0.0/12
  43. scheduler: {}
  44. ---
  45. apiVersion: kubeproxy.config.k8s.io/v1alpha1
  46. kind: KubeProxyConfiguration
  47. featureGates:
  48. SupportIPVSProxyMode: true
  49. mode: ipvs
  50. EOF
  51. # 启动
  52. kubeadm init --config=kubeadm-init.yaml | tee kubeadm-init.log
  53. mkdir -p $HOME/.kube
  54. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  55. sudo chown $(id -u):$(id -g) $HOME/.kube/config
  56. mkdir -p /root/install-k8s/core
  57. mv /root/kubeadm-init.* /root/install-k8s/core

安装 flannel

  1. mkdir -p /root/install-k8s/plugin/flannel
  2. cd /root/install-k8s/plugin/flannel
  3. wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
  4. kubectl apply -f /root/install-k8s/plugin/flannel/kube-flannel.yml

本文转自:https://blog.csdn.net/weixin_45456679/article/details/123423237

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议