其他分享
首页 > 其他分享> > Kubernetes 安装手册

Kubernetes 安装手册

作者:互联网

2022-06-03 14:56 字数 10612 阅读 14    

Kubernetes 安装手册(非高可用版)

K8S

 

 

 

集群信息

 

1. 节点规划

部署k8s集群的节点按照用途可以划分为如下2类角色:

本例为了演示slave节点的添加,会部署一台master+2台slave,节点规划如下:

主机名节点ip角色部署组件
k8s-master 172.21.32.15 master etcd, kube-apiserver, kube-controller-manager, kubectl, kubeadm, kubelet, kube-proxy, flannel
k8s-slave1 172.21.32.11 slave kubectl, kubelet, kube-proxy, flannel
k8s-slave2 172.21.32.9 slave kubectl, kubelet, kube-proxy, flannel
 

2. 组件版本

组件版本说明
CentOS 7.6.1810  
Kernel Linux 3.10.0-1062.9.1.el7.x86_64  
etcd 3.3.15 使用容器方式部署,默认数据挂载到本地路径
coredns 1.6.2  
kubeadm v1.16.2  
kubectl v1.16.2  
kubelet v1.16.2  
kube-proxy v1.16.2  
flannel v0.11.0  
 

安装前准备工作

 

1. 设置hosts解析

操作节点:所有节点(k8s-master,k8s-slave)均需执行

 
  1. # 在master节点
  2. $ hostnamectl set-hostname k8s-master #设置master节点的hostname
  3. # 在slave-1节点
  4. $ hostnamectl set-hostname k8s-slave1 #设置slave1节点的hostname
  5. # 在slave-2节点
  6. $ hostnamectl set-hostname k8s-slave2 #设置slave2节点的hostname
 
  1. $ cat >>/etc/hosts<<EOF
  2. 172.21.32.15 k8s-master
  3. 172.21.32.11 k8s-slave1
  4. 172.21.32.9 k8s-slave2
  5. EOF
 

2. 调整系统配置

操作节点: 所有的master和slave节点(k8s-master,k8s-slave)需要执行

本章下述操作均以k8s-master为例,其他节点均是相同的操作(ip和hostname的值换成对应机器的真实值)

如果节点间无安全组限制(内网机器间可以任意访问),可以忽略,否则,至少保证如下端口可通:
k8s-master节点:TCP:6443,2379,2380,60080,60081UDP协议端口全部打开
k8s-slave节点:UDP协议端口全部打开

 
  1. iptables -P FORWARD ACCEPT
 
  1. swapoff -a
  2. # 防止开机自动挂载 swap 分区
  3. sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
 
  1. sed -ri 's#(SELINUX=).*#\1disabled#' /etc/selinux/config
  2. setenforce 0
  3. systemctl disable firewalld && systemctl stop firewalld
 
  1. cat <<EOF > /etc/sysctl.d/k8s.conf
  2. net.bridge.bridge-nf-call-ip6tables = 1
  3. net.bridge.bridge-nf-call-iptables = 1
  4. net.ipv4.ip_forward=1
  5. vm.max_map_count=262144
  6. EOF
  7. modprobe br_netfilter
  8. sysctl -p /etc/sysctl.d/k8s.conf
 
  1. $ curl -o /etc/yum.repos.d/Centos-7.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  2. $ curl -o /etc/yum.repos.d/docker-ce.repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  3. $ cat <<EOF > /etc/yum.repos.d/kubernetes.repo
  4. [kubernetes]
  5. name=Kubernetes
  6. baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
  7. enabled=1
  8. gpgcheck=0
  9. repo_gpgcheck=0
  10. gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
  11. http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
  12. EOF
  13. $ yum clean all && yum makecache
 

3. 安装docker

操作节点: 所有节点

 
  1. ## 查看所有的可用版本
  2. $ yum list docker-ce --showduplicates | sort -r
  3. ##安装旧版本 yum install docker-ce-cli-18.09.9-3.el7 docker-ce-18.09.9-3.el7
  4. ## 安装源里最新版本
  5. $ yum install docker-ce -y
  6. ## 配置docker加速
  7. $ mkdir -p /etc/docker
  8. vi /etc/docker/daemon.json
  9. {
  10. "insecure-registries": [
  11. "172.21.32.15:5000"
  12. ],
  13. "registry-mirrors" : [
  14. "https://8xpk5wnt.mirror.aliyuncs.com"
  15. ]
  16. }
  17. ## 启动docker
  18. $ systemctl enable docker && systemctl start docker
 

部署kubernetes

 

1. 安装 kubeadm, kubelet 和 kubectl

操作节点: 所有的master和slave节点(k8s-master,k8s-slave) 需要执行

 
  1. $ yum install -y kubelet-1.19.2 kubeadm-1.19.2 kubectl-1.19.2 --disableexcludes=kubernetes
  2. ## 查看kubeadm 版本
  3. $ kubeadm version
  4. ## 设置kubelet开机启动
  5. $ systemctl enable kubelet
 

2. 初始化配置文件

操作节点: 只在master节点(k8s-master)执行

 
  1. $ kubeadm config print init-defaults > kubeadm.yaml
  2. $ cat kubeadm.yaml
  3. apiVersion: kubeadm.k8s.io/v1beta2
  4. bootstrapTokens:
  5. - groups:
  6. - system:bootstrappers:kubeadm:default-node-token
  7. token: abcdef.0123456789abcdef
  8. ttl: 24h0m0s
  9. usages:
  10. - signing
  11. - authentication
  12. kind: InitConfiguration
  13. localAPIEndpoint:
  14. advertiseAddress: 172.21.32.15 # apiserver地址,因为单master,所以配置master的节点内网IP
  15. bindPort: 6443
  16. nodeRegistration:
  17. criSocket: /var/run/dockershim.sock
  18. name: k8s-master
  19. taints:
  20. - effect: NoSchedule
  21. key: node-role.kubernetes.io/master
  22. ---
  23. apiServer:
  24. timeoutForControlPlane: 4m0s
  25. apiVersion: kubeadm.k8s.io/v1beta2
  26. certificatesDir: /etc/kubernetes/pki
  27. clusterName: kubernetes
  28. controllerManager: {}
  29. dns:
  30. type: CoreDNS
  31. etcd:
  32. local:
  33. dataDir: /var/lib/etcd
  34. imageRepository: registry.aliyuncs.com/google_containers # 修改成阿里镜像源
  35. kind: ClusterConfiguration
  36. kubernetesVersion: v1.16.2
  37. networking:
  38. dnsDomain: cluster.local
  39. podSubnet: 10.244.0.0/16 # Pod 网段,flannel插件需要使用这个网段
  40. serviceSubnet: 10.96.0.0/12
  41. scheduler: {}

对于上面的资源清单的文档比较杂,要想完整了解上面的资源对象对应的属性,可以查看对应的 godoc 文档,地址: https://godoc.org/k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta2

 

3. 提前下载镜像

操作节点:只在master节点(k8s-master)执行

 
  1. # 查看需要使用的镜像列表,若无问题,将得到如下列表
  2. $ kubeadm config images list --config kubeadm.yaml
  3. registry.aliyuncs.com/google_containers/kube-apiserver:v1.16.0
  4. registry.aliyuncs.com/google_containers/kube-controller-manager:v1.16.0
  5. registry.aliyuncs.com/google_containers/kube-scheduler:v1.16.0
  6. registry.aliyuncs.com/google_containers/kube-proxy:v1.16.0
  7. registry.aliyuncs.com/google_containers/pause:3.1
  8. registry.aliyuncs.com/google_containers/etcd:3.3.15-0
  9. registry.aliyuncs.com/google_containers/coredns:1.6.2
  10. # 提前下载镜像到本地
  11. $ kubeadm config images pull --config kubeadm.yaml
  12. [config/images] Pulled registry.aliyuncs.com/google_containers/kube-apiserver:v1.16.0
  13. [config/images] Pulled registry.aliyuncs.com/google_containers/kube-controller-manager:v1.16.0
  14. [config/images] Pulled registry.aliyuncs.com/google_containers/kube-scheduler:v1.16.0
  15. [config/images] Pulled registry.aliyuncs.com/google_containers/kube-proxy:v1.16.0
  16. [config/images] Pulled registry.aliyuncs.com/google_containers/pause:3.1
  17. [config/images] Pulled registry.aliyuncs.com/google_containers/etcd:3.3.15-0
  18. [config/images] Pulled registry.aliyuncs.com/google_containers/coredns:1.6.2

重要更新:如果出现不可用的情况,请使用如下方式来代替:

  1. 还原kubeadm.yaml的imageRepository

    1. ...
    2. imageRepository: k8s.gcr.io
    3. ...
    4. ## 查看使用的镜像源
    5. kubeadm config images list --config kubeadm.yaml
    6. k8s.gcr.io/kube-apiserver:v1.16.0
    7. k8s.gcr.io/kube-controller-manager:v1.16.0
    8. k8s.gcr.io/kube-scheduler:v1.16.0
    9. k8s.gcr.io/kube-proxy:v1.16.0
    10. k8s.gcr.io/pause:3.1
    11. k8s.gcr.io/etcd:3.3.15-0
    12. k8s.gcr.io/coredns:1.6.2
  2. 使用docker hub中的镜像源来下载,注意上述列表中要加上处理器架构,通常我们使用的虚拟机都是amd64

    1. $ docker pull mirrorgooglecontainers/kube-scheduler-amd64:v1.16.0
    2. $ docker pull mirrorgooglecontainers/etcd-amd64:3.3.15-0
    3. ...
    4. $ docker tag mirrorgooglecontainers/etcd-amd64:3.3.15-0 k8s.gcr.io/etcd:3.3.15-0
 

4. 初始化master节点

操作节点:只在master节点(k8s-master)执行

 
  1. kubeadm init --config kubeadm.yaml

若初始化成功后,最后会提示如下信息:

 
  1. ...
  2. Your Kubernetes master has initialized successfully!
  3. To start using your cluster, you need to run the following as a regular user:
  4. mkdir -p $HOME/.kube
  5. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  6. sudo chown $(id -u):$(id -g) $HOME/.kube/config
  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 172.21.32.15:6443 --token abcdef.0123456789abcdef \
  12. --discovery-token-ca-cert-hash sha256:1c4305f032f4bf534f628c32f5039084f4b103c922ff71b12a5f0f98d1ca9a4f

接下来按照上述提示信息操作,配置kubectl客户端的认证

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

⚠️注意:此时使用 kubectl get nodes查看节点应该处于notReady状态,因为还未配置网络插件

若执行初始化过程中出错,根据错误信息调整后,执行kubeadm reset后再次执行init操作即可

 

5. 添加slave节点到集群中

操作节点:所有的slave节点(k8s-slave)需要执行
在每台slave节点,执行如下命令,该命令是在kubeadm init成功后提示信息中打印出来的,需要替换成实际init后打印出的命令。

 
  1. kubeadm join 172.21.32.15:6443 --token abcdef.0123456789abcdef \
  2. --discovery-token-ca-cert-hash sha256:1c4305f032f4bf534f628c32f5039084f4b103c922ff71b12a5f0f98d1ca9a4f
 

6. 安装flannel插件

操作节点:只在master节点(k8s-master)执行

 
  1. wget https://raw.githubusercontent.com/coreos/flannel/2140ac876ef134e0ed5af15c65e414cf26827915/Documentation/kube-flannel.yml
 
  1. $ vi kube-flannel.yml
  2. ...
  3. containers:
  4. - name: kube-flannel
  5. image: quay.io/coreos/flannel:v0.11.0-amd64
  6. command:
  7. - /opt/bin/flanneld
  8. args:
  9. - --ip-masq
  10. - --kube-subnet-mgr
  11. - --iface=eth0 # 如果机器存在多网卡的话,指定内网网卡的名称,默认不指定的话会找第一块网
  12. resources:
  13. requests:
  14. cpu: "100m"
  15. ...
 
  1. # 先拉取镜像,此过程国内速度比较慢
  2. $ docker pull quay.io/coreos/flannel:v0.11.0-amd64
  3. # 执行flannel安装
  4. $ kubectl create -f kube-flannel.yml
 

7. 设置master节点是否可调度(可选)

操作节点:k8s-master

默认部署成功后,master节点无法调度业务pod,如需设置master节点也可以参与pod的调度,需执行:

 
  1. $ kubectl taint node k8s-master node-role.kubernetes.io/master:NoSchedule-
 

8. 验证集群

操作节点: 在master节点(k8s-master)执行

 
  1. $ kubectl get nodes #观察集群节点是否全部Ready
  2. NAME STATUS ROLES AGE VERSION
  3. k8s-master Ready master 22h v1.13.3
  4. k8s-slave Ready <none> 22h v1.13.3

创建测试nginx服务

 
  1. $ kubectl run test-nginx --image=nginx:alpine

查看pod是否创建成功,并访问pod ip测试是否可用

 
  1. $ kubectl get po -o wide
  2. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  3. test-nginx-5bd8859b98-5nnnw 1/1 Running 0 9s 10.244.1.2 k8s-slave1 <none> <none>
  4. $ curl 10.244.1.2
  5. ...
  6. <h1>Welcome to nginx!</h1>
  7. <p>If you see this page, the nginx web server is successfully installed and
  8. working. Further configuration is required.</p>
  9. <p>For online documentation and support please refer to
  10. <a href="http://nginx.org/">nginx.org</a>.<br/>
  11. Commercial support is available at
  12. <a href="http://nginx.com/">nginx.com</a>.</p>
  13. <p><em>Thank you for using nginx.</em></p>
  14. </body>
  15. </html>
 

9. 部署dashboard

 
  1. # 推荐使用下面这种方式
  2. $ wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta5/aio/deploy/recommended.yaml
  3. $ vi recommended.yaml
  4. # 修改Service为NodePort类型
  5. ......
  6. kind: Service
  7. apiVersion: v1
  8. metadata:
  9. labels:
  10. k8s-app: kubernetes-dashboard
  11. name: kubernetes-dashboard
  12. namespace: kubernetes-dashboard
  13. spec:
  14. ports:
  15. - port: 443
  16. targetPort: 8443
  17. selector:
  18. k8s-app: kubernetes-dashboard
  19. type: NodePort # 加上type=NodePort变成NodePort类型的服务
  20. ......
 
  1. kubectl create -f recommended.yaml
  2. kubectl -n kubernetes-dashboard get svc
  3. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  4. dashboard-metrics-scraper ClusterIP 10.105.62.124 <none> 8000/TCP 31m
  5. kubernetes-dashboard NodePort 10.103.74.46 <none> 443:30133/TCP 31m
 
  1. $ vi admin.conf
  2. kind: ClusterRoleBinding
  3. apiVersion: rbac.authorization.k8s.io/v1beta1
  4. metadata:
  5. name: admin
  6. annotations:
  7. rbac.authorization.kubernetes.io/autoupdate: "true"
  8. roleRef:
  9. kind: ClusterRole
  10. name: cluster-admin
  11. apiGroup: rbac.authorization.k8s.io
  12. subjects:
  13. - kind: ServiceAccount
  14. name: admin
  15. namespace: kubernetes-dashboard
  16. ---
  17. apiVersion: v1
  18. kind: ServiceAccount
  19. metadata:
  20. name: admin
  21. namespace: kubernetes-dashboard
  22. $ kubectl create -f admin.conf
  23. $ kubectl -n kubernetes-dashboard get secret |grep admin-token
  24. admin-token-fqdpf kubernetes.io/service-account-token 3 7m17s
  25. # 使用该命令拿到token,然后粘贴到
  26. $ kubectl -n kubernetes-dashboard get secret admin-token-fqdpf -o jsonpath={.data.token}|base64 -d
  27. eyJhbGciOiJSUzI1NiIsImtpZCI6Ik1rb2xHWHMwbWFPMjJaRzhleGRqaExnVi1BLVNRc2txaEhETmVpRzlDeDQifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi10b2tlbi1mcWRwZiIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJhZG1pbiIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6IjYyNWMxNjJlLTQ1ZG...

 

 

 

10. 清理环境

如果你的集群安装过程中遇到了其他问题,我们可以使用下面的命令来进行重置:

 
  1. $ kubeadm reset
  2. $ ifconfig cni0 down && ip link delete cni0
  3. $ ifconfig flannel.1 down && ip link delete flannel.1
  4. $ rm -rf /var/lib/cni/
    2022-06-03 14:56 字数 10612 阅读 14      

标签:kube,kubernetes,Kubernetes,手册,master,kubeadm,k8s,安装,节点
来源: https://www.cnblogs.com/mayongjie/p/16386886.html