欢迎访问www.showerlee.com, 您的支持就是我前进的动力.

Kubernetes之Pod, Replicaset, Deployment, Label, Service

showerlee 2018-02-22 15:16 DevTools, Kubernetes 阅读 (13,593) 抢沙发

接着上一篇Kubernates1.9+Docker17离线安装部署, 给大家介绍Kubernetes的一些重要概念和组件

Pod:

Pod是一组紧密关联的容器集合,它们共享PID、IPC、Network和UTS namespace,是Kubernetes调度的基本单位。Pod的设计理念是支持多个容器在一个Pod中共享网络和文件系统,可以通过进程间通信和文件共享这种简单高效的方式组合完成服务.

缺点: 不支持高并发, 高可用, 当Pod当机后无法自动恢复.

1.创建Pod

# vi pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: demo
spec:
  containers:
  - image: httpd
    name: httpd
    imagePullPolicy: Always

# kubectl create -f pod.yaml

2.查看Pod

# kubectl get pods

NAME    READY     STATUS    RESTARTS   AGE
demo    1/1       Running      0       8d

# kubectl describe pods

...

3.删除Pod

# kubectl delete pod demo

Replicaset:

Replicaset在继承Pod的所有特性的同时, 它可以利用预先创建好的模板定义副本数量并自动控制, 通过改变Pod副本数量实现Pod的扩容和缩容

缺点: 无法修改template模板, 也就无法发布新的镜像版本

1.创建Replicaset

# vi replicaset.yaml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: demo-rc
  labels:
    app: demo-rc
spec:
  replicas: 2
  selector:
    matchLabels:
      app: demo-rc
  template:
    metadata:
      labels:
        app: demo-rc
    spec:
      containers:
      - name: httpd
        image: httpd
        imagePullPolicy: Always

# kubectl create -f replicaset.yaml

2.查看replicaset

# kubectl get replicaset

NAME      READY     STATUS    RESTARTS   AGE
demo-rc    1/1       Running      0       8d

# kubectl describe replicaset

...

3.删除replicaset

# kubectl delete replicaset demo-rc

Deployment

Deployment在继承Pod和Replicaset的所有特性的同时, 它可以实现对template模板进行实时滚动更新并具备我们线上的Application life circle的特性.

1.创建Deployment

# vi deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpd-deployment
  labels:
    app: httpd-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: httpd-demo
  template:
    metadata:
      labels:
        app: httpd-demo
    spec:
      containers:
      - name: httpd
        image: httpd
        imagePullPolicy: Always
        ports:
        - containerPort: 80
        env:
        - name: VERSION
          value: "v1"

# kubectl create -f deployment.yaml

2.查看Deployment

# kubectl get deployment

NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
httpd-deployment   2         2         2            2           8d

# kubectl get pods -o wide

NAME                               READY     STATUS    RESTARTS   AGE       IP            NODE
httpd-deployment-956697567-8mqch   1/1       Running   0          8d        10.244.0.36   kube-master
httpd-deployment-956697567-wcbs6   1/1       Running   0          8d        10.244.0.37   kube-master

# kubectl describe deployment

...

3.更新deployment

通过此命令可以呼出vi编辑器对模板进行编辑.

# kubectl edit -f deployment.yaml

通过此命令使当前编辑结果生效.

# kubectl apply -f deployment.yaml

再次查看可以看到老版本的deployment已经下架, 新版本的已经生效.

# kubectl get deployment

NAME                          DESIRED   CURRENT   READY     AGE
httpd-deployment-6b98d94474   0         0         0         1m
httpd-deployment-956697567    2         2         2         7m

4.扩容与缩容

可以修改replicas的赋值对deployment进行扩容与缩容

#  kubectl scale deployment/httpd-deployment --replicas=1

5.删除deployment

# kubectl delete deployment httpd-deployment

Lable

Label是attach到Pod的一对键/值对,用来传递用户定义的属性。比如,你可能创建了一个"tier"和“app”标签,通过Label(tier=frontend, app=myapp)来标记前端Pod容器,使用Label(tier=backend, app=myapp)标记后台Pod。然后可以使用Selectors选择带有特定Label的Pod,让具体某一个Pod或者Deployment去使用某一个Service实现特定的网络配置.

Service

Service是应用服务的抽象,通过labels为应用提供负载均衡和服务发现。匹配labels的Pod IP和端口列表组成endpoints,由kube-proxy负责将服务IP负载均衡到这些endpoints上。
每个Service都会自动分配一个cluster IP(仅在集群内部可访问的虚拟地址)和DNS名,其他容器可以通过该地址或DNS来访问服务,而不需要了解后端容器的运行。

1.更改NodePort限制

Kubernetes默认对外的NodePort限制范围为30000-32767, 这里如果要使用一些常用的端口(80, 8080, 443)需将这个范围放大.

# vi /etc/kubernetes/manifests/kube-apiserver.yaml

在--service-cluster-ip-range与insecure-port间添加如下node port配置

...
- --service-cluster-ip-range=10.96.0.0/12
- --service-node-port-range=0-32767
- --insecure-port=0
....

重启服务

# systemctl restart kubelet


2.创建Service

# vi svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: demo
spec:
  type: NodePort
  ports:
    - port: 80
      nodePort: 80
  selector:
    app: httpd-demo

# kubectl create -f svc.yaml

Tip: 如果要对某一Pod或deployment添加对外访问端口, 这里service添加的selector的键值需与之相对应.

3.查看开放端口

# kubectl get svc demo

NAME      TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)     AGE
demo      NodePort   10.100.96.157   <none>        80:80/TCP   1h

# kubectl describe service demo

Name:                     demo
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 app=httpd-demo
Type:                     NodePort
IP:                       10.100.96.157
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  80/TCP
Endpoints:                10.244.0.36:80,10.244.0.37:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

4.验证外网是否可以访问

截图00

本文相关代码:

https://git.showerlee.com/showerlee/kube-deploy

Finished...

正文部分到此结束
版权声明:除非注明,本文由(showerlee)原创,转载请保留文章出处!
本文链接:http://www.showerlee.com/archives/2246

继续浏览:k8s

还没有评论,快来抢沙发!

发表评论

icon_wink.gif icon_neutral.gif icon_mad.gif icon_twisted.gif icon_smile.gif icon_eek.gif icon_sad.gif icon_rolleyes.gif icon_razz.gif icon_redface.gif icon_surprised.gif icon_mrgreen.gif icon_lol.gif icon_idea.gif icon_biggrin.gif icon_evil.gif icon_cry.gif icon_cool.gif icon_arrow.gif icon_confused.gif icon_question.gif icon_exclaim.gif