关闭防火墙
$ sudo ufw disable
备注:
临时禁用(重启后失效)
$ sudo setenforce 0 #0代表permissive 1代表enforcing
永久禁用
$ sudo vi /etc/selinux/config
SELINUX=permissive
备注:
1.3.1 内核开启ipv4转发
1.修改/etc/sysctl.conf,开启ipv4转发:
$ sudo vim /etc/sysctl.conf
net.ipv4.ip_forward = 1 #开启ipv4转发,允许内置路由
2.写入后执行如下命令生效:
$ sudo sysctl -p
备注:
参考: https://docs.docker.com/v17.09/engine/userguide/networking/default_network/container-communication/#communicating-to-the-outside-world
1.3.2 防火墙修改FORWARD链默认策略
方案一
临时生效:
$ sudo iptables -P FORWARD ACCEPT
iptables的配置重启后会丢失,可以将配置写进/etc/rc.local中,重启后自动执行:
/usr/sbin/iptables -P FORWARD ACCEPT
方案二
{
"iptables": false
}
备注:
1.禁掉所有的swap分区
$ sudo swapoff -a
2.同时还需要修改/etc/fstab文件,注释掉 SWAP 的自动挂载,防止机子重启后swap启用。
备注:
$ sudo tee /etc/sysctl.d/k8s.conf <<-'EOF'
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
$ sudo sysctl --system
1.卸载旧docker
$ sudo apt-get remove docker docker-engine docker.io
2.安装依赖,使得apt可以使用https
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common
3.添加docker的GPG key:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
4.设置docker镜像源
$ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
$ sudo add-apt-repository \
"deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
$(lsb_release -cs) \
stable"
5.安装指定版本docker-ce
$ apt-cache madison docker-ce
docker-ce | 18.06.1~ce~3-0~ubuntu | https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic/stable amd64 Packages
docker-ce | 18.06.0~ce~3-0~ubuntu | https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic/stable amd64 Packages
docker-ce | 18.03.1~ce~3-0~ubuntu | https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic/stable amd64 Packages
安装18.03.1版:
$ sudo apt-get install -y docker-ce=18.03.1~ce~3-0~ubuntu
6.启动并设置开机自启动docker
$ sudo systemctl enable docker && sudo systemctl start docker
7.将当前登录用户加入docker用户组中
$ sudo usermod -aG docker lk
备注:
为docker做如下配置:
最终配置如下:
$ sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://xxxxxxxx.mirror.aliyuncs.com"],
"iptables": false,
"ip-masq": false,
"storage-driver": "overlay2",
"graph": "/home/lk/docker"
}
EOF
$ sudo systemctl restart docker
可以在 /etc/docker/daemon.json 文件里加上:
{
"registry-mirrors": [
"https://kfwkfulq.mirror.aliyuncs.com",
"https://2lqq34jg.mirror.aliyuncs.com",
"https://pee6w651.mirror.aliyuncs.com",
"https://registry.docker-cn.com",
"http://hub-mirror.c.163.com"
]
}
这是阿里云的源
$ sudo mkdir /etc/systemd/system/docker.service.d
$ sudo tee /etc/systemd/system/docker.service.d/http-proxy.conf <<-'EOF'
[Service]
Environment="HTTP_PROXY=http://xxx.xxx.xxx.xxx:xxxx"
Environment="NO_PROXY=localhost,127.0.0.0/8"
EOF
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
3.1.1 创建kubernetes的repo
创建kubernetes的source文件:
sudo apt-get update && sudo apt-get install -y apt-transport-https curl
sudo curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -
sudo tee /etc/apt/sources.list.d/kubernetes.list <<-'EOF'
deb https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial main
EOF
sudo apt-get update
3.1.2 安装kubeadm、kubelet、kubectl
1.查看可用软件版本:
$ apt-cache madison kubeadm
kubeadm | 1.12.1-00 | https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial/main amd64 Packages
kubeadm | 1.12.0-00 | https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial/main amd64 Packages
kubeadm | 1.11.3-00 | https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial/main amd64 Packages
......
2.安装指定版本:
sudo apt-get install -y kubelet=1.18.3-00 kubeadm=1.18.3-00 kubectl=1.18.3-00
sudo apt-mark hold kubelet=1.18.3-00 kubeadm=1.18.3-00 kubectl=1.18.3-00
3.设置开机自启动并运行kubelet:
sudo systemctl enable kubelet && sudo systemctl start kubelet
备注:
4.1.1 提前下载所需镜像
看一下kubernetes v1.18.3需要哪些镜像:
$ kubeadm config images list --kubernetes-version=v1.18.3
然后执行这部直接把需要的镜像下载,然后 kubeadm init初始化集群 :
kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.18.3 --pod-network-cidr=10.244.0.0/16
部署成功会输出如下内容:
Your Kubernetes master 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/
You can now join any number of machines by running the following on each node
as root:
kubeadm join 192.168.1.175:6443 --token v1nj22.l30dctzysf2jynly --discovery-token-ca-cert-hash sha256:0170607e7e069ffde2f2b6b440e7982f066887e59db49e9a62ac9518924af690
记下其中的token,加入node节点时会用到。
4.1.2 检查kubelet使用的cgroup driver
1.查看 Docker 使用的 cgroup driver:
$ docker info | grep -i cgroup
-> Cgroup Driver: cgroupfs
2.查看kubelet指定的cgroup driver
Kubernetes文档中kubelet的启动参数–cgroup-driver string Driver that the
kubelet uses to manipulate cgroups on the host. Possible values:
‘cgroupfs’, ‘systemd’ (default
“cgroupfs”)。默认值为cgroupfs。yum安装kubelet、kubeadm时生成10-kubeadm.conf文件中可能将这个参数值改成了systemd。
查看kubelet的配置文件(1.12.0版本的封装在/var/lib/kubelet/kubeadm-flags.env文件中),如果是默认的cgroupfs,不需要修改。否则需要修改/etc/default/kubelet(或者/var/lib/kubelet/kubeadm-flags.env)
文件:
$ sudo vim /etc/default/kubelet
KUBELET_KUBEADM_EXTRA_ARGS=--cgroup-driver=<value>
$ sudo systemctl daemon-reload
$ sudo systemctl restart kubelet
4.1.3 创建kubectl使用的kubeconfig文件
$ mkdir -p $HOME/.kube
$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ sudo chown $(id -u):$(id -g) $HOME/.kube/config
4.1.4 设置master参与工作负载
$ kubectl taint nodes --all node-role.kubernetes.io/master-
node/lk-thinkpad-t470 untainted
## 4.2.1 calico部署
calico的数据存储可以有两种方式:
1.到release页面获取安装包,这里用的是v3.8.7版本
这个下载特别慢,需要的可以给我留言,把压缩包发给你
wget https://github.com/projectcalico/calico/releases/download/v3.8.7/release-v3.8.7.tgz
2.解压后load release-v3.2.3/images下的镜像
calico-cni.tar
alico-kube-controllers.tar
calico-node.tar
calico-typha.tar
yaml中的配置需要修改下:
CALICO_IPV4POOL_CIDR 下面的ip修改为:10.244.0.0/16
3.创建rbac
先:
kubectl apply rbac/rbac-kdd-calico.yaml
然后再:
kubectl apply calico.yaml
执行上面两句时候记得注意目录
4. 为calico-node创建clusterrolebinding
kubectl create clusterrolebinding kube-system-default-role-binding --clusterrole=cluster-admin --serviceaccount=kube-system:calico-node
5.启动
kubectl apply -f release-v3.8.7/k8s-manifests/hosted/calico.yaml
启动了两类pod:
kubeadm join 192.168.1.175:6443 --token w2ks3i.13l40j8ux38oz31r --discovery-token-ca-cert-hash sha256:387a9081b55dbed4263c22c9a8ffd5e7270c1bcdcca4299c2a69cba7d3df74e7
注意:默认token的有效期为24小时,当过期之后,该token就不可用了。此时可以重新生成token:
kubeadm token generate
kubeadm token create <generated-token> --print-join-command --ttl=0
设置–ttl=0代表永不过期