init pod -RC- RS --deployment

22  初始化容器 Init Container

kubectl get pods --all-namespaces -o wide
ssh node01
docker ps |grep liveness-exec
docker ps
k8s.gcr.io/pause-amd64.3.1   就是infra容器

docker ps |grep hrjr-web
docker ps
k8s.gcr.io/pause-amd64.3.1   就是infra容器

infra容器就是网络共享命名空间的容器

initpod1.yaml

---
apiVersion: v1
kind: Pod
metadata:
  name: init-pod
  labels:
    app: init
spec:
  initContainers:
    - name: init-myservice
      image: busybox
      command: ['sh', '-c', 'unit nslookup myservice; do echo waiting for myservice; sleep 2; done;']
    - name: init-mydb
      image: busybox
      command: ['sh','-c','until nslookup mydb; do echo waiting for mydb; sleep 2; done;']
  containers:
    - name: main-container
      image: busybox
      command: ['sh','-c', 'echo The app is running! && sleep 3600']
 

kubectl apply -f init-pod1.yaml



initservice.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: myservice
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 6379


kubectl create -f initservice.yaml
kubectl get pods
kubectl get svc


vi initservice.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: myservice
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 6379

---
apiVersion: v1
kind: Service
metadata:
  name: mydb
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 6378


kubectl apply -f initservice.yaml



initconfig.yaml

---
apiVersion: v1
kind: Pod
metadata:
  name: init-demo
  labels:
    app: init
spec:
  initContainers:
  - name: install
    image: busybox
    command:
    - wget
    - "-O"
    - "/work-dir/index.html"
    - http://www.baidu.com
    volumeMounts:
    - name: workdir
      mountPath: /work-dir
  containers:
    - name: nginx
      image: nginx
      ports:
      - containerPort: 80
      volumeMounts:
      - name: workdir
        mountPath: /usr/share/nginx/html
  volumes:
    - name: workdir
      emptyDir: {}

kubectl create -f initconfig.yaml

kubectl exec -it init-demo --/bin/bash



23 Replication Controller 与 Replica Set

RC && RS

rc-demo.yaml

---
apiVersion: v1
kind: ReplicationController
metadata:
  name: rc-demo
  labels:
    app: rc
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: rc
    spec:
      containers:
      - name: nginx-demo
        image: nginx
        ports:
        - containerPort: 80


kubectl create -f rc-demo.yaml
kubectl get replicationcontroller
简写:
kubectl get rc
kubectl describe rc rc-demo
kubectl get pods

修改副本数量为2个以后,更新rc-demo
kubectl apply -f rc-demo.yaml
kubectl edit rc rc-demo
dashboard 控制面板 scale 修改pod数量

在线升级nginx版本,滚动更新功能
kubectl rolling-update rc-demo --image=nginx:1.7.9

24 Deployment 的使用

deploy-demo.yaml

---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: nginx-deploy
  labels:
    app: nginx-demo
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80


kubectl get rs

---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: nginx-deploy
  labels:
    app: nginx-demo
spec:
  replicas: 3
  minReadySeconds: 5      #5秒钟后更新
  strategy:              
    type: RollingUpdate   #滚动更新
    rollingUpdate:
      maxSurge: 1         #更新中 比原pod数最多多1个出来
      maxUnavailable: 1   #更新中 最多允许1个pod不能提供服务
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80


查看更新状态:
kubectl apply -f deploy-demo.yaml
kubectl rollout status deployment nginx-deploy   升级状态
kubectl rollout pause deployment nginx-deploy    暂停升级
kubectl rollout resume deployment nginx-deploy   恢复升级
kubectl get rs
kubectl get deployment
kubectl describe deployment nginx-deploy

kubectl rollout pause deployment nginx-deploy

kubectl rollout history deployment nginx-deploy   查看更新历史

kubectl apply -f deploy-demo.yaml --record=true    记录更新时使用的命令



---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: nginx-deploy
  labels:
    app: nginx-demo
spec:
  replicas: 3
  revisionHistoryLimit: 15   #最小数量历史版本为 15个
  minReadySeconds: 5         #5秒钟后更新
  strategy:              
    type: RollingUpdate      #滚动更新
    rollingUpdate:
      maxSurge: 1            #更新中 比原pod数最多多1个出来
      maxUnavailable: 1      #更新中 最多允许1个pod不能提供服务
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80


kubectl get rs   
kubectl describe rs nginx-deploy-xxxyyy   
Annotations:  deployment.kubernetes.io/revision=2   即是历史版本

kubectl delete rs nginx-deploy-xxxyyy  删除pod
kubectl rollout history deployment nginx-deploy

回滚到上一个版本:
kubectl rollout undo deployment nginx-deploy
kubectl rollout status deployment nginx-deploy
kubectl get deployment nginx-deploy
kubectl get rs
kubectl get pods
kubectl describe pod nginx-deploy


回滚到指定版本: 修改 yaml 文件

回滚到 3 号版本:
kubectl rollout undo deployment nginx-deploy --to-revision=3


kubectl rollout history deployment nginx-deploy

posted @ 2019-11-07 11:42  walkersss  阅读(166)  评论(0编辑  收藏  举报