Loading

CentOS7 使用 kubeadm 部署 K8s(单机/集群)

一、环境准备(所有)

1. Linux 环境

# 关闭防火墙
systemctl stop firewalld && systemctl disable firewalld

# 关闭 swap
swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

# 关闭 selinux
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

# 时间同步
yum -y install ntpdate
ntpdate time.windows.com
hwclock --systohc

# 将桥接的IPv4流量传递到iptables的链
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system  # 生效

2. 安装 Docker

wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
# yum list docker-ce --showduplicates | sort -r
yum -y install docker-ce-20.10.6-3.el7
systemctl enable docker && systemctl start docker
docker --version
# 换成阿里Docker仓库
cat > /etc/docker/daemon.json << EOF
{
  "registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF
systemctl restart docker
docker info

3. 安装 kubeadm、kubelet 和 kubectl

cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
systemctl enable kubelet

二、单机部署

# 设置hostname
hostnamectl set-hostname k8s-master210
cat >> /etc/hosts << EOF
192.168.11.210 k8s-master
EOF

# 初始化 Master
kubeadm init \
--apiserver-advertise-address=192.168.11.210 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.18.0 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16

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

# 去除污点
kubectl describe node k8s-master210 | grep Taints
kubectl taint nodes k8s-master210 node-role.kubernetes.io/master-

# 部署CNI网络插件
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl get pods -n kube-system  # 查看运行状态

三、集群部署

设置 hosts

# 设置主机名
hostnamectl set-hostname k8s-master  # k8s-node1 / k8s-node2
hostname

# 配置 hosts(只在master执行)
cat >> /etc/hosts << EOF
192.168.11.201 k8s-master
192.168.11.202 k8s-node1
192.168.11.203 k8s-node2
EOF

初始化 Master

kubeadm init \
--apiserver-advertise-address=192.168.11.201 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.18.0 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl get nodes

初始化 Node

kubeadm join 192.168.11.201:6443 --token 0bgtnj.6xl01261s02wqc7z \
    --discovery-token-ca-cert-hash sha256:6553e9758f5eb18d81e793e936fffb7a168c943a6429de0834e1946033ce6f80

[ERROR FileContent--proc-sys-net-ipv4-ip_forward]: /proc/sys/net/ipv4/ip_forward contents are not set to 1

kubeadm reset
echo 1 > /proc/sys/net/ipv4/ip_forward

部署 CNI 网络插件(Master)

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl get pods -n kube-system  # 查看运行状态

测试

kubectl create deployment nginx --image=nginx  # 创建一个nginx
kubectl expose deployment nginx --port=80 --type=NodePort  # 对外暴露端口
kubectl get pod,svc
# http://192.168.11.201:30182/
posted @ 2021-06-02 15:17  LB477  阅读(4165)  评论(1编辑  收藏  举报