码迷,mamicode.com
首页 > 其他好文 > 详细

K8S 入门

时间:2021-03-15 11:26:20      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:server   ada   tar   mat   creat   lease   work   The   ntp   

一、启动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下面。

 

 

endding

K8S 入门

标签:server   ada   tar   mat   creat   lease   work   The   ntp   

原文地址:https://www.cnblogs.com/reatual/p/14530135.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!