一、概述

详情参考阿里云说明:https://help.aliyun.com/document_detail/98886.html?spm=a2c4g.11186623.6.1078.323b1c9bpVKOry

项目资源分配(数据库、中间件除外):


二、部署镜像仓库

1)  部署docker-compose,然后参考下文部署docker。

  1. $ sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  2. $ sudo chmod +x /usr/local/bin/docker-compose
  3. $ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
  4. $ docker-compose --version
  5. docker-compose version 1.26.2, build 1110ad01

2)  创建镜像仓库域名证书。

  1. mkdir -p /data/cert && chmod -R 777 /data/cert && cd /data/cert
  2. openssl req -x509 -sha256 -nodes -days 3650 -newkey rsa:2048-keyout harbor.key -out harbor.crt -subj "/CN=hub.jhmy.com"

3)  下载harbor离线包,编辑harbor.yml,修改主机地址、证书路径、仓库密码。

4)  执行install.sh部署,完成之后访问 https://hostip 即可。

  1. 部署流程:检查环境 -> 导入镜像 -> 准备环境 -> 准备配置 -> 开始启动


三、   系统初始化

1)        设置主机名以及域名解析

  1. hostnamectl set-hostname k8s101
  2. cat >> /etc/hosts <<EOF
  3. 172.1.1.114 hub.jhmy.com
  4. 172.1.1.101 k8s101
  5. 172.1.1.102 k8s102
  6. 172.1.1.103 k8s103
  7. 172.1.1.104 k8s104
    ……
  8. 172.1.1.99 k8sapi
  9. EOF

2)        节点之前建立无密登录

  1. ssh-keygen
  2. ssh-copy-id -i .ssh/id_rsa.pub root@k8s-node1

3)        安装依赖包、常用软件,以及同步时间时区

  1. yum -y install vim curl wget unzip ntpdate net-tools ipvsadm ipset sysstat conntrack libseccomp
  2. ntpdate ntp1.aliyun.com && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

4)        关闭swap、selinux、firewalld

  1. swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
  2. setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
  3. systemctl stop firewalld && systemctl disable firewalld

5)        调整系统内核参数

  1. cat > /etc/sysctl.d/kubernetes.conf <<EOF
  2. net.bridge.bridge-nf-call-iptables=1
  3. net.bridge.bridge-nf-call-ip6tables=1
  4. net.ipv6.conf.all.disable_ipv6=1
  5. net.ipv4.ip_forward=1
  6. net.ipv4.tcp_tw_recycle=0
  7. vm.swappiness=0
  8. fs.file-max=2000000
  9. fs.nr_open=2000000
  10. fs.inotify.max_user_instances=512
  11. fs.inotify.max_user_watches=1280000
  12. net.netfilter.nf_conntrack_max=524288
  13. EOF
  14.  
  15. modprobe br_netfilter && sysctl -p /etc/sysctl.d/kubernetes.conf

6)        加载系统ipvs相关模块

  1. cat > /etc/sysconfig/modules/ipvs.modules <<EOF
  2. #!/bin/bash
  3. modprobe -- ip_vs
  4. modprobe -- ip_vs_rr
  5. modprobe -- ip_vs_wrr
  6. modprobe -- ip_vs_sh
  7. modprobe -- nf_conntrack_ipv4
  8. EOF
  9.  
  10. chmod 755 /etc/sysconfig/modules/ipvs.modules
  11. sh /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_

7)        安装nfs文件共享服务

  1. yum -y install nfs-common nfs-utils rpcbind
  2. systemctl start nfs && systemctl enable nfs
  3. systemctl start rpcbind && systemctl enable rpcbind

四、   部署高可用集群

1)        安装部署docker

  1. # 设置镜像源,安装docker及组件
  2. yum install -y yum-utils device-mapper-persistent-data lvm2
  3. yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  4. yum install -y docker-ce-19.03.5 docker-ce-cli-19.03.5
  5.  
  6. # 设置镜像加速,仓库地址,日志模式
  7. mkdir /etc/docker
  8. cat > /etc/docker/daemon.json <<EOF
  9. {
  10. "registry-mirrors": ["https://jc3y13r3.mirror.aliyuncs.com"],
  11. "insecure-registries":["hub.jhmy.com"],
    "data-root": "/data/docker",
  12. "exec-opts": ["native.cgroupdriver=systemd"],
  13. "log-driver": "json-file",
  14. "log-opts": { "max-size": "100m" }
  15. }
  16. EOF
  17.  
  18. # 重启docker,设置启动
  19. mkdir -p /etc/systemd/system/docker.service.d
  20. systemctl daemon-reload && systemctl restart docker && systemctl enable docker

2)        安装部署kubernetes

  1. # 设置kubernetes镜像源
  2. cat <<EOF > /etc/yum.repos.d/kubernetes.repo
  3. [kubernetes]
  4. name=Kubernetes
  5. baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
  6. enabled=1
  7. gpgcheck=0
  8. repo_gpgcheck=0
  9. gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
  10. http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
  11. EOF
  12.  
  13. # 安装kubeadm、kebelet、kubectl
  14. yum -y install kubeadm-1.17.5 kubelet-1.17.5 kubectl-1.17.5 --setopt=obsoletes=0
  15. systemctl enable kubelet.service

3)        初始化管理节点

任选一台master节点,修改当前master节点 /etc/hosts,把 k8sapi 对应解析地址修改为当前节点地址(系统初始化时我们统一配置成slb负载地址了)。

虽然我们打算利用阿里云的SLB进行kube-apiserver负载,但是此时集群未启动,无法监听k8sapi端口,也就是还无法访问到SLB负载的端口,

那么集群初始化将会失败,所以我们暂时先用当前节点地址作为负载地址,也就是自己负载自己,来先实现集群初始化。

注意:因为是正式环境,我们尽量修改一些默认值,比如:token、apiserver端口、etcd数据路径、podip网段等。

  1. # kubeadm config print init-defaults > kubeadm-config.yaml
    # vim kubeadm-config.yaml
  1. apiVersion: kubeadm.k8s.io/v1beta2
  2. bootstrapTokens:
  3. - groups:
  4. - system:bootstrappers:kubeadm:default-node-token
  5. token: token0.123456789kubeadm
  6. ttl: 24h0m0s
  7. usages:
  8. - signing
  9. - authentication
  10. kind: InitConfiguration
  11. localAPIEndpoint:
  12. advertiseAddress: 172.1.1.101
  13. bindPort: 6333
  14. nodeRegistration:
  15. criSocket: /var/run/dockershim.sock
  16. name: k8s
  17. taints:
  18. - effect: NoSchedule
  19. key: node-role.kubernetes.io/master
  20. ---
  21. apiServer:
  22. timeoutForControlPlane: 4m0s
  23. apiVersion: kubeadm.k8s.io/v1beta2
  24. certificatesDir: /etc/kubernetes/pki
  25. clusterName: kubernetes
  26. controlPlaneEndpoint: "k8sapi:6333"
  27. controllerManager: {}
  28. dns:
  29. type: CoreDNS
  30. etcd:
  31. local:
  32. dataDir: /data/etcd
  33. imageRepository: registry.aliyuncs.com/google_containers
  34. kind: ClusterConfiguration
  35. kubernetesVersion: v1.17.5
  36. networking:
  37. dnsDomain: cluster.local
  38. serviceSubnet: 10.96.0.0/12
  39. podSubnet: 10.233.0.0/16
  40. scheduler: {}
  41. ---
  42. apiVersion: kubeproxy.config.k8s.io/v1alpha1
  43. kind: KubeProxyConfiguration
  44. featureGates:
  45. SupportIPVSProxyMode: true
  46. mode: ipvs
  1. # kubeadm init --config=kubeadm-config.yaml --upload-certs | tee kubeadm-init.log

k8s主节点初始化完成后,打开阿里云负载均衡配置,增加SLB内网对kube-apiserver负载配置(这里只能用四层TCP)。

暂且只配置当前master地址,等待其他master节点加入成功后再添加,因为其他两台master还未加入,此时如果配置其他master地址,SLB负载均衡状态将会异常,那其他节点尝试加入集群将会失败。

4)        加入其余管理节点和工作节点

  1. # 根据初始化日志提示,执行kubeadm join命令加入其他管理节点
  2. kubeadm join 192.168.17.100:6444 --token abcdef.0123456789abcdef \
  3. --discovery-token-ca-cert-hash sha256:56d53268517... \
  4. --experimental-control-plane --certificate-key c4d1525b6cce4....
  5.  
  6. # 根据日志提示,所有管理节点执行以下命令,赋予用户命令权限。
  7. mkdir -p $HOME/.kube
  8. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  9. sudo chown $(id -u):$(id -g) $HOME/.kube/config
  10.  
  11. # 根据初始化日志提示,执行kubeadm join命令加入其他工作节点
  12. kubeadm join 192.168.17.100:6444 --token abcdef.0123456789abcdef \
  13. --discovery-token-ca-cert-hash sha256:260796226d………… 
  1. 注意:token有效期为24小时,失效后请在主节点使用以下命令重新生成
  1. kubeadm token create --print-join-command

修改新加入master节点apiserver端口,以及补全阿里云SLB apiserver负载地址。

  1. # 修改kube-apiserver监听端口
  2. sed -i 's/6443/6333/g' /etc/kubernetes/manifests/kube-apiserver.yaml
  3. # 重启kube-apiserver容器
  4. docker restart `docker ps | grep k8s_kube-apiserver | awk '{print $1}'`
  5. # 查看kube-apiserver监听端口
  6. ss -anp | grep "apiserver" | grep 'LISTEN'

注意:如果忘记修改,后面部署可能会出现错误,比如kube-prometheus

  1. [root@ymt-130 manifests]# kubectl -n monitoring logs pod/prometheus-operator-5bd99d6457-8dv29
  2. ts=2020-08-27T07:00:51.38650537Z caller=main.go:199 msg="Starting Prometheus Operator version '0.34.0'."
  3. ts=2020-08-27T07:00:51.38962086Z caller=main.go:96 msg="Staring insecure server on :8080"
  4. ts=2020-08-27T07:00:51.39038717Z caller=main.go:315 msg="Unhandled error received. Exiting..." err="communicating with server failed: Get https://10.96.0.1:443/version?timeout=32s: dial tcp 10.96.0.1:443: connect: connection refused"

5)        部署网络,检查集群健康状况

  1. # 执行准备好的yaml部署文件
  2. kubectl apply -f kube-flannel.yaml
  3.  
  4. # 检查集群部署情况
  5. kubectl get cs && kubectl get nodes && kubectl get pod --all-namespaces
  6.  
  7. # 检查etcd集群健康状态(需要上传etcdctl二进制文件)
  8. [root@k8s101 ~]# etcdctl --cert /etc/kubernetes/pki/etcd/peer.crt --key /etc/kubernetes/pki/etcd/peer.key --endpoints https://172.1.1.101:2379,https://172.1.1.102:2379,https://172.1.1.103:2379 --insecure-skip-tls-verify endpoint health
  9. https://172.1.1.101:2379 is healthy: successfully committed proposal: took = 12.396169ms
  10. https://172.1.1.102:2379 is healthy: successfully committed proposal: took = 12.718211ms
  11. https://172.1.1.103:2379 is healthy: successfully committed proposal: took = 13.174164ms

6)        Kubelet驱逐策略优化

  1. # 修改工作节点kubelet启动参数,更改Pod驱逐策略
  1. vim /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf
  2. Environment="EVICTION_HARD=--eviction-hard=memory.available<2Gi,nodefs.available<5Gi,imagefs.available<100Gi"
  3. Environment="EVICTION_RECLAIM=--eviction-minimum-reclaim=memory.available=0Mi,nodefs.available=1Gi,imagefs.available=2Gi"

  1. # 重启kubelet容器,并查看kubelet进程启动参数
  1. [root@k8s104 ~]# systemctl daemon-reload && systemctl restart kubelet
  2. [root@k8s104 ~]# ps -ef | grep kubelet | grep -v grep
  3. [root@k8s104 ~]# ps -ef | grep "/usr/bin/kubelet" | grep -v grep
  4. root 24941 1 2 Aug27 ? 03:00:12 /usr/bin/kubelet --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf
    --config=/var/lib/kubelet/config.yaml --cgroup-driver=systemd --network-plugin=cni --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.1
    --eviction-hard=memory.available<2Gi,nodefs.available<5Gi,imagefs.available<100Gi --eviction-minimum-reclaim=memory.available=0Mi,nodefs.available=1Gi,imagefs.available=2Gi

更多信息:Kubelet 对资源紧缺状况的应对


五、   部署功能组件

1)        部署七层路由Ingress

  1. # 部署Ingress路由和基础组件转发规则
  1. kubectl apply -f nginx-ingress
  1. # 通过修改nginx-config来配置负载地址和最大连接数
  1. kubectl edit cm nginx-config -n nginx-ingress

  1. # 可以适当调整Ingress对外开放端口,然后进行阿里云SLB外网工作负载配置(所有工作节点)

更多详情:Nginx全局配置

2)        部署页面工具Dashboard

  1. # 执行准备好的yaml部署文件
  1. kubectl apply -f kube-dashboard.yml
  1. # 等待部署完成
  1. kubectl get pod -n kubernetes-dashboard

  1. # 通过域名登录控制页面, Token需要使用命令查看(本地需要配置域名解析)
  1. kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep dashboard-admin | awk '{print $1}')
  1. https://k8s.dashboard.com:IngressPort

3)        部署日志收集Filebeat

  1. # 修改匹配日志、logstash地址、宿主机目录

  1. # 然后执行部署即可
  1. kubectl apply -f others/kube-filebeat.yml
  1.  

  1. ---
  2. apiVersion: v1
  3. kind: ConfigMap
  4. metadata:
  5. name: filebeat-config
  6. namespace: kube-system
  7. labels:
  8. k8s-app: filebeat
  9. data:
  10. filebeat.yml: |-
  11. filebeat.inputs:
  12. - type: log
  13. paths:
  14. - /home/ymt/logs/appdatamonitor/warn.log
  15. output.logstash:
  16. hosts: ["10.88.88.169:5044"]
  17. ---
  18. # filebeat.config:
  19. # inputs:
  20. # # Mounted `filebeat-inputs` configmap:
  21. # path: ${path.config}/inputs.d/*.yml
  22. # # Reload inputs configs as they change:
  23. # reload.enabled: false
  24. # modules:
  25. # path: ${path.config}/modules.d/*.yml
  26. # # Reload module configs as they change:
  27. # reload.enabled: false
  28.  
  29. # To enable hints based autodiscover, remove `filebeat.config.inputs` configuration and uncomment this:
  30. #filebeat.autodiscover:
  31. # providers:
  32. # - type: kubernetes
  33. # hints.enabled: true
  34.  
  35. # processors:
  36. # - add_cloud_metadata:
  37.  
  38. # cloud.id: ${ELASTIC_CLOUD_ID}
  39. # cloud.auth: ${ELASTIC_CLOUD_AUTH}
  40.  
  41. # output.elasticsearch:
  42. # hosts: ['${ELASTICSEARCH_HOST:elasticsearch}:${ELASTICSEARCH_PORT:9200}']
  43. # username: ${ELASTICSEARCH_USERNAME}
  44. # password: ${ELASTICSEARCH_PASSWORD}
  45. ---
  46. # apiVersion: v1
  47. # kind: ConfigMap
  48. # metadata:
  49. # name: filebeat-inputs
  50. # namespace: kube-system
  51. # labels:
  52. # k8s-app: filebeat
  53. # data:
  54. # kubernetes.yml: |-
  55. # - type: docker
  56. # containers.ids:
  57. # - "*"
  58. # processors:
  59. # - add_kubernetes_metadata:
  60. # in_cluster: true
  61. ---
  62. apiVersion: apps/v1
  63. kind: DaemonSet
  64. metadata:
  65. name: filebeat
  66. namespace: kube-system
  67. labels:
  68. k8s-app: filebeat
  69. spec:
  70. selector:
  71. matchLabels:
  72. k8s-app: filebeat
  73. template:
  74. metadata:
  75. labels:
  76. k8s-app: filebeat
  77. spec:
  78. serviceAccountName: filebeat
  79. terminationGracePeriodSeconds: 30
  80. containers:
  81. - name: filebeat
  82. # image: docker.elastic.co/beats/filebeat:6.7.2
  83. image: registry.cn-shanghai.aliyuncs.com/leozhanggg/elastic/filebeat:6.7.1
  84. args: [
  85. "-c", "/etc/filebeat.yml",
  86. "-e",
  87. ]
  88. # env:
  89. # - name: ELASTICSEARCH_HOST
  90. # value: elasticsearch
  91. # - name: ELASTICSEARCH_PORT
  92. # value: "9200"
  93. # - name: ELASTICSEARCH_USERNAME
  94. # value: elastic
  95. # - name: ELASTICSEARCH_PASSWORD
  96. # value: changeme
  97. # - name: ELASTIC_CLOUD_ID
  98. # value:
  99. # - name: ELASTIC_CLOUD_AUTH
  100. # value:
  101. securityContext:
  102. runAsUser: 0
  103. # If using Red Hat OpenShift uncomment this:
  104. #privileged: true
  105. resources:
  106. limits:
  107. memory: 200Mi
  108. requests:
  109. cpu: 100m
  110. memory: 100Mi
  111. volumeMounts:
  112. - name: config
  113. mountPath: /etc/filebeat.yml
  114. readOnly: true
  115. subPath: filebeat.yml
  116. # - name: inputs
  117. # mountPath: /usr/share/filebeat/inputs.d
  118. # readOnly: true
  119. - name: data
  120. mountPath: /usr/share/filebeat/data
  121. - name: ymtlogs
  122. mountPath: /home/ymt/logs
  123. readOnly: true
  124. # - name: varlibdockercontainers
  125. # mountPath: /var/lib/docker/containers
  126. # readOnly: true
  127. volumes:
  128. - name: config
  129. configMap:
  130. defaultMode: 0600
  131. name: filebeat-config
  132. - name: ymtlogs
  133. hostPath:
  134. path: /home/ymt/logs
  135. # - name: varlibdockercontainers
  136. # hostPath:
  137. # path: /var/lib/docker/containers
  138. # - name: inputs
  139. # configMap:
  140. # defaultMode: 0600
  141. # name: filebeat-inputs
  142. # data folder stores a registry of read status for all files, so we don't send everything again on a Filebeat pod restart
  143. - name: data
  144. hostPath:
  145. path: /var/lib/filebeat-data
  146. type: DirectoryOrCreate
  147. ---
  148. apiVersion: rbac.authorization.k8s.io/v1
  149. kind: ClusterRoleBinding
  150. metadata:
  151. name: filebeat
  152. subjects:
  153. - kind: ServiceAccount
  154. name: filebeat
  155. namespace: kube-system
  156. roleRef:
  157. kind: ClusterRole
  158. name: filebeat
  159. apiGroup: rbac.authorization.k8s.io
  160. ---
  161. apiVersion: rbac.authorization.k8s.io/v1
  162. kind: ClusterRole
  163. metadata:
  164. name: filebeat
  165. labels:
  166. k8s-app: filebeat
  167. rules:
  168. - apiGroups: [""] # "" indicates the core API group
  169. resources:
  170. - namespaces
  171. - pods
  172. verbs:
  173. - get
  174. - watch
  175. - list
  176. ---
  177. apiVersion: v1
  178. kind: ServiceAccount
  179. metadata:
  180. name: filebeat
  181. namespace: kube-system
  182. labels:
  183. k8s-app: filebeat
  184. ---

kube-filebeat.yaml

  1. 注意:因为我们logstashES均部署在外部,所以这里k8s集群仅部署了filebeat,用于收集日志传输到集群外部logstash

4)        部署监控平台Prometheus

  1. # 先部署默认组件
  1. cd kube-prometheus-0.3.0/manifests
  2. kubectl create -f setup && sleep 5 && kubectl create -f .
  1. # 等待部署完成
  1. kubectl get pod -n monitoring

  1. # 然后修改自定义监控配置,执行升级脚本
  1. cd custom && sh upgrade.sh
  1. *   告警配置:alertmanager.yaml
  1. *   默认告警规则:prometheus-rules.yaml
  1. *   新增告警规则:prometheus-additional-rules.yaml
  1. *   新增监控项配置:prometheus-additional.yaml    #调整监控项及地址
  1. *   监控配置:prometheus-prometheus.yaml    #调整副本数和资源限制
  1. # 通过域名登录监控页面(本地需要配置域名解析)
  1.    http://k8s.grafana.com:IngressPort     # 默认用户和密码都是admin
  1.    http://k8s.prometheus.com:IngressPort
  1.    http://k8s.alertmanager.com:IngressPort
  1. # 点击添加按钮 ->Import ->Upload .json file,导入监控仪表板。
  1. *   k8s-model.json
  1. *   node-model.json

详情参考:Kubernetes实战总结 - 自定义Prometheus


五、   其他问题说明

1)  Kubectl命令使用

  1. # 命令自动部署设置
  2. yum install -y bash-completion
  3. source /usr/share/bash-completion/bash_completion
  4. source <(kubectl completion bash)
  5. echo "source <(kubectl completion bash)" >> ~/.bashrc

官方文档:Kubernetes kubectl 命令表

网络博文:kubernetes常用命令整理

 

2)  延长证书有效期

  1. # 查看证书有效期
  2. kubeadm alpha certs check-expiration
  3. # 重新生成所有证书
  4. kubeadm alpha certs renew all
  5. # 分别重启所有主节点组件容器
  6. docker ps | \
  7. grep -E 'k8s_kube-apiserver|k8s_kube-controller-manager|k8s_kube-scheduler|k8s_etcd_etcd' | \
  8. awk -F ' ' '{print $1}' |xargs docker restart
  1.  

3)  卸载k8s集群节点

  1. # 将要卸载的节点标记为不能再调度
  2. kubectl cordon k8s-node1
  3. # 将该节点上运行的容器平滑迁移到其他节点上
  4. kubectl drain nodeA --delete-local-data --force
  5. # 从集群上删除该节点
  6. kubectl delete node k8s-node1
  7.  
  8. # 在删除的节点上重置配置
  9. kubeadm reset
  10. # 根据提示手动删除相应文件
  11. rm -rf /etc/cni/net.d
  12. ipvsadm --clear
  13. rm -rf /root/.kube/
  14. # 停止 kubelet服务
  15. systemctl stop kubelet
  16.  
  17. # 查看安装过的k8s的软件包
  18. yum list installed | grep 'kube'
  19. # 卸载k8s相关安装包
  20. yum remove kubeadm.x86_64 kubectl.x86_64 cri-tools.x86_64 kubernetes-cni.x86_64 kubelet.x86_64
  1.  

4)  彻底清除节点网络

  1. # 重置节点
  2. kubeadm reset -f
  3. # 清除配置
  4. rm -rf $HOME/.kube/config /etc/cni/net.d && ipvsadm --clear
  5. # 停止docker
  6. systemctl stop kubelet && systemctl stop docker
  7. # 删除网络配置和路由记录
  8. rm -rf /var/lib/cni/
  9. ip link delete cni0
  10. ip link delete flannel.1
  11. ip link delete dummy0
  12. ip link delete kube-ipvs0
  13. # 重启docker和network
  14. systemctl restart docker && systemctl restart kubelet && systemctl restart network
  1. # 有时候更换网路插件可能会出现podcidr错误,可以进行手动更改
  2. kubectl describe node k8s112 | grep PodCIDR
  3. kubectl patch node k8s112 -p '{"spec":{"podCIDR":"10.233.0.0/16"}}'
  1.  

5)  部署应用到master节点

  1. #增加不可调度容忍和主节点节点亲和性
  2. tolerations:
  3. - key: node-role.kubernetes.io/master
  4. effect: NoSchedule
  5. affinity:
  6. nodeAffinity:
  7. requiredDuringSchedulingIgnoredDuringExecution:
  8. nodeSelectorTerms:
  9. - matchExpressions:
  10. - key: node-role.kubernetes.io/master
  11. operator: Exists
  1. 注意:我们部署k8s dashboard时有时发现使用主节点地址打开特别的卡,但是我们使用部署的节点打开就非常的流畅,
       那么我们只需要给dashboard增加此配置,即让dashboard部署在主节点,这样使用主节点打开就会非常的流畅了。

6)  修改k8s节点名称

  1. # 阿里云自建K8S集群可能会出现连接apiserver失败情况,一般是由于K8S在做DNS名称解析的时候出现了较长的解析请求,可以通过修改node名称的方式解决。
  2. hostname ymt-140
  3. vim /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf
  4. Environment="KUBELET_HOSTNAME=--hostname-override=ymt-140"
  5. $KUBELET_HOSTNAME
  6. systemctl daemon-reload && systemctl restart kubelet && ps -ef | grep /usr/bin/kubelet | grep -v grep
  7. journalctl -xe -u kubelet
  1.  

7)  部署日志记录

  1. [root@k8s101 ~]# kubeadm init --config=kubeadm-config.yaml --upload-certs | tee kubeadm-init.log
  2. W0819 09:24:09.326568 28880 validation.go:28] Cannot validate kube-proxy config - no validator is available
  3. W0819 09:24:09.326626 28880 validation.go:28] Cannot validate kubelet config - no validator is available
  4. [init] Using Kubernetes version: v1.17.5
  5. [preflight] Running pre-flight checks
  6. [preflight] Pulling images required for setting up a Kubernetes cluster
  7. [preflight] This might take a minute or two, depending on the speed of your internet connection
  8. [preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
  9. [kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
  10. [kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
  11. [kubelet-start] Starting the kubelet
  12. [certs] Using certificateDir folder "/etc/kubernetes/pki"
  13. [certs] Generating "ca" certificate and key
  14. [certs] Generating "apiserver" certificate and key
  15. [certs] apiserver serving cert is signed for DNS names [k8s101 kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local k8sapi] and IPs [10.96.0.1 172.1.1.101]
  16. [certs] Generating "apiserver-kubelet-client" certificate and key
  17. [certs] Generating "front-proxy-ca" certificate and key
  18. [certs] Generating "front-proxy-client" certificate and key
  19. [certs] Generating "etcd/ca" certificate and key
  20. [certs] Generating "etcd/server" certificate and key
  21. [certs] etcd/server serving cert is signed for DNS names [k8s101 localhost] and IPs [172.1.1.101 127.0.0.1 ::1]
  22. [certs] Generating "etcd/peer" certificate and key
  23. [certs] etcd/peer serving cert is signed for DNS names [k8s101 localhost] and IPs [172.1.1.101 127.0.0.1 ::1]
  24. [certs] Generating "etcd/healthcheck-client" certificate and key
  25. [certs] Generating "apiserver-etcd-client" certificate and key
  26. [certs] Generating "sa" key and public key
  27. [kubeconfig] Using kubeconfig folder "/etc/kubernetes"
  28. [kubeconfig] Writing "admin.conf" kubeconfig file
  29. [kubeconfig] Writing "kubelet.conf" kubeconfig file
  30. [kubeconfig] Writing "controller-manager.conf" kubeconfig file
  31. [kubeconfig] Writing "scheduler.conf" kubeconfig file
  32. [control-plane] Using manifest folder "/etc/kubernetes/manifests"
  33. [control-plane] Creating static Pod manifest for "kube-apiserver"
  34. [control-plane] Creating static Pod manifest for "kube-controller-manager"
  35. W0819 09:24:14.028737 28880 manifests.go:214] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC"
  36. [control-plane] Creating static Pod manifest for "kube-scheduler"
  37. W0819 09:24:14.029728 28880 manifests.go:214] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC"
  38. [etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
  39. [wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
  40. [apiclient] All control plane components are healthy after 16.502551 seconds
  41. [upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
  42. [kubelet] Creating a ConfigMap "kubelet-config-1.17" in namespace kube-system with the configuration for the kubelets in the cluster
  43. [upload-certs] Storing the certificates in Secret "kubeadm-certs" in the "kube-system" Namespace
  44. [upload-certs] Using certificate key:
  45. 8782750a5ffd83f0fdbe635eced5e6b1fc4acd73a2a13721664494170a154a01
  46. [mark-control-plane] Marking the node k8s101 as control-plane by adding the label "node-role.kubernetes.io/master=''"
  47. [mark-control-plane] Marking the node k8s101 as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
  48. [bootstrap-token] Using token: zwx051.085210868chiscdc
  49. [bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
  50. [bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
  51. [bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
  52. [bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
  53. [bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
  54. [kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
  55. [addons] Applied essential addon: CoreDNS
  56. [addons] Applied essential addon: kube-proxy
  57.  
  58. Your Kubernetes control-plane has initialized successfully!
  59.  
  60. To start using your cluster, you need to run the following as a regular user:
  61.  
  62. mkdir -p $HOME/.kube
  63. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  64. sudo chown $(id -u):$(id -g) $HOME/.kube/config
  65.  
  66. You should now deploy a pod network to the cluster.
  67. Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  68. https://kubernetes.io/docs/concepts/cluster-administration/addons/
  69.  
  70. You can now join any number of the control-plane node running the following command on each as root:
  71.  
  72. kubeadm join k8sapi:6333 --token zwx051.085210868chiscdc \
  73. --discovery-token-ca-cert-hash sha256:de4d9a37423fecd5313a76d99ad60324cdb0ca6a38254de549394afa658c98b2 \
  74. --control-plane --certificate-key 8782750a5ffd83f0fdbe635eced5e6b1fc4acd73a2a13721664494170a154a01
  75.  
  76. Please note that the certificate-key gives access to cluster sensitive data, keep it secret!
  77. As a safeguard, uploaded-certs will be deleted in two hours; If necessary, you can use
  78. "kubeadm init phase upload-certs --upload-certs" to reload certs afterward.
  79.  
  80. Then you can join any number of worker nodes by running the following on each as root:
  81.  
  82. kubeadm join k8sapi:6333 --token zwx051.085210868chiscdc \
  83. --discovery-token-ca-cert-hash sha256:de4d9a37423fecd5313a76d99ad60324cdb0ca6a38254de549394afa658c98b2
  84.  
  85. [root@k8s102 ~]# kubeadm join k8sapi:6333 --token zwx051.085210868chiscdc \
  86. > --discovery-token-ca-cert-hash sha256:de4d9a37423fecd5313a76d99ad60324cdb0ca6a38254de549394afa658c98b2 \
  87. > --control-plane --certificate-key 8782750a5ffd83f0fdbe635eced5e6b1fc4acd73a2a13721664494170a154a01
  88. [preflight] Running pre-flight checks
  89. [preflight] Reading configuration from the cluster...
  90. [preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
  91. [preflight] Running pre-flight checks before initializing the new control plane instance
  92. [preflight] Pulling images required for setting up a Kubernetes cluster
  93. [preflight] This might take a minute or two, depending on the speed of your internet connection
  94. [preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
  95. [download-certs] Downloading the certificates in Secret "kubeadm-certs" in the "kube-system" Namespace
  96. [certs] Using certificateDir folder "/etc/kubernetes/pki"
  97. [certs] Generating "apiserver" certificate and key
  98. [certs] apiserver serving cert is signed for DNS names [k8s101 kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local k8sapi] and IPs [10.96.0.1 172.1.1.102]
  99. [certs] Generating "apiserver-kubelet-client" certificate and key
  100. [certs] Generating "etcd/server" certificate and key
  101. [certs] etcd/server serving cert is signed for DNS names [k8s101 localhost] and IPs [172.1.1.102 127.0.0.1 ::1]
  102. [certs] Generating "etcd/healthcheck-client" certificate and key
  103. [certs] Generating "etcd/peer" certificate and key
  104. [certs] etcd/peer serving cert is signed for DNS names [k8s101 localhost] and IPs [172.1.1.102 127.0.0.1 ::1]
  105. [certs] Generating "apiserver-etcd-client" certificate and key
  106. [certs] Generating "front-proxy-client" certificate and key
  107. [certs] Valid certificates and keys now exist in "/etc/kubernetes/pki"
  108. [certs] Using the existing "sa" key
  109. [kubeconfig] Generating kubeconfig files
  110. [kubeconfig] Using kubeconfig folder "/etc/kubernetes"
  111. [endpoint] WARNING: port specified in controlPlaneEndpoint overrides bindPort in the controlplane address
  112. [kubeconfig] Writing "admin.conf" kubeconfig file
  113. [kubeconfig] Writing "controller-manager.conf" kubeconfig file
  114. [kubeconfig] Writing "scheduler.conf" kubeconfig file
  115. [control-plane] Using manifest folder "/etc/kubernetes/manifests"
  116. [control-plane] Creating static Pod manifest for "kube-apiserver"
  117. W0819 10:31:17.604671 4058 manifests.go:214] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC"
  118. [control-plane] Creating static Pod manifest for "kube-controller-manager"
  119. W0819 10:31:17.612645 4058 manifests.go:214] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC"
  120. [control-plane] Creating static Pod manifest for "kube-scheduler"
  121. W0819 10:31:17.613524 4058 manifests.go:214] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC"
  122. [check-etcd] Checking that the etcd cluster is healthy
  123. [kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.17" ConfigMap in the kube-system namespace
  124. [kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
  125. [kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
  126. [kubelet-start] Starting the kubelet
  127. [kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
  128. [etcd] Announced new etcd member joining to the existing etcd cluster
  129. [etcd] Creating static Pod manifest for "etcd"
  130. [etcd] Waiting for the new etcd member to join the cluster. This can take up to 40s
  131. {"level":"warn","ts":"2020-08-19T10:31:31.039+0800","caller":"clientv3/retry_interceptor.go:61","msg":"retrying of unary invoker failed","target":"passthrough:///https://172.1.1.102:2379","attempt":0,"error":"rpc error: code = DeadlineExceeded desc = context deadline exceeded"}
  132. [upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
  133. [mark-control-plane] Marking the node k8s101 as control-plane by adding the label "node-role.kubernetes.io/master=''"
  134. [mark-control-plane] Marking the node k8s101 as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
  135.  
  136. This node has joined the cluster and a new control plane instance was created:
  137.  
  138. * Certificate signing request was sent to apiserver and approval was received.
  139. * The Kubelet was informed of the new secure connection details.
  140. * Control plane (master) label and taint were applied to the new node.
  141. * The Kubernetes control plane instances scaled up.
  142. * A new etcd member was added to the local/stacked etcd cluster.
  143.  
  144. To start administering your cluster from this node, you need to run the following as a regular user:
  145.  
  146. mkdir -p $HOME/.kube
  147. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  148. sudo chown $(id -u):$(id -g) $HOME/.kube/config
  149.  
  150. Run 'kubectl get nodes' to see this node join the cluster.

kubeadm-init.log

  1.  

作者:Leozhanggg

出处:https://www.cnblogs.com/leozhanggg/p/13522155.html

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  1.  

Kubernetes实战总结 - 阿里云ECS自建K8S集群的更多相关文章

  1. 阿里云ECS服务器部署HADOOP集群(一):Hadoop完全分布式集群环境搭建

    准备: 两台配置CentOS 7.3的阿里云ECS服务器: hadoop-2.7.3.tar.gz安装包: jdk-8u77-linux-x64.tar.gz安装包: hostname及IP的配置: ...

  2. 阿里云ECS服务器部署HADOOP集群(二):HBase完全分布式集群搭建(使用外置ZooKeeper)

    本篇将在阿里云ECS服务器部署HADOOP集群(一):Hadoop完全分布式集群环境搭建的基础上搭建,多添加了一个 datanode 节点 . 1 节点环境介绍: 1.1 环境介绍: 服务器:三台阿里 ...

  3. 阿里云ECS服务器部署HADOOP集群(三):ZooKeeper 完全分布式集群搭建

    本篇将在阿里云ECS服务器部署HADOOP集群(一):Hadoop完全分布式集群环境搭建的基础上搭建,多添加了一个 datanode 节点 . 1 节点环境介绍: 1.1 环境介绍: 服务器:三台阿里 ...

  4. 阿里云ECS服务器部署HADOOP集群(六):Flume 安装

    本篇将在阿里云ECS服务器部署HADOOP集群(一):Hadoop完全分布式集群环境搭建的基础上搭建. 1 环境介绍 一台阿里云ECS服务器:master 操作系统:CentOS 7.3 Hadoop ...

  5. 阿里云ECS服务器部署HADOOP集群(七):Sqoop 安装

    本篇将在 阿里云ECS服务器部署HADOOP集群(一):Hadoop完全分布式集群环境搭建 阿里云ECS服务器部署HADOOP集群(二):HBase完全分布式集群搭建(使用外置ZooKeeper) 阿 ...

  6. 阿里云ECS服务器部署HADOOP集群(五):Pig 安装

    本篇将在阿里云ECS服务器部署HADOOP集群(一):Hadoop完全分布式集群环境搭建的基础上搭建. 1 环境介绍 一台阿里云ECS服务器:master 操作系统:CentOS 7.3 Hadoop ...

  7. 阿里云ECS服务器部署HADOOP集群(四):Hive本地模式的安装

    本篇将在阿里云ECS服务器部署HADOOP集群(一):Hadoop完全分布式集群环境搭建的基础上搭建. 本地模式需要采用MySQL数据库存储数据. 1 环境介绍 一台阿里云ECS服务器:master ...

  8. 自建k8s集群日志采集到阿里云日志服务

    自建k8s集群 的master 节点安装 logtail 采集工具 wget http://logtail-release-cn-hangzhou.oss-cn-hangzhou.aliyuncs.c ...

  9. k8s 开船记-故障公告:自建 k8s 集群在阿里云上大翻船

    非常非常抱歉,新年上班第一天, 在今天阿里云上气候突变情况下,由于我们开船技术差,在今天 10:15~12:00 左右的访问高峰,我们竟然把船给开翻了,造成近2个小时整个博客站点无法访问,由此给您带来 ...

随机推荐

  1. python数据处理PDF高清电子书

    点击获取提取码:jzgv 内容简介 本书采用基于项目的方法,介绍用Python完成数据获取.数据清洗.数据探索.数据呈现.数据规模化和自动化的过程.主要内容包括:Python基础知识,如何从CSV.E ...

  2. Springboot使用JdbcTemplate的使用

    在spring-boot-starter-jdbc这个依赖包中一共分成四个部分. core,JdbcTemplate等相关核心接口和类 datasource,数据源相关的辅助类 object,将基本的 ...

  3. 使用idea 时出现classnotfound

    如果是web项目,而且确定包在.. 不用担心,点开Project structure 在artifacts 选择就好了..双击就可以  包就进lib下了

  4. js数组中如何去除重复值?

    在日常开发中,我们可能会遇到将一个数组中里面的重复值去除,那么,我就将我自己所学习到的几种方法分享出来 去除数组重复值方法: 1,利用indexOf()方法去除 思路:创建一个新数组,然后循环要去重的 ...

  5. Java助教工作总结

    很荣幸在步入在研究生之际,有机会能协助代老师完成面向对象程序设计(java)课程的教学工作.这也是我人生中第一次接触助教工作,好多东西不太清楚,也没经验,有什么做的不好的,还望老师同学及时指出. 上周 ...

  6. 实验03——java十进制转二、八、十六进制;打印'中'的十六进制;进制转换的api

    package cn.tedu.demo; /** * @author 赵瑞鑫 E-mail:1922250303@qq.com * @version 创建时间:2020年7月16日 上午10:22: ...

  7. 5、Java 修饰符

    引言:Java的修饰符根据修饰的对象不同,分为类修饰符.方法修饰符.变量修饰符,其中每种修饰符又分为访问控制修饰符和非访问控制修饰符. 1.访问控制修饰符的总结 四个关键字:public.protec ...

  8. 1、Java 开发环境配置

    Java 开发环境配置 在本章节中我们将为大家介绍如何搭建Java开发环境. Windows 上安装开发环境 Linux 上安装开发环境 安装 Eclipse 运行 Java window系统安装ja ...

  9. Mysql Column 'xxxxx' in field list is ambiguous"

    使用了关联查询,两张表有相同的字段,所以说取值含糊不清: 使用别名.列名解决: 如 a.description

  10. JS学习第四天

    循环语句 while(){ }: while :先判断再执行,如果while的条件结果为false,那么执行语句块中代码一句都不走 do(){ }while; do   while:先执行代码块中的语 ...