5-06 3 views
配置Docker代理
1 2 3 4 5 |
cat > /etc/docker/daemon.json << EOF { "registry-mirrors": ["https://pqbap4ya.mirror.aliyuncs.com"] } EOF |
安装kubeadm、kubelet、kubectl
配置yum源
1 2 3 4 5 6 7 8 9 |
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 |
安装
1 2 |
yum install -y kubelet-1.18.8 kubeadm-1.18.8 kubectl-1.18.8 systemctl enable kubelet |
关闭swap分区
1 2 3 4 |
swapoff -a # 永久关闭 sed -ri 's/.*swap.*/#&/' /etc/fstab |
配置主机名
统一配置为k8s-dev.${IP}
1 |
hostnamectl set-hostname `ip addr | grep ens192$ |sed -n 's/\(.*\)inet[[:space:]]\(.*\)\/[1-3][0-9] \(.*\)/\2/p'` k8s-dev.`ip addr | grep ens192$ |sed -n 's/\(.*\)inet[[:space:]]\(.*\)\/[1-3][0-9] \(.*\)/\2/p'` |
搭建K8S集群
Master
1 2 3 4 5 6 7 8 9 10 |
master_ip=`ip addr | grep ens192$ |sed -n 's/\(.*\)inet[[:space:]]\(.*\)\/[1-3][0-9] \(.*\)/\2/p'` # 因为k8s.gcr无法访问,这里使用阿里云的代理 注:10.244.0.0/16 对应的是flannel的默认网段 kubeadm init \ --apiserver-advertise-address=${master_ip} \ --image-repository registry.aliyuncs.com/google_containers \ --kubernetes-version v1.18.8 \ --service-cidr=10.1.0.0/16 \ --pod-network-cidr=10.244.0.0/16 |
看到如下信息,说明master节点安装完成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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: kubeadm join 192.168.111.200:6443 --token 3xxxxa.8noaxxxxxxxxmoxk \ --discovery-token-ca-cert-hash sha256:c455738e0a6xxxxxxxxxxxxxxxxxxxxxxxxxx71d27a7a2e98004 |
注:token只有24个小时的时效,过期后获取token按如下步骤
Node节点
1 2 3 |
## 加入集群 kubeadm join 192.168.111.200:6443 --token 3xxxxa.8noaxxxxxxxxmoxk \ --discovery-token-ca-cert-hash sha256:c455738e0a6xxxxxxxxxxxxxxxxxxxxxxxxxx71d27a7a2e98004 |
Master
在Master节点上查看node节点的状态
1 |
kubectl get nodes |
1 2 3 |
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME k8s-dev.192.168.111.200 Ready master 8m19s v1.18.8 192.168.111.200 <none> CentOS Linux 7 (Core) 3.10.0-1160.24.1.el7.x86_64 docker://20.10.6 k8s-dev.192.168.111.201 Ready <none> 64s v1.18.8 192.168.111.201 <none> CentOS Linux 7 (Core) 3.10.0-1160.24.1.el7.x86_64 docker://20.10.6 |
安装flannel
1 2 3 4 |
# 下载flannel部署文档 wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml kubectl apply -f kube-flannel.yml |
> kubectl get pod -n kube-system
1 2 3 4 5 6 7 8 9 10 11 |
NAME READY STATUS RESTARTS AGE coredns-7ff77c879f-5h25x 1/1 Running 0 24m coredns-7ff77c879f-sbdc2 1/1 Running 0 24m etcd-k8s-dev.192.168.111.200 1/1 Running 0 24m kube-apiserver-k8s-dev.192.168.111.200 1/1 Running 0 24m kube-controller-manager-k8s-dev.192.168.111.200 1/1 Running 0 24m kube-flannel-ds-mk8p2 1/1 Running 0 66s kube-flannel-ds-xrppn 1/1 Running 0 66s kube-proxy-cksn4 1/1 Running 0 24m kube-proxy-pq48p 1/1 Running 0 17m kube-scheduler-k8s-dev.192.168.111.200 1/1 Running 0 24m |
1 2 3 4 5 |
# 测试 kubectl create deployment nginx --image=nginx kubectl expose deployment nginx --port=80 --type=NodePort |
> kubectl get pods,svc
1 2 3 4 5 6 |
NAME pod/nginx-f89759699-bv5xw 0/1 ContainerCreating 0 12s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 29m service/nginx NodePort 10.1.177.35 <none> 80:31651/TCP 6s |
> http://192.168.111.200:31651/
> kubectl get nodes -o wide
1 2 3 |
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME k8s-dev.192.168.111.200 Ready master 9m v1.18.8 192.168.111.200 <none> CentOS Linux 7 (Core) 3.10.0-1160.24.1.el7.x86_64 docker://20.10.6 k8s-dev.192.168.111.201 Ready <none> 105s v1.18.8 192.168.111.201 <none> CentOS Linux 7 (Core) 3.10.0-1160.24.1.el7.x86_64 docker://20.10.6 |
如果想赏钱,可以用微信扫描下面的二维码,一来能刺激我写博客的欲望,二来好维护云主机的费用; 另外再次标注博客原地址 itnotebooks.com 感谢!
