kubectl管理Pod-1: 应用生命周期管理

kubernetes基本架构图:围绕kube-api解耦合的cs架构,而master节点通过kubectl下发应用管理指令
kubectl管理Pod-1: 应用生命周期管理_第1张图片

快速获取kubectl命令帮助

  • 预创建资源:kubectl create deploy nginx --image=nginx:1.15 -o yaml --dry-run
  • 获取已有资源:kubectl get deploy nginx1 -o yaml
  • 获取资源配置字段名:kubectl explain deploy.spec.template.spec
[root@master ~]# kubectl create deploy nginx --image=nginx:1.15 -o yaml --dry-run
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.15
        name: nginx
        
[root@master ~]# kubectl get deploy
NAME     READY   UP-TO-DATE   AVAILABLE   AGE
nginx1   2/2     2            2           5d22h
[root@master ~]# kubectl get deploy nginx1 -o yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app: nginx1
  name: nginx1
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx1
  template:
    metadata:
      labels:
        app: nginx1
    spec:
      containers:
      - image: nginx:1.15
        imagePullPolicy: IfNotPresent
        name: nginx1
        resources: {}
      restartPolicy: Always
      
[root@master ~]# kubectl explain deploy.spec.template.spec.containers.imagePullPolicy
KIND:     Deployment
VERSION:  extensions/v1beta1
FIELD:    imagePullPolicy 
DESCRIPTION:
     Image pull policy. One of Always, Never, IfNotPresent. Defaults to Always
     if :latest tag is specified, or IfNotPresent otherwise. Cannot be updated.
     More info:
     https://kubernetes.io/docs/concepts/containers/images#updating-images

1, 创建应用(pod-controller, service)

kubectl管理Pod-1: 应用生命周期管理_第2张图片

a, 使用命令行

此处nginx被分配到: 一个副本,在node2上创建

#1, 命令直接创建:默认pod是一个副本
[root@master yamls]# kubectl create deployment nginx --image=nginx:1.15
deployment.apps/nginx created

#2, 查看创建的对象:pod,deploy,replicaset;  service
[root@master yamls]# kubectl get  all -o wide
NAME                         READY   STATUS    RESTARTS   AGE   IP            NODE    NOMINATED NODE   READINESS GATES
pod/nginx-5ddcc6cb74-qvtb7   1/1     Running   0          10s   10.244.1.27   node2              
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE   SELECTOR

deployment.apps/nginx   1/1     1            1           10s   nginx        nginx:1.15   app=nginx
NAME                               DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES       SELECTOR
replicaset.apps/nginx-5ddcc6cb74   1         1         1       10s   nginx        nginx:1.15   app=nginx,pod-template-hash=5ddcc6cb74

service/kubernetes   ClusterIP   10.1.0.1             443/TCP   16h   
NAME                    READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES       SELECTOR

b, 使用yaml文件

########## 单独创建pod: 没有控制器管理,删除pod就不再创建了######
## 创建命令: kubectl create -f pod.yaml    ##kubectl apply -f pod.yaml :重新配置应用更改
[root@master yamls]# cat pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: nginx1
  namespace: default
spec:
  containers:
  - image: nginx
    imagePullPolicy: IfNotPresent
    name: nginx1

########## 通过控制器管理pod: 删除pod会重新创建,直到满足副本数######
## 创建命令: kubectl create -f deploy.yaml    ##kubectl apply -f deploy.yaml :重新配置应用更改
[root@master yamls]# cat deploy.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx1
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx1
  template:
    metadata:
      labels:
        app: nginx1
    spec:
      containers:
      - image: nginx
        imagePullPolicy: IfNotPresent
        name: nginx1

2,发布应用:暴露服务访问接口

a, 使用命令行

#1, ClusterIp类型的服务:只供集群内部访问 ( 默认ClusterIp)
[root@master yamls]# kubectl expose deployment.apps/nginx --port=80 --name=nginx-service-clusterip
service/nginx-service-clusterip exposed
[root@master yamls]# kubectl expose deployment.apps/nginx --port=80 --type=NodePort --name=nginx-service-nodeport

[root@master yamls]# 
[root@master yamls]# kubectl get all -o wide
NAME                         READY   STATUS    RESTARTS   AGE   IP            NODE    NOMINATED NODE   READINESS GATES
pod/nginx-5ddcc6cb74-qvtb7   1/1     Running   0          15m   10.244.1.27   node2              

NAME                              TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE   SELECTOR
service/nginx-service-clusterip   ClusterIP   10.1.219.85           80/TCP    26s   app=nginx
service/nginx-service-nodeport    NodePort    10.1.122.82           80:30408/TCP   40s    app=nginx

#2, 通过该接口访问服务: clusterip:80 -->pod在node2上,相应的服务也会在node2上被监听
[root@master yamls]# ssh node2 curl 10.1.219.85
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   612  100   612    0     0   398k      0 -- --:--:--     0


Welcome to nginx!

#3, 访问该接口访问服务:  节点ip:暴露的服务端口
[root@master yamls]# curl node2:30408



Welcome to nginx!

b, 使用yaml文件

## 创建命令: kubectl create -f service.yaml    ##kubectl apply -f service.yaml :重新配置应用更改
[root@master yamls]# cat service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: nginx1
  namespace: default
spec:
  ports:
  - nodePort: 31000
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx1
  type: NodePort
  #type: ClusterIp

3, 应用滚动更新/回滚:版本迭代

#1,查看要升级的应用
[root@master yamls]# kubectl get deploy
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   1/1     1            1           28m
[root@master yamls]# kubectl set image deploy nginx nginx=1.17
deployment.extensions/nginx image updated
[root@master yamls]# kubectl get pod
NAME                     READY   STATUS              RESTARTS   AGE
nginx-57f7c6746-l6rcb    0/1     ContainerCreating   0          7s
nginx-5ddcc6cb74-qvtb7   1/1     Running             0          29m

4, 应用下线:删除

#模拟docker删除所有容器, 可以用如下命令删除所有应用,留下空白的集群环境
 kubectl delete  $(kubectl get all |awk '{print $1}' |egrep -vi "name|kubernetes")
 kubectl get deploy,pod |grep -vi name|awk '{print $1}'|xargs kubectl delete 

#删除deploy --> replicat-set, pod, 删除service
 kubectl delte deployment.apps/nginx1  #kubectl delete deploy nginx1
 kubectl delte service/service-nginx1  #kubectl delete service service-nginx1

你可能感兴趣的:(云计算-k8s)