vmware 新建三台虚拟机
配置4核cpu 2G内存
k8s-master 10.6.249.63
k8s-node1 10.6.249.62
k8s-node2 10.6.249.64
1. 在三台服务器上执行
cat <> /etc/hosts
10.6.249.63 k8s-master
10.6.249.62 k8s-node01
10.6.249.64 k8s-node02
EOF
#主机名对应修改
2. 配置阿里源
docker源 :wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O/etc/yum.repos.d/docker-ce.repo
centos 源:wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
EPEL 源: wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
kubernetes源:cat < /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
[[email protected] k8s]# yum makecache
3.关闭firewalld 和 seLinux
[[email protected] k8s]# systemctl stop firewalld && systemctl disable
firewalld
[[email protected] k8s]# iptables -F
[[email protected] k8s]# setenforce 0 && sed -i
‘s/^SELINUX=.*/SELINUX=disabled/’ /etc/selinux/config
3. 停用swap
[[email protected] k8s]# swapoff -a && sed -i ‘/ swap / s/^(.*)$/#\1/g’ /etc/fstab
4.调整内核参数
cat > /etc/sysctl.d/kubernetes.conf <<EOF
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
net.ipv4.tcp_tw_recycle=0
vm.swappiness=0 # 禁止使用swap空间,只有当系统OOM时才允许使用它
vm.overcommit_memory=1 # 不检查物理内存是否够用
vm.panic_on_oom=0 # 开启OOM
fs.inotify.max_user_instances=8192
fs.inotify.max_user_watches=1048576
fs.file-max=52706963
fs.nr_open=52706963
net.ipv6.conf.all.disable_ipv6=1
net.netfilter.nf_conntrack_max=2310720
EOF
~~]# sysctl -p /etc/sysctl.d/kubernetes.conf
如果出现如下错误
sysctl: cannot stat /proc/sys/net/ipv4/netfilter/ip_conntrack_max: No such file or directory
~]# modprobe ip_conntrack 加载下模块
~]# timedatectl set-timezone Asia/Shanghai
5. 安装kubeadm,kubelet,kubectl docker
~] # yum install -y kubelet-1.18.1-0 kubeadm-1.18.1-0 kubectl-1.18.1-0 docker-ce
[[email protected] ~]# cat /etc/docker/daemon.json
{
“registry-mirrors”: [“https://qykgx85g.mirror.aliyuncs.com”]
}
#使用阿里镜像加速器
[[email protected] ~]# systemctl daemon-reload
~]# systemctl enable docker &&systemctl start docker
~]# systemctl enable kubelet && systemctl start kubelet
以上操作所有节点都执行
6. 初始化master节点
(在master上操作)
kubeadm init --kubernetes-version=1.18.1
–apiserver-advertise-address=10.6.249.63
–image-repository registry.aliyuncs.com/google_containers
–service-cidr=10.1.0.0/16
–pod-network-cidr=10.244.0.0/16
修改对应的master IP地址
执行完成会出现如下信息
Your Kubernetes control-plane has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown (id -g) $HOME/.kube/config
You should now deploy a pod network to the cluster.
Run “kubectl apply -f [podnetwork].yaml” with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 10.6.249.63:6443 --token po66ap.p0o8tyykt6aef0tx
–discovery-token-ca-cert-hash sha256:ed0511571f45936aaada793645b251d9ef03230bb50b3c4a4300ddc142696a1d
7. 保存好kubeadm join
~]# mkdir -p $HOME/.kube
~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
~]# sudo chown (id -g) $HOME/.kube/config
8. 安装flannel(网络插件)
mkidr /k8s && cd /k8s
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
查看yaml需要镜像
#cat kube-flannel.yml |grep image|uniq
image: quay.io/coreos/flannel:v0.12.0-amd64
image: quay.io/coreos/flannel:v0.12.0-arm64
image: quay.io/coreos/flannel:v0.12.0-arm
image: quay.io/coreos/flannel:v0.12.0-ppc64le
image: quay.io/coreos/flannel:v0.12.0-s390x
从阿里云上拉取镜像
docker pull registry.cn-shanghai.aliyuncs.com/yijindami/flannel:v0.12.0-amd64
docker pull registry.cn-shanghai.aliyuncs.com/yijindami/flannel:v0.12.0-arm64
docker pull registry.cn-shanghai.aliyuncs.com/yijindami/flannel:v0.12.0-arm
docker pull registry.cn-shanghai.aliyuncs.com/yijindami/flannel:v0.12.0-ppc64le
docker pull registry.cn-shanghai.aliyuncs.com/yijindami/flannel:v0.12.0-s390x
重命名镜像,和kube-flannel.yml文件中的镜像名称保存一致
docker tag registry.cn-shanghai.aliyuncs.com/yijindami/flannel:v0.12.0-amd64 quay.io/coreos/flannel:v0.12.0-amd64
docker tag registry.cn-shanghai.aliyuncs.com/yijindami/flannel:v0.12.0-arm64 quay.io/coreos/flannel:v0.12.0-arm64
docker tag registry.cn-shanghai.aliyuncs.com/yijindami/flannel:v0.12.0-arm quay.io/coreos/flannel:v0.12.0-arm
docker tag registry.cn-shanghai.aliyuncs.com/yijindami/flannel:v0.12.0-ppc64le quay.io/coreos/flannel:v0.12.0-ppc64le
docker tag registry.cn-shanghai.aliyuncs.com/yijindami/flannel:v0.12.0-s390x quay.io/coreos/flannel:v0.12.0-s390x
安装flannel网络
[[email protected]~]# kubectl apply -f kube-flannel.yml
~]# kubectl get pod --all-namespaces -o wide
#等待几分钟,确保所有的Pod都处于Running状态
以上操作在master上执行
9 .node 加入集群
以下操作在node1 和node2 节点上执行
找到之前保存的 kubeadm json
~k8s-node1]#
kubeadm join 10.6.249.63:6443 --token po66ap.p0o8tyykt6aef0tx
–discovery-token-ca-cert-hash sha256:ed0511571f45936aaada793645b251d9ef03230bb50b3c4a4300ddc142696a1d
~k8s-node2]#
kubeadm join 10.6.249.63:6443 --token po66ap.p0o8tyykt6aef0tx
–discovery-token-ca-cert-hash sha256:ed0511571f45936aaada793645b251d9ef03230bb50b3c4a4300ddc142696a1d
10.到master主机 查看节点信息
~k8s-master]# kubectl get nodes -o wide
参考地址:
https://www.cnblogs.com/xiao987334176/p/12696740.html
https://www.cnblogs.com/caoxb/p/11243472.html
https://www.cnblogs.com/woailifang/p/12763847.html