部署一个容器
创建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