Kubernetes初探

在部署公司的告警系统Prometheus时,看到很多博客都在用k8s,这里也学习一下,其实几年前都想学,因为种种原因搁置到现在,惭愧

这里先说一下k8s,是一种容器编排软件,什么是编排?我理解的是:用了docker后,发现软件发布变得方便极了,但同时,如果想要扩展、备份就需要收到写一些脚本什么的,所以k8s就来了。官网关于k8s的定义是容器化集群管理系统。原文:
Kubernetes is a portable, extensible, open source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation。
那么Kubernetes 都能干什么呢?
在这里插入图片描述
也就是:

  1. 服务发现、负载均衡
  2. 存储编排
  3. 滚动更新、回退
  4. 资源自动装箱
  5. 自我修复
  6. 密钥和配置管理

总之这些都是一个健壮的系统所需要的东西。可以先不理解,在官网的互动教程中学习使用,比如Learn Kubernetes Basics 中,有关于replica的操作,根据操作来加深理解。我目前也只学了Learn Kubernetes Basics,在自己的电脑装了minikube,准备把其他的教程能看的都看一看。
这里写一下,初次装minikube,所需要的步骤,毕竟踩了很多坑,花了很长时间才装好。

# 自己的虚拟机装的是centos7.9 minimal。装好后先改下yum的源,我一般用tuna的源 :
# 对于 CentOS 7
sudo sed -e 's|^mirrorlist=|#mirrorlist=|g' \
         -e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.tuna.tsinghua.edu.cn|g' \
         -i.bak \
         /etc/yum.repos.d/CentOS-*.repo

# 对于 CentOS 8
sudo sed -e 's|^mirrorlist=|#mirrorlist=|g' \
         -e 's|^#baseurl=http://mirror.centos.org/$contentdir|baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos|g' \
         -i.bak \
         /etc/yum.repos.d/CentOS-*.repo
# 更新软件包缓存
sudo yum makecache

下面开始正式的步骤:

1、装docker,请参照官网。新建用户kube,配置sudo权限

# 添加用户
adduser kube
# 修改权限
nano /etc/sudoers
kube    ALL=(ALL)      NOPASSWD: ALL

2、配置docker镜像仓库

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://hj3oa7u5.mirror.aliyuncs.com"]
}
EOF
# 重启docker
sudo systemctl daemon-reload
sudo systemctl restart docker
# 设置docker 开机自启动
sudo systemctl enable docker.service

3、配置yum kubernetes仓库

# 把gpgcheck设置为0,关掉gpg签名,要不然也会报错
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
# 更新下 yum
yum clean all
yum -y makecache

4、安装 kubelet kubeadm kubectl(按理说我用的minikube,不用装这些,但是不装启动不了)

https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
# Set SELinux in permissive mode (effectively disabling it)
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
sudo yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
sudo systemctl enable --now kubelet

5、关闭一堆防火墙/内存交换和命令补全

# 关闭防火墙
sudo systemctl stop firewalld
# 开机也不要启动firewalld
sudo systemctl disable firewalld.service
# 如果不想关闭,也可以这样开放某个端口
# 因为 kubectl proxy 默认是8001,在开启minikube dashboard时,我这样
# kubectl proxy --port=8001 --address='192.168.56.105' --accept-hosts='^.*' &
# 但是怎么都访问不通,在本地curl可以,后来才知道是firewall给拦截了,费了很长时间才知道
firewall-cmd --zone=public --add-port=8001/tcp --permanent
firewall-cmd --reload

# 关闭内存交换
sudo swapoff -a
# 永久禁用
sed -i.bak '/swap/s/^/#/' /etc/fstab

# 内核参数修改(这一步我也不知道为啥)
[root@master ~]# cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
[root@master ~]# sysctl -p /etc/sysctl.d/k8s.conf

# kubectl 自动补全
[root@master ~]# echo "source <(kubectl completion bash)" >> ~/.bash_profile
[root@master ~]# source .bash_profile 

6、启动小提示

# --image-mirror-country='cn' 使用阿里镜像
# --kubernetes-version=v1.23.3 直接start 目前(2022年9月15日)是1.24 ,总是报错
# --registry-mirror 拉取docker时用的源,官方文档上用的是k8s.gcr.io,这个一般访问不通
minikube start --image-mirror-country='cn' --driver=docker --kubernetes-version=v1.23.3 --registry-mirror=https://hj3oa7u5.mirror.aliyuncs.com

# 启动 dashboard 时,如果虚拟机是minimal,没有浏览器,就用proxy代理出来,记得关闭firewalld
minikube dashboard
http://192.168.56.105:8001/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/

kubectl proxy --port=8001 --address='192.168.56.105' --accept-hosts='^.*' &

# 如果还是kicbase下载不了,可以把base-image 设置成anjone/kicbase,这个是dockerhub里面的镜像,可以访问
minikube start --vm-driver=docker  --base-image="anjone/kicbase"   --registry-mirror=https://f1z25q5p.mirror.aliyuncs.com

# 往集群里 添加节点
minikube node add
minikube node delete
minikube node list
# 创建时指定 profile(其实就是cluster name),下回就不用带那么多参数了
minikube start --profile hahah
// 或者-p
minikube start -p hahha
# list profile
minikube profile list

# 如果你也是虚拟机的话,u may need this
minikube start --listen-address=0.0.0.0

//至今 没有找到像kind一样,启动时指定config的方法

2022年9月18日 修改:
minikube启动失败原因找到了,排除那些交换分区之类的错误,如果还是启动失败,就加上
container-runtime=containerd,指定containerd。
原因(引用官网中文):
v1.24 之前的 Kubernetes 版本直接集成了 Docker Engine 的一个组件,名为 dockershim。 这种特殊的直接整合不再是 Kubernetes 的一部分 (这次删除被作为 v1.20 发行版本的一部分宣布)。

你可以阅读检查 Dockershim 移除是否会影响你以了解此删除可能会如何影响你。 要了解如何使用 dockershim 进行迁移, 请参阅从 dockershim 迁移。

如果你正在运行 v1.25 以外的 Kubernetes 版本,查看对应版本的文档。

所以所以,如果坚持用docker那么需要安装一个叫做cri-dockerd的家伙,只能说被大佬之间的博弈波及了····
解决:要么降低k8s的版本,要么指定container-runtime为containerd

# 降低版本
minikube start \
--image-mirror-country='cn' \
--driver=docker \
--kubernetes-version=v1.23.3 \
--registry-mirror=https://hj3oa7u5.mirror.aliyuncs.com
# 使用containerd 			   
minikube start \
--image-mirror-country='cn' \
--driver=docker \
--container-runtime=containerd \
--registry-mirror=https://hj3oa7u5.mirror.aliyuncs.com
# 按理说 minikube 默认的runtime就是containerd ,但是没有这个参数就是启动不了
[kube@node01 ~]$ minikube start --help | grep runtime
    --container-runtime='':
        The container runtime to be used. Valid options: docker, cri-o, containerd (default: auto)
        If set, force the container runtime to use systemd as cgroup manager. Defaults to false.

这里记录下Learn Kubernetes Basics 里面的所有命令,以备不时之需。

官方教程:
Interactive Tutorial:

-----------------Module 1 - Create a Kubernetes cluster------------
Step 1 Cluster up and running:
minikube version
minikube start

Step 2 Cluster version:
kubectl version

Step 3 Cluster details:
kubectl cluster-info
kubectl get nodes

-----------------Module 2 - Deploy an app------------
Step 1 kubectl basics:
kubectl version
kubectl get nodes

Step 2 Deploy our app:
kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1
kubectl create deployment kubernetes-bootcamp --image=jocatalin/kubernetes-bootcamp:v1
kubectl get deployments

Step 3 View our app:
echo -e "\n\n\n\e[92mStarting Proxy. After starting it will not output a response. Please click the first Terminal Tab\n"; 
kubectl proxy
curl http://localhost:8001/version
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
echo Name of the Pod: $POD_NAME
curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME/

-----------------Module 3 - Explore your app------------
Step 1 Check application configuration
kubectl get pods
kubectl describe pods

Step 2 Show the app in the terminal
echo -e "\n\n\n\e[92mStarting Proxy. After starting it will not output a response. Please click the first Terminal Tab\n"; kubectl proxy
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
echo Name of the Pod: $POD_NAME
curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME/proxy/

Step 3 View the container logs
kubectl logs $POD_NAME

Step 4 Executing command on the container
kubectl exec $POD_NAME -- env
kubectl exec -ti $POD_NAME -- bash
curl localhost:8080
exit

-----------------Module 4 - Expose your app publicly------------
Step 1 Create a new service

kubectl get services
kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080
kubectl describe services/kubernetes-bootcamp
export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
echo NODE_PORT=$NODE_PORT
curl $(minikube ip):$NODE_PORT

Step 2: Using labels
kubectl describe deployment
kubectl get pods -l app=kubernetes-bootcamp
kubectl get services -l app=kubernetes-bootcamp
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
echo Name of the Pod: $POD_NAME
kubectl label pods $POD_NAME version=v1
kubectl describe pods $POD_NAME
kubectl get pods -l version=v1

Step 3 Deleting a service
kubectl delete service -l app=kubernetes-bootcamp
kubectl get services
curl $(minikube ip):$NODE_PORT
kubectl exec -ti $POD_NAME -- curl localhost:8080

----------------Module 5 - Scale up your app---------------------
Step 1: Scaling a deployment
kubectl get deployments
kubectl get rs
kubectl scale deployments/kubernetes-bootcamp --replicas=4
kubectl get deployments
kubectl get pods -o wide
kubectl describe deployments/kubernetes-bootcamp

Step 2: Load Balancing
kubectl describe services/kubernetes-bootcamp
export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
echo NODE_PORT=$NODE_PORT
curl $(minikube ip):$NODE_PORT

Step 3: Scale Down
kubectl scale deployments/kubernetes-bootcamp --replicas=2
kubectl get deployments
kubectl get pods -o wide

----------------Module 6 - Update your app---------------------

Step 1: Update the version of the app  	
kubectl get deployments 	
kubectl get pods
kubectl describe pods
kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
kubectl get pods

Step 2: Verify an update
kubectl describe services/kubernetes-bootcamp
export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
echo NODE_PORT=$NODE_PORT
curl $(minikube ip):$NODE_PORT
kubectl rollout status deployments/kubernetes-bootcamp
kubectl describe pods

Step 3: Rollback an update
kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=gcr.io/google-samples/kubernetes-bootcamp:v10
kubectl get deployments
kubectl get pods
kubectl describe pods
kubectl rollout undo deployments/kubernetes-bootcamp
kubectl get pods
kubectl describe pods

最后,在学习kubernetes的过程中,伟大的G+F+W给我带来了强大的助(zu)力,本人表示万分感谢,此致,敬礼!!!
hi !ho !

参考文章:
https://kubernetes.io/docs/tutorials/kubernetes-basics/
https://gitee.com/moxi159753/LearningNotes/tree/master/K8S/1_Kubernetes%E7%AE%80%E4%BB%8B
https://blog.51cto.com/loong576/2405624
https://blog.csdn.net/a765717/article/details/120193786

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值