简称k8s
是一个容器编排系统
为了解决容器部署环境下,容器太多管理困难的问题
架构
多主多从架构
基本概念
- MasterNode 主节点
- APIServer
- Controller Manager
- ETCD 键值数据库
- SCHED 调度者
- Cloud Controller Manager
- WorkNode 工作节点
- kube-proxy
- kubelet
安装
安装步骤
- 在每个服务器安装Docker环境
- 安装kubelet
- 安装kubectl 和 kubeadm
- 使用kubeadm初始化
1. 开通服务器
按照开通云服务器的方法开通三台服务器
2. 安装docker
参考之前的docker安装步骤
3.服务器环境处理
https://kubernetes.io/zh/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
基于文档处理服务器环境
3.1 设置hostname
# 设置三台服务器的hostname
hostnamectl set-hostname [hostname]
3.2 禁用SELINUX
# 将 SELinux 设置为 permissive 模式(相当于将其禁用)
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
3.3 禁用swap分区
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
3.4 允许 iptables 检查桥接流量
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sudo sysctl --system
4. 安装kubelet kubeadm kubectl
4.1 设置yum安装源为阿里云
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
exclude=kubelet kubeadm kubectl
EOF
4.2 安装kubelet kubeam kubectl
sudo yum install -y kubelet-1.20.9 kubeadm-1.20.9 kubectl-1.20.9 --disableexcludes=kubernetes
# 启动kubelet
sudo systemctl enable --now kubelet
5. 使用kubeadm安装k8s
5.1 准备镜像
主节点执行
# 创建一个shell脚本
sudo tee ./images.sh <<-'EOF'
#!/bin/bash
images=(
kube-apiserver:v1.20.9
kube-proxy:v1.20.9
kube-controller-manager:v1.20.9
kube-scheduler:v1.20.9
coredns:1.7.0
etcd:3.4.13-0
pause:3.2
)
for imageName in ${images[@]} ; do
docker pull registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/$imageName
done
EOF
# 修改权限执行脚本
chmod +x ./images.sh && ./images.sh
5.2 初始化主节点
#所有机器添加master域名映射,以下ip需要修改为自己的ip
echo "172.16.0.208 cluster-endpoint" >> /etc/hosts
# 然后各个节点可以ping通cluster-endpoint
ping cluster-endpoint
#主节点初始化 只在主节点执行
kubeadm init \
--apiserver-advertise-address=172.16.0.208 \
--control-plane-endpoint=cluster-endpoint \
--image-repository registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images \
--kubernetes-version v1.20.9 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=192.168.0.0/16
#所有网络范围不重叠
初始化失败原因检查:
- master 域名映射 必须为局域网ip
- 检查kubelet是否启动
如果初始化失败,或者初始化过程中出现问题,导致init失败,但是docker镜像已经在运行的情况
使用命令重置k8s
kubeadm reset -y
初始化成功后可以看到如下:
保留上述提示命令:
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
Alternatively, if you are the root user, you can run:
export KUBECONFIG=/etc/kubernetes/admin.conf
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 control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:
kubeadm join cluster-endpoint:6443 --token byw8pn.82wht0x3yhzy9vt2 \
--discovery-token-ca-cert-hash sha256:b8877f8540c29b5b72241712c5f1df1ae9c45835563ae2894df51d833a46d9d5 \
--control-plane
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join cluster-endpoint:6443 --token byw8pn.82wht0x3yhzy9vt2 \
--discovery-token-ca-cert-hash sha256:b8877f8540c29b5b72241712c5f1df1ae9c45835563ae2894df51d833a46d9d5
如上文所说,想要使用k8s还需要执行如下命令
主节点执行
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
export KUBECONFIG=/etc/kubernetes/admin.conf
执行后主节点就算是初始化成功了
# 查看k8s节点状态 此时状态是NotReady 因为还没有安装网络插件
kubectl get nodes
5.1.1 安装网络插件
# 下载配置文件
curl https://docs.projectcalico.org/manifests/calico.yaml -O
# 安装插件
kubectl apply -f calico.yaml
# 查看k8s中的资源
kubectl get pod -A
5.1.2 完成主节点配置
此时主节点就配置完成了
#查看主节点状态 可以看到主节点已经完成 如果没完成稍微等待一下 等上文中的pod资源全部完成后,就可以了
kubectl get nodes
从上文中可以得到如下几个命令:
# 添加主节点
kubeadm join cluster-endpoint:6443 --token byw8pn.82wht0x3yhzy9vt2 \
--discovery-token-ca-cert-hash sha256:b8877f8540c29b5b72241712c5f1df1ae9c45835563ae2894df51d833a46d9d5 \
--control-plane
# 添加工作节点
kubeadm join cluster-endpoint:6443 --token byw8pn.82wht0x3yhzy9vt2 \
--discovery-token-ca-cert-hash sha256:b8877f8540c29b5b72241712c5f1df1ae9c45835563ae2894df51d833a46d9d5
5.2 添加工作节点
在两个工作节点上执行
token只有24h有效
kubeadm join cluster-endpoint:6443 --token byw8pn.82wht0x3yhzy9vt2 \
--discovery-token-ca-cert-hash sha256:b8877f8540c29b5b72241712c5f1df1ae9c45835563ae2894df51d833a46d9d5
# 如果token过期 在master节点运行此命令重新获取token
kubeadm token create --print-join-command
添加后在主节点查看节点状态
# 看到多了两个节点 但是不是Ready状态 那是正在安装资源
kubectl get nodes
# 查看资源安装进度 可以使用watch -n 1 [命令] 每1s查看一次
kubectl get pod -A
6. 安装k8s dashboard
安装k8s官方提供的可视化界面
6.1 安装应用
# 直接远程下载
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.3.1/aio/deploy/recommended.yaml
# 如果远程下载不到,可以线下下载 再复制进服务器创建yaml文件
6.2 开放端口
kubectl edit svc kubernetes-dashboard -n kubernetes-dashboard
#修改 type 属性
#type: ClusterIP 改为 type: NodePort
# kubuctl 获取dashboard端口 之后需要将这个端口号在云服务器安全组中放行
kubectl get svc -A | grep kubernetes-dashboard
6.3 创建账号
创建访问账号,准备一个yaml文件; vi dash-user.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin-user
namespace: kubernetes-dashboard
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: admin-user
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: admin-user
namespace: kubernetes-dashboard
kubectl apply -f dash-user.yaml
6.4 获取访问令牌
kubectl -n kubernetes-dashboard get secret $(kubectl -n kubernetes-dashboard get sa/admin-user -o jsonpath="{.secrets[0].name}") -o go-template="{{.data.token | base64decode}}"
eyJhbGciOiJSUzI1NiIsImtpZCI6Im4taXUyMUstaTVOVmdYMTVZVndTc2xPbG5RZzBUUlZwdkY5bzB0TFltNkkifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLW5jd3oyIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiIxZmNjYWI1Zi1iOWRkLTQ5MGYtYmI2NS03NDU1ZjA0NGQzODkiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZXJuZXRlcy1kYXNoYm9hcmQ6YWRtaW4tdXNlciJ9.dpqKh8xAeQ7s-cp6bhUnnfH8QH0Zbg0r2xggjiXX3NwbxLTUBBf4uT3bDcXHlLcaQMcRWJ5TvxczNUVlJYjcP8bm-ckQyLTtgjRs_NKffiiNWNSe_blneKbcyTTf7KxTt1R70PnBOG6oHmkBYrwCHV9XWnvjlVO2TstAqF1doa3yCxaCz6Sg1u8qaRlLZ6Ou7bD3t4Y-31dajI2HoVJEKU6IiEfOzN0C2RqA9yHY6ATkotQ95ALdTfYvU1iWpsLsnQTf94WwAnueqruTFFyf_AMjPx2OirH98cMis7P_x9uJ0pA9xaNrBc3dX0JYgFFHO0Elan-CDyie1GPCZ0Q2zw
6.5 访问
ip为任意一节点ip 端口为上述查询出来并放行安全组的端口
https
输入token登录