Kubernetes(k8s)入门、单机版安装、kuberctl指令、k8s服务实例

1.切换root

1.关闭centos自带的防火墙

# systemctl disable firewalld


# systemctl stop firewalld 

2.安装etcd和kubernetes软件(会自动安装docker)

# yum install -y etcd kubernetes

2.修改两处配置

Docker配置文件/etc/sysconfig/docker, OPTIONS=’–selinux-enabled=false –insecure-registry gcr.io’
Kubernetes apiservce配置文件/etc/kubernetes/apiserver,把–admission_control参数钟的ServiceAccount删除

3.启动所有服务

[root@localhost kubernetes]# systemctl start etcd
[root@localhost kubernetes]# systemctl start docker
[root@localhost kubernetes]# systemctl start kube-apiserver
[root@localhost kubernetes]# systemctl start kube-controller-manager
[root@localhost kubernetes]# systemctl start kube-scheduler
[root@localhost kubernetes]# systemctl start kubelet
[root@localhost kubernetes]# systemctl start kube-proxy

测试,查看

部署nginx服务

[root@localhost kubernetes]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED                  SIZE
docker.io/nginx     latest              5699ececb21c        Less than a second ago   109 MB
docker.io/ubuntu    16.04               5e8b97a2a082        24 hours ago             114 MB
hongdada/nginx      v3                  c5cf58738d6b        3 days ago               109 MB
docker.io/busybox   latest              8c811b4aec35        2 weeks ago              1.15 MB
docker.io/tomcat    latest              33e02377a00f        8 weeks ago              554 MB
[root@localhost kubernetes]# kubectl run my-nginx --image=hongdada/nginx:v3 --port=80
deployment "my-nginx" created
[root@localhost kubernetes]# kubectl get pod
NAME                        READY     STATUS              RESTARTS   AGE
my-nginx-3156591236-q1jvn   0/1       ContainerCreating   0          11s

创建gcr.io/google_containers/pause-amd64:3.0镜像

[root@localhost kubernetes]# docker pull googlecontainer/pause-amd64:3.0
Trying to pull repository docker.io/googlecontainer/pause-amd64 ... 
3.0: Pulling from docker.io/googlecontainer/pause-amd64
4f4fb700ef54: Pull complete 
ce150f7a21ec: Pull complete 
Digest: sha256:f04288efc7e65a84be74d4fc63e235ac3c6c603cf832e442e0bd3f240b10a91b
Status: Downloaded newer image for docker.io/googlecontainer/pause-amd64:3.0
[root@localhost kubernetes]# docker tag googlecontainer/pause-amd64:3.0 gcr.io/google_containers/pause-amd64:3.0
[root@localhost kubernetes]# docker image ls
REPOSITORY                              TAG                 IMAGE ID            CREATED                  SIZE
docker.io/nginx                         latest              5699ececb21c        Less than a second ago   109 MB
docker.io/ubuntu                        16.04               5e8b97a2a082        24 hours ago             114 MB
hongdada/nginx                          v3                  c5cf58738d6b        3 days ago               109 MB
docker.io/busybox                       latest              8c811b4aec35        2 weeks ago              1.15 MB
docker.io/tomcat                        latest              33e02377a00f        8 weeks ago              554 MB
docker.io/googlecontainer/pause-amd64   3.0                 99e59f495ffa        2 years ago              747 kB
gcr.io/google_containers/pause-amd64    3.0                 99e59f495ffa        2 years ago              747 kB

kubernetes指令:

# 查看版本
$ kubectl  version
Client Version: version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.2", GitCommit:"269f928217957e7126dc87e6adfa82242bfe5b1e", GitTreeState:"clean", BuildDate:"2017-07-03T15:31:10Z", GoVersion:"go1.7.4", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.2", GitCommit:"269f928217957e7126dc87e6adfa82242bfe5b1e", GitTreeState:"clean", BuildDate:"2017-07-03T15:31:10Z", GoVersion:"go1.7.4", Compiler:"gc", Platform:"linux/amd64"}

# 显示集群信息
$ kubectl cluster-info
Kubernetes master is running at http://localhost:8080
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

# 查看集群中有几个Node
$ kubectl get nodes
NAME        STATUS    AGE
127.0.0.1   Ready     8m

# 运行一个镜像
$ kubectl run my-nginx --image=nginx --replicas=2 --port=80
deployment "my-nginx" created

# 查看pod
$ kubectl  get pods
NAME                       READY     STATUS              RESTARTS   AGE
my-nginx-379829228-cwlbb   0/1       ContainerCreating   0          20s
my-nginx-379829228-czk6w   1/1       Running             0          20s

# 查看服务详情信息
$ kubectl  describe pod my-nginx-379829228-cwlbb
Name:        my-nginx-3156591236-q1jvn
Namespace:    default
Node:        127.0.0.1/127.0.0.1
Start Time:    Thu, 07 Jun 2018 05:33:48 +0800
Labels:        pod-template-hash=3156591236
        run=my-nginx
Status:        Pending
IP:        
Controllers:    ReplicaSet/my-nginx-3156591236
Containers:
  my-nginx:
    Container ID:        
    Image:            hongdada/nginx:v3
    Image ID:            
    Port:            80/TCP
    State:            Waiting
      Reason:            ContainerCreating
    Ready:            False
    Restart Count:        0
    Volume Mounts:        <none>
    Environment Variables:    <none>
Conditions:
  Type        Status
  Initialized     True 
  Ready     False 
  PodScheduled     True 
No volumes.
QoS Class:    BestEffort
Tolerations:    <none>
Events:
  FirstSeen    LastSeen    Count    From            SubObjectPath    Type        Reason        Message
  ---------    --------    -----    ----            -------------    --------    ------        -------
  2m        2m        1    {default-scheduler }            Normal        Scheduled    Successfully assigned my-nginx-3156591236-q1jvn to 127.0.0.1
  2m        45s        4    {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)"

  1m    3s    7    {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\""

# 查看已部署
$ kubectl  get deployments
NAME       DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
my-nginx   1         1         1            0           3m

# 删除pod
$ kubectl delete pod my-nginx-3156591236-q1jvn
pod "my-nginx-3156591236-q1jvn" deleted

# 再次查看pod,发现由于replicas机制,pod又生成一个新的
$ kubectl  get pods
NAME                        READY     STATUS              RESTARTS   AGE
my-nginx-3156591236-qm0fq   0/1       ContainerCreating   0          8s

# 删除部署的my-nginx服务。彻底删除pod
$ kubectl delete deployment my-nginx
deployment "my-nginx" deleted

#再次查看pod
$ kubectl get pods
No resources found.

对比docker命令:

https://blog.csdn.net/qq_34701586/article/details/78732470

猜你喜欢

转载自www.cnblogs.com/hongdada/p/9255182.html