部署一个容器
创建 tomcat 容器
kubectl create deployment tomcat6 –image=tomcat:6.0.53-jre8
[root@master k8s]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 9m7s
tomcat6 NodePort 10.96.74.175 <none> 80:32059/TCP 18s
创建成功
deployment.apps/tomcat6 created
开放服务
kubectl expose deployment tomcat6 –port=80 –target-port=8080 –type=NodePort
查看
kubectl get svc
查看更多
kubectl get svc -o wide
查看部署状态
kubectl get all
NAME READY STATUS RESTARTS AGE 部署的 pod
pod/tomcat6-5f7ccf4cb9-s8p2m 0/1 ContainerCreating 0 28s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE 服务
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 15h
NAME READY UP-TO-DATE AVAILABLE AGE 本次部署
deployment.apps/tomcat6 0/1 1 0 28s
NAME DESIRED CURRENT READY AGE 应用复制
replicaset.apps/tomcat6-5f7ccf4cb9 1 1 0 28s
查看 pod 部署在哪个节点
[root@kmaster ~]# kubectl get all -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/tomcat6-5f7ccf4cb9-s8p2m 0/1 ContainerCreating 0 4m52s <none> n1 <none> <none>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 15h <none>
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/tomcat6 0/1 1 0 4m52s tomcat tomcat:6.0.53-jre8 app=tomcat6
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
replicaset.apps/tomcat6-5f7ccf4cb9 1 1 0 4m52s tomcat tomcat:6.0.53-jre8 app=tomcat6,pod-template-hash=5f7ccf4cb9
删除 pod
kubectl get all
kubectl delete deployment.apps/tomcat6
kubectl delete service/kubernetes
创建的 deployment 会管理 replicas、replicas 控制这 pod 的数量,有 pod 故障自动会拉起新的 pod
kubectl delete pod/tomcat6-5f7ccf4cb9-4665b
查看名称空间
kubectl get namespaces
强制删除掉线 node 中的 pod
kubectl delete pod [pod name] –force –grace-period=0 -n [namespace]
这里 pod 没有 namespace 的话那就用默认的 default 名称空间
注意:必须加-n 参数指明 namespace,否则可能报错 pod not found。查看 pod 所在的命名空间
kubectl get pods –all-namespaces
查看 svc 所在的命名空间
kubectl get svc –all-namespaces
查看 pod 启动状态
kubectl get pod
NAME READY STATUS RESTARTS AGE
redis-master-c9gzd 0/1 ContainerCreating 0 17m
kubectl describe pod redis-master-c9gzd
kubectl describe pod nginx-ingress-controller-xrk82 -n ingress-nginx
扩容 pod
kubectl scale –replicas=3 deployment tomcat6
缩容 pod
kubectl scale –replicas=0 deployment tomcat6
输出 yaml 不执行
[root@master /]# kubectl create deployment tomcat7 –image=tomcat:6.0.53-jre8 –dry-run -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: tomcat7
name: tomcat7
spec:
replicas: 1
selector:
matchLabels:
app: tomcat7
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: tomcat7
spec:
containers:
– image: tomcat:6.0.53-jre8
name: tomcat
resources: {}
status: {}
容器 pod 的 yaml 指定输出
[root@master /]# kubectl create deployment tomcat7 –image=tomcat:6.0.53-jre8 –dry-run -o yaml > tomcat6.yaml
删除一些无关紧要的配置
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: tomcat6
name: tomcat6
spec:
replicas: 3
selector:
matchLabels:
app: tomcat6
template:
metadata:
labels:
app: tomcat6
spec:
containers:
– image: tomcat:6.0.53-jre8
name: tomcat
应用 yaml
kubectl apply -f tomcat6.yaml
查看以启动的 pod 的 yaml
kubectl get pod tomcat6-5f7ccf4cb9-4665b -o yaml
kubectl get pod httpd-gv4bl -o yaml -n openebs
一个 pod 运行多个容器
apiVersion: v1
kind: Pod
metadata:
labels:
app: tomcat6-new
name: tomcat6-new
namespace: default
spec:
containers:
– image: tomcat:6.0.53-jre8
imagePullPolicy: IfNotPresent
name: tomcat-new
– image: nginx
imagePullPolicy: IfNotPresent
name: nginx
运行
kubectl apply -f mypod.yaml
查看 pod 日志输出
1、查看指定 pod 的日志
kubectl logs <pod_name>
kubectl logs -f <pod_name> #类似 tail -f 的方式查看(tail -f 实时查看日志文件 tail -f 日志文件 log)
2、查看指定 pod 中指定容器的日志
kubectl logs <pod_name> -c <container_name>
查看所有名称空间的 pod
kubectl get pods –all-namespaces
卸载 K8S
kubeadm reset 重置
systemctl stop kubelet 停止 kubelet