Thursday, December 14, 2023

Install kubectl on Fedora and start k8s cluster in GCP

 HOWTO




Install kubectl using RPM


dave@dave:~$ cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://pkgs.k8s.io/core:/stable:/v1.29/rpm/
enabled=1
gpgcheck=1
gpgkey=https://pkgs.k8s.io/core:/stable:/v1.29/rpm/repodata/repomd.xml.key
EOF
[sudo] password for dave: 
[kubernetes]
name=Kubernetes
baseurl=https://pkgs.k8s.io/core:/stable:/v1.29/rpm/
enabled=1
gpgcheck=1
gpgkey=https://pkgs.k8s.io/core:/stable:/v1.29/rpm/repodata/repomd.xml.key
dave@dave:~$ sudo yum install -y kubectl
Fedora 39 - x86_64 - Updates                                                                                                                                                                                                                                                                  118 kB/s |  16 kB     00:00    
Fedora 39 - x86_64 - Updates                                                                                                                                                                                                                                                                  1.8 MB/s | 2.7 MB     00:01    
Kubernetes                                                                                                                                                                                                                                                                                     12 kB/s | 5.7 kB     00:00    
Dependencies resolved.
==============================================================================================================================================================================================================================================================================================================================
 Package                                                                   Architecture                                                             Version                                                                                Repository                                                                    Size
==============================================================================================================================================================================================================================================================================================================================
Installing:
 kubectl                                                                   x86_64                                                                   1.29.0-150500.1.1                                                                      kubernetes                                                                    10 M

Transaction Summary
==============================================================================================================================================================================================================================================================================================================================
Install  1 Package

Total download size: 10 M
Installed size: 47 M
Downloading Packages:
kubectl-1.29.0-150500.1.1.x86_64.rpm                                                                                                                                                                                                                                                           12 MB/s |  10 MB     00:00    
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                                                                                                                          12 MB/s |  10 MB     00:00     
Kubernetes                                                                                                                                                                                                                                                                                    7.9 kB/s | 1.7 kB     00:00    
Importing GPG key 0x9A296436:
 Userid     : "isv:kubernetes OBS Project <isv:kubernetes@build.opensuse.org>"
 Fingerprint: DE15 B144 86CD 377B 9E87 6E1A 2346 54DA 9A29 6436
 From       : https://pkgs.k8s.io/core:/stable:/v1.29/rpm/repodata/repomd.xml.key
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                                                                                                                                                      1/1 
  Installing       : kubectl-1.29.0-150500.1.1.x86_64                                                                                                                                                                                                                                                                     1/1 
  Verifying        : kubectl-1.29.0-150500.1.1.x86_64                                                                                                                                                                                                                                                                     1/1 

Installed:
  kubectl-1.29.0-150500.1.1.x86_64                                                                                                                                                                                                                                                                                            

Complete!
dave@dave:~$ 


Check k8s cluster via gcloud

$ gcloud container clusters list
NAME          LOCATION       MASTER_VERSION  MASTER_IP     MACHINE_TYPE  NODE_VERSION    NUM_NODES  STATUS
dave-cluster  europe-west10  1.27.3-gke.100  1.1.1.1  e2-small      1.27.3-gke.100  1          RUNNING


Connect to GKE from localhost via gcloud and kubectl

 sudo yum install google-cloud-sdk-gke-gcloud-auth-plugin
[sudo] password for dave: 
Last metadata expiration check: 0:29:49 ago on Thu 14 Dec 2023 10:05:11 AM CET.
Dependencies resolved.
==================================================================================================================================================================================================================
 Package                                                                 Architecture                           Version                                    Repository                                        Size
==================================================================================================================================================================================================================
Installing:
 google-cloud-sdk-gke-gcloud-auth-plugin                                 x86_64                                 457.0.0-1                                  google-cloud-sdk                                 3.6 M

Transaction Summary
==================================================================================================================================================================================================================
Install  1 Package

Total download size: 3.6 M
Installed size: 12 M
Is this ok [y/N]: y
Downloading Packages:
4b31246c89a3b0d6ec3d0120181b2e556f2a4f483a412c1f89bca3d475061438-google-cloud-sdk-gke-gcloud-auth-plugin-457.0.0-1.x86_64.rpm                                                     9.9 MB/s | 3.6 MB     00:00    
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                             9.8 MB/s | 3.6 MB     00:00     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                                          1/1 
  Installing       : google-cloud-sdk-gke-gcloud-auth-plugin-457.0.0-1.x86_64                                                                                                                                 1/1 
  Running scriptlet: google-cloud-sdk-gke-gcloud-auth-plugin-457.0.0-1.x86_64                                                                                                                                 1/1 
  Verifying        : google-cloud-sdk-gke-gcloud-auth-plugin-457.0.0-1.x86_64                                                                                                                                 1/1 

Installed:
  google-cloud-sdk-gke-gcloud-auth-plugin-457.0.0-1.x86_64                                                                                                                                                        

Complete!

Check the version
dave@dave:~$ gke-gcloud-auth-plugin --version
Kubernetes v1.28.2-alpha+dafa3f6c671d0a0d879f3b203588c782ecb87a18

Update the kubectl configuration to use the plugin:
$ gcloud container clusters get-credentials dave-cluster \
    --zone=europe-west10
Fetching cluster endpoint and auth data.
kubeconfig entry generated for dave-cluster.
dave@dave:~$ kubectl get namespaces
NAME                       STATUS   AGE
default                    Active   13m
gke-gmp-system             Active   12m
gke-managed-filestorecsi   Active   13m
gmp-public                 Active   12m
kube-node-lease            Active   13m
kube-public                Active   13m
kube-system                Active   13m

Interact with k8s cluster via kubectl
dave@dave:~$ kubectl config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://34.32.43.197
  name: gke_dave-terraform_europe-west10_dave-cluster
contexts:
- context:
    cluster: gke_dave-terraform_europe-west10_dave-cluster
    user: gke_dave-terraform_europe-west10_dave-cluster
  name: gke_dave-terraform_europe-west10_dave-cluster
current-context: gke_dave-terraform_europe-west10_dave-cluster
kind: Config
preferences: {}
users:
- name: gke_dave-terraform_europe-west10_dave-cluster
  user:
    exec:
      apiVersion: client.authentication.k8s.io/v1beta1
      args: null
      command: gke-gcloud-auth-plugin
      env: null
      installHint: Install gke-gcloud-auth-plugin for use with kubectl by following
        https://cloud.google.com/blog/products/containers-kubernetes/kubectl-auth-changes-in-gke
      interactiveMode: IfAvailable
      provideClusterInfo: true

Current context
dave@dave:~$ kubectl config current-context
gke_dave-terraform_europe-west10_dave-cluster

Store cluster info
$ gcloud container clusters get-credentials dave-cluster \
    --zone=europe-west10
Fetching cluster endpoint and auth data.
kubeconfig entry generated for dave-cluster.



Google HOWTO for GKE k8s cluster creation

Create GKE cluster in GCP

Enable Google Kubernetes Engine in a Cloud project

For access to Google Kubernetes Engine, select or create a Cloud project to work in, then enable the required APIs:

  1.  to ensure that you have the permissions you need, or select an existing project in which you have the relevant permissions.

  2. The necessary APIs are enabled:
    • Kubernetes Engine API
    • Artifact Registry API


Create a GKE cluster

  1. Click the Navigation menu  icon, then Kubernetes Engine.

    You can see where it is by clicking the following button:

  2. Click Clusters.

  3. Click Create.

  4. Choose Standard mode and click Configure.

  5. In the Name field, enter the name hello-cluster.

  6. Under Location type, select Zonal and then select a Compute Engine zone from the Zone drop-down list, such as us-west1-a.

  7. Click Create. This creates a GKE cluster. After the cluster is ready, a green checkmark appears next to the cluster name.

Deploy the sample app to GKE

  1. Click Workloads.

  2. Click Deploy.

  3. In the Edit container section, select Existing container image.

  4. In the Image path field, click Select.

  5. In the Select container image pane, select the hello-app image you pushed to Artifact Registry.

  6. Click Select.

  7. Click Done.

  8. Click Continue.

  9. In the Configuration section, under Labels, enter app for Key
    and hello-app for Value.

  10. Under the Configuration section, click the View YAML button under Configuration YAML. This opens a YAML configuration file representing the two Kubernetes API resources about to be deployed into your cluster.

  11. Click Close.

  12. Click Deploy.

    When the Deployment Pods are ready, the Deployment details page opens.

  13. Under Managed pods, note the three running Pods for the hello-app Deployment.

Expose a sample app to the internet

  1. Click Workloads.

  2. Click on hello-app in the Name column.

  3. From the Deployment details page, click the Actions > Expose button.

  4. In the Expose dialog, under the Port Mapping section, set

  5. Click Expose to create a Kubernetes Service for hello-app.

    When the Load Balancer is ready, the Service details page opens.

  6. Scroll down to the Exposing services secion and copy the external IP from the Endpoints column for the newly exposed Load balancer .

    • Note that Endpoints are used as External IP addresses for the next sections.

Click Next to learn how to deploy a new version of your app.

🎉 Success

You have successfully deployed a containerized web application!



Clean up

To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, delete your project if you're not going to use it again. If you intend to use it again, you can delete the individual resources.

Delete the project

To delete the Cloud project:

```bash
gcloud projects delete ${PROJECT_ID}
```

Delete individual resources

  1. Delete the Service: This deallocates the Cloud Load Balancer created for your Service:

    kubectl delete service \ hello-app-service
  2. Delete the cluster: This deletes the resources that make up the cluster. Replace COMPUTE_ZONE in the command below with your own zone:

    gcloud container clusters delete \ hello-cluster --zone \ COMPUTE_ZONE
  3. Delete your container images: This deletes the Docker images you pushed to Artifact Registry. Replace PROJECT-ID with your own:

    gcloud artifacts docker images \ delete \ ${REGION}-docker.pkg.dev/${PROJECT_ID}/hello-repo/hello-app:v1 \ --delete-tags --quiet gcloud artifacts docker images \ delete \ ${REGION}-docker.pkg.dev/${PROJECT_ID}/hello-repo/hello-app:v2 \ --delete-tags --quiet

Do more with Google Kubernetes Engine

No comments:

Post a Comment