当前位置:   article > 正文

【云原生】超详细的Kubernetes(K8S )部署教程_mkdir -p $home/.kube

mkdir -p $home/.kube

目录

1.修改源镜像地址

2.关闭、禁用防火墙

3.需要关闭 swap

4.创建 /etc/sysctl.d/k8s.conf 文件

5.安装 docker (跳过,已安装)

6.修改 docker 配置文件

7.安装 kubelet、kubeadm、kubectl

8.google_containers 配置(可以等 kubeadm init 报错时根据版本做 docker pull)

9.初始化节点,node 节点可以跳过直接到 step11

10.初始化完毕,根据日志提示执 config 命令

11.node 安装(集群模式下,单机后面的步骤都不需要了)

12.将 node 加到 master 中

13.master 节点验证集群及组件,命令查看集群信息


1.修改源镜像地址

  1. # 安装wget命令
  2. $ yum -y install wget
  3. # 备份系统自带的源镜像地址
  4. $ mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
  5. # 下载阿里云的源镜像地址
  6. $ wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  7. # 生成缓冲
  8. $ yum makecache

2.关闭、禁用防火墙

  1. $ systemctl stop firewalld
  2. $ systemctl disable firewalld
  3. # setenforce是Linux的selinux防火墙配置命令, 执行setenforce 0 表示关闭selinux防火墙)
  4. $ setenforce 0

3.需要关闭 swap

  1. $ swapoff -a
  2. # 注释掉 SWAP 的自动挂载
  3. $ vim /etc/fstab
  4. >>
  5. #/dev/mapper/centos-swap swap swap defaults 0 0
  6. # 使用free -m确认swap已经关闭。swap部分为0
  7. $ free -m

4.创建 /etc/sysctl.d/k8s.conf 文件

  1. $ cd /etc/sysctl.d
  2. $ vim k8s.conf
  3. >>
  4. net.bridge.bridge-nf-call-ip6tables = 1
  5. net.bridge.bridge-nf-call-iptables = 1
  6. net.ipv4.ip_forward = 1
  7. vm.swappiness=0
  8. # 使得配置文件生效
  9. $ modprobe br_netfilter
  10. $ sysctl -p /etc/sysctl.d/k8s.conf

5.安装 docker (跳过,已安装)

6.修改 docker 配置文件

  1. $ vim /etc/docker/daemon.json
  2. >>
  3. {
  4. "registry-mirrors": ["https://obww7jh1.mirror.aliyuncs.com"],
  5. "exec-opts": ["native.cgroupdriver=systemd"]
  6. }
  7. # 重启docker
  8. $ systemctl daemon-reload
  9. $ systemctl restart docker

exec-opts是k8s需要的配置

7.安装 kubelet、kubeadm、kubectl

  1. $ cat <<EOF > /etc/yum.repos.d/kubernetes.repo
  2. [kubernetes]
  3. name=Kubernetes
  4. baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
  5. enabled=1
  6. gpgcheck=1
  7. repo_gpgcheck=1
  8. gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
  9. EOF
  10. $ yum install -y kubelet kubeadm kubectl
  11. $ systemctl enable --now kubelet

8.google_containers 配置(可以等 kubeadm init 报错时根据版本做 docker pull)

在kubernetes init之前输入以下命令,由于coredns改名为coredns/coredns了

  1. $ docker pull coredns/coredns:1.8.4
  2. $ docker tag coredns/coredns:1.8.4 registry.aliyuncs.com/google_containers/coredns:v1.8.4

9.初始化节点,node 节点可以跳过直接到 step11

  1. # 如果出错disable swap,执行步骤3
  2. $ kubeadm init --pod-network-cidr=10.244.0.0/16 --image-repository=registry.aliyuncs.com/google_containers
  3. # 如果初始化过程出现问题,使用如下命令重置
  4. $ kubeadm reset
  5. $ rm -rf /var/lib/cni/
  6. $ rm -f $HOME/.kube/config
  7. $ systemctl daemon-reload && systemctl restart kubelet

10.初始化完毕,根据日志提示执 config 命令

  1. To start using your cluster, you need to run the following as a regular user:
  2. mkdir -p $HOME/.kube
  3. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  4. sudo chown $(id -u):$(id -g) $HOME/.kube/config
  5. Alternatively, if you are the root user, you can run:
  6. export KUBECONFIG=/etc/kubernetes/admin.conf
  7. You should now deploy a pod network to the cluster.
  8. Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  9. https://kubernetes.io/docs/concepts/cluster-administration/addons/
  10. Then you can join any number of worker nodes by running the following on each as root:
  11. kubeadm join 10.3.69.72:6443 --token va5ioe.yovvkc002zpz2txv \
  12. --discovery-token-ca-cert-hash sha256:90fc7d96264c7fa6e6e816de44ebf4ae36034d0cef42453d1a01e199502eaac5

根据这个提示安装

  1. $ HOME = /root
  2. $ mkdir -p $HOME/.kube
  3. $ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  4. $ sudo chown $(id -u):$(id -g) $HOME/.kube/config

还需要部署一个 Pod Network 到集群中,此处选择 flannel

$ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

11.node 安装(集群模式下,单机后面的步骤都不需要了)

重复step1 - step8 安装 docker kubelet kubeadm kubectl等,不需要init初始化

理论上来说,只需要在master上面操作集群,node上面不能通过kubectl等工具操作集群。

但是希望在node上面通过kubectl操作集群的话,只需要配置master相同的$HOME/.kube/config

  1. # 在master上,将master中的admin.conf 拷贝到node中,这里是scp的方式,使用客户端手动上传也是可以的
  2. $ scp /etc/kubernetes/admin.conf root@10.11.90.5:/root/
  3. # 在node上
  4. $ HOME = /root
  5. $ mkdir -p $HOME/.kube
  6. $ sudo cp -i $HOME/admin.conf $HOME/.kube/config
  7. $ sudo chown $(id -u):$(id -g) $HOME/.kube/config

12.将 node 加到 master 中

  1. # 在master上面查看kubeadm token
  2. $ kubeadm token list
  3. >>ts699z.y6lmkkn7td2e2lt3
  4. # 如果执行kubeadm token list没有结果,说明token过期了
  5. # 默认情况下,kubeadm生成的token只有24小时有效期,过了时间需要创建一个新的
  6. $ kubeadm token create
  7. # 也可以生成一个永不过期的token,当然这是有安全隐患的
  8. $ kubeadm token create --ttl 0
  9. # 在master上面查看discovery-token-ca-cert-hash
  10. $ openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
  11. >>90fc7d96264c7fa6e6e816de44ebf4ae36034d0cef42453d1a01e199502eaac5
  12. # 在node上面执行join命令
  13. $ kubeadm join 10.11.90.5:6443 --token ts699z.y6lmkkn7td2e2lt3 --discovery-token-ca-cert-hash sha256:42e4caa1339f6929e7b844a24668d6874383e6688f057905fca375aabb9946e3

13.master 节点验证集群及组件,命令查看集群信息

$ kubectl get nodes

k8s删除添加node节点

  1. # 查看节点数和删除node节点(master节点)
  2. $ kubectl get node
  3. NAME STATUS ROLES AGE VERSION
  4. ebgmaster Ready control-plane,master 37d v1.22.0
  5. ebgnode1 Ready <none> 37d v1.22.0
  6. ebgnode2 NotReady <none> 37d v1.22.0
  7. # 删除节点
  8. $ kubectl delete nodes ebgnode2

登陆node节点宿主机,在被删除的node节点清空集群信息

  1. $ kubeadm reset
  2. [reset] WARNING: Changes made to this host by 'kubeadm init' or 'kubeadm join' will be reverted.
  3. [reset] Are you sure you want to proceed? [y/N]: y
  4. [preflight] Running pre-flight checks
  5. W0924 17:09:23.935058 92357 removeetcdmember.go:80] [reset] No kubeadm config, using etcd pod spec to get data directory
  6. [reset] No etcd config found. Assuming external etcd
  7. [reset] Please, manually reset etcd to prevent further issues
  8. [reset] Stopping the kubelet service
  9. [reset] Unmounting mounted directories in "/var/lib/kubelet"
  10. [reset] Deleting contents of config directories: [/etc/kubernetes/manifests /etc/kubernetes/pki]
  11. [reset] Deleting files: [/etc/kubernetes/admin.conf /etc/kubernetes/kubelet.conf /etc/kubernetes/bootstrap-kubelet.conf /etc/kubernetes/controller-manager.conf /etc/kubernetes/scheduler.conf]
  12. [reset] Deleting contents of stateful directories: [/var/lib/kubelet /var/lib/dockershim /var/run/kubernetes /var/lib/cni]
  13. The reset process does not clean CNI configuration. To do so, you must remove /etc/cni/net.d
  14. The reset process does not reset or clean up iptables rules or IPVS tables.
  15. If you wish to reset iptables, you must do so manually by using the "iptables" command.
  16. If your cluster was setup to utilize IPVS, run ipvsadm --clear (or similar)
  17. to reset your system's IPVS tables.
  18. The reset process does not clean your kubeconfig files and you must remove them manually.
  19. Please, check the contents of the $HOME/.kube/config file.
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/73960
推荐阅读
相关标签
  

闽ICP备14008679号