每天5分钟玩转Kubernetes勘误

TMaize 于 2021-12-27 发布

Chapter 1:先把 Kubernetes 跑起来

1.3 部署应用

kubectl run kubernetes-bootcamp --image=docker.io/jocatalin/kubernetes-bootcamp:v1 --port 8080

修改为:

kubectl create deployment kubernetes-bootcamp --image=docker.io/jocatalin/kubernetes-bootcamp:v1

Chapter 4:Kubernetes 架构

4.4 用例子把它们串起来

kubectl run httpd-app --image=httpd --replicas=2

修改为:

kubectl create deployment httpd-app --image=httpd

kubectl scale deployment/httpd-app --replicas=2

Chapter 5:运行应用

5.1.1 运行 Deployment

kubectl run nginx-deployment --image=nginx:1.7.9 --replicas=2

修改为:

kubectl create deployment nginx-deployment --image=nginx:1.7.9

kubectl scale deployment/nginx-deployment --replicas=2

5.1.3 Deployment 配置文件简介

通过 yaml 文件部署应用(nginx-deployment.yaml):

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: web_server
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9

修改为:

apiVersion: apps/v1                 # Api 接口版本
kind: Deployment                    # 定义控制器
metadata:
  name: nginx-deployment            # deployment 名称
spec:
  replicas: 2                       # 副本数
  selector:                         # 选择标签
    matchLabels:                    # 定义标签
      app: nginx-deployment         # 标签名 
  template:                         # pod 容器
    metadata:
      labels:                       # 定义标签
        app: nginx-deployment       # 标签名
    spec:
      containers:
      - name: nginx                 # 容器名
        image: nginx:1.7.9          # 容器镜像

5.1.5 Failover(没有解决)

在关闭 node2 后节点状态变为 NotReady ,与书上一致

1

但 node2 上跑的 pod 状态并没有变成 Unknown ,也并没有在 node1 上重新创建新的 pod

,与书上不一致

2

并且在使用 kubectl 删除原部署并重新启动部署后,node2 的 pod 无法正常删除

3

直到重启 node2,才成功删除原 pod

4


5.2.3 运行自己的 DaemonSet

通过 yml 文件部署 DaemonSet(node-exporter.yml):

apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  name: node-exporter-daemonset
spec:
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      hostNetwork: true
      containers:
      - name: node-exporter
        image: prom/node-exporter
        imagePullPolicy: IfNotPresent
        command:
        - /bin/node_exporter
        - --path.procfs
        - /host/proc
        - --path.sysfs
        - /host/sys
        - --collector.filesystem.ignored-mount-points
        - ^/(sys|proc|dev|host|etc)($|/)
        volumeMounts:
        - name: proc
          mountPath: /host/proc
        - name: sys
          mountPath: /host/sys
        - name: root
          mountPath: /rootfs
      volumes:
      - name: proc
        hostPath:
          path: /proc
      - name: sys
        hostPath:
          path: /sys
      - name: root
        hostPath:
          path: /

修改 yaml 文件为:

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: node-exporter-daemonset
spec:
  selector:
    matchLabels:
      app: node-exporter-daemonset
  template:
    metadata:
      labels:
        app: node-exporter-daemonset
    spec:
      hostNetwork: true
      containers:
      - name: node-exporter
        image: prom/node-exporter
        imagePullPolicy: IfNotPresent
        command:
        - /bin/node_exporter
        - --path.procfs
        - /host/proc
        - --path.sysfs
        - /host/sys
        - --collector.filesystem.ignored-mount-points
        - ^/(sys|proc|dev|host|etc)($|/)
        volumeMounts:
        - name: proc
          mountPath: /host/proc
        - name: sys
          mountPath: /host/sys
        - name: root
          mountPath: /rootfs
      volumes:
      - name: proc
        hostPath:
          path: /proc
      - name: sys
        hostPath:
          path: /sys
      - name: root
        hostPath:
          path: /

5.3 Job

kubectl get pod --show-all

使用-owide参数代替

kubectl get pod -owide

Chapter 6:通过 Service 访问 Pod

6.1 创建 Service

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: httpd
spec:
  replicas: 3
  template:
    metadata:
      labels:
        run: httpd
    spec:
      containers:
      - name: httpd
        image: httpd
        ports:
        - containerPort: 80

修改 yaml 文件为:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpd
spec:
  replicas: 3
  selector:
    matchLabels:
      run: httpd
  template:
    metadata:
      labels:
        run: httpd
    spec:
      containers:
      - name: httpd
        image: httpd
        ports:
        - containerPort: 80

6.3 DNS 访问 Service

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: httpd2
  namespace: kube-public
spec:
  replicas: 3
  template:
    metadata:
      labels:
        run: httpd2
    spec:
      containers:
      - name: httpd2
        image: httpd
        ports:
        - containerPort: 80

---
apiVersion: v1
kind: Service
metadata:
  name: httpd2-svc
  namespace: kube-public
spec:
  selector:
    run: httpd2
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 80
error: unable to recognize "httpd2.yml": no matches for kind "Deployment" in version "apps/v1beta1"

修改 yaml 文件为:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpd2
  namespace: kube-public
spec:
  replicas: 3
  selector:
    matchLabels:
      run: httpd2
  template:
    metadata:
      labels:
        run: httpd2
    spec:
      containers:
      - name: httpd2
        image: httpd
        ports:
        - containerPort: 80

---
apiVersion: v1
kind: Service
metadata:
  name: httpd2-svc
  namespace: kube-public
spec:
  selector:
    run: httpd2
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 80

Chapter 8:Health Check

8.5 Health Check 在滚动更新中的应用

apiVersion: app/v1beta1
kind: Deployment
metadata:
  name: app
spec:
  replicas: 10
  template:
    metadata:
      labels:
        run: app
    spec:
      containers:
      - name: app
        image: busybox
        args:
        - /bin/sh
        - -c
        - sleep 10; touch /tmp/healthy; sleep 30000
        readinessProbe:
          exec:
            command:
            - cat
            - /tmp/healthy
            initialDelaySeconds: 10
            periodSeconds: 5
error: unable to recognize "app.v1.yml": no matches for kind "Deployment" in version "app/v1beta1"

修改 yaml 文件为:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app
spec:
  replicas: 10
  selector:
    matchLabels:
      run: app
  template:
    metadata:
      labels:
        run: app
    spec:
      containers:
      - name: app
        image: busybox
        args:
        - /bin/sh
        - -c
        - sleep 10; touch /tmp/healthy; sleep 30000
        readinessProbe:
          exec:
            command:
            - cat
            - /tmp/healthy
          initialDelaySeconds: 10
          periodSeconds: 5