引言
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上启动所有容器的维护工作 |
无法被容器化部署—->必须手动下载 |
- systemctl status kubelet
- systemctl daemon-reload
- systemctl enable kubelet && systemctl start kubelet
- journalctl -xeu kubelet
2.基础安装
```shell【step1】 获取-阿里源
sudo curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
【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
> 【脚本】[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)
<a name="ZDKU9"></a>
# 3.集群搭建
![](https://cdn.nlark.com/yuque/0/2022/jpeg/247346/1645770443839-d2cc25c4-c3dc-4a4d-a0dd-47f6e2233245.jpeg)
| mater | 192.168.1.161 |
| --- | --- |
| node1 | 192.168.1.159 |
> **参考**
> [ubuntu20.04设置配置静态ip方法](https://www.cnblogs.com/kehoudaanxianjie/p/13139636.html)
> <br />
<a name="rfbBH"></a>
## 1.集群初始化
集群的初始化主要使用kubeadm这个工具。
```shell
#【step1----master+worker执行】设置:homename
hostnamectl set-hostname master
hostnamectl set-hostname node1
【step2----master+worker执行】 配置:hosts
cat >> /etc/hosts << EOF
192.168.1.161 master
192.168.1.159 node1
EOF
【step3----master执行】前置配置
swapoff -a # 关闭-交换分区
kubeadm reset # 重置-kubeadm
rm -fv /etc/cni/net.d
systemctl daemon-reload #重置加载服务
systemctl restart kubelet # 重启-kubelet
iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X # iptables的配置
【step4----master执行】使用kubeadm进行“集群初始化”
#--apiserver-advertise-address=master_ip[192.168.1.161]---< 配置master机器的ip
# --pod-network-cidr=10.244.0.0/16------< 固定的配置【不需要修改ip】
#--image-repository registry.aliyuncs.com/google_containers -----< 指定【镜像仓库】阿里
kubeadm init \
--apiserver-advertise-address=192.168.1.161 \
--image-repository registry.aliyuncs.com/google_containers \
--pod-network-cidr=10.244.0.0/16
1.【master-强制】关闭swap
# master执行
#【step1】 关闭-交换分区
swapoff -a
#【step2】进入文件:/etc/fstab,并注释“/swap.img" ----> #[操作] /swap.img none swap sw 0 0
vim /etc/fstab
2.集群初始化-成功的标识
2.集群配置
当集群初始化成功后,我们将需要进行一系列的集群配置,如下所示:
1.【master】集群基础配置
【step5----master执行】
mkdir -p $HOME/.kube
rm -rf /etc/kubernetes/manifests
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
export KUBECONFIG=/etc/kubernetes/admin.conf
2.【master】集群-安装网络插件flannel
【step6----master执行】
# wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f kube-flannel.yml
3.worker关联绑定master
worker关联绑定master的步骤如下(图)所示:
【step7----node1执行】
kubeadm join 192.168.1.161:6443 --token oxm60t.nxiz36thk8zm5xto \
--discovery-token-ca-cert-hash sha256:63f5a4ca08bddd29ecd98e8b1f64db003626bbb928471921499268ed503a03a6
1.[master] 获取集群的token和sha256
应用场景 当忘记k8s的token和sha256时,可以在master机器运行以下脚本:
kubeadm token create --print-join-command
# 获取原始的kubeadm的token及sha256
# #获取token
# kubeadm token list | awk -F" " '{print $1}' |tail -n 1
# #获取sha256
# openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^ .* //'
#
# #获取本机ip
# ip a | grep inet | grep -v inet6 | grep -v '127.0.0.1' | awk '{print $2}' | head -n 1
def getInfo():
import subprocess
token_shell="kubeadm token list | awk -F\" \" '{print $1}' |tail -n 1 "
token = subprocess.check_output(token_shell, shell=True)
token = token.decode('utf-8')
print(token)
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/^ .* //'"
sha256_raw = subprocess.check_output(sha256_shell, shell=True)
sha256_raw = sha256_raw.decode('utf-8')
#print("sha256_raw:\t", sha256_raw)
sha256 = sha256_raw[9:]
print("sha256:\t", sha256)
ip_shell = "ip a | grep inet | grep -v inet6 | grep -v '127.0.0.1' | awk '{print $2}' | head -n 1"
ip_raw = subprocess.check_output(ip_shell, shell=True)
ip_raw = ip_raw.decode('utf-8')
#print("ip_raw:\t",ip_raw)
ip_list = ip_raw.split("/")
ip=ip_list[0]
print("ip:\t", ip)
#print("ip-type:\t", type(ip))
#kubeadm join 192.168.1.123:6443 --token bdmbao.h9dp14frv3rifzwy --discovery-token-ca-cert-hash sha256:ed9edf18f1a8922c42e9672e6245a6ab1db111f32d17d270929e2999ba34b5d8
k8s_join_info = f"kubeadm join {ip}:6443 -token {token} --discovery-token-ca-cert-hash sha256:{sha256}"
with open("k8s_join.log",'w') as f:
f.writelines(k8s_join_info)
print("k8s_join-info:\t", k8s_join_info, end=' ')
if __name__ == '__main__':
getInfo()
4.查看集群nodes
master查看集群nodes的步骤如下(图)所示:
【step8----master执行】
kubectl get nodes
4.部署nginx
1.创建nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod ##pod名称
labels:
app: nginx ###写上这个pod的标签,方便svc连接
spec:
containers:
- name: nginx-pod ##pod容器名称
image: nginx:latest ###镜像
imagePullPolicy: IfNotPresent ###镜像拉取策略
ports:
- containerPort: 80
2.创建nginx-svc.yaml进行外网映射
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
spec:
selector:
app: nginx ###关联容器标签
ports:
- port: 80 ###容器端口
nodePort: 30002 ###nodeport映射为30002端口,便于外部主机访问
type: NodePort
3.执行yaml
kubectl apply -f nginx-pod.yaml
kubectl apply -f nginx-svc.yaml
kubectl get pod
kubectl apply -f nginx-pod.yaml && kubectl apply -f nginx-svc.yaml