k8s之hello-world

环境:CentOs 7.2

一、使用k8s搭建hello-world,安装kubernetes
1.关闭CentOS自带的防火墙

1
2
systemctl disable firewalld
systemctl stop firewalld

2.安装etcd和kubernetes

1
yum install -y etcd kubernetes

3.修改配置文件/etc/sysconfig/docker和/etc/kubernetes/apiserver为如下。

1
2
3
/etc/sysconfig/docker/

OPTIONS='--selinux-enabled=false --log-driver=journald --signature-verification=false'

1
2
3
# 将--admission-control参数中的ServiceAccount删除
/etc/kubernetes/apiserver
KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"

4.按如下顺序启动所有的服务

1
2
3
4
5
6
7
systemctl start etcd
systemctl start docker
systemctl start kube-apiserver
systemctl start kube-controller-manager
systemctl start kube-scheduler
systemctl start kubelet
systemctl start kube-proxy

二、搭建hello-world案例
image
1.拉取镜像

1
2
3
docker pull kubeguide/guestbook-php-frontend  # php web服务
docker pull kubeguide/redis-master
docker pull kubeguide/guestbook-redis-slave

  1. 创建redis-master Pod和服务
  • 为redis-master服务创建名为redis-master的RC定义文件:redis-master-controller.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    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

  • 创建与之关联的Service,编辑文件redis-master-service.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    apiVersion: v1
    kind: Service
    metadata:
    name: redis-master
    labels:
    name: redis-master
    spec:
    ports:
    - port: 6379
    targetPort: 6379
    selector:
    name: redis-master
  • 发布Service
    kubectl create -f redis-master-service.yaml

3.创建redis-slave Pod和服务

  • 创建Pod
    vim redis-slave-controller.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    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
  • 发布Pod到集群
    kubectl create -f redis-slave-controller.yaml

  • 创建Service
    vim redis-slave-service.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    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

4.创建frontend Pod和服务

  • 创建Pod
    vim frontend-controller.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    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
  • 发布Pod到集群
    kubectl create -f frontend-controller.yaml

  • 创建服务
    vim frontend-service.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    apiVersion: v1
    kind: Service
    metadata:
    name: frontend
    labels:
    name: frontend
    spec:
    type: NodePort
    ports:
    - port: 80
    nodePort: 30001
    selector:
    name: frontend
  • 发布Service
    kubectl create -f frontend-service.yaml

5.查看Pod和服务状态
image

集群中其它Pod如何访问redis-master中的服务?
redis-master服务被分配了一个值为10.254.187.40的虚拟IP,kubernetes集群中的其它Pod就可以通过这个IP访问redis-master服务。由于IP地址是在服务创建后由kebernetes系统自动分配的,其它Pod中无法预先知道某个Service的虚拟IP,为此kuberntes通过使用环境变量来实现服务发现,在每个Pod的容器里都增加了一组Service相关的环境变量,用来记录从服务名到虚拟IP地址的映射关系。
image
image

5.通过浏览器访问网页,输入URL: http://虚拟机IP:30001
image


在创建过程中可能会用到的命令

1
2
3
4
5
6
7
8
kubectl get pods    #查看pod状态
kubectl get rc #查看RC
kubectl get service #查看服务

kubectl logs [pod-name] # 查看日志
kubectl delete pods [pod-name] #删除pod
kubectl delete rc [rc-name] #删除rc
kubectl delete service [service-name] #删除服务


所遇问题:
1.docker启动失败,提示Error starting daemon: SELinux is not supported with the overlay2 graph driver on this kernel. Either boot into a newer kernel or disable selinux in docker (—selinux-enabled=false)

vim /etc/sysconfig/docker

1
OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false'

改为

1
OPTIONS='--selinux-enabled=false --log-driver=journald --signature-verification=false'

2.创建pod一直处于ContainerCreating状态,原因是不能拉取pod的基础镜像,[https://blog.csdn.net/weixin_34054866/article/details/87525597]

1
2
3
4
5
yum install -y *rhsm*

wget http://mirror.centos.org/centos/7/os/x86_64/Packages/python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm

rpm2cpio python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm | cpio -iv --to-stdout ./etc/rhsm/ca/redhat-uep.pem | tee /etc/rhsm/ca/redhat-uep.pem

3.启动frontend pod时报错, AH00534: apache2: Configuration error: No MPM loaded

1
2
3
4
5
systemctl stop docker              //停掉docker服务
rm -rf /var/lib/docker //注意会清掉docker images的镜像,需重新拉取镜像
vi /etc/sysconfig/docker-storage //将文件里的overlay2改成devicemapper即可
DOCKER_STORAGE_OPTIONS="--storage-driver overlay2 " #修改前
DOCKER_STORAGE_OPTIONS="--storage-driver devicemapper " #修改后

重启docker服务
systemctl start docker


Ref:
1.kubernetes权威指南
2.https://blog.csdn.net/a1010256340/article/details/80106156
3.https://blog.csdn.net/weixin_34054866/article/details/87525597
4.https://www.cnblogs.com/guyeshanrenshiwoshifu/p/9147238.html