k8s_kind-创建pod-拉取私有仓库镜像

发布时间 2023-12-12 20:00:48作者: 辰令

kind本地配置

 查看您创建的所有集群,您可以使用该kind get clusters 命令
 在kubernetes内使用私有镜像仓库之前,我们需要先有一个私有镜像仓库,并保证这个仓库是可用的
    检查私有镜像仓库是否可用
   kind create cluster --name datapre
 ## 以将本机镜像导入到 Kind 集群中去
  [root@docker ~]# kind load docker-image --nodes kind-control-plane 10.*.*.*:*/roloid:latest

   kindest/node 镜像的版本,一般与 K8S 发布的版本一一对应
      https://hub.docker.com/r/kindest/node/tags
kind的配置文件
方式
 --config 参数传递配置文件给 kind,配置文件可修改的内容主要有 role 和 节点使用的镜像
 
 创建一个 test1.yaml 的文件,内容如下
     kind: Cluster
     # 一共两个节点,一个主节点,一个从节点
     apiVersion: kind.x-k8s.io/v1alpha4
     nodes:
     - role: control-plane  # 主节点
     - role: worker  # 从节点
 kind create cluster --config=test2.yaml --name=my-cluuster2 		
 创建高可用的 k8s 集群,创建一个 test2.yaml 文件,代码如下
 一共六个节点,
 三个 control-plane 节点,三个 workers 节点	
###
 kind create cluster  --name=datepre
 kind  get  clusters	
  kubectl cluster-info --context kind-kind
  kubectl get nodes	
	
 0.docker pull   到本地
 1.kind load docker-image 10.*.*.*:*/roloid:latest  --name datepre
   kind load docker-image --nodes datepre-control-plane 10.*.*.*:*/roloid:latest --name datepre
   kind load docker-image --nodes kind-control-plane 10.*.*.*:*/roloid:latest --name kind
 2.	
	
当有多个集群的时候,我们可以使用 kubectl 来切换要管理的集群
设置新建的k8s集群从私有http仓库加载镜像	

K8S

  命令行操作
  使用YAML创建配置文件 YAML是一种人类可读的、专门用于配置信息的文本格
     YAML是JSON的超集,这意味着任何有效的JSON文件也是有效的YAML文件 maps of lists和lists of maps

kubectl apply -f (echo 'apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: private-reg-container
    image: 10.*.*.*:*/roloid:latest
  imagePullSecrets: 
  - name: regcred' )
	
也可以创建成文件-yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: 10.*.*.*:*/roloid:latest
	imagePullPolicy: IfNotPresent
    ports:
      - containerPort: 80

容器编排YAML

###方式一:
	kubectl apply -f roloid.yaml 命令应用该 YAML 文件,Kubernetes 会自动创建相关的 Pod 对象,并将镜像加载到集群
  使用xxx.yaml文件,使用kubectl create -f xxx.yaml 来创建一个新的Pod
   ## 文件格式检查
   kubectl apply -f mysql-rc.yaml
### 方式二
  现在大多数的kubernetes集群,是使用helm来进行镜像包管理的
  
 创建应用时,当Node节点从私有仓库Pull镜像时,需要有相应凭证,才能使用私有Docker仓库  
   从私有仓库拉取镜像 | Kubernetes
        ImagePullSecrets 是一个在 Kubernetes 中用于授权拉取私有镜像的字段。
   	     它是一个列表,包含一个或多个令牌,用于认证访问私有镜像仓库	  
           insecureLocalRegistryPort
           registry.insecureRegistries
   		
   	k8s的docker registry secret中,保存的是docker的用户名、密码的信息,并且是使用加密的方式进行保存的
       imagePullSecrets:
           - name: pers-registry-secret-name

secret

    kubectl create secret docker-registry pers-registry-secret-name --docker-server=10.*.*.*:* --docker-username=admin --docker-password=123456 
      secrets为刚刚创建的秘钥,image为刚刚推送到私人仓库的image
	kubectl delete secret pers-registry-secret-name
	kubectl create -f  secret.yaml
	
	kubectl get secret

pod

 kubectl get pods 
 kubectl describe pods/my-pod 
 kubectl describe pod  my-pod 
 Pod是Kubernetes管理的最小运行单元 一个Pod规定了其能够使用的硬件资源,达到隔离的作用	
     Container运行在Pod中,多个紧密相关联的Container,一般会让他们运行在同一个Pod
 kubectl delete  pods/my-pod  
 kubectl describe pod  my-pod 
##进入pod中的busybox容器,查看文件内容
# 补充一个命令: kubectl exec  pod名称 -n 命名空间 -it -c 容器名称 /bin/sh  在容器内部执行命令 
	 kubectl exec my-pod -it  -c my-container /bin/bash  或者 kubectl exec my-pod -it  -c my-container /bin/sh

## 强大的 kubectl debug 命令
####pod 状态
 Pending  Running   Succeeded  Failed  Unknown	
yaml文件
    command: ["/bin/sh","-c","while true;do /bin/echo $(date +%T);sleep 60; done;"]
    env: # 设置环境变量列表
    - name: "username"
      value: "admin"
    - name: "password"
limits:用于限制运行时容器的最大占用资源,当容器占用资源超过limits时会被终止,并进行重启
requests :用于设置容器需要的最小资源,如果环境资源不够,容器将无法启动
可以通过上面两个选项设置资源的上下限	 	 
容器探测  liveness probes:存活性探针  readiness probes:就绪性探针
     livenessProbe 决定是否重启容器,readinessProbe 决定是否将请求转发给容器。
钩子函数: 钩子函数能够感知自身生命周期中的事件,并在相应的时刻到来时运行用户指定的程序代码 		 

kubectl 帮助系统

  在这里,可通过一个命令来查看每种资源的可配置项
      kubectl explain 资源类型         查看某种资源可以配置的一级属性
      kubectl explain 资源类型.属性     查看属性的子属性 
	      kubectl explain pod
		  kubectl explain pod.metadata
          kubectl explain pod.spec.containers	  
    busybox并不是一个程序,而是类似于一个工具类的集合
	   拉取 BusyBox 映像,运行如下命令 $ sudo docker pull busybox
	Busybox容器在K8S中主要用于临时调试和测试目的,并不适合用作生产环境中的主要容器。
	   在生产环境中,应使用包含所需软件的正式镜像来创建K8S Pod