K8S 入门练习

一、启动pod

1. 手动启动

apiVersion: v1
kind: Pod
metadata: 
  name: nginx-test
  labels: 
    app: nginx-test
spec:
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent   ########优先使用本地docker image,如果找不到了,再去网上下载
    ports:
    - containerPort: 80

2. 用rs启动

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx-test
spec:
  replicas: 2
  selector: 
    matchLabels:
      tier: nginx-test
  template:
    metadata:
      labels:
        tier: nginx-test
    spec:
      containers:
      - name: nginx
        image: nginx
        imagePullPolicy: IfNotPresent
        env:
        - name: Get_HOSTS_FROM
          value: dns
        ports:
        - containerPort: 80

二、配置svc

1. 配置deployment,管理pod

apiVersion: apps/v1
kind: Deployment
metadata:
  # deployment名字
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-test
  # 定义Pod的模板
  template:
    metadata:
      labels:
        app: nginx-test
    spec:
      containers:
      - name: nginx
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
root@master:~# kubectl get deployments
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   1/1     1            1           31m

2. 配置svc,关联pod

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx-test
spec:
  ports:
  - port: 80
    targetPort: 8080
  selector:
    app: nginx-test
root@master:~# k get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP    51d
nginx        ClusterIP   10.111.79.179   <none>        8080/TCP   29m
root@master:~# curl 10.111.79.179:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
root@master:~#
curl 10.111.79.179:8080

三、pv/pvc 应用

本例中,存储采用hostpath,位于host的/tmp目录

1. pvsmall.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pvsmall
  labels:
    name: pvsmall
spec:
  hostPath: 
    path: "/tmp"
  accessModes: ["ReadWriteMany","ReadWriteOnce"]
  capacity:
    storage: 1Gi

2. pvbig.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pvbig
  labels:
    name: pvbig
spec:
  hostPath:
    path: "/tmp"    
  accessModes: ["ReadWriteOnce"]
  capacity:
    storage: 2Gi

3. pvc2g.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc2g
  namespace: default
spec:
  accessModes: ["ReadWriteOnce"]
  resources:
    requests:
      storage: 2Gi

4. create pvsmall,pvbig,pvc2g

root@master:/root/pv# kubectl create -f pvsmall.yaml 
persistentvolume/pvsmall created
root@master:/root/pv# kubectl create -f pvbig.yaml 
persistentvolume/pvbig created
root@master:/root/pv# kubectl create -f pvc2g.yaml 
persistentvolumeclaim/pvc2g created
root@master:/root/pv# kubectl get pv,pvc
NAME                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM           STORAGECLASS   REASON   AGE
persistentvolume/pvbig     2Gi        RWO            Retain           Bound       default/pvc2g                           10s
persistentvolume/pvsmall   1Gi        RWO,RWX        Retain           Available                                           15s

NAME                          STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
persistentvolumeclaim/pvc2g   Bound    pvbig    2Gi        RWO                           8s
root@master:/root/pv# 
create pv,pvc

2g的pvc claim,会选到2g的pv上,1g不满足

5. create deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-test
  # 定义Pod的模板
  template:
    metadata:
      labels:
        app: nginx-test
    spec:
      containers:
      - name: nginx
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        volumeMounts:
          - name: www
            mountPath: /usr/share/nginx/html
      volumes:
      - name: www
        persistentVolumeClaim:
          claimName: pvc2g
root@master:/root/pv# kubectl create -f deployment.yaml 
deployment.apps/nginx-deployment created
root@master:/root/pv# kubectl get deployments
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   1/1     1            1           6s
root@master:/root/pv# 
create deployment

6. check下pv是否生效

root@master:/root/pv# kubectl get deployments
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   1/1     1            1           103s
root@master:/root/pv# kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
busybox                             1/1     Running   2          123m
nginx-deployment-596c45ccf8-ccslm   1/1     Running   0          104s
root@master:/root/pv# kubectl exec -it nginx-deployment-596c45ccf8-ccslm -- /bin/bash
root@nginx-deployment-596c45ccf8-ccslm:/# ls /usr/share/nginx/html/
systemd-private-2996b7aae3b7456fa565724b9f07ed3f-systemd-timesyncd.service-79oY1P  test313
root@nginx-deployment-596c45ccf8-ccslm:/# 
check if pv working

可以看到,/tmp 目录挂载到了容器里的,/usr/share/nginx/html下面。

 四、pvc删除后,pv回收复用

解决办法: 手动修改该pv

1. pvc删除后

root@master:/root/pv# k get pv
NAME      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM           STORAGECLASS   REASON   AGE
pvbig     2Gi        RWO            Retain           Released    default/pvc2g                           38m
pvsmall   1Gi        RWO,RWX        Retain           Available                                           39m
root@master:/root/pv# 

2. 此时pv为Released,并不是Available,所以还无法复用

root@master:/root/pv# kubectl get pv pvbig -o json
{
    "apiVersion": "v1",
    "kind": "PersistentVolume",
    "metadata": {
        "annotations": {
            "pv.kubernetes.io/bound-by-controller": "yes"
        },
        "creationTimestamp": "2021-03-13T11:44:23Z",
        "finalizers": [
            "kubernetes.io/pv-protection"
        ],
        "labels": {
            "name": "pvbig"
        },
        "managedFields": [
            {
                "apiVersion": "v1",
                "fieldsType": "FieldsV1",
                "fieldsV1": {
                    "f:metadata": {
                        "f:labels": {
                            ".": {},
                            "f:name": {}
                        }
                    },
                    "f:spec": {
                        "f:accessModes": {},
                        "f:capacity": {
                            ".": {},
                            "f:storage": {}
                        },
                        "f:hostPath": {
                            ".": {},
                            "f:path": {},
                            "f:type": {}
                        },
                        "f:persistentVolumeReclaimPolicy": {},
                        "f:volumeMode": {}
                    }
                },
                "manager": "kubectl",
                "operation": "Update",
                "time": "2021-03-13T11:44:23Z"
            },
            {
                "apiVersion": "v1",
                "fieldsType": "FieldsV1",
                "fieldsV1": {
                    "f:metadata": {
                        "f:annotations": {
                            ".": {},
                            "f:pv.kubernetes.io/bound-by-controller": {}
                        }
                    },
                    "f:spec": {
                        "f:claimRef": {
                            ".": {},
                            "f:apiVersion": {},
                            "f:kind": {},
                            "f:name": {},
                            "f:namespace": {},
                            "f:resourceVersion": {},
                            "f:uid": {}
                        }
                    },
                    "f:status": {
                        "f:phase": {}
                    }
                },
                "manager": "kube-controller-manager",
                "operation": "Update",
                "time": "2021-03-13T12:23:17Z"
            }
        ],
        "name": "pvbig",
        "resourceVersion": "495927",
        "selfLink": "/api/v1/persistentvolumes/pvbig",
        "uid": "23c556b0-9e5f-4c8b-be5a-30811f18f99f"
    },
    "spec": {
        "accessModes": [
            "ReadWriteOnce"
        ],
        "capacity": {
            "storage": "2Gi"
        },
        "claimRef": {
            "apiVersion": "v1",
            "kind": "PersistentVolumeClaim",
            "name": "pvc2g",
            "namespace": "default",
            "resourceVersion": "490273",
            "uid": "c769a790-40bd-4d09-860f-7cee90302910"
        },
        "hostPath": {
            "path": "/tmp",
            "type": ""
        },
        "persistentVolumeReclaimPolicy": "Retain",
        "volumeMode": "Filesystem"
    },
    "status": {
        "phase": "Released"
    }
}
kubectl get pv

删除红框部分:

 

 3. 删除后

root@master:/root/pv# kubectl get pv
NAME      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
pvbig     2Gi        RWO            Retain           Available                                   47m
pvsmall   1Gi        RWO,RWX        Retain           Available                                   47m
root@master:/root/pv# 

此时pv为Available状态,可用。

 四、kubectl 端口妆发

1. 配置nginx pod

root@master:/root/nginx# k get pods -owide
NAME         READY   STATUS    RESTARTS   AGE    IP                NODE    NOMINATED NODE   READINESS GATES
busybox      1/1     Running   2          168m   192.168.166.137   node1   <none>           <none>
nginx-test   1/1     Running   0          6s     192.168.104.23    node2   <none>           <none>

2. master上curl

root@master:/root/nginx# curl 192.168.104.23:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
curl 192.168.104.23:80

3. 做端口转发

root@master:/root/nginx# kubectl port-forward nginx-test 8080:80

Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80

默认只在本地转发,如果想0.0.0.0

root@master:/root/nginx# kubectl port-forward nginx-test --address 0.0.0.0 8080:80
Forwarding from 0.0.0.0:8080 -> 80

 

posted @ 2021-03-13 19:50  一个门外汉  阅读(359)  评论(0编辑  收藏  举报