本文共 8230 字,大约阅读时间需要 27 分钟。
kubectl get deploy 查看多少个控制器 = kubectl get deployments
创建名称空间资源 namespace数据集群资源kubectl create -h[root@k8s-master01 .ssh]# kubectl create namespace develop #创建develop名称资源空间namespace/develop created[root@k8s-master01 .ssh]# kubectl create namespace testingnamespace/testing created[root@k8s-master01 .ssh]# kubectl create namespace prod[root@k8s-master01 .ssh]# kubectl get ns #查看资源类型
[root@k8s-master01 .ssh]# kubectl delete ns prod= [root@k8s-master01 .ssh]# kubectl delete ns/testing
[root@k8s-master01 .ssh]# kubectl get ns[root@k8s-master01 .ssh]# kubectl get ns/testing[root@k8s-master01 .ssh]# kubectl get ns/develop -o yaml 输出yaml信息[root@k8s-master01 .ssh]# kubectl get ns/develop -o wide 长格式展示[root@k8s-master01 .ssh]# kubectl get ns/develop -o json 输出json格式[root@k8s-master01 .ssh]# kubectl get ns/default -o yaml
[root@k8s-master01 .ssh]# kubectl describe ns/default #当前状态信息
[root@k8s-master01 .ssh]# kubectl create deployment nginx-dep --image=ngnx:1.14-alpine= [root@k8s-master01 .ssh]# kubectl create deploy nginx-dep --image=ngnx:1.14-alpine
[root@k8s-master01 .ssh]# kubectl get all
[root@k8s-master01 .ssh]# kubectl get pods
kubectl delete pods/nginx-7db9fccd9b-b7cxl 删除会自动创建一个新的pods强制删除: 需要删除对应的deploy [root@k8s-master01 .ssh]# kubectl get deployments && [root@k8s-master01 .ssh]# kubectl delete deployments
kubectl exec -it pods
请求方式 clients->serverice->pods
防火墙: DNAT -d 192.168.1.2 --dport 80 -json DNAT --to-destination 192.168.1.2:80创建:一个service[root@k8s-master01 ~]# kubectl create service clusterip -h查看帮助[root@k8s-master01 ~]# kubectl create deployment nginx-dep --image=nginx
kubectl get pods -o wide #详细信息kubectl get pods -o json #json格式展示信息kubectl get pods -o yaml #yaml信息
service作为负载均衡和代理后端服务器,作为一个请求接口 相当于ADNAT -d --port 代理到后端的pods(由于当你删除一个pod的时候,kube会给你自动创建一个新的pod,但是ip地址可能是改变的,设置一个service会自动发现你这新的pod 并负载到新的pod)[root@k8s-master01 ~]# kubectl create service clusterip nginx-dep --tcp=80:80
[root@k8s-master01 ~]# kubectl scale -h 例如: 我扩充nginx-dep为三个[root@k8s-master01 ~]# 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[root@k8s-master01 ~]# kubectl scale --replicas=3 deployment nginx-dep deployment.extensions/nginx-dep scaled[root@k8s-master01 ~]# 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
[root@k8s-master01 cfg]# kubectl create service nodeport nginx --tcp=88:80
deployment->nginx deploy -> nginx pods ->services -> nginx services -> client
同时 会在iptables创建NAT的规则 [root@k8s-node01 ~]# iptables -t nat -vnL
[root@k8s-master01 cfg]# kubectl api-versions #查看api-version版本 多个版本可以共存 api接口中可以多个组合为一个逻辑组合,每个组合可以单独进化
陈述式命令: run expose delete和get等命令,他们可以指直接用于k8s系统的活动对象,简单易用,但不支持代码复用,修改复审以及审核日志等功能。要依赖资源配置文件中,也被称之为资源清单 陈述式对象配置 生命式对象配置 例子: 创建一个ns的yaml文件 可以根据:kubectl get ns default -o yaml --export 显示yaml查看 [root@k8s-master01 ~]# cat develop_ns.yaml apiVersion: v1 kind: Namespace metadata: creationTimestamp: null name: develop [root@k8s-master01 ~]# kubectl create -f develop_ns.yaml #指定yaml创建ns
[root@k8s-master01 ~]# kubectl apply -f develop_ns.yaml #允许重复执行 可以指定整个目录创建 = [root@k8s-master01 ~]# kubectl create -f develop_ns.yaml #重复执行会报错 不可以指定整个目录创建 创建一个pods的配置文件 [root@k8s-master01 ~]# kubectl get pods nginx-7db9fccd9b-dq9wz -o yaml --export > nginx_pods.yaml 使用上面的当作模板,进行修改为适合自己的yaml文件[root@k8s-master01 ~]# 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: {}
[root@k8s-master01 ~]# kubectl apply -f nginx_pods.yaml pod/pod-demo created [root@k8s-master01 ~]# 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
[root@k8s-master01 ~]# kubectl explain pods|svc|ns.....
[root@k8s-master01 ~]# kubectl explain pods.metadata
[root@k8s-master01 ~]# 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"] [root@k8s-master01 ~]# kubectl apply -f create_pods.yaml [root@k8s-master01 ~]# 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
[root@k8s-master01 ~]# kubectl exec pod-demo -n prod -it -- /bin/bash
[root@k8s-master01 ~]# 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
[root@k8s-master01 ~]# kubectl explain pods.metadata [root@k8s-master01 ~]# kubectl explain pods.spec apiVersion: v1 kind: Pod metadata: name: centos-demo namespace: prod spec: containers: - name: nginx-demo image: nginx - name: centos image: ansible/centos7-ansible imagePullPolicy: IfNotPresent resources: [] #容器性能限制,cpu,内存等等 command: ["/bin/sh","-c","sleep 10"] hostNetwork: true #共享宿主机的网络ip,会在宿主机启动监听端口,可以直接访问 restartPolicy: true #自动重启 priority: 0 # 设置权重 nodeName: # 指定node节点存放容器 dnsPolicy: ClusterFirst # Pod 内预设的 DNS 配置 四个结果: None #None 表示会清除 Pod 预设的 DNS 配置,当 dnsPolicy 设置成这个值之后,Kubernetes 不会为 Pod 预先载入任何自身逻辑判断得到的 DNS 配置 Default #Default 表示 Pod 里面的 DNS 配置继承了宿主机上的 DNS 配置 ClusterFirst # 相对于上述的 Default,ClusterFirst 是完全相反的操作,它会预先把 kube-dns(或 CoreDNS)的信息当作预设参数写入到该 Pod 内的 DNS 配置。 ClusterFirstHostNet #继承宿主机的 enableServiceLinks: true schedulerName: default-scheduler #scheduler名字
[root@k8s-master01 ~]# cat nginx_pods.yaml apiVersion: v1 kind: Pod metadata: creationTimestamp: null name: pod-demo1 namespace: prod spec: containers: - image: nginx imagePullPolicy: IfNotPresent name: nginx-1 hostNetwork: true访问测试
转载于:https://blog.51cto.com/9025736/2397755