K8S-1.18.2版本部署

一、环境准备

1、规划

IP地址主机名角色
10.30.33.1k8s001master
10.30.33.2k8s002worker
10.30.33.3k8s003worker

2、关闭firewalld防火墙

systemctl stop firewalld
systemctl disable firewalld

3、关闭selinux

setenforce 0
sed -i '/^SELINUX=enforcing/cSELINUX=disabled' /etc/selinux/config

4、关闭swap

swapoff -a
sed -i '/swap/s/^/#/g' /etc/fstab

5、配置主机名并添加解析

hostnamectl set-hostname k8s001
echo "10.30.33.1 k8s001" >> /etc/hosts
echo "10.30.33.2 k8s002" >> /etc/hosts
echo "10.30.33.3 k8s003" >> /etc/hosts

二、所有节点安装基础环境

1、安装docker

yum -y install docker

2、配置docker镜像加速

echo '{"registry-mirrors": ["https://frz7i079.mirror.aliyuncs.com"]}' > /etc/docker/daemon.json

3、启动docker

systemctl enable docker
systemctl restart docker

4、配置系统参数

cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sysctl -p /etc/sysctl.d/k8s.conf

5、安装k8s工具

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
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.2-0 kubeadm-1.18.2-0 kubectl-1.18.2-0 --disableexcludes=kubernetes
systemctl restart kubelet
systemctl enable kubelet

三、在Master上安装k8s集群

1、初始化

pod_cidr="192.168.0.0/16"
kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version=v1.18.2 --pod-network-cidr=${pod_cidr}

2、配置kubectl

mkdir -p /root/.kube
cp -i /etc/kubernetes/admin.conf /root/.kube/config

3、配置kubectl选项自动补全

yum -y install bash-completion
sed -i '2isource <(kubectl completion bash)' /etc/profile
source /etc/profile
# 重新登录

4、添加worker节点

token=`kubeadm token list | grep default-node-token | awk '{print $1}'`

hash=`openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'`

ssh root@$k8s002 "kubeadm join k8s001:6443 --token ${token} --discovery-token-ca-cert-hash sha256:${hash}"
ssh root@$k8s003 "kubeadm join k8s001:6443 --token ${token} --discovery-token-ca-cert-hash sha256:${hash}"

# 也可以用以下方式获取添加命令
kubeadm token create --print-join-command

# 此时用kubectl get node查看会发现所有node节点都是未准备好的状态,这是因为还没有安装网络插件的原因

5、部署Flannel网络插件

# 该文件中使用我自己上传到阿里云的镜像
wget http://ftp.ps-ef.cn/k8s/yaml/kube-flannel.yml

# POD网段
pod_cidr="192.168.0.0/16"
sed -i -r "s#10.244.0.0/16#${pod_cidr}#g" kube-flannel.yml

# 使用哪个网卡做为出接口,如果节点之间网卡名称不一致,建议不做此项配置
flannel_interface="ens192"
sed -i -r "s#        - --iface=#        - --iface=${flannel_interface}#g" kube-flannel.yml
kubectl apply -f kube-flannel.yml

本文链接:

http://blog.ps-ef.cn/Kubernetes/93.html
1 + 8 =
快来做第一个评论的人吧~