引言

k8s的安装与集群的搭建,往往是我们深入学习k8s的拦路虎(因为无法成功安装k8s环境,而不得已放弃学习k8s)。今天有幸在公司运维大哥的指导下,成功安装k8s环境、集群搭建及部署一个简单的nginx服务。 学习内容

  • k8s基本环境的安装【基于Ubuntu20.04-server】
  • k8s集群搭建
    • 集群初始化
    • 集群配置
  • 部署nginx服务

前提 保障已安装docker环境【3.[Ubuntu20.04]一键安装Docker

1.简述

1.k8s的工具集

名称 用途 备注
kubeadm 集群安装工具
kubectl 命令行工具
kubelet
- 【负责】维护当前节点agent的状态status
- 【负责】节点agent上启动所有容器的维护工作
无法被容器化部署—->必须手动下载

【step2】更新ubuntu的本地软件源

sudo apt-get update

【step3】安装软件:kubelet kubeadm kubectl

sudo apt install -y kubelet kubeadm kubectl

【step4】验证:kubeadm是否安装成功

kubeadm version

【step5】设置:开机自启动,并启动服务

systemctl enable kubelet && systemctl start kubelet

  1. > 【脚本】[install_k8s (2).sh](https://www.yuque.com/attachments/yuque/0/2022/sh/247346/1645516971637-cb3c4dca-cf63-4282-8c60-c15ef619376c.sh?_lake_card=%7B%22src%22%3A%22https%3A%2F%2Fwww.yuque.com%2Fattachments%2Fyuque%2F0%2F2022%2Fsh%2F247346%2F1645516971637-cb3c4dca-cf63-4282-8c60-c15ef619376c.sh%22%2C%22name%22%3A%22install_k8s+%282%29.sh%22%2C%22size%22%3A220%2C%22type%22%3A%22text%2Fx-sh%22%2C%22ext%22%3A%22sh%22%2C%22status%22%3A%22done%22%2C%22taskId%22%3A%22uffd30921-dafb-4d0c-8e73-e76a38f8fd8%22%2C%22taskType%22%3A%22upload%22%2C%22id%22%3A%22u16e467e2%22%2C%22card%22%3A%22file%22%7D)
  2. <a name="ZDKU9"></a>
  3. # 3.集群搭建
  4. ![](https://cdn.nlark.com/yuque/0/2022/jpeg/247346/1645770443839-d2cc25c4-c3dc-4a4d-a0dd-47f6e2233245.jpeg)
  5. | mater | 192.168.1.161 |
  6. | --- | --- |
  7. | node1 | 192.168.1.159 |
  8. > **参考**
  9. > [ubuntu20.04设置配置静态ip方法](https://www.cnblogs.com/kehoudaanxianjie/p/13139636.html)
  10. > <br />
  11. <a name="rfbBH"></a>
  12. ## 1.集群初始化
  13. 集群的初始化主要使用kubeadm这个工具。
  14. ```shell
  15. #【step1----master+worker执行】设置:homename
  16. hostnamectl set-hostname master
  17. hostnamectl set-hostname node1
  18. 【step2----master+worker执行】 配置:hosts
  19. cat >> /etc/hosts << EOF
  20. 192.168.1.161 master
  21. 192.168.1.159 node1
  22. EOF
  23. 【step3----master执行】前置配置
  24. swapoff -a # 关闭-交换分区
  25. kubeadm reset # 重置-kubeadm
  26. rm -fv /etc/cni/net.d
  27. systemctl daemon-reload #重置加载服务
  28. systemctl restart kubelet # 重启-kubelet
  29. iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X # iptables的配置
  30. 【step4----master执行】使用kubeadm进行“集群初始化”
  31. #--apiserver-advertise-address=master_ip[192.168.1.161]---< 配置master机器的ip
  32. # --pod-network-cidr=10.244.0.0/16------< 固定的配置【不需要修改ip】
  33. #--image-repository registry.aliyuncs.com/google_containers -----< 指定【镜像仓库】阿里
  34. kubeadm init \
  35. --apiserver-advertise-address=192.168.1.161 \
  36. --image-repository registry.aliyuncs.com/google_containers \
  37. --pod-network-cidr=10.244.0.0/16

1.【master-强制】关闭swap

  1. # master执行
  2. #【step1】 关闭-交换分区
  3. swapoff -a
  4. #【step2】进入文件:/etc/fstab,并注释“/swap.img" ----> #[操作] /swap.img none swap sw 0 0
  5. vim /etc/fstab

image.png

2.集群初始化-成功的标识

若出现有下图一样的内容,则标识k8s集群已初始化成功。
image.png

2.集群配置

当集群初始化成功后,我们将需要进行一系列的集群配置,如下所示:

1.【master】集群基础配置

  1. step5----master执行】
  2. mkdir -p $HOME/.kube
  3. rm -rf /etc/kubernetes/manifests
  4. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  5. sudo chown $(id -u):$(id -g) $HOME/.kube/config
  6. export KUBECONFIG=/etc/kubernetes/admin.conf

2.【master】集群-安装网络插件flannel

1.[VM]Ubuntu20.04安装k8s - 图4

  1. step6----master执行】
  2. # wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
  3. wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
  4. kubectl apply -f kube-flannel.yml

3.worker关联绑定master

worker关联绑定master的步骤如下(图)所示:

  1. step7----node1执行】
  2. kubeadm join 192.168.1.161:6443 --token oxm60t.nxiz36thk8zm5xto \
  3. --discovery-token-ca-cert-hash sha256:63f5a4ca08bddd29ecd98e8b1f64db003626bbb928471921499268ed503a03a6

image.png

1.[master] 获取集群的token和sha256

应用场景 当忘记k8s的token和sha256时,可以在master机器运行以下脚本:

  1. kubeadm token create --print-join-command

image.png

  1. # 获取原始的kubeadm的token及sha256
  2. # #获取token
  3. # kubeadm token list | awk -F" " '{print $1}' |tail -n 1
  4. # #获取sha256
  5. # openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^ .* //'
  6. #
  7. # #获取本机ip
  8. # ip a | grep inet | grep -v inet6 | grep -v '127.0.0.1' | awk '{print $2}' | head -n 1
  9. def getInfo():
  10. import subprocess
  11. token_shell="kubeadm token list | awk -F\" \" '{print $1}' |tail -n 1 "
  12. token = subprocess.check_output(token_shell, shell=True)
  13. token = token.decode('utf-8')
  14. print(token)
  15. sha256_shell = "openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^ .* //'"
  16. sha256_raw = subprocess.check_output(sha256_shell, shell=True)
  17. sha256_raw = sha256_raw.decode('utf-8')
  18. #print("sha256_raw:\t", sha256_raw)
  19. sha256 = sha256_raw[9:]
  20. print("sha256:\t", sha256)
  21. ip_shell = "ip a | grep inet | grep -v inet6 | grep -v '127.0.0.1' | awk '{print $2}' | head -n 1"
  22. ip_raw = subprocess.check_output(ip_shell, shell=True)
  23. ip_raw = ip_raw.decode('utf-8')
  24. #print("ip_raw:\t",ip_raw)
  25. ip_list = ip_raw.split("/")
  26. ip=ip_list[0]
  27. print("ip:\t", ip)
  28. #print("ip-type:\t", type(ip))
  29. #kubeadm join 192.168.1.123:6443 --token bdmbao.h9dp14frv3rifzwy --discovery-token-ca-cert-hash sha256:ed9edf18f1a8922c42e9672e6245a6ab1db111f32d17d270929e2999ba34b5d8
  30. k8s_join_info = f"kubeadm join {ip}:6443 -token {token} --discovery-token-ca-cert-hash sha256:{sha256}"
  31. with open("k8s_join.log",'w') as f:
  32. f.writelines(k8s_join_info)
  33. print("k8s_join-info:\t", k8s_join_info, end=' ')
  34. if __name__ == '__main__':
  35. getInfo()

get_k8sadm_info.py

4.查看集群nodes

master查看集群nodes的步骤如下(图)所示:

  1. step8----master执行】
  2. kubectl get nodes

image.png

4.部署nginx

1.创建nginx-pod.yaml

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: nginx-pod ##pod名称
  5. labels:
  6. app: nginx ###写上这个pod的标签,方便svc连接
  7. spec:
  8. containers:
  9. - name: nginx-pod ##pod容器名称
  10. image: nginx:latest ###镜像
  11. imagePullPolicy: IfNotPresent ###镜像拉取策略
  12. ports:
  13. - containerPort: 80

2.创建nginx-svc.yaml进行外网映射

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: nginx-svc
  5. spec:
  6. selector:
  7. app: nginx ###关联容器标签
  8. ports:
  9. - port: 80 ###容器端口
  10. nodePort: 30002 ###nodeport映射为30002端口,便于外部主机访问
  11. type: NodePort

3.执行yaml

  1. kubectl apply -f nginx-pod.yaml
  2. kubectl apply -f nginx-svc.yaml
  3. kubectl get pod
  4. kubectl apply -f nginx-pod.yaml && kubectl apply -f nginx-svc.yaml

4.验证是否部署成功

image.png

image.png