k8s学习一:centos7单机安装k8s

初始安装

  1. yum install -y etcd kubernetes
  2. vim /etc/sysconfig/docker
# 内容改为如下
OPTIONS='--selinux-enabled=false --insecure-registry gcr.io'
  1. vim /etc/kubernetes/apiserver 去掉–admission-control中的ServiceAccount
  2. systemctl start etcd.service
  3. systemctl start docker
  4. systemctl start kube-apiserver.service
  5. systemctl start kube-controller-manager.service
  6. systemctl start kube-scheduler.service
  7. systemctl start kubelet.service
  8. systemctl start kube-proxy.service
  9. mkdir /data
  10. vim mysql-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
    name: mysql
spec:
    replicas: 1
    selector:
        app: mysql
    template:
        metadata:
            labels:
                app: mysql
        spec:
            containers:
                - name: mysql
                  image: mysql
                  ports:
                      - containerPort: 3306
                  env:
                      - name: MYSQL_ROOT_PASSWORD
                        value: "123456"
  1. kubectl create -f mysql-rc.yaml
  2. kubectl get rc
NAME      DESIRED   CURRENT   READY     AGE
mysql     1         1         0         12m
  1. kubectl get pods
NAME          READY     STATUS              RESTARTS   AGE
mysql-zqgck   0/1       ContainerCreating   0          13m

发现status一直处于ContainerCreating状态!!!

  1. kubectl describe pod mysql (该命令用于排查上面状态一直卡在ContainerCreating的情况)
Name:       mysql-zqgck
Namespace:  default
Node:       127.0.0.1/127.0.0.1
Start Time: Tue, 05 Jun 2018 10:25:31 -0400
Labels:     app=mysql
Status:     Pending
IP:     
Controllers:    ReplicationController/mysql
Containers:
  mysql:
    Container ID:   
    Image:      mysql
    Image ID:       
    Port:       3306/TCP
    State:      Waiting
      Reason:       ContainerCreating
    Ready:      False
    Restart Count:  0
    Volume Mounts:  
    Environment Variables:
      MYSQL_ROOT_PASSWORD:  123456
Conditions:
  Type      Status
  Initialized   True 
  Ready     False 
  PodScheduled  True 
No volumes.
QoS Class:  BestEffort
Tolerations:    
Events:
  FirstSeen LastSeen    Count   From            SubObjectPath   Type        Reason      Message
  --------- --------    -----   ----            -------------   --------    ------      -------
  14m       14m     1   {default-scheduler }            Normal      Scheduled   Successfully assigned mysql-zqgck to 127.0.0.1
  14m       3m      7   {kubelet 127.0.0.1}         Warning     FailedSync  Error syncing pod, skipping: failed to "StartContainer" for "POD" with ErrImagePull: "image pull failed for registry.access.redhat.com/rhel7/pod-infrastructure:latest, this may be because there are no credentials on this request.  details: (open /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt: no such file or directory)"

  14m   4s  60  {kubelet 127.0.0.1}     Warning FailedSync  Error syncing pod, skipping: failed to "StartContainer" for "POD" with ImagePullBackOff: "Back-off pulling image \"registry.access.redhat.com/rhel7/pod-infrastructure:latest\""
  1. 发现错误open /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt: no such file or directory,网上找的方法:
- 运行 ll /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt 发现其指向/etc/rhsm/ca/redhat-uep.pem
- 查看发现没有/etc/rhsm/ca/redhat-uep.pem 文件
- 运行 yum install *rhsm* 发现已安装

问题没解决。。。。继续

- 运行yum search rhsm
================================================ N/S matched: rhsm ================================================
python-rhsm.x86_64 : A Python library to communicate with a Red Hat Unified Entitlement Platform
python-rhsm-certificates.x86_64 : Certificates required to communicate with a Red Hat Unified Entitlement Platform
subscription-manager-rhsm.x86_64 : A Python library to communicate with a Red Hat Unified Entitlement Platform
subscription-manager-rhsm-certificates.x86_64 : Certificates required to communicate with a Red Hat Unified
                                              : Entitlement Platform
- 发现python-rhsm-certificates.x86_64比较像,则运行yum install python-rhsm-certificates
软件包 python-rhsm-certificates-1.19.10-1.el7_4.x86_64 被已安装的 subscription-manager-rhsm-certificates-1.20.11-1.el7.centos.x86_64 取代
无须任何处理

无语,已被取代,现在考虑是不是版本问题。。。。。网上找。。。。

- 运行wget http://mirror.centos.org/centos/7/os/x86_64/Packages/python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm 手动下载
- chmod +x 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

发现ll /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt已经指向正确了

- kubectl delete -f mysql-rc.yaml
- kubectl get pods
NAME          READY     STATUS        RESTARTS   AGE
mysql-zqgck   0/1       Terminating   0          38m

尼玛悲剧,发现一直卡在Terminating状态。。。。继续问度娘。。。

- 运行systemctl status kube-apiserver.service
- kubectl get pods
No resources found.

好了,终于删掉了,继续重新创建。。。。

- kubectl create -f mysql-rc.yaml
- kubectl get rc
NAME      DESIRED   CURRENT   READY     AGE
mysql     1         1         1         1m
- kubectl get pods
NAME          READY     STATUS    RESTARTS   AGE
mysql-gtcj4   1/1       Running   0          12s

终于成功了。。。。Running了。。。。

  1. docker ps查看运行的容器:docker ps | grep mysql
2ecf0982d281        mysql                                                        "docker-entrypoint..."   2 minutes ago       Up 2 minutes                            k8s_mysql.f6601b53_mysql-gtcj4_default_970ff879-68d2-11e8-85b7-001c42f5e75e_b1252eaf
5acb60408a8c        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           2 minutes ago       Up 2 minutes                            k8s_POD.1d520ba5_mysql-gtcj4_default_970ff879-68d2-11e8-85b7-001c42f5e75e_6b6bbff9
  1. vim mysql-svc.yaml 创建service文件
apiVersion: v1
kind: Service
metadata:
    name: mysql
spec:
    ports:
        - port: 3306
    selector:
        app: mysql
  1. kubectl create -f mysql-svc.yaml 创建service
  2. kubectl get svc
NAME         CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
kubernetes   10.254.0.1      <none>        443/TCP    1h
mysql        10.254.65.184   <none>        3306/TCP   14s

这个还顺利,直接就ok了

  1. docker ps| grep mysql即可看到运行的进程

第二天服务器重启后

  1. systemctl status etcd.service
etcd.service - Etcd Server
   Loaded: loaded (/usr/lib/systemd/system/etcd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

悲剧,没有设置自启动。。。。重启服务。。。。

  1. 启动服务
- systemctl start etcd.service
- systemctl start docker
- systemctl start kube-apiserver.service
- systemctl start kube-controller-manager.service
- systemctl start kube-scheduler.service
- systemctl start kubelet.service
- systemctl start kube-proxy.service 
  1. kubectl get pods
[root@centos-linux ~]# kubectl get pods
NAME          READY     STATUS    RESTARTS   AGE
mysql-gtcj4   1/1       Running   1          10h

服务启动后,发现mysql已经启动了

你可能感兴趣的:(k8s+docker)