一、复习

1、碰到镜像拉取失败的话,假设本地docker images有这个镜像,就可以通过这个配置跳过远端拉镜像的步骤。

[root@master fuxi]# cat pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: pod0729fuxi
spec:
  containers:
    - name: pod
      image: nginx
      imagePullPolicy: IfNotPresent

2、扩容或者缩容deployment

[root@master 0718]# kubectl scale deployment 0729-deployment --replicas=3
deployment.apps/0729-deployment scaled
[root@master 0718]# kubectl get deploy
NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
0722-deployment-xs   0/3     3            0           5m20s
0729-deployment      0/3     3            0           101m

3、修改镜像中nginx的版本号

kubectl set image deployment 0729-deployment nginx=nginx:1.22.0

4、碰到的问题nginx的镜像一直pull不成功

repository does not exist or may require 'docker login': denied: requested a

暂时不清楚是网络问题还是docker权限问题

目前解决的思路是:

1、 通过yaml文件设置imagePullPolicy: IfNotPresent,从本地dock images里面pull
2、手动docker pull nginx.1.22.0或者nginx.1.18或者nginx
3、手动升级daemonset或者deployment里面nginx的版本号
kubectl set image deployment 0729-deployment nginx=nginx:1.22.0
kubectt set image daemonset  0729-daemonset nginx=nginx:1.22.0
----------------------------------------------------------------------------
[root@master ~]# kubectl get pods -A
NAMESPACE     NAME                                      READY   STATUS    RESTARTS   AGE
default       0722-deployment-xs-84f945bdd6-5zlkl       1/1     Running   0          3d18h
default       0722-deployment-xs-84f945bdd6-nqtmw       1/1     Running   0          3d18h
default       0722-deployment-xs-84f945bdd6-vhw4f       1/1     Running   0          3d18h
default       0729-daemonset-78wt4                      1/1     Running   0          3d14h
default       0729-daemonset-cbkr9                      1/1     Running   0          3d14h
default       0729-deployment-86d8cf4dd9-4txsz          1/1     Running   0          3d14h
default       0729-deployment-86d8cf4dd9-9vrrv          1/1     Running   0          3d17h
default       0729-deployment-86d8cf4dd9-qpg4p          1/1     Running   0          3d14h
default       pod0729fuxi                               1/1     Running   0          3d19h
kube-system   calico-kube-controllers-6b77fff45-hpwl8   1/1     Running   0          45d
kube-system   calico-node-dp5gj                         1/1     Running   0          45d
kube-system   calico-node-xblsj                         1/1     Running   0          45d
kube-system   coredns-6d8c4cb4d-crrl9                   1/1     Running   0          45d
kube-system   coredns-6d8c4cb4d-l6msv                   1/1     Running   0          45d
kube-system   etcd-master                               1/1     Running   1          45d
kube-system   kube-apiserver-master                     1/1     Running   1          45d
kube-system   kube-controller-manager-master            1/1     Running   1          45d
kube-system   kube-proxy-sk9kq                          1/1     Running   0          45d
kube-system   kube-proxy-x8b6p                          1/1     Running   0          45d
kube-system   kube-scheduler-master                     1/1     Running   1          45d

5、查看资源的历史版本

kubectl rollout history deployment deploy1 
root@master ~]# kubectl rollout history deploy 0729-deployment
deployment.apps/0729-deployment 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

二、statefulset

StatefulSet 是用来管理有状态的应用,例如数据库。
前面我们部署的应用,都是不需要存储数据,不需要记住状态的,可以随意扩充副本,每个副本都是一样的,可替代的。
而像数据库、Redis 这类有状态的,则不能随意扩充副本。
StatefulSet 会固定每个 Pod 的名字。

1、创建statefulset的容器服务

service:
[root@master 0729]# cat statefulset-service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: statefulset-service-0802
spec:
  selector:
    apps: statefulset                //选择器的值key=value要跟statefulset的值一致
  ports:
  - name:
    port: 80
    targetPort: 80
  clusterIP: None
statefulset:
[root@master 0729]# cat statefulset.yaml 
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: statefulset-1230
spec:
  serviceName: "statefulset-service"
  selector:
    matchLabels: 
      apps: statefulset                //跟下面的label以及service的选择器的key-value值一致
  replicas: 2
  template:
    metadata:
      labels:
        apps: statefulset               //跟上面的matchLabel以及service的选择器的key-value值一致
    spec:
      containers:
        - name: statefulset-pod
          image: nginx

2、改动nginx的html,模拟调用

root@statefulset-1230-0:/usr/share/nginx/html# cat index.html 
hello world
root@statefulset-1230-0:/usr/share/nginx/html# pwd
/usr/share/nginx/html
echo "hello world"
-------------------------------------------------------------
root@statefulset-1230-0:/# curl statefulset-service-0802
hello world

一定要熟练去查看service和statefulset的serviceName是否一致。

kubectl get statefulset statefulset-1230 -o yaml
kubectl get pod statefulset-1230-0 -o  yaml
kubectl get service statefulset-service-0802 -o yaml

三、Job

Job用于批量处理短暂的一次性任务,并保证指定数量的Pod成功结束。

快捷创建job语句

kubectl create job jobname --image=busybox --dry-run -o yaml
[root@master 0718]# cat job0804.yaml 
apiVersion: batch/v1
kind: Job
metadata:
  name: first-job
  namespace: default
  labels:
    first-job: job
  annotations: { key: "my annotation",}
spec:
  template:
    metadata:
    spec:
      containers:
      - name: job-image
        image: busybox
      restartPolicy: Never
[root@master 0718]# kubectl get pod first-job-jvl8r
NAME              READY   STATUS      RESTARTS   AGE
first-job-jvl8r   0/1     Completed   0          13m

四、cronjob

cronjob是管理job,也就是每一个周期创建一个job去执行任务,一次只能管理一个job。

快捷创建cronjob语句

kubectl create cronjob cronjobName --image=busybox --schedule="* * * * *" --dry-run -o yaml
[root@master 0729]# cat cronjob.yaml 
apiVersion: batch/v1
kind: CronJob
metadata:
  name: cronjob-1613
  namespace: "1613"
spec:
  jobTemplate:
    metadata:
    spec:
      metadata:
      template:
        spec:
          containers:
          - image: busybox
            name: myjob1613
          restartPolicy: Never
  schedule: '* * * * *'
-----------------------------------------------------------
[root@master 0729]# kubectl get pod -n 1613 -w
NAME                          READY   STATUS      RESTARTS   AGE
cronjob-1613-27661466-8cqqw   0/1     Completed   0          83s
cronjob-1613-27661467-lkcg2   0/1     Completed   0          23s
cronjob-1613-27661468-gv8s2   0/1     Pending     0          0s
cronjob-1613-27661468-gv8s2   0/1     Pending     0          0s
cronjob-1613-27661468-gv8s2   0/1     ContainerCreating   0          0s
cronjob-1613-27661468-gv8s2   0/1     ContainerCreating   0          1s
cronjob-1613-27661468-gv8s2   0/1     Completed           0          4s
cronjob-1613-27661468-gv8s2   0/1     Completed           0          4s
cronjob-1613-27661468-gv8s2   0/1     Completed           0          4s

五、annotation

注解的使用

[root@master 0729]# cat pod-annotion.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: annotation-pod
  annotations: {"111":"2222",}
spec:
  containers:
    - name: pod
      image: nginx
----------------------------------------------------------------
[root@master 0729]# kubectl get pod annotation-pod -o yaml
apiVersion: v1
kind: Pod
metadata:
  annotations:
    "111": "2222"
......

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐