该篇文章介绍利用kubeadm进行安装部署K8S
软件环境:
软件 | 版本 |
---|---|
操作系统 | CentOS7.9_x64 (mini) |
Docker | 19-ce |
Kubernetes | 1.20 |
服务器规划:
角色 | IP |
---|---|
k8s-master | 192.168.124.129 |
k8s-node1 | 192.168.124.130 |
k8s-node2 | 192.168.124.131 |
硬件要求:
角色 | CPU | 内存 | 硬盘 |
---|---|---|---|
master | 2核 | 4G | 20G |
node | 4核 | 8G | 20G |
systemctl stop firewalld
systemctl disable firewalld
sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久
setenforce 0 # 临时
swapoff -a # 临时
sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久
hostnamectl set-hostname <hostname>
bash
vim /etc/hosts
192.168.124.129 master-k8s
192.168.124.130 node1-k8s
192.168.124.131 node2-k8s
或者
cat >> /etc/hosts << EOF
192.168.124.129 master_k8s
192.168.124.130 node1_k8s
192.168.124.131 node2_k8s
EOF
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system # 生效
yum install ntpdate -y
ntpdate time.windows.com
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum -y install docker-ce
systemctl enable docker && systemctl start docker
配置镜像下载加速器:
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF
systemctl restart docker
docker info
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
由于版本更新频繁,这里指定版本号部署:
yum install -y kubeadm-1.20.0 kubelet-1.20.0 kubectl-1.20.0
systemctl enable kubelet
kubeadm init \
--apiserver-advertise-address=192.168.124.129 \
--kubernetes-version v1.20.0 \
--image-repository registry.aliyuncs.com/google_containers \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16 \
--ignore-preflight-errors=all
• --apiserver-advertise-address 集群通告地址
• --image-repository 由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里指定阿里云镜像仓库地址。可以魔法上网,不用配。如果不能,可以配置:
• --kubernetes-version K8s版本,与上面安装的一致
• --service-cidr 集群内部虚拟网络,Pod统一访问入口
• --pod-network-cidr Pod网络,,与下面部署的CNI网络组件yaml中保持一致
或者使用配置文件引导:
vi kubeadm.conf
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
kubernetesVersion: v1.20.0
imageRepository: registry.aliyuncs.com/google_containers
networking:
podSubnet: 10.244.0.0/16
serviceSubnet: 10.96.0.0/12
kubeadm init --config kubeadm.conf --ignore-preflight-errors=all
例如:
kubeadm join 192.168.124.129:6443 --token 0kc7co.tyz7u44w4eoenip9 \
--discovery-token-ca-cert-hash sha256:615f82948aac7cd18d0dfbfec64c5f983f815e9814c995eefbe06471ec0211f3
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
NAME STATUS ROLES AGE VERSION
localhost.localdomain NotReady control-plane,master 20s v1.20.0
注:由于网络插件还没有部署,还没有准备就绪 NotReady
在192.168.124.130/131(Node)执行:
向集群添加新节点,执行在kubeadm init输出的kubeadm join命令:
kubeadm join 192.168.124.129:6443 --token 0kc7co.tyz7u44w4eoenip9 \
--discovery-token-ca-cert-hash sha256:615f82948aac7cd18d0dfbfec64c5f983f815e9814c995eefbe06471ec0211f3
默认token有效期为24小时,当过期之后,该token就不可用了。这时就需要重新创建token,可以直接使用命令快捷生成:
kubeadm token create --print-join-command
Calico是一个纯三层的数据中心网络方案,是目前Kubernetes主流的网络方案。
wget https://docs.projectcalico.org/manifests/calico.yaml
修改完后文件后,部署:
kubectl apply -f calico.yaml
kubectl get pods -n kube-system
在Kubernetes集群中创建一个pod,验证是否正常运行:
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pod,svc # 会显示访问port
访问地址:http://MasterIP:Port,如果可以访问nginx,说明就安装部署完成了。
Dashboard是官方提供的一个UI,可用于基本管理K8s资源:
wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.3/aio/deploy/recommended.yaml
默认Dashboard只能集群内部访问,修改Service为NodePort类型,暴露到外部:
vim recommended.yaml
...
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
spec:
ports:
- port: 443
targetPort: 8443
nodePort: 30001
selector:
k8s-app: kubernetes-dashboard
type: NodePort # 添加改行即可
...
kubectl apply -f recommended.yaml
kubectl get pods -n kubernetes-dashboard
参看容器的端口:例如kubernetes-dashboard:
kubectl get pods,svc -n kubernetes-dashboard # 会暴露port
访问地址:https://192.168.124.129:31623 :
创建service account并绑定默认cluster-admin管理员集群角色:
# 创建用户
kubectl create serviceaccount dashboard-admin -n kube-system
# 用户授权
kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
# 获取用户Token
kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')