1. # 只在 master 节点执行
    2. # 替换 x.x.x.x 为 master 节点实际 IP(请使用内网 IP)
    3. # export 命令只在
    4. ell 会话中有效,开启新的 shell 窗口后,如果要继续安装过程,请重新执行此处的 export 命令
    5. export MASTER_IP=x.x.x.x
    6. # 替换 apiserver.demo 为 您想要的 dnsName
    7. export APISERVER_NAME=apiserver.demo
    8. # Kubernetes 容器组所在的网段,该网段安装完成后,由 kubernetes 创建,事先并不存在于您的物理网络中
    9. export POD_SUBNET=10.100.0.1/16
    10. echo "${MASTER_IP} ${APISERVER_NAME}" >> /etc/hosts
    11. curl -sSL https://kuboard.cn/install-script/v1.20.x/init_master.sh | sh -s 1.20.1
    1. # 在 master 节点和 worker 节点都要执行
    2. # 最后一个参数 1.18.2 用于指定 kubenetes 版本,支持所有 1.18.x 版本的安装
    3. # 腾讯云 docker hub 镜像
    4. # export REGISTRY_MIRROR="https://mirror.ccs.tencentyun.com"
    5. # DaoCloud 镜像
    6. # export REGISTRY_MIRROR="http://f1361db2.m.daocloud.io"
    7. # 阿里云 docker hub 镜像
    8. export REGISTRY_MIRROR=https://registry.cn-hangzhou.aliyuncs.com
    1. #!/bin/bash
    2. # 在 master 节点和 worker 节点都要执行
    3. # 安装 docker
    4. # 参考文档如下
    5. # https://docs.docker.com/install/linux/docker-ce/centos/
    6. # https://docs.docker.com/install/linux/linux-postinstall/
    7. # 卸载旧版本
    8. yum remove -y docker \
    9. docker-client \
    10. docker-client-latest \
    11. docker-common \
    12. docker-latest \
    13. docker-latest-logrotate \
    14. docker-logrotate \
    15. docker-selinux \
    16. docker-engine-selinux \
    17. docker-engine
    18. # 设置 yum repository
    19. yum install -y yum-utils \
    20. device-mapper-persistent-data \
    21. lvm2
    22. yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    23. # 安装并启动 docker
    24. yum install -y docker-ce-19.03.9 docker-ce-cli-19.03.9 containerd.io
    25. systemctl enable docker
    26. systemctl start docker
    27. # 安装 nfs-utils
    28. # 必须先安装 nfs-utils 才能挂载 nfs 网络存储
    29. yum install -y nfs-utils
    30. yum install -y wget
    31. # 关闭 防火墙
    32. systemctl stop firewalld
    33. systemctl disable firewalld
    34. # 关闭 SeLinux
    35. setenforce 0
    36. sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
    37. # 关闭 swap
    38. swapoff -a
    39. yes | cp /etc/fstab /etc/fstab_bak
    40. cat /etc/fstab_bak |grep -v swap > /etc/fstab
    41. # 修改 /etc/sysctl.conf
    42. # 如果有配置,则修改
    43. sed -i "s#^net.ipv4.ip_forward.*#net.ipv4.ip_forward=1#g" /etc/sysctl.conf
    44. sed -i "s#^net.bridge.bridge-nf-call-ip6tables.*#net.bridge.bridge-nf-call-ip6tables=1#g" /etc/sysctl.conf
    45. sed -i "s#^net.bridge.bridge-nf-call-iptables.*#net.bridge.bridge-nf-call-iptables=1#g" /etc/sysctl.conf
    46. sed -i "s#^net.ipv6.conf.all.disable_ipv6.*#net.ipv6.conf.all.disable_ipv6=1#g" /etc/sysctl.conf
    47. sed -i "s#^net.ipv6.conf.default.disable_ipv6.*#net.ipv6.conf.default.disable_ipv6=1#g" /etc/sysctl.conf
    48. sed -i "s#^net.ipv6.conf.lo.disable_ipv6.*#net.ipv6.conf.lo.disable_ipv6=1#g" /etc/sysctl.conf
    49. sed -i "s#^net.ipv6.conf.all.forwarding.*#net.ipv6.conf.all.forwarding=1#g" /etc/sysctl.conf
    50. # 可能没有,追加
    51. echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
    52. echo "net.bridge.bridge-nf-call-ip6tables = 1" >> /etc/sysctl.conf
    53. echo "net.bridge.bridge-nf-call-iptables = 1" >> /etc/sysctl.conf
    54. echo "net.ipv6.conf.all.disable_ipv6 = 1" >> /etc/sysctl.conf
    55. echo "net.ipv6.conf.default.disable_ipv6 = 1" >> /etc/sysctl.conf
    56. echo "net.ipv6.conf.lo.disable_ipv6 = 1" >> /etc/sysctl.conf
    57. echo "net.ipv6.conf.all.forwarding = 1" >> /etc/sysctl.conf
    58. # 执行命令以应用
    59. sysctl -p
    60. # 配置K8S的yum源
    61. cat <<EOF > /etc/yum.repos.d/kubernetes.repo
    62. [kubernetes]
    63. name=Kubernetes
    64. baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
    65. enabled=1
    66. gpgcheck=0
    67. repo_gpgcheck=0
    68. gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
    69. http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
    70. EOF
    71. # 卸载旧版本
    72. yum remove -y kubelet kubeadm kubectl
    73. # 安装kubelet、kubeadm、kubectl
    74. # 将 ${1} 替换为 kubernetes 版本号,例如 1.17.2
    75. # yum install -y kubelet-${1} kubeadm-${1} kubectl-${1}
    76. yum install -y kubelet-1.18.3 kubeadm-1.18.3 kubectl-1.18.3
    77. # 修改docker Cgroup Driver为systemd
    78. # # 将/usr/lib/systemd/system/docker.service文件中的这一行 ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
    79. # # 修改为 ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --exec-opt native.cgroupdriver=systemd
    80. # 如果不修改,在添加 worker 节点时可能会碰到如下错误
    81. # [WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd".
    82. # Please follow the guide at https://kubernetes.io/docs/setup/cri/
    83. sed -i "s#^ExecStart=/usr/bin/dockerd.*#ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --exec-opt native.cgroupdriver=systemd#g" /usr/lib/systemd/system/docker.service
    84. # 设置 docker 镜像,提高 docker 镜像下载速度和稳定性
    85. # 如果您访问 https://hub.docker.io 速度非常稳定,亦可以跳过这个步骤
    86. curl -sSL https://kuboard.cn/install-script/set_mirror.sh | sh -s ${REGISTRY_MIRROR}
    87. # 重启 docker,并启动 kubelet
    88. systemctl daemon-reload
    89. systemctl restart docker
    90. systemctl enable kubelet && systemctl start kubelet
    91. docker version

    关于初始化时用到的环境变量

    • APISERVER_NAME 不能是 master 的 hostname
    • APISERVER_NAME 必须全为小写字母、数字、小数点,不能包含减号
    • POD_SUBNET 所使用的网段不能与 master节点/worker节点 所在的网段重叠。该字段的取值为一个 CIDR 值,如果您对 CIDR 这个概念还不熟悉,请仍然执行 export POD_SUBNET=10.100.0.1/16 命令,不做修改
    1. # 只在 master 节点执行
    2. # 替换 x.x.x.x 为 master 节点的内网IP
    3. # export 命令只在当前 shell 会话中有效,开启新的 shell 窗口后,如果要继续安装过程,请重新执行此处的 export 命令
    4. export MASTER_IP=192.168.13.99
    5. # 替换 apiserver.demo 为 您想要的 dnsName
    6. export APISERVER_NAME=apiserver.demo
    7. # Kubernetes 容器组所在的网段,该网段安装完成后,由 kubernetes 创建,事先并不存在于您的物理网络中
    8. export POD_SUBNET=10.100.0.1/16
    9. echo "${MASTER_IP} ${APISERVER_NAME}" >> /etc/hosts
    1. #!/bin/bash
    2. # 只在 master 节点执行
    3. # 脚本出错时终止执行
    4. set -e
    5. if [ ${#POD_SUBNET} -eq 0 ] || [ ${#APISERVER_NAME} -eq 0 ]; then
    6. echo -e "\033[31;1m请确保您已经设置了环境变量 POD_SUBNET 和 APISERVER_NAME \033[0m"
    7. echo 当前POD_SUBNET=$POD_SUBNET
    8. echo 当前APISERVER_NAME=$APISERVER_NAME
    9. exit 1
    10. fi
    11. # 查看完整配置选项 https://godoc.org/k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta2
    12. rm -f ./kubeadm-config.yaml
    13. cat <<EOF > ./kubeadm-config.yaml
    14. apiVersion: kubeadm.k8s.io/v1beta2
    15. kind: ClusterConfiguration
    16. kubernetesVersion: v1.18.3
    17. imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
    18. controlPlaneEndpoint: "${APISERVER_NAME}:6443"
    19. networking:
    20. serviceSubnet: "10.96.0.0/16"
    21. podSubnet: "${POD_SUBNET}"
    22. dnsDomain: "cluster.local"
    23. EOF
    24. # kubeadm init
    25. # 根据您服务器网速的情况,您需要等候 3 - 10 分钟
    26. kubeadm init --config=kubeadm-config.yaml --upload-certs
    27. # 配置 kubectl
    28. rm -rf /root/.kube/
    29. mkdir /root/.kube/
    30. cp -i /etc/kubernetes/admin.conf /root/.kube/config
    31. # 安装 calico 网络插件
    32. # 参考文档 https://docs.projectcalico.org/v3.13/getting-started/kubernetes/self-managed-onprem/onpremises
    33. echo "安装calico-3.13.1"
    34. rm -f calico-3.13.1.yaml
    35. wget https://kuboard.cn/install-script/calico/calico-3.13.1.yaml
    36. kubectl apply -f calico-3.13.1.yaml