Kubernetes(三)

第十二章 重做k8s使用harbor作为私有仓库

报错总结:
1.如果要删除的镜像正在被容器使用,那么你是删不了的
2.harbor卸载不干净,/data/harbor/目录下的数据也要删除
3.secret配置只写了一个dp,实际上有几个deployment就需要写几个

1.停止harbor正在运行的容器
2.删除harbor的容器
docker ps -a|grep "goharbor"|awk '{print "docker rm "$1}'
3.删除harbor的镜像
dockerimages|grep "goharbor"|awk '{print "docker rmi "$1":"$2}'

4.解压并修改harbor配置文件
hostname: 10.0.0.11
port: 8888
harbor_admin_password: 123456
data_volume: /data/harbor

5.执行安装并访问
./install.sh
http://10.0.0.11:8888

6.创建一个私有仓库k8s

7.配置docker信任仓库并重启--三台服务器都操作!!!
    {
      "registry-mirrors": ["https://ig2l319y.mirror.aliyuncs.com"],
      "exec-opts": ["native.cgroupdriver=systemd"],
      "insecure-registries" : ["http://10.0.0.11:8888"]
    }

systemctl restart docker

注意!!!node1重启后harbor会失效,需要重启harbor
cd /opt/harbor
docker-compose stop
docker-compose start

8.docker登陆harbor
docker login 10.0.0.11:8888

9.将docker登陆凭证转化为k8s能识别的base64编码
[root@node1 ~]# cat /root/.docker/config.json|base64
ewoJImF1dGhzIjogewoJCSIxMC4wLjAuMTE6ODg4OCI6IHsKCQkJImF1dGgiOiAiWVdSdGFXNDZN
VEl6TkRVMiIKCQl9Cgl9LAoJIkh0dHBIZWFkZXJzIjogewoJCSJVc2VyLUFnZW50IjogIkRvY2tl
ci1DbGllbnQvMTguMDkuOSAobGludXgpIgoJfQp9

10.编写Secert资源配置清单
[root@node1 ~/demo]# cat harbor-secret.yaml 
apiVersion: v1
kind: Secret
metadata:
  name: harbor-secret
data:
  .dockerconfigjson: ewoJImF1dGhzIjogewoJCSIxMC4wLjAuMTE6ODg4OCI6IHsKCQkJImF1dGgiOiAiWVdSdGFXNDZNVEl6TkRVMiIKCQl9Cgl9LAoJIkh0dHBIZWFkZXJzIjogewoJCSJVc2VyLUFnZW50IjogIkRvY2tlci1DbGllbnQvMTguMDkuOSAobGludXgpIgoJfQp9
type: kubernetes.io/dockerconfigjson

11.应用Secret资源
kubectl delete -f harbor-secret.yaml 
kubectl create -f harbor-secret.yaml
kubectl get secrets

12.修改镜像tag并上传到harbor
docker tag kubeguide/tomcat-app:v1 10.0.0.11:8888/k8s/tomcat-app:v1
docker tag mysql:5.7 10.0.0.11:8888/k8s/mysql:5.7

docker push 10.0.0.11:8888/k8s/tomcat-app:v1
docker push 10.0.0.11:8888/k8s/mysql:5.7 


13.修改demo资源配置清单
####mysql
imagePullSecrets:
      - name: harbor-secret

###tomcat
imagePullSecrets:
      - name: harbor-secret


14.应用资源清单并查看
kubectl apply -f .
kubectl get pod 

第十三章 POD控制器介绍

0.控制器作用
  pod类型的资源,删除pod后,不会重建
  替用户监视并保证相应的节点上始终有用户所期望的副本数量的pod在运行
  如果所运行的pod副本数超过了用户期望的,那么控制器就会删掉,直到和用户期望的一致 
  如果所运行的pod副本数低于用户期望的,那么控制器就会创建,直到和用户期望的一致 

1.常用控制器类型
  ReplicationController RC: 
  ReplicationSet RS:
    按用户期望的副本创建pod,并始终保持相应数量副本
  Deployment: 
    Deployment通过控制RS来保证POD始终保持相应的数量副本
    支持滚动更新,回滚,回滚默认保留10个版本
    提供声明式配置,支持动态修改
    管理无状态应用最理想的控制器
    node节点可能会运行0个或多个POD
  DeamonSet:
    一个节点只运行一个,必须是始终运行的状态
  StatefulSet:
    有状态应用
  Job:
    只运行一次的任务,不需要一直运行的任务
    确认任务完成才会退出
  Cronjob:
    周期性的任务

第十四章 ReplicaSet控制器

YB5IMDQW8D))UY@`W5)H)OW.jpg
1.编写RS控制器资源配置清单
cat >nginx-rs.yaml <<EOF
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx-rs
  namespace: default
spec:
  replicas: 2 
  selector: 
    matchLabels:
      app: nginx
  template:
    metadata:
      name: nginx-pod
      labels: 
        app: nginx
    spec:
      containers:
      - name: nginx-containers
        image: nginx:1.14.0
        imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 80
EOF

2.应用RS资源配置清单
kubectl create -f nginx-rs.yaml

3.查看RS资源
kubectl get rs 
kubectl get pod -o wide 

4.动态修改配置 扩容 收缩 升级
kubectl edit rs nginx
kubectl scale rs nginx --replicas=5

5.修改yaml文件应用修改
vim nginx-rs.yaml
kubectl apply -f nginx-rs.yaml

第十五章 Deployment控制器

1.Deployment资源配置清单
cat >nginx-dp.yaml<<EOF
apiVersion: apps/v1
kind: Deployment 
metadata:
  name: nginx-deployment 
  namespace: default
spec:
  replicas: 2 
  selector: 
    matchLabels:
      app: nginx
  template:
    metadata:
      name: nginx-pod
      labels: 
        app: nginx
    spec:
      containers:
      - name: nginx-containers
        image: nginx:1.14.0
        imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 80
EOF

2.应用资源配置清单
kubectl create -f nginx-dp.yaml

3.查看
kubectl get pod -o wide
kubectl get deployments.apps
kubectl describe deployments.apps nginx-deployment

4.更新版本
方法1: 命令行根据资源配置清单修改镜像
kubectl set image -f nginx-dp.yaml nginx-containers=nginx:1.16.0

查看有没有更新
kubectl get pod 
kubectl describe deployments.apps nginx-deployment
kubectl describe pod nginx-deployment-7c596b4d95-6ztld

方法2: 命令行根据资源类型修改镜像
打开2个窗口:
第一个窗口监控pod状态
kubectl get pod -w

第二个窗口更新操作
kubectl set image deployment nginx-deployment nginx-containers=nginx:1.14.0

查看更新后的deployment信息
kubectl describe deployments.apps nginx-deployment 
----------------------------------------------------
  Normal  ScalingReplicaSet  14m                  deployment-controller  Scaled up replica set nginx-deployment-7c596b4d95 to 1
  Normal  ScalingReplicaSet  14m                  deployment-controller  Scaled down replica set nginx-deployment-9c74bb6c7 to 1
  Normal  ScalingReplicaSet  14m                  deployment-controller  Scaled up replica set nginx-deployment-7c596b4d95 to 2
  Normal  ScalingReplicaSet  13m                  deployment-controller  Scaled down replica set nginx-deployment-9c74bb6c7 to 0
  Normal  ScalingReplicaSet  8m30s                deployment-controller  Scaled up replica set nginx-deployment-9c74bb6c7 to 1
  Normal  ScalingReplicaSet  8m29s (x2 over 32m)  deployment-controller  Scaled up replica set nginx-deployment-9c74bb6c7 to 2
  Normal  ScalingReplicaSet  8m29s                deployment-controller  Scaled down replica set nginx-deployment-7c596b4d95 to 1
  Normal  ScalingReplicaSet  8m28s                deployment-controller  Scaled down replica set nginx-deployment-7c596b4d95 to 0
----------------------------------------------------
更新过程:
nginx-deployment-7c596b4d95-8z7kf   #老的版本
nginx-deployment-7c596b4d95-6ztld   #老的版本

nginx-deployment-9c74bb6c7-pgfxz    0/1     Pending   
nginx-deployment-9c74bb6c7-pgfxz    0/1     Pending
nginx-deployment-9c74bb6c7-pgfxz    0/1     ContainerCreating  #拉取新版本镜像
nginx-deployment-9c74bb6c7-pgfxz    1/1     Running            #运行新POD
nginx-deployment-7c596b4d95-8z7kf   1/1     Terminating        #停止一个旧的POD
nginx-deployment-9c74bb6c7-h7mk2    0/1     Pending            
nginx-deployment-9c74bb6c7-h7mk2    0/1     Pending           
nginx-deployment-9c74bb6c7-h7mk2    0/1     ContainerCreating  #拉取新版本镜像
nginx-deployment-9c74bb6c7-h7mk2    1/1     Running            #运行新POD
nginx-deployment-7c596b4d95-6ztld   1/1     Terminating        #停止一个旧的POD
nginx-deployment-7c596b4d95-8z7kf   0/1     Terminating        #等待旧的POD结束
nginx-deployment-7c596b4d95-6ztld   0/1     Terminating        #等待旧的POD结束

查看滚动更新状态:
kubectl rollout status deployment nginx-deployment

5.回滚上一个版本
kubectl describe deployments.apps nginx-deployment 
kubectl rollout undo deployment nginx-deployment
kubectl describe deployments.apps nginx-deployment 

6.回滚到指定版本
v1 1.14.0
v2 1.15.0
v3 3.333.3
回滚到v1版本

创建第一版 1.14.0 
kubectl create -f nginx-dp.yaml  --record

更新第二版 1.15.0 
kubectl set image deployment nginx-deployment nginx-containers=nginx:1.15.0

更新第三版 1.99.0
kubectl set image deployment nginx-deployment nginx-containers=nginx:1.16.0

查看所有历史版本
kubectl rollout history deployment nginx-deployment

查看指定历史版本信息
kubectl rollout history deployment nginx-deployment --revision=1

回滚到指定版本
kubectl rollout undo deployment nginx-deployment --to-revision=1

7.扩缩容
kubectl scale deployment nginx-deployment --replicas=5
kubectl scale deployment nginx-deployment --replicas=2
8OPC_Y{AW7(6{(R7}C5X_}W.jpg
image.png
image.png
WMUEDEXK%)%VY@0}LBC72P3.jpg
VA%YYO7CJ}DM25GNX5}DXPH.jpg

推荐阅读更多精彩内容