查看资源类型
查看多少个控制器
kubectl get deploy 查看多少个控制器 = kubectl get deployments
创建名称空间资源 namespace数据集群资源
kubectl create -h
[[email protected] .ssh]# kubectl create namespace develop #创建develop名称资源空间
namespace/develop created
[[email protected] .ssh]# kubectl create namespace testing
namespace/testing created
[[email protected] .ssh]# kubectl create namespace prod
[[email protected] .ssh]# kubectl get ns #查看资源类型
删除名称空间
[[email protected] .ssh]# kubectl delete ns prod
= [[email protected] .ssh]# kubectl delete ns/testing
查看空间信息
[[email protected] .ssh]# kubectl get ns
[[email protected] .ssh]# kubectl get ns/testing
[[email protected] .ssh]# kubectl get ns/develop -o yaml 输出yaml信息
[[email protected] .ssh]# kubectl get ns/develop -o wide 长格式展示
[[email protected] .ssh]# kubectl get ns/develop -o json 输出json格式
[[email protected] .ssh]# kubectl get ns/default -o yaml
输出名称空间的描述信息
[[email protected] .ssh]# kubectl describe ns/default #当前状态信息
创建一个容器镜像
[[email protected] .ssh]# kubectl create deployment nginx-dep --image=ngnx:1.14-alpine
= [[email protected] .ssh]# kubectl create deploy nginx-dep --image=ngnx:1.14-alpine
显示所有资源
[[email protected] .ssh]# kubectl get all
显示pods
[[email protected] .ssh]# kubectl get pods
删除pods
kubectl delete pods/nginx-7db9fccd9b-b7cxl 删除会自动创建一个新的pods
强制删除: 需要删除对应的deploy
[[email protected] .ssh]# kubectl get deployments && [[email protected] .ssh]# kubectl delete deployments
进入pods
kubectl exec -it pods
创建一个nginx的pods
请求方式 clients->serverice->pods
防火墙: DNAT -d 192.168.1.2 --dport 80 -json DNAT --to-destination 192.168.1.2:80
创建:一个service
[[email protected] ~]# kubectl create service clusterip -h查看帮助
创建一个nginx pod
[[email protected] ~]# kubectl create deployment nginx-dep --image=nginx
查看pods
kubectl get pods -o wide #详细信息
kubectl get pods -o json #json格式展示信息
kubectl get pods -o yaml #yaml信息
访问测试
为nginx-dep创建一个service
service作为负载均衡和代理后端服务器,作为一个请求接口 相当于ADNAT -d --port 代理到后端的pods(由于当你删除一个pod的时候,kube会给你自动创建一个新的pod,但是ip地址可能是改变的,设置一个service会自动发现你这新的pod 并负载到新的pod)
[[email protected] ~]# kubectl create service clusterip nginx-dep --tcp=80:80
扩缩容 pods
[[email protected] ~]# kubectl scale -h
例如: 我扩充nginx-dep为三个
[[email protected] ~]# kubectl get deploy -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx 3/3 3 3 26h nginx nginx run=nginx
nginx-dep 1/1 1 1 25h nginx nginx app=nginx-dep
[[email protected] ~]# kubectl scale --replicas=3 deployment nginx-dep
deployment.extensions/nginx-dep scaled
[[email protected] ~]# kubectl get deploy -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx 3/3 3 3 26h nginx nginx run=nginx
nginx-dep 3/3 3 3 25h nginx nginx app=nginx-dep
这时候:service 负载应该也变更为 三个节点
创建nodeport的service
[[email protected] cfg]# kubectl create service nodeport nginx --tcp=88:80
这样可以访问任何一个宿主机 实现访问对用的pods
也可以访问service实现负载访问pods(master节点没有添加为node 所以只能到个个node节点访问)
k8s管理pods方式
deployment->nginx deploy -> nginx pods ->services -> nginx services -> client
会自动生成iptables的nat规则
同时 会在iptables创建NAT的规则
[[email protected] ~]# iptables -t nat -vnL
api资源类型
.
上图可以看出 所有的操作都需PAI server交互 所有的请求 操作都需要经过API server,能操作etcd数据的只有API server服务器,其他组件是不能直接操作etcd的, 整个集群的所有定义的状态信息和切换信息都在etcd存储 所以etcd一旦崩溃,所有的数据就会丢失,所以etcd要定期备份和高可用。
apiserver
[[email protected] cfg]# kubectl api-versions #查看api-version版本 多个版本可以共存
api接口中可以多个组合为一个逻辑组合,每个组合可以单独进化
资源对象管理方式
陈述式命令: run expose delete和get等命令,他们可以指直接用于k8s系统的活动对象,简单易用,但不支持代码复用,修改复审以及审核日志等功能。要依赖资源配置文件中,也被称之为资源清单
陈述式对象配置
生命式对象配置
例子: 创建一个ns的yaml文件
可以根据:kubectl get ns default -o yaml --export 显示yaml查看
[[email protected] ~]# cat develop_ns.yaml
apiVersion: v1
kind: Namespace
metadata:
creationTimestamp: null
name: develop
[[email protected] ~]# kubectl create -f develop_ns.yaml #指定yaml创建ns
.
[[email protected] ~]# kubectl apply -f develop_ns.yaml #允许重复执行 可以指定整个目录创建
= [[email protected] ~]# kubectl create -f develop_ns.yaml #重复执行会报错 不可以指定整个目录创建
创建一个pods的配置文件
[[email protected] ~]# kubectl get pods nginx-7db9fccd9b-dq9wz -o yaml --export > nginx_pods.yaml
使用上面的当作模板,进行修改为适合自己的yaml文件
[[email protected] ~]# cat nginx_pods.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
name: pod-demo
namespace: develop
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx-demo
resources: {}
dnsPolicy: ClusterFirst
enableServiceLinks: true
priority: 0
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
查看当前新建立的pods
[[email protected] ~]# kubectl apply -f nginx_pods.yaml
pod/pod-demo created
[[email protected] ~]# kubectl get pods -n develop -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod-demo 1/1 Running 0 2m44s 172.17.89.4 192.168.1.73 <none> <none>
查看各个yaml配置清单
[[email protected] ~]# kubectl explain pods|svc|ns.....
查看各个yaml清单的子模块
[[email protected] ~]# kubectl explain pods.metadata
自动一个pods的yaml
[[email protected]aster01 ~]# cat create_pods.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-demo
namespace: prod
spec:
containers:
- name: centos
image: centos:latest
imagePullPolicy: IfNotPresentNotPresent
command: ["/bin/sh","-c","sleep 10"],"-c","sleep 10"]
- name: create-pods2
image: nginx
command:
- /bin/sh
- -c
- "sleep 100"和下面的command效果一样
command: ["/bin/sh","-c","sleep 10"]
[[email protected] ~]# kubectl apply -f create_pods.yaml
[[email protected] ~]# kubectl get pods -n prod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod-demo 2/2 Running 2 56s 172.17.89.5 192.168.1.73 <none> <none>
进入容器访问
默认访问 启动的
[[email protected] ~]# kubectl exec pod-demo -n prod -it -- /bin/bash
指定访问容器pods
[[email protected] ~]# kubectl exec pod-demo -c bbos -n prod -it -- /bin/bash
-c, --container=‘‘: Container name. If omitted, the first container in the pod will be chosen
-i, --stdin=false: Pass stdin to the container
-t, --tty=false: Stdin is a TTY
原文地址:https://blog.51cto.com/9025736/2397755
时间: 2024-11-02 23:52:45