Centos 安装 k8s
程序员文章站
2024-03-11 10:10:01
...
一、环境准备
1、修改 master 主机名
hostnamectl set-hostname master1
2、关闭 selinux
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
3、禁用swap
# 临时
swapoff -a
# 永久,打开/etc/fstab注释掉swap那一行
sed -i 's/.*swap.*/#&/' /etc/fstab
4、修改内核参数
cat <<EOF > /etc/sysctl.d/k8s.conf
vm.swappiness = 0
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
# 加载
modprobe br_netfilter
# 生效
sysctl -p /etc/sysctl.d/k8s.conf
5、修改Cgroup Driver
# 消除告警
vim /etc/docker/daemon.json
{
"registry-mirrors": ["http://hub-mirror.c.163.com"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
# 重启生效
systemctl restart docker
6、设置 kubenetes 源
# 配置 yum 源
vim /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
7、安装
# 查看版本
yum list kubelet --showduplicates | sort -r
# 安装指定版本
yum install -y kubectl-1.17.2 kubeadm-1.17.2 kubelet-1.17.2
kubelet 运行在集群所有节点上,用于启动Pod和容器等对象的工具
kubeadm 用于初始化集群,启动集群的命令工具
kubectl 用于和集群通信的命令行,通过kubectl可以部署和管理应用,查看各种资源,创建、删除和更新各种组件
一、master 安装
1、初始化 k8s
kubeadm init \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.17.2 \
--apiserver-advertise-address 192.168.1.111 \
--pod-network-cidr=10.244.0.0/16
--image-repository:指定镜像源
--kubernetes-version:指定 k8s 版本,要跟上面安装的保持一致
--apiserver-advertise-address:指定master的interface
--pod-network-cidr:指定Pod网络的范围,这里使用flannel网络方案
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 -u):$(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:
# 记录这行,后面 node 节点加入集群会用到
kubeadm join 192.168.1.111:6443 --token 8a1x7a.84gh8ghc9c3z7uak \
--discovery-token-ca-cert-hash sha256:16ebeae9143006938c81126050f8fc8527d2a6b1c4991d07b9282f47cf4203d6
2、加载环境变量
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
3、安装 pod 网络
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
4、启动 kubelet
systemctl start kubelet
二、 node 节点 安装
按上面的环境配置,安装 k8s
hostnamectl set-hostname node1 主机名
1、加入集群
# 显示,在 node节点 上面执行
kubeadm join 192.168.1.111:6443 --token j88bsx.o0ugzfnxqdl5s58e \
--discovery-token-ca-cert-hash sha256:16ebeae9143006938c81126050f8fc8527d2a6b1c4991d07b9282f47cf4203d6
# 查看 token, 在 master 执行
kubeadm token list
# 如果过期,可以在 master 节点重新生成
kubeadm token create --print-join-command
三、查看集群
[[email protected] ~]# kubectl get node
# 显示
NAME STATUS ROLES AGE VERSION
master1 Ready master 155m v1.17.2
node1 Ready <none> 3m5s v1.19.0
1、如何删除 node
# 删除 node1 节点
kubectl delete node node1
# 重置
kubeadm reset
# 重新加入
kubeadm join