1.为什么使用k8s

1.1注册问题

1.1.1.环境变量

在创建一个Pod时,kubelet在该Pod的所有容器中为当前所有Service添加一系列环境变量。

例如,已存在名称为“redis-master”的Service,它对外暴露6379的TCP端口,且集群IP为10.0.0.11。kubelet会为新建的容器添加以下环境变量:

REDIS_MASTER_SERVICE_HOST=10.0.0.11
REDIS_MASTER_SERVICE_PORT=6379

通过环境变量来创建Service会带来一个不好的结果,即任何被某个Pod所访问的Service,必须先于该Pod创建,否则和这个后创建的Service相关的环境变量,

将不会被加入该Pod的容器中

1.1.2.DNS

DNS服务器通过Kubernetes API Server监控与Service相关的活动。当监控到添加Service的时,DNS服务器为每个Service创建一系列DNS记录。

例如:有个叫做”my-service“的service,他对应的kubernetesnamespace为”my-ns“,那么会有他对应的dns记录,叫做”my-service.my-ns“。

那么在my-ns的namespace中的pod都可以对my-service做name解析来轻松找到这个service。在其他namespace中的pod解析”my-service.my-ns“来找到他。

解析出来的结果是这个service对应的cluster ip。

1.2负载均衡
每个节点上都有一个软件实现的服务代理来实现负载均衡 kube-proxy
1.3部署问题
确定部署实例数,系统自动部署
1.4运维问题
自动监控,自我修复
1.5使用建议集中配置
集中配置,并且实时配置实时生效

2.demo php redis主从

2.1 redis-master-controller.yaml

vim  redis-master-controller.yaml


apiVersion: v1
kind: ReplicationController
metadata:
  name: redis-master
  labels:
    name: redis-master
spec:
  replicas: 1
  selector:
    name: redis-master
  template:
    metadata:
      labels:
        name: redis-master
    spec:
       containers:
        - name: master
          image: kubeguide/redis-master
          ports:
          - containerPort: 6379
		  
kubectl create -f redis-master-controller.yaml

2.2 redis-master-service.yaml

vim redis-master-service.yaml


apiVersion: v1
kind: Service
metadata:
  name: redis-master
  labels:
    name: redis-master
spec:
  ports:
  - port: 6379
    targetPort: 6379
  selector:
    name: redis-master
	
kubectl create -f redis-master-service.yaml 

2.3 redis-slave-controller.yaml

vim redis-slave-controller.yaml

apiVersion: v1
kind: ReplicationController
metadata:
  name: redis-slave
  labels:
    name: redis-slave
spec:
  replicas: 2
  selector:
    name: redis-slave
  template:
    metadata:
      labels:
        name: redis-slave
    spec:
      containers:
      - name: slave
        image: kubeguide/guestbook-redis-slave
        env:
        - name: GET_HOSTS_FROM
          value: env
        ports:
        - containerPort: 6379
		
kubectl create -f redis-slave-controller.yaml

2.4 redis-slave-service.yaml

vim redis-slave-service.yaml			 



apiVersion: v1
kind: Service 
metadata:
  name: redis-slave   
  labels:
    name: redis-slave 
spec:
  ports:   
  - port: 6379   
  selector:
    name: redis-slave
	
kubectl create -f redis-slave-service.yaml

2.5 frontend-controller.yaml

vim frontend-controller.yaml


apiVersion: v1 
kind: ReplicationController 
metadata:   
  name: frontend   
  labels:
       name: frontend
spec:   
  replicas: 3   
  selector:     
    name: frontend
  template:
    metadata:       
      labels:
        name: frontend
    spec:       
      containers:       
      - name: frontend
        image: kubeguide/guestbook-php-frontend
        env:
        - name: GET_HOSTS_FROM
          value: env        
        ports:         
        - containerPort: 80

kubectl create -f frontend-controller.yaml		

2.6 frontend-service.yaml

vim frontend-service.yaml


apiVersion: v1
kind: Service 
metadata:   
  name: frontend   
  labels:     
    name: frontend 
spec:   
  type: NodePort   
  ports:   
  - port: 80     
    nodePort: 30001   
  selector:     
    name: frontend 
	
kubectl create -f frontend-service.yaml 

3.资源隔离与调度问题

kubectl label nodes kubernetes-minion1 zone=test

apiVersion: v1 
kind: ReplicationController 
metadata:   
  name: frontend   
  labels:
       name: frontend
spec:   
  replicas: 3   
  selector:     
    name: frontend
  template:
    metadata:       
      labels:
        name: frontend
    spec:       
      containers:       
      - name: frontend
        image: kubeguide/guestbook-php-frontend
        env:
        - name: GET_HOSTS_FROM
          value: env        
        ports:         
        - containerPort: 80 
        nodeSelector:         
          zone: test

4.扩容与升级问题

kubectl get rc 查看服务
kubectl scale rc redis-slave --replicas=3

5.滚动升级

kubectl rolling-update frontend --image=image:v2

6.资源配额

vim /etc/kubernetes/apiserver
kube-apiserver ... --admission_control=LimitRanger,ResourceQuota

apiVersion: v1 
kind: ReplicationController 
metadata:   
  name: frontend   
  labels:
       name: frontend
spec:   
  replicas: 3   
  selector:     
    name: frontend
  template:
    metadata:       
      labels:
        name: frontend
    spec:       
      containers:       
      - name: frontend
        image: kubeguide/guestbook-php-frontend
        env:
        - name: GET_HOSTS_FROM
          value: env        
        ports:         
        - containerPort: 80 
        nodeSelector:         
          zone: test
        resources:           
          limits:             
            cpu: 0.5             
            memory: 128Mi

7.私有docker registry

7.1主节点创建镜像容器

docker run -d -e SETTINGS_FLAVOR=dev -e STORAGE_PATH=/tmp/registry -v /opt/data/registry:/tmp/registry  -p 5000:5000 registry

7.2主节点下载容器

docker pull google/pause
docker images 记录id  f9d5de079539

重命名

docker tag f9d5de079539    192.168.100.23:5000/google/pause 

7.3所有节点修改守护进程配置

vim /etc/sysconfig/docker
增加 --insecure-registry 192.168.100.23:5000

7.4从节点修改配置

vim /etc/kubernetes/kubelet

# The address for the info server to serve on (set to 0.0.0.0 or "" for all interfaces) 
KUBELET_ADDRESS="--address=0.0.0.0  --pod_infra_container_image=192.168.100.23:5000/google/pause"  

7.5主节点上传容器

docker push 192.168.100.23:5000/google/pause

7.6从节点下载容器

docker pull 192.168.100.23:5000/google/pause
Logo

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

更多推荐