CentOS一键安装K8s cluster

作者: 正Neng量 | 来源:发表于2020-02-26 00:02 被阅读0次

直接运行脚本,一步完成配置。需要至少2台Centos7.4+,1个master-node,1-N个worker-node.

配置master节点

#!/bin/bash

# user could change hostname
export hostname=master-node

# k8s version used
export kube_version=1.17.0

# update yum 
sudo yum update

# add kubenetes repository
sudo bash -c 'cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF'

# install docker
if ! [ -x "$(command -v docker)" ]; then
  echo "Docker installing ..."
  sudo yum install docker
  sudo systemctl enable docker.service
  sudo systemctl start docker
else
  echo "docker has already been installed"
fi

# set user into docker group, need logout to take effect
#sudo usermod -aG docker tas

# install kubeadm kubelet kubectl
sudo yum install -y kubelet-$kube_version  kubeadm-$kube_version kubectl-$kube_version
sudo systemctl enable kubelet
sudo systemctl start kubelet

#turn off swap
sudo swapoff -a

# set hostname and add line to hosts file
sudo hostnamectl set-hostname $hostname
sudo sed -i "1i127.0.0.1       ${hostname}" /etc/hosts

#Update Iptables Settings
sudo bash -c 'cat  <<EOF >> /etc/sysctl.d/99-sysctl.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF'
sudo sysctl --system

# Configure Firewall
firewall_status=$(sudo firewall-cmd --state)
if [ $firewall_status == "running" ]; then
  sudo firewall-cmd --permanent --add-port=6443/tcp
  sudo firewall-cmd --permanent --add-port=2379-2380/tcp
  sudo firewall-cmd --permanent --add-port=10250/tcp
  sudo firewall-cmd --permanent --add-port=10251/tcp
  sudo firewall-cmd --permanent --add-port=10252/tcp
  sudo firewall-cmd --permanent --add-port=10255/tcp
  sudo firewall-cmd --permanent --add-port=8472/udp
  sudo firewall-cmd --add-masquerade --permanent
  sudo firewall-cmd --permanent --add-port=53/tcp
  sudo firewall-cmd --permanent --add-port=30000-32767/tcp
  sudo firewall-cmd --reload
fi

#Disable firewalld for the time being(some issues with firewall)
#sudo systemctl stop firewalld

#Disable SELinux
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config

# For master-node
sudo kubeadm init --pod-network-cidr=10.244.0.0/16

mkdir -p $HOME/.kube
sudo cp /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
#export KUBECONFIG=/etc/kubernetes/kubelet.conf

#Deploy Pod Network to Cluster
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

执行上面的脚本,从返回消息中找到如下信息,将在配置worker节点的脚本中使用:

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 10.97.170.98:6443 --token fwhlz2.r0en0tkopmsjinit \
    --discovery-token-ca-cert-hash sha256:9844a8db82627a49838966d025d2d6b88f1ad7baabafe69d4f98d9979653a155 

配置worker节点

  1. 自定义第四行hostname,如worker1, worker2
  2. 修改最后一行,用上一步中的返回信息替换,然后执行脚本

# user could change hostname
export hostname=worker-node1

# k8s version used
export kube_version=1.17.0

# update yum 
sudo yum update

# add kubenetes repository
sudo bash -c 'cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF'

# install docker
if ! [ -x "$(command -v docker)" ]; then
  echo "Docker installing ..."
  sudo yum install docker
  sudo systemctl enable docker.service
  sudo systemctl start docker
else
  echo "docker has already been installed"
fi

# set user into docker group, need logout to take effect
#sudo usermod -aG docker tas

# install kubeadm kubelet kubectl
sudo yum install -y kubelet-$kube_version  kubeadm-$kube_version kubectl-$kube_version
sudo systemctl enable kubelet
sudo systemctl start kubelet

#turn off swap
sudo swapoff -a

# set hostname and add line to hosts file
sudo hostnamectl set-hostname $hostname
sudo sed -i "1i127.0.0.1       ${hostname}" /etc/hosts

#Update Iptables Settings
sudo bash -c 'cat  <<EOF >> /etc/sysctl.d/99-sysctl.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF'
sudo sysctl --system

# Configure Firewall
firewall_status=$(sudo firewall-cmd --state)
if [ $firewall_status == "running" ]; then
  sudo firewall-cmd --permanent --add-port=6443/tcp
  sudo firewall-cmd --permanent --add-port=2379-2380/tcp
  sudo firewall-cmd --permanent --add-port=10250/tcp
  sudo firewall-cmd --permanent --add-port=10251/tcp
  sudo firewall-cmd --permanent --add-port=10252/tcp
  sudo firewall-cmd --permanent --add-port=10255/tcp
  sudo firewall-cmd --permanent --add-port=8472/udp
  sudo firewall-cmd --permanent --add-port=30000-32767/tcp
  sudo firewall-cmd --add-masquerade --permanent
  sudo firewall-cmd --permanent --add-port=53/tcp
  sudo firewall-cmd --reload
fi
#disable firewall for the time being(some issues)
#sudo systemctl stop firewalld

# copy the output from master node kubeadm init
kubeadm join 10.97.170.98:6443 --token fwhlz2.r0en0tkopmsjinit  --discovery-token-ca-cert-hash sha256:9844a8db82627a49838966d025d2d6b88f1ad7baabafe69d4f98d9979653a155 

卸载环境

#!/bin/bash

echo "reset kubeadm"
sudo kubeadm reset

while true; do
    echo "Do you want to uninstall kubernetes software?"
    read -p "(y/n) " yn
    case $yn in
        [Yy]* ) uninstall_kube=yes; break;;
        [Nn]* ) break;;
        * ) echo "Please answer yes or no.";;
    esac
done

if [ "$uninstall_kube" = "yes" ]; then
    echo "##########################################################################"
    echo "uninstall k8s components"
    echo "##########################################################################"
    sudo yum autoremove kubeadm kubectl kubelet kubernetes-cni
    sudo rm -rf ~/.kube
fi

while true; do
    echo "Do you want to uninstall docker?"
    read -p "(y/n) " yn
    case $yn in
        [Yy]* ) uninstall_docker=yes; break;;
        [Nn]* ) break;;
        * ) echo "Please answer yes or no.";;
    esac
done

if [ "$uninstall_docker" = "yes" ]; then
    echo "##########################################################################"
    echo "uninstall k8s components"
    echo "##########################################################################"
    sudo yum autoremove docker-engine docker docker.io docker-ce
    sudo rm -rf /var/lib/docker /etc/docker
    sudo groupdel docker
    sudo rm -rf /var/run/docker.sock
fi

相关文章

网友评论

    本文标题:CentOS一键安装K8s cluster

    本文链接:https://www.haomeiwen.com/subject/xsakchtx.html