kubernetes 脚本快速安装
- 1、三台机器设置自己的 hostname(不能是 localhost)
1# 修改 hostname; k8s-01要变为自己的hostname
2hostnamectl set-hostname k8s-01
3# 设置 hostname 解析
4echo "127.0.0.1 $(hostname)" >> /etc/hosts
2、所有机器批量执行如下脚本
1#先在所有机器执行 vi k8s.sh 2# 进入编辑模式(输入i),把如下脚本复制 3# 所有机器给脚本权限 chmod +x k8s.sh 4#执行脚本 ./k8s.sh
1#/bin/sh
2
3#######################开始设置环境##################################### \n
4
5
6printf "##################正在配置所有基础环境信息################## \n"
7
8
9printf "##################关闭selinux################## \n"
10sed -i 's/enforcing/disabled/' /etc/selinux/config
11setenforce 0
12printf "##################关闭swap################## \n"
13swapoff -a
14sed -ri 's/.*swap.*/#&/' /etc/fstab
15
16printf "##################配置路由转发################## \n"
17cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
18br_netfilter
19EOF
20echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.d/k8s.conf
21
22## 必须 ipv6流量桥接
23echo 'net.bridge.bridge-nf-call-ip6tables = 1' >> /etc/sysctl.d/k8s.conf
24## 必须 ipv4流量桥接
25echo 'net.bridge.bridge-nf-call-iptables = 1' >> /etc/sysctl.d/k8s.conf
26echo "net.ipv6.conf.all.disable_ipv6 = 1" >> /etc/sysctl.d/k8s.conf
27echo "net.ipv6.conf.default.disable_ipv6 = 1" >> /etc/sysctl.d/k8s.conf
28echo "net.ipv6.conf.lo.disable_ipv6 = 1" >> /etc/sysctl.d/k8s.conf
29echo "net.ipv6.conf.all.forwarding = 1" >> /etc/sysctl.d/k8s.conf
30modprobe br_netfilter
31sudo sysctl --system
32
33
34printf "##################配置ipvs################## \n"
35cat <<EOF | sudo tee /etc/sysconfig/modules/ipvs.modules
36#!/bin/bash
37modprobe -- ip_vs
38modprobe -- ip_vs_rr
39modprobe -- ip_vs_wrr
40modprobe -- ip_vs_sh
41modprobe -- nf_conntrack_ipv4
42EOF
43
44chmod 755 /etc/sysconfig/modules/ipvs.modules
45sh /etc/sysconfig/modules/ipvs.modules
46
47
48printf "##################安装ipvsadm相关软件################## \n"
49yum install -y ipset ipvsadm
50
51
52
53
54printf "##################安装docker容器环境################## \n"
55sudo yum remove docker*
56sudo yum install -y yum-utils
57sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
58yum install -y docker-ce-19.03.9 docker-ce-cli-19.03.9 containerd.io
59systemctl enable docker
60systemctl start docker
61
62sudo systemctl daemon-reload
63sudo systemctl restart docker
64
65
66printf "##################安装k8s核心包 kubeadm kubelet kubectl################## \n"
67cat <<EOF > /etc/yum.repos.d/kubernetes.repo
68[kubernetes]
69name=Kubernetes
70baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
71enabled=1
72gpgcheck=0
73repo_gpgcheck=0
74gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
75 http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
76EOF
77
78###指定k8s安装版本
79yum install -y kubelet-1.21.0 kubeadm-1.21.0 kubectl-1.21.0
80
81###要把kubelet立即启动。
82systemctl enable kubelet
83systemctl start kubelet
84
85printf "##################下载api-server等核心镜像################## \n"
86sudo tee ./images.sh <<-'EOF'
87#!/bin/bash
88docker pull k8s.gcr.io/kube-apiserver:v1.21.9
89docker pull k8s.gcr.io/kube-controller-manager:v1.21.9
90docker pull k8s.gcr.io/kube-scheduler:v1.21.9
91docker pull k8s.gcr.io/kube-proxy:v1.21.9
92docker pull k8s.gcr.io/pause:3.4.1
93docker pull k8s.gcr.io/etcd:3.4.13-0
94docker pull k8s.gcr.io/coredns/coredns:v1.8.0
95EOF
96
97chmod +x ./images.sh && ./images.sh
98
99### k8s的所有基本环境全部完成
- 3、使用 kubeadm 引导集群(参照初始化 master 继续做)
1
2#### --apiserver-advertise-address 的地址一定写成自己master机器的ip地址
3#### 虚拟机或者其他云厂商给你的机器ip 10.96 192.168
4#### 以下的只在master节点执行
5kubeadm init \
6--apiserver-advertise-address=10.12.12.24 \
7--kubernetes-version v1.21.0 \
8--service-cidr=10.96.0.0/16 \
9--pod-network-cidr=10.124.0.0/16
- 4、master 结束以后,按照控制台引导继续往下
1## 第一步
2mkdir -p $HOME/.kube
3sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
4sudo chown $(id -u):$(id -g) $HOME/.kube/config
5
6##第二步
7export KUBECONFIG=/etc/kubernetes/admin.conf
8
9##第三步 部署网络插件
10kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
11
12
13
14##第四步,用控制台打印的kubeadm join 去其他node节点执行
15kubeadm join 10.170.11.8:6443 --token cnb7x2.lzgz7mfzcjutn0nk \
16 --discovery-token-ca-cert-hash sha256:00c9e977ee52632098aadb515c90076603daee94a167728110ef8086d0d5b37d
初始化 worker 节点(worker 执行)
1##过期怎么办
2kubeadm token create --print-join-command
3kubeadm join --token y1eyw5.ylg568kvohfdsfco --discovery-token-ca-cert-hash sha256: 6c35e4f73f72afd89bf1c8c303ee55677d2cdb1342d67bb23c852aba2efc7c73
- 5、验证集群
1#等一会,在master节点执行
2kubectl get nodes
- 6、设置 kube-proxy 的 ipvs 模式
1##修改kube-proxy默认的配置
2kubectl edit cm kube-proxy -n kube-system
3## 修改mode: "ipvs"
4
5##改完以后重启kube-proxy
6### 查到所有的kube-proxy
7kubectl get pod -n kube-system |grep kube-proxy
8### 删除之前的即可
9kubectl delete pod 【用自己查出来的kube-proxy-dw5sf kube-proxy-hsrwp kube-proxy-vqv7n】 -n kube-system
10
11###