欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Kubernetes使用kubeadm安装

程序员文章站 2022-07-13 22:13:21
...

关闭防火墙

systemctl stop firewalld 
systemctl disable firewalld
setenforce 0
sed -i 's/enforcing/disabled/' /etc/selinux/config

关闭Swap

## 临时关闭
swapoff -a
##永久关闭
vim /etc/fstab
整行注释掉 /dev/mapper/centos-swap swap

修改主机名并添加对解析

## 修改主机名:
hostnamectl set-hostname master
hostnamectl set-hostname node1
hostnamectl set-hostname node2

## 添加hosts
vim /etc/hosts
192.168.149.130  master
192.168.149.131  node1
192.168.149.132  node2

将桥接的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

所有主机安装docker

wget -O /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo
sudo sed -i 's+download.docker.com+mirrors.cloud.tencent.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
sudo yum makecache fast
sudo yum -y install docker-ce
systemctl start docker
systemctl enable docker

安装kubeadm,kubelet和kubectl

## 在所有节点上运行(master和node)
cat <<EOF | sudo tee /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

sudo yum install -y kubelet kubeadm kubectl

在Master执行init

sudo kubeadm init --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers \
      --pod-network-cidr 10.244.0.0/16 \
      --v 5 \
      --ignore-preflight-errors=all

使用kubectl工具

## 在master上执行
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

安装Pod网络插件(CNI)

## 在master上执行
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

在Node节点上执行Join加入集群

## token的值在master执行init后最下面有
kubeadm join 192.168.149.130:6443 --token uqbs9j.wax4yw1hu83evx7d \
	--discovery-token-ca-cert-hash sha256:846eabddfc096245598992965da5b64189c16d244b3ca1e997509ed5f9204ca8 

测试集群

kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pod,svc
kubectl get pod,svc -o wide

访问地址:http://NodeIP:Port